/** Initialize this index, assuming that it has not already been initialized. */ public void initializeIndex() { synchronized (indexStore) { if (hasBeenInitialized) { return; } hasBeenInitialized = true; indexStore.clear(); if (!initializeIndexFrom(getIndexFile())) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("Clearing index after failing to read from index file"); } indexStore.clear(); if (!initializeBundledLibraries()) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("Failed to initialize bundled libraries"); } return; } if (!indexUserLibraries()) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("Clearing index after failing to index user libraries"); } indexStore.clear(); initializeBundledLibraries(); } } if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("After initializing the index"); } } }
/** * Initialize this index to contain information about the bundled libraries. The index store is * expected to have been cleared before invoking this method. * * @return {@code true} if the bundled libraries were successfully indexed */ private boolean initializeBundledLibraries() { synchronized (indexStore) { hasBeenInitialized = true; if (!initializeIndexFrom(getInitialIndexFile())) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("Clearing index after failing to read from initial index file"); } indexStore.clear(); if (!indexBundledLibraries()) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("Clearing index after failing to index bundled libraries"); } indexStore.clear(); return false; } // TODO(brianwilkerson) Restore the following line once we figure out how to know that the // bundled libraries (and only the bundled libraries) have been indexed. // writeIndexTo(getInitialIndexFile()); } } return true; }
/** Write index statistics to the log. */ public void logIndexStats(String message) { int relationshipCount; int attributeCount; int elementCount; int resourceCount; synchronized (indexStore) { relationshipCount = indexStore.getRelationshipCount(); attributeCount = indexStore.getAttributeCount(); elementCount = indexStore.getElementCount(); resourceCount = indexStore.getResourceCount(); } DartCore.logInformation( message + ": " + relationshipCount + " relationships and " + attributeCount + " attributes in " + elementCount + " elements in " + resourceCount + " resources"); }
/** * Initialize this index from the given file. * * @param indexFile the file from which this index is to be initialized * @return <code>true</code> if the index was correctly initialized */ private boolean initializeIndexFrom(File indexFile) { if (indexFile == null) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { DartCore.logInformation("Index file was null"); } return false; } else if (!indexFile.exists()) { if (DartCoreDebug.TRACE_INDEX_STATISTICS) { DartCore.logInformation("Index file " + indexFile.getAbsolutePath() + " does not exist"); } return false; } if (DartCoreDebug.TRACE_INDEX_STATISTICS) { DartCore.logInformation( "About to initialize the index from file " + indexFile.getAbsolutePath() + " (size = " + indexFile.getTotalSpace() + " bytes)"); } try { boolean wasRead = readIndexFrom(indexFile); if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("After initializing the index from file " + indexFile.getAbsolutePath()); } synchronized (indexStore) { return wasRead && indexStore.getResourceCount() > 0; } } catch (Exception exception) { DartCore.logError("Could not read index file " + indexFile.getAbsolutePath(), exception); } if (DartCoreDebug.TRACE_INDEX_STATISTICS) { logIndexStats("Deleting corrupted index file " + indexFile.getAbsolutePath()); } try { indexFile.delete(); } catch (Exception exception) { DartCore.logError( "Could not delete corrupt index file " + indexFile.getAbsolutePath(), exception); } return false; }
/** * Write the contents of this index to the given output stream. * * @param output the output stream to which this index will be written * @throws IOException if the index could not be written to the given output stream */ private void writeIndex(ObjectOutputStream output) throws IOException { IndexWriter writer = indexStore.createIndexWriter(); writer.writeIndex(output); }
/** * Read the contents of this index from the given input stream. * * @param input the input stream from which this index will be read * @return {@code true} if the file was correctly read * @throws IOException if the index could not be read from the given input stream */ private boolean readIndex(ObjectInputStream input) throws IOException { IndexReader reader = indexStore.createIndexReader(); return reader.readIndex(input); }
/** * Return the number of relationships that are currently recorded in this index. * * @return the number of relationships that are currently recorded in this index */ public int getRelationshipCount() { synchronized (indexStore) { return indexStore.getRelationshipCount(); } }