@Override
  public IIndexFragmentFile commitUncommittedFile() throws CoreException {
    if (uncommittedFile == null) return null;

    int defectiveStateChange = uncommittedFile.getTimestamp() == 0 ? 1 : 0;
    int unresolvedIncludeStateChange = uncommittedFile.hasUnresolvedInclude() ? 1 : 0;

    PDOMFile file;
    if (fileBeingUpdated == null) {
      // New file, insert it into the index.
      file = uncommittedFile;
      getFileIndex().insert(file.getRecord());
    } else {
      // Existing file.
      if (fileBeingUpdated.getTimestamp() == 0) defectiveStateChange -= 1;
      if (fileBeingUpdated.hasUnresolvedInclude()) unresolvedIncludeStateChange -= 1;
      fileBeingUpdated.replaceContentsFrom(uncommittedFile);
      file = fileBeingUpdated;
      fileBeingUpdated = null;
    }
    if (defectiveStateChange > 0) {
      getIndexOfDefectiveFiles().insert(file.getRecord());
    } else if (defectiveStateChange < 0) {
      getIndexOfDefectiveFiles().delete(file.getRecord());
    }
    if (unresolvedIncludeStateChange > 0) {
      getIndexOfFilesWithUnresolvedIncludes().insert(file.getRecord());
    } else if (unresolvedIncludeStateChange < 0) {
      getIndexOfFilesWithUnresolvedIncludes().delete(file.getRecord());
    }

    fEvent.fFilesWritten.add(uncommittedKey.getLocation());
    uncommittedFile = null;
    uncommittedKey = null;
    return file;
  }
  @Override
  public void clearFile(IIndexFragmentFile file) throws CoreException {
    assert file.getIndexFragment() == this;
    IIndexFileLocation location = file.getLocation();
    PDOMFile pdomFile = (PDOMFile) file;
    pdomFile.clear();
    IIndexInclude include = pdomFile.getParsedInContext();
    if (include != null) {
      PDOMFile includedBy = (PDOMFile) include.getIncludedBy();
      if (includedBy.getTimestamp() > 0)
        getIndexOfFilesWithUnresolvedIncludes().insert(includedBy.getRecord());
    }

    fEvent.fClearedFiles.add(location);
  }
  public PDOMMacroReferenceName(
      PDOMLinkage linkage, IASTName name, PDOMFile file, PDOMMacroContainer container)
      throws CoreException {
    this.linkage = linkage;
    Database db = linkage.getDB();
    record = db.malloc(RECORD_SIZE);

    db.putRecPtr(record + CONTAINER_REC_OFFSET, container.getRecord());
    db.putRecPtr(record + FILE_REC_OFFSET, file.getRecord());

    // Record our location in the file
    IASTFileLocation fileloc = name.getFileLocation();
    db.putInt(record + NODE_OFFSET_OFFSET, fileloc.getNodeOffset());
    db.putShort(record + NODE_LENGTH_OFFSET, (short) fileloc.getNodeLength());
    container.addReference(this);
  }
 @Override
 public boolean hasLastingDefinition(PDOMBinding binding) throws CoreException {
   if (fileBeingUpdated == null) {
     return binding.hasDefinition();
   }
   // Definitions in fileBeingUpdated will soon go away, so look for a definition elsewhere.
   for (PDOMName name = binding.getFirstDefinition();
       name != null;
       name = name.getNextInBinding()) {
     if (!fileBeingUpdated.getPDOM().equals(name.getPDOM())
         || fileBeingUpdated.getRecord() != name.getFileRecord()) {
       return true;
     }
   }
   return false;
 }
  /**
   * Uses the specified location converter to update each internal representation of a file
   * location. The file index is rebuilt with the new representations. Individual PDOMFile records
   * are unmoved so as to maintain referential integrity with other PDOM records.
   *
   * <p><b>A write-lock must be obtained before calling this method</b>
   *
   * @param newConverter the converter to use to update internal file representations
   * @throws CoreException
   */
  public void rewriteLocations(final IIndexLocationConverter newConverter) throws CoreException {
    final List<PDOMFile> pdomfiles = new ArrayList<PDOMFile>();
    getFileIndex()
        .accept(
            new IBTreeVisitor() {
              @Override
              public int compare(long record) throws CoreException {
                return 0;
              }

              @Override
              public boolean visit(long record) throws CoreException {
                PDOMFile file = PDOMFile.recreateFile(WritablePDOM.this, record);
                pdomfiles.add(file);
                return true;
              }
            });

    clearFileIndex();
    final List<PDOMFile> notConverted = new ArrayList<PDOMFile>();
    for (PDOMFile file : pdomfiles) {
      String internalFormat = newConverter.toInternalFormat(file.getLocation());
      if (internalFormat != null) {
        file.setInternalLocation(internalFormat);
        getFileIndex().insert(file.getRecord());
      } else {
        notConverted.add(file);
      }
    }

    // remove content where converter returns null
    for (PDOMFile file : notConverted) {
      file.convertIncludersToUnresolved();
      file.clear();
    }
  }
 void setFile(PDOMFile file) throws CoreException {
   linkage.getDB().putRecPtr(record + FILE_REC_OFFSET, file != null ? file.getRecord() : 0);
 }
 @Override
 protected boolean isCommitted(PDOMMacroReferenceName name) throws CoreException {
   return uncommittedFile == null
       || !uncommittedFile.getPDOM().equals(name.getPDOM())
       || uncommittedFile.getRecord() != name.getFileRecord();
 }