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

64
LINES

< > BotCompany Repo | #1010475 // bigDB - Bigger-Than-RAM DB

JavaX fragment (include)

concept BigDB {
  S name;
}

concept BigEntry {
  new Ref<BigDB> db;
  S globalID;
  long fileNr, headerStart, start, length;
  
  S text() { ret bigDB_getText(this); }
  
  void setText(S text) {
    bigDB_newStringEntry(text, this);
  }
  
  toString { ret "BigEntry " + globalID; }
}

sbool bigDB_inited;

svoid bigDB() {
  lock dbLock();
  if (bigDB_inited) ret;
  bigDB_inited = true;
  dbIndexing(BigEntry, 'globalID);
  bigDB_standardDB();
}

static BigDB bigDB_standardDB() {
  ret uniq(BigDB, name := "");
}

static BigEntry bigDB_newStringEntry(S text) {
  ret bigDB_newStringEntry(text, null);
}

static BigEntry bigDB_newStringEntry(S text, BigEntry entry) {
  lock dbLock();
  BigDB db = entry != null ? entry.db! : bigDB_standardDB();
  long fileNr = bigDB_fileNrToAppendTo();
  S globalID = entry != null ? entry.globalID : aGlobalID();
  File file = bigDB_file(db, fileNr);
  long length = fileSize(file);
  long l = lUtf8(text);
  S header = globalID + " " + l + "\n";
  long start = length + lUtf8(header);
  appendToTextFile(file, header + text + "\n");
  if (entry == null) entry = cnew(BigEntry, +db, +globalID);
  cset(entry, +fileNr, headerStart := length, +start, length := l);
  ret entry;
}

static long bigDB_fileNrToAppendTo() {
  ret 1;
}

static File bigDB_file(BigDB db, long nr) {
  ret getProgramFile("db." + urlencode(appendIfNempty(unnull(db.name), ".")) + nr);
}

static S bigDB_getText(BigEntry entry) {
  File f = bigDB_file(entry.db!, entry.fileNr);
  ret fromUtf8(loadBinaryFilePart(f, entry.start, entry.start+entry.length));
}

Author comment

Began life as a copy of #1010406

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: #1010475
Snippet name: bigDB - Bigger-Than-RAM DB
Eternal ID of this version: #1010475/10
Text MD5: 129b7ec6b4a0220b8257e4a4d0ec5ade
Author: stefan
Category: javax / concepts
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-08 23:41:16
Source code size: 1631 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 464 / 494
Version history: 9 change(s)
Referenced in: [show references]