sclass TripleIndex extends VirtualNodeIndex { SyncListMultiMap index = caseInsensitiveSyncListMultiMap(); SyncListMultiMap[] positionalIndices = repF(3, f caseInsensitiveSyncListMultiMap); new CompactHashSet websByID; bool activated; // are we the main index (see tripleIndex()) int size() { ret numWebs(); } int numWebs() { ret l(websByID); } int numTerms() { ret index.keysSize(); } Collection indexedTerms() { ret keys(index); } // query is shortened term L get(S query) { ret ai_triplesToWebNodes_lazyList(query, index.get(query)); } // query is shortened term L> getTripleRefs(S query) { ret ai_triplesToTripleRefs_lazyList(query, index.get(query)); } L> getTripleRefs(S query, int position) { ret ai_triplesToTripleRefs_lazyList(query, positionalIndices[position].get(query)); } // query is shortened term L getTriples(S query) { ret index.get(query); } bool hasShortTerm(S s) { ret index.containsKey(s); } Web getWeb(GlobalID id) { ret webFromTriple(websByID.find(dummyTripleWebWithGlobalID(id))); } void addWeb(Web web) { if (web == null) ret; TripleWeb w = ai_webToTripleWeb(web); if (w == null) fail("Skipping non-tripelizable web: " + webToStringShort(web)); addTriple(w); } void addTriple(TripleWeb w) { if (w == null) ret; synchronized(index) { for (S s : sortedInPlace(ll(w.a, w.b, w.c))) index.put(s, w); positionalIndices[0].add(w.a); positionalIndices[1].add(w.b); positionalIndices[2].add(w.c); } websByID.add(w); } void removeWeb(Web web) { if (web != null) removeTriple(ai_webToTripleWeb(web)); } void removeTriples(Collection l) { new MultiMap mm; for (TripleWeb w : l) { mm.put(ai_shortenForIndex(w.a), w); mm.put(ai_shortenForIndex(w.b), w); mm.put(ai_shortenForIndex(w.c), w); } for (S term : keys(mm)) { HashSet webs = asHashSet(mm.get(term)); indexRemoveMulti(term, webs); } } void removeTriple(TripleWeb w) { if (w == null) ret; GlobalID id = w.globalID(); websByID.remove(dummyTripleWebWithGlobalID(id)); indexRemove(ai_shortenForIndex(w.a), id); indexRemove(ai_shortenForIndex(w.b), id); indexRemove(ai_shortenForIndex(w.c), id); } // internal void indexRemove(S term, GlobalID globalID) { L l = index.get(term); for i over l: if (eq(l.get(i).globalID(), globalID)) { index.remove(term, l.get(i)); ret; } } // internal void indexRemoveMulti(S term, Set set) { L l = index.get(term); synchronized(l) { removeSetFromListQuickly(l, set); } } void clear() { index.clear(); websByID.clear(); } void activate { if (!activated) { activated = true; ai_onNewOrRemovedWeb(func(Web web) { addWeb(web); false; }, func(Web web) { removeWeb(web); false; }); } } Collection allTriples() { ret websByID; } }