asclass ConceptFieldIndexBase implements IConceptIndex, IFieldIndex, IConceptCounter, AutoCloseable {
Concepts concepts;
Class cc;
S field;
Map objectToValue = syncHashMap();
MultiSetMap valueToObject; // initialized in subclass
// bool indexNulls = true; // set this only when there are few null values in this field in the db. or when the point is to index a non-existing field (for singletons)
*() { init(); }
*(Class cc, S field) { this(db_mainConcepts(), cc, field); }
*(Concepts *concepts, Class *cc, S *field) {
this();
concepts.addConceptIndex(this);
updateAll();
register();
updateAll();
}
void updateAll() {
for (A c : setToIndex())
updateImpl(c);
}
Cl setToIndex() {
ret list(concepts, cc);
}
abstract void init();
abstract void register();
public void update(Concept c) {
if (!isInstance(cc, c)) ret;
updateImpl((A) c);
}
synchronized void updateImpl(A c) {
Val newValue = cast cget(c, field);
Val oldValue = objectToValue.get(c);
if (newValue == oldValue && (oldValue != null || objectToValue.containsKey(c))) ret;
valueToObject.remove(oldValue, c);
valueToObject.put(newValue, c);
put(objectToValue, c, newValue);
}
public synchronized void remove(Concept c) {
if (!isInstance(cc, c)) ret;
if (!objectToValue.containsKey(c)) ret;
Val value = objectToValue.get(c);
objectToValue.remove(c);
valueToObject.remove(value, (A) c);
}
synchronized A get(Val value) {
ret valueToObject.getFirst(value);
}
// older version - may return empty list as null
// should be phased out everywhere!
public synchronized Cl getAll(Val value) {
ret valueToObject.get(value);
}
public synchronized L allValues() { ret cloneKeys_noSync(valueToObject.data); }
public ItIt objectIterator() {
ret navigableMultiSetMapValuesIterator_concurrent(valueToObject, this);
}
public synchronized MultiSet allValues_multiSet() {
ret multiSetMapToMultiSet(valueToObject);
}
public Class extends Concept> conceptClass() { ret cc; }
public int countConcepts() { ret l(objectToValue); }
public Cl allConcepts() { ret (Cl) keys(objectToValue); }
O mutex() { this; }
close {
concepts.removeConceptIndex(this);
}
}