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)

sclass DBWatcher {
  S dbID;
  RemoteDB db;
  long dbDate;
  long changeCount = 0;
  long exportedChangeCount = 0;
  
  *() {}
  *(S *dbID) {}
  
  bool live() { ret db != null; }
  
  void closeDB {
    if (db != null) {
      db.close();
      db = null;
    }
  }
  
  void tryConnect {
    closeDB();
    pcall-short {
      //print("Trying to connect to " + dbID);
      db = new RemoteDB(dbID);
      if (db != null) print("Connected to " + dbID + ".");
    }
  }
  
  void step {
    bool liveBefore = live();
    if (!live()) tryConnect();
    if (!liveBefore && live()) changeCount = 0;
    if (live() && getLiveData()) ret;
    closeDB();
    getStaticData();
  }
  
  bool getLiveData() false on exception {
    time2 {
      long cc = db.xchangeCount();
      int n = db.xcount();
    }
    bool change = cc != changeCount;
    if (change) ++exportedChangeCount;
    changeCount = cc;
    print((change ? "CHANGE. " : "") + "Live change count: " + cc + ", objects: " + n);
    true;
  }
  
  bool getStaticData() false on exception {
    File conceptsFile = getFilePossiblyGZipped(getProgramFile(dbID, "concepts.structure"));
    long mod = conceptsFile.lastModified();
    bool change = mod != dbDate;
    if (change) ++exportedChangeCount;
    dbDate = mod;
    print((change ? "CHANGE. " : "") + "DB date: " + dbDate);
    true;
  }
  
  void run {
    repeat with sleep 1 {
      step();
    }
  }
}

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: 466 / 1037
Referenced in: [show references]