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

65
LINES

< > BotCompany Repo | #1007498 // DBWatcher - watch changes in a DB (live or from disk)

JavaX fragment (include)

1  
sclass DBWatcher {
2  
  S dbID;
3  
  RemoteDB db;
4  
  long dbDate;
5  
  long changeCount = 0;
6  
  long exportedChangeCount = 0;
7  
  
8  
  *() {}
9  
  *(S *dbID) {}
10  
  
11  
  bool live() { ret db != null; }
12  
  
13  
  void closeDB {
14  
    if (db != null) {
15  
      db.close();
16  
      db = null;
17  
    }
18  
  }
19  
  
20  
  void tryConnect {
21  
    closeDB();
22  
    pcall-short {
23  
      //print("Trying to connect to " + dbID);
24  
      db = new RemoteDB(dbID);
25  
      if (db != null) print("Connected to " + dbID + ".");
26  
    }
27  
  }
28  
  
29  
  void step {
30  
    bool liveBefore = live();
31  
    if (!live()) tryConnect();
32  
    if (!liveBefore && live()) changeCount = 0;
33  
    if (live() && getLiveData()) ret;
34  
    closeDB();
35  
    getStaticData();
36  
  }
37  
  
38  
  bool getLiveData() false on exception {
39  
    time2 {
40  
      long cc = db.xchangeCount();
41  
      int n = db.xcount();
42  
    }
43  
    bool change = cc != changeCount;
44  
    if (change) ++exportedChangeCount;
45  
    changeCount = cc;
46  
    print((change ? "CHANGE. " : "") + "Live change count: " + cc + ", objects: " + n);
47  
    true;
48  
  }
49  
  
50  
  bool getStaticData() false on exception {
51  
    File conceptsFile = getFilePossiblyGZipped(getProgramFile(dbID, "concepts.structure"));
52  
    long mod = conceptsFile.lastModified();
53  
    bool change = mod != dbDate;
54  
    if (change) ++exportedChangeCount;
55  
    dbDate = mod;
56  
    print((change ? "CHANGE. " : "") + "DB date: " + dbDate);
57  
    true;
58  
  }
59  
  
60  
  void run {
61  
    repeat with sleep 1 {
62  
      step();
63  
    }
64  
  }
65  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007498
Snippet name: DBWatcher - watch changes in a DB (live or from disk)
Eternal ID of this version: #1007498/1
Text MD5: 45707a712c4404b67674a35941a7ba52
Author: stefan
Category: javax / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-25 16:07:07
Source code size: 1479 bytes / 65 lines
Pitched / IR pitched: No / No
Views / Downloads: 536 / 1116
Referenced in: [show references]