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)

1  
concept BigDB {
2  
  S name;
3  
}
4  
5  
concept BigEntry {
6  
  new Ref<BigDB> db;
7  
  S globalID;
8  
  long fileNr, headerStart, start, length;
9  
  
10  
  S text() { ret bigDB_getText(this); }
11  
  
12  
  void setText(S text) {
13  
    bigDB_newStringEntry(text, this);
14  
  }
15  
  
16  
  toString { ret "BigEntry " + globalID; }
17  
}
18  
19  
sbool bigDB_inited;
20  
21  
svoid bigDB() {
22  
  lock dbLock();
23  
  if (bigDB_inited) ret;
24  
  bigDB_inited = true;
25  
  dbIndexing(BigEntry, 'globalID);
26  
  bigDB_standardDB();
27  
}
28  
29  
static BigDB bigDB_standardDB() {
30  
  ret uniq(BigDB, name := "");
31  
}
32  
33  
static BigEntry bigDB_newStringEntry(S text) {
34  
  ret bigDB_newStringEntry(text, null);
35  
}
36  
37  
static BigEntry bigDB_newStringEntry(S text, BigEntry entry) {
38  
  lock dbLock();
39  
  BigDB db = entry != null ? entry.db! : bigDB_standardDB();
40  
  long fileNr = bigDB_fileNrToAppendTo();
41  
  S globalID = entry != null ? entry.globalID : aGlobalID();
42  
  File file = bigDB_file(db, fileNr);
43  
  long length = fileSize(file);
44  
  long l = lUtf8(text);
45  
  S header = globalID + " " + l + "\n";
46  
  long start = length + lUtf8(header);
47  
  appendToTextFile(file, header + text + "\n");
48  
  if (entry == null) entry = cnew(BigEntry, +db, +globalID);
49  
  cset(entry, +fileNr, headerStart := length, +start, length := l);
50  
  ret entry;
51  
}
52  
53  
static long bigDB_fileNrToAppendTo() {
54  
  ret 1;
55  
}
56  
57  
static File bigDB_file(BigDB db, long nr) {
58  
  ret getProgramFile("db." + urlencode(appendIfNempty(unnull(db.name), ".")) + nr);
59  
}
60  
61  
static S bigDB_getText(BigEntry entry) {
62  
  File f = bigDB_file(entry.db!, entry.fileNr);
63  
  ret fromUtf8(loadBinaryFilePart(f, entry.start, entry.start+entry.length));
64  
}

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: 465 / 495
Version history: 9 change(s)
Referenced in: [show references]