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

128
LINES

< > BotCompany Repo | #1009918 // Local Snippets DB [OK, now with regular update every minute]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (12922L/93K).

!7

static double updateInterval = 60;
sbool infoBoxes, fullUpdateAtStart = true;
sbool snippetsDB_inited;

!include once #1014750 // CSnippet

static Thread downloadThread;

p-subst {
  //restartWith384MBHeap(); // for xfullgrab
  autoRestart10Mins();
  snippetsDB_init();
}

svoid snippetsDB_init() {
  lock programLock();
  if (snippetsDB_inited) ret;
  set snippetsDB_inited;
  mainConcepts.autoSaveInterval = 1000*60; // every minute
  mainConcepts.noXFullGrab = true;
  dbIndexing(CSnippet, 'id);
  for (CSnippet s) s.legacyCompaction();
  botWithCommandList("Snippets.");
  if (fullUpdateAtStart) fullDownload();
  doEvery(0, updateInterval, f update);
}

svoid fullDownload {
  L<Snippet> l = listAllSnippets();
  printWithTime(n(l, "snippet") + " on server");
  slurp(l);
  downloadSomeText();
}

static int slurp(L<Snippet> l) {
  lock dbLock();
  int changes = 0;
  for (Snippet s : l) {
    CSnippet cs = uniq(CSnippet, id := parseSnippetID(s.id));
    changes += cset(cs, title := s.title, type := s.type);
    if (neq(cs.md5, s.md5))
      changes += cset(cs, md5 := s.md5, text := null);
  }
  S text = n2(changes, "changed snippet");
  if (infoBoxes && changes != 0) infoBox(text);
  printWithTime(text);
  ret changes;
}

// with text
svoid update {
  L<Snippet> l = listRecentlyChangedSnippets();
  printWithTime(n(l, "snippet") + " on server");
  slurp(l);
  downloadSomeText();
}

answer {
  lock dbLock();
  if "full download" {
    fullDownload();
    ret "OK, have " + n(countConcepts(CSnippet), "snippet");
  }
  
  if "update" {
    update();
    ret "OK";
  }
  
  if "download some text" {
    if (downloadThread != null) ret "Already downloading";
    downloadSomeText();
    ret "Yo";
  }
  
  if "stop downloading" {
    cancelThread(downloadThread);
    downloadThread = null;
    ret "OK";
  }
  
  if "get snippet text *" {
    CSnippet c = findConcept(CSnippet, id := parseSnippetID($1));
    if (c == null) ret "not found";
    downloadText(c);
    ret "OK " + quote(c.text());
  }
  
  if "count" ret n2(countConcepts(CSnippet), "snippet");
  
  if "all snippet ids"
    ret struct(map fsI(sorted(collect(CSnippet, 'id))));

  if "kill" { cleanKillIn1(); ret "OK"; }
}

svoid downloadText(CSnippet s) {
  if (s.text != null) ret;
  cset(s, text := eq(s.md5, emptyMD5()) ? "" : toUtf8(loadSnippet(s.id)));
}

svoid downloadSomeText {
  lock dbLock();
  if (downloadThread != null) ret;
  downloadThread = startThread(r {
    try {
      int n = 0; for (CSnippet s) if (s.text != null) ++n;
      printWithTime("Have text for " + n + "/" + n(countConcepts(CSnippet), "concept");
      new ConcurrentEvaluator e;
      e.coresToUse = 10;
      for (final CSnippet s) {
        ping();
        if (s.text == null) e.add(r {
          ping();
          downloadText(s);
          /*++n;
          if ((n % 100) == 0)
            printWithTime("Have text for " + n + "/" + n(countConcepts(CSnippet), "concept");*/
        });
      }
      e.startWaitCleanUp();
    } finally {
      downloadThread = null;
    }
  });
}

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: #1009918
Snippet name: Local Snippets DB [OK, now with regular update every minute]
Eternal ID of this version: #1009918/55
Text MD5: ba551e38235e53fd6fecb37c8a9e5cd9
Transpilation MD5: af424bbc02729c6be69f1407cc047c05
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-06-03 01:06:34
Source code size: 3177 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 730 / 2982
Version history: 54 change(s)
Referenced in: #1014460 - Upload list of all snippet IDs (needs Local Snippets DB, #1009918)
#1014753 - Snippet Title Reasoning v1 [OK]
#1015245 - Bench loading all snippets
#1015253 - loadSnippetsDBForAnalysis
#1016026 - Snippets DB [Dyn Module, warning: loads whole snippet DB in memory]
#1016871 - Local Snippets DB v2 [with text on disk, OK]
#3000512 - Smart Bot's answer to: !eval centralRestartLog_raw()
#3000526 - Smart Bot's answer to: !fresh latestPrograms()