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

126
LINES

< > BotCompany Repo | #1031072 - ConceptsShadowLogger [dev.]

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

Libraryless. Click here for Pure Java version (16217L/104K).

sclass ConceptsShadowLogger implements AutoCloseable {
  // concepts we are logging
  Concepts cc;
  int delay = 500; // how long to wait before writing changes (ms)
  bool verbose;
  
  // all the shadows, sorted by ID
  L<ConceptShadow> shadows;
  
  // IDs of concepts that have changed (fullChange = all changed)
  new HashSet<Long> changedIDs;
  bool fullChange;
  
  // where to send the shadow log (optional)
  PrintWriter writer;
  
  // our internal lock & queue
  Lock lock = lock();
  transient Q q = startQ("ConceptsShadowLogger");

  IVF1<ConceptsChange> changeHandler = change -> {
    lock lock;
    if (change cast ConceptChange)
      changedIDs.add(change.c.id);
    else if (change cast ConceptDelete)
      changedIDs.add(change.id);
    else if (change cast FullChange)
      fullChange = true;
  };
  
  *() {}
  *(Concepts *cc) {}
  
  void install {
    makeShadows();
    cc.onChange(changeHandler);
    //cc.addPreSave();
  }
  
  void uninstall {
    cc.removeChangeListener(changeHandler);
  }
  
  void makeShadows {
    shadows = allConceptShadows(cc);
  }
  
  void flush {
    q.add(r flush_impl);
  }
  
  void flush_impl {
    sleep(delay);

    Set<Long> changedIDs;
    {
      lock lock;
      if (fullChange) {
        changedIDs = allConceptIDs(cc);
        fullChange = false;
      } else {
        changedIDs = this.changedIDs;
        this.changedIDs = new HashSet;
      }
    }
    
    // make new shadows (id, shadow) - shadow will be null when object was deleted
    LPair<Long, ConceptShadow> newShadows = map(sorted(changedIDs),
      id -> pair(id, conceptShadow(cc.getConcept(id))));
    new L<CreatedDeletedChanged<ConceptShadow>> diffs;
    new L<ConceptShadow> updatedShadows;
    
    int i1 = 0, i2 = 0;
    LPair<Long, ConceptShadow> l1 = newShadows;
    L<ConceptShadow> l2 = shadows;
    while (i1 < l(l1) && i2 < l(l2)) {
      long id1, ConceptShadow s1 = unpair newShadows.get(i1);
      ConceptShadow s2 = shadows.get(i2);
      long id2 = s2.id();
      if (id1 < id2) {
        diffs.add(new CreatedDeletedChanged.Created(s1));
        updatedShadows.add(s1);
        ++i1;
      } else if (id1 > id2) {
        updatedShadows.add(s2);
        ++i2;
      } else {
        if (s1 == null)
          diffs.add(new CreatedDeletedChanged.Deleted(s2));
        else {
          if (eq(s1, s2))
            print("Unchanged concept: " + id1);
          else
            diffs.add(new CreatedDeletedChanged.Changed(s2, s1));
          updatedShadows.add(s1);
        }
        ++i1;
        ++i2;
      }
    }
      
    while (i1 < l(l1)) {
      long id, ConceptShadow s = unpair l1.get(i1++);
      if (s != null) {
        diffs.add(new CreatedDeletedChanged.Created(s));
        updatedShadows.add(s);
      } else
        print("Concept already gone again: " + id);
    }
    updatedShadows.addAll(subList(l2, i2));
    
    shadows = updatedShadows;

    if (verbose)
      pnl("SHADOW DIFF", diffs);
    
    if (writer != null && nempty(diffs)) {
      writer.println(structure(diffs));
      writer.flush();
    }
  }
  
  public void close {
    runInQAndWait(q, r { dispose writer });
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1031072
Snippet name: ConceptsShadowLogger [dev.]
Eternal ID of this version: #1031072/24
Text MD5: 1f42fa6cb26379c07e8abdf19dfb56f3
Transpilation MD5: f6c749edd93dedd36be62a81c825a191
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-05-01 16:50:57
Source code size: 3288 bytes / 126 lines
Pitched / IR pitched: No / No
Views / Downloads: 45 / 219
Version history: 23 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1031072 & http://1031072.tinybrain.de