!7
// not tested on null values
sclass FieldIndex implements IConceptIndex {
//Concepts concepts;
Class cc;
S field;
new HashMap objectToValue;
new MultiMap valueToObject;
*() {}
*(Concepts concepts, Class *cc, S *field) {
concepts.addConceptIndex(this);
for (A c : list(concepts, cc))
update(c);
}
public synchronized void update(Concept c) {
Val newValue = cast cget(c, field);
Val oldValue = objectToValue.get(c);
if (newValue != oldValue) {
valueToObject.remove(oldValue, (A) c);
valueToObject.put(newValue, (A) c);
objectToValue.put((A) c, newValue);
}
}
public synchronized void remove(Concept c) {
Val value = cast cget(c, field);
objectToValue.remove(c);
valueToObject.remove(value, (A) c);
}
synchronized A get(Val value) {
ret first(valueToObject.get(value));
}
synchronized L getAll(Val value) {
ret valueToObject.get(value);
}
}
concept CC {
S a, b;
}
p {
CC cc = nu(CC, a := "hello", b := "world");
FieldIndex index = new(mainConcepts, CC, "a");
assertEquals("world", index.get("hello").b);
assertNull(index.get("Hello"));
cset(cc, a := "yo");
assertNull(index.get("hello"));
assertEquals("world", index.get("yo").b);
cc.delete();
assertNull(index.get("yo"));
cc = nu(CC, a := "another", b := "test");
assertEquals("test", index.get("another").b);
print("OK");
}