@Override public boolean isValid() { try { return index.getStatus() != Index.Status.INVALID; } catch (IOException ex) { //Exceptions.printStackTrace(ex); } return false; }
@Override public void unitRemoved(int unitId) { if (unitId < 0) { return; } try { String unitPrefix = toPrimaryKeyPrefix(unitId); Collection<? extends IndexDocument> queryRes = index.query(TextIndexStorageManager.FIELD_UNIT_ID, unitPrefix, Queries.QueryKind.EXACT, "_sn"); // NOI18N TreeSet<String> keys = new TreeSet<String>(); for (IndexDocument doc : queryRes) { keys.add(doc.getPrimaryKey()); } for (String pk : keys) { index.removeDocument(pk); } } catch (IOException ex) { ex.printStackTrace(System.err); } catch (InterruptedException ex) { // don't report interrupted exception } }
synchronized void store() { if (unsavedQueue.isEmpty()) { return; } long start = System.currentTimeMillis(); StoreQueueEntry entry = unsavedQueue.poll(); while (entry != null) { final CndTextIndexKey key = entry.key; // use unitID+fileID for primary key, otherwise indexed files from different projects overwrite each others IndexDocument doc = IndexManager.createDocument(toPrimaryKey(key)); for (CharSequence id : entry.ids) { doc.addPair(TextIndexStorageManager.FIELD_IDS, id.toString(), true, false); } index.addDocument(doc); entry = unsavedQueue.poll(); } try { index.store(false); } catch (Exception ex) { Exceptions.printStackTrace(ex); } LOG.log(Level.FINE, "Cnd Text Index store took {0}ms", System.currentTimeMillis() - start); }
@Override public Collection<CndTextIndexKey> query(final CharSequence text) { // force store store(); try { // load light weight document with primary key field _sn only // it's enough to restore CndTextIndexKey, but reduces memory by not loading FIELD_IDS set Collection<? extends IndexDocument> queryRes = index.query(TextIndexStorageManager.FIELD_IDS, text.toString(), Queries.QueryKind.EXACT, "_sn"); // NOI18N HashSet<CndTextIndexKey> res = new HashSet<CndTextIndexKey>(queryRes.size()); for (IndexDocument doc : queryRes) { res.add(fromPrimaryKey(doc.getPrimaryKey())); } LOG.log(Level.FINE, "Cnd Text Index query for {0}:\n\t{1}", new Object[]{text, res}); return res; } catch (Exception ex) { //Exceptions.printStackTrace(ex); } return Collections.<CndTextIndexKey>emptyList(); }