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

128
LINES

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

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

Transpiled version (16217L) is out of date.

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 ConceptCreate)
      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 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031072
Snippet name: ConceptsShadowLogger [dev.]
Eternal ID of this version: #1031072/25
Text MD5: 14499b6b0b22403ccb2b7868752b0723
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-22 01:08:12
Source code size: 3365 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 228 / 596
Version history: 24 change(s)
Referenced in: [show references]