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.

1  
sclass ConceptsShadowLogger implements AutoCloseable {
2  
  // concepts we are logging
3  
  Concepts cc;
4  
  int delay = 500; // how long to wait before writing changes (ms)
5  
  bool verbose;
6  
  
7  
  // all the shadows, sorted by ID
8  
  L<ConceptShadow> shadows;
9  
  
10  
  // IDs of concepts that have changed (fullChange = all changed)
11  
  new HashSet<Long> changedIDs;
12  
  bool fullChange;
13  
  
14  
  // where to send the shadow log (optional)
15  
  PrintWriter writer;
16  
  
17  
  // our internal lock & queue
18  
  Lock lock = lock();
19  
  transient Q q = startQ("ConceptsShadowLogger");
20  
21  
  IVF1<ConceptsChange> changeHandler = change -> {
22  
    lock lock;
23  
    if (change cast ConceptChange)
24  
      changedIDs.add(change.c.id);
25  
    else if (change cast ConceptCreate)
26  
      changedIDs.add(change.c.id);
27  
    else if (change cast ConceptDelete)
28  
      changedIDs.add(change.id);
29  
    else if (change cast FullChange)
30  
      fullChange = true;
31  
  };
32  
  
33  
  *() {}
34  
  *(Concepts *cc) {}
35  
  
36  
  void install {
37  
    makeShadows();
38  
    cc.onChange(changeHandler);
39  
    //cc.addPreSave();
40  
  }
41  
  
42  
  void uninstall {
43  
    cc.removeChangeListener(changeHandler);
44  
  }
45  
  
46  
  void makeShadows {
47  
    shadows = allConceptShadows(cc);
48  
  }
49  
  
50  
  void flush {
51  
    q.add(r flush_impl);
52  
  }
53  
  
54  
  void flush_impl {
55  
    sleep(delay);
56  
57  
    Set<Long> changedIDs;
58  
    {
59  
      lock lock;
60  
      if (fullChange) {
61  
        changedIDs = allConceptIDs(cc);
62  
        fullChange = false;
63  
      } else {
64  
        changedIDs = this.changedIDs;
65  
        this.changedIDs = new HashSet;
66  
      }
67  
    }
68  
    
69  
    // make new shadows (id, shadow) - shadow will be null when object was deleted
70  
    LPair<Long, ConceptShadow> newShadows = map(sorted(changedIDs),
71  
      id -> pair(id, conceptShadow(cc.getConcept(id))));
72  
    new L<CreatedDeletedChanged<ConceptShadow>> diffs;
73  
    new L<ConceptShadow> updatedShadows;
74  
    
75  
    int i1 = 0, i2 = 0;
76  
    LPair<Long, ConceptShadow> l1 = newShadows;
77  
    L<ConceptShadow> l2 = shadows;
78  
    while (i1 < l(l1) && i2 < l(l2)) {
79  
      long id1, ConceptShadow s1 = unpair newShadows.get(i1);
80  
      ConceptShadow s2 = shadows.get(i2);
81  
      long id2 = s2.id();
82  
      if (id1 < id2) {
83  
        diffs.add(new CreatedDeletedChanged.Created(s1));
84  
        updatedShadows.add(s1);
85  
        ++i1;
86  
      } else if (id1 > id2) {
87  
        updatedShadows.add(s2);
88  
        ++i2;
89  
      } else {
90  
        if (s1 == null)
91  
          diffs.add(new CreatedDeletedChanged.Deleted(s2));
92  
        else {
93  
          if (eq(s1, s2))
94  
            print("Unchanged concept: " + id1);
95  
          else
96  
            diffs.add(new CreatedDeletedChanged.Changed(s2, s1));
97  
          updatedShadows.add(s1);
98  
        }
99  
        ++i1;
100  
        ++i2;
101  
      }
102  
    }
103  
      
104  
    while (i1 < l(l1)) {
105  
      long id, ConceptShadow s = unpair l1.get(i1++);
106  
      if (s != null) {
107  
        diffs.add(new CreatedDeletedChanged.Created(s));
108  
        updatedShadows.add(s);
109  
      } else
110  
        print("Concept already gone again: " + id);
111  
    }
112  
    updatedShadows.addAll(subList(l2, i2));
113  
    
114  
    shadows = updatedShadows;
115  
116  
    if (verbose)
117  
      pnl("SHADOW DIFF", diffs);
118  
    
119  
    if (writer != null && nempty(diffs)) {
120  
      writer.println(structure(diffs));
121  
      writer.flush();
122  
    }
123  
  }
124  
  
125  
  public void close {
126  
    runInQAndWait(q, r { dispose writer });
127  
  }
128  
}

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: 230 / 599
Version history: 24 change(s)
Referenced in: [show references]