Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

82
LINES

< > BotCompany Repo | #1031103 // ConceptFieldIndexBase

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (18838L/115K).

1  
asclass ConceptFieldIndexBase<A extends Concept, Val> implements IConceptIndex, IFieldIndex<A, Val>, IConceptCounter, AutoCloseable {
2  
  Concepts concepts;
3  
  Class<A> cc;
4  
  S field;
5  
  Map<A, Val> objectToValue = syncHashMap();
6  
  MultiSetMap<Val, A> valueToObject; // initialized in subclass
7  
  // 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)
8  
  
9  
  *() { init(); }
10  
  *(Class<A> cc, S field) { this(db_mainConcepts(), cc, field); }
11  
  *(Concepts *concepts, Class<A> *cc, S *field) {
12  
    this();
13  
    concepts.addConceptIndex(this);
14  
    updateAll();
15  
    register();
16  
    updateAll();
17  
  }
18  
  
19  
  void updateAll() {
20  
    for (A c : setToIndex())
21  
      updateImpl(c);
22  
  }
23  
  
24  
  Cl<A> setToIndex() {
25  
    ret list(concepts, cc);
26  
  }
27  
  
28  
  abstract void init();
29  
  abstract void register();
30  
31  
  public void update(Concept c) {
32  
    if (!isInstance(cc, c)) ret;
33  
    updateImpl((A) c);
34  
  }
35  
  
36  
  synchronized void updateImpl(A c) {
37  
    Val newValue = cast cget(c, field);
38  
    Val oldValue = objectToValue.get(c);
39  
    if (newValue == oldValue && (oldValue != null || objectToValue.containsKey(c))) ret;
40  
    valueToObject.remove(oldValue, c);
41  
    valueToObject.put(newValue, c);
42  
    put(objectToValue, c, newValue);
43  
  }
44  
  
45  
  public synchronized void remove(Concept c) {
46  
    if (!isInstance(cc, c)) ret;
47  
    if (!objectToValue.containsKey(c)) ret;
48  
    Val value = objectToValue.get(c);
49  
    objectToValue.remove(c);
50  
    valueToObject.remove(value, (A) c);
51  
  }
52  
  
53  
  synchronized A get(Val value) {
54  
    ret valueToObject.getFirst(value);
55  
  }
56  
  
57  
  // older version - may return empty list as null
58  
  // should be phased out everywhere!
59  
  public synchronized Cl<A> getAll(Val value) {
60  
    ret valueToObject.get(value);
61  
  }
62  
  
63  
  public synchronized L<Val> allValues() { ret cloneKeys_noSync(valueToObject.data); }
64  
  
65  
  public ItIt<A> objectIterator() {
66  
    ret navigableMultiSetMapValuesIterator_concurrent(valueToObject, this);
67  
  }
68  
  
69  
  public synchronized MultiSet<Val> allValues_multiSet() {
70  
    ret multiSetMapToMultiSet(valueToObject);
71  
  }
72  
  
73  
  public Class<? extends Concept> conceptClass() { ret cc; }
74  
  public int countConcepts() { ret l(objectToValue); }
75  
  public Cl<Concept> allConcepts() { ret (Cl) keys(objectToValue); }
76  
  
77  
  O mutex() { this; }
78  
  
79  
  close {
80  
    concepts.removeConceptIndex(this);
81  
  }
82  
}

Author comment

Began life as a copy of #1014416

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031103
Snippet name: ConceptFieldIndexBase
Eternal ID of this version: #1031103/20
Text MD5: fb7fd3ccb394d3ac8bc146f59f0d0031
Transpilation MD5: 7f6a6be7d8c05cbb7f83193d117488d8
Author: stefan
Category: javax / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-06 11:11:26
Source code size: 2480 bytes / 82 lines
Pitched / IR pitched: No / No
Views / Downloads: 194 / 429
Version history: 19 change(s)
Referenced in: [show references]