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).

asclass ConceptFieldIndexBase<A extends Concept, Val> implements IConceptIndex, IFieldIndex<A, Val>, IConceptCounter, AutoCloseable {
  Concepts concepts;
  Class<A> cc;
  S field;
  Map<A, Val> objectToValue = syncHashMap();
  MultiSetMap<Val, A> 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<A> cc, S field) { this(db_mainConcepts(), cc, field); }
  *(Concepts *concepts, Class<A> *cc, S *field) {
    this();
    concepts.addConceptIndex(this);
    updateAll();
    register();
    updateAll();
  }
  
  void updateAll() {
    for (A c : setToIndex())
      updateImpl(c);
  }
  
  Cl<A> 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<A> getAll(Val value) {
    ret valueToObject.get(value);
  }
  
  public synchronized L<Val> allValues() { ret cloneKeys_noSync(valueToObject.data); }
  
  public ItIt<A> objectIterator() {
    ret navigableMultiSetMapValuesIterator_concurrent(valueToObject, this);
  }
  
  public synchronized MultiSet<Val> allValues_multiSet() {
    ret multiSetMapToMultiSet(valueToObject);
  }
  
  public Class<? extends Concept> conceptClass() { ret cc; }
  public int countConcepts() { ret l(objectToValue); }
  public Cl<Concept> allConcepts() { ret (Cl) keys(objectToValue); }
  
  O mutex() { this; }
  
  close {
    concepts.removeConceptIndex(this);
  }
}

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: 265 / 525
Version history: 19 change(s)
Referenced in: #1031515 - ConceptFieldIndexBase (backup)
#1032783 - ConceptFieldIndexBase_certainValues
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)