Skip to content

Commit

Permalink
book & rad APIs stabilizing
Browse files Browse the repository at this point in the history
  • Loading branch information
amark committed Jan 16, 2024
1 parent dc5f90a commit d7f1947
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 61 deletions.
22 changes: 15 additions & 7 deletions lib/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var sT = setTimeout, B = sT.Book || (sT.Book = function(text){
return b.set(word, is);
};
// TODO: if from text, preserve the separator symbol.
b.list = [{from: text, size: (text||'').length, substring: sub, toString: to, book: b, get: b}];
b.list = [{from: text, size: (text||'').length, substring: sub, toString: to, book: b, get: b, read: list}];
b.page = page;
b.set = set;
b.get = get;
Expand All @@ -27,7 +27,7 @@ var sT = setTimeout, B = sT.Book || (sT.Book = function(text){

function page(word){
var b = this, l = b.list, i = spot(word, l, b.parse), p = l[i];
if('string' == typeof p){ l[i] = p = {size: -1, first: b.parse? b.parse(p) : p, substring: sub, toString: to, book: b, get: b} } // TODO: test, how do we arrive at this condition again?
if('string' == typeof p){ l[i] = p = {size: -1, first: b.parse? b.parse(p) : p, substring: sub, toString: to, book: b, get: b, read: list} } // TODO: test, how do we arrive at this condition again?
//p.i = i;
return p;
// TODO: BUG! What if we get the page, it turns out to be too big & split, we must then RE get the page!
Expand Down Expand Up @@ -55,12 +55,12 @@ function got(word, page){
a = slot(has); // edge case bug?
if(word != B.decode(a[0])){ return }
}
has = l[i] = b.all[word] = {word: word, is: B.decode(a[1]), page: page, substring: subt, toString: tot}; // TODO: convert to a JS value!!! Maybe index! TODO: BUG word needs a page!!!! TODO: Check for other types!!!
has = l[i] = b.all[word] = {word: ''+word, is: B.decode(a[1]), page: page, substring: subt, toString: tot}; // TODO: convert to a JS value!!! Maybe index! TODO: BUG word needs a page!!!! TODO: Check for other types!!!
return has.is;
}

function spot(word, sorted, parse){ parse = parse || spot.no || (spot.no = function(t){ return t }); // TODO: BUG???? Why is there substring()||0 ? // TODO: PERF!!! .toString() is +33% faster, can we combine it with the export?
var L = sorted, min = 0, page, found, l = word.length, max = L.length, i = max/2;
var L = sorted, min = 0, page, found, l = (word=''+word).length, max = L.length, i = max/2;
while(((word < (page = (parse(L[i=i>>0])||'').substring())) || ((parse(L[i+1])||'').substring() <= word)) && i != min){ // L[i] <= word < L[i+1]
i += (page <= word)? (max - (min = i))/2 : -((max = i) - min)/2;
}
Expand All @@ -73,11 +73,17 @@ function from(a, t, l){
(l = a.from = slot(t = t||a.from||'')).toString = join;
return l;
}
function list(each){ each = each || function(x){return x}
// TODO: BUG!!! in limbo items need to get situated before calling this, if there are any. (obviously, we shouldn't do it again if limbo has previously been sorted).
var i = 0, l = from(this)||[], w, r = [], p = this.book.parse || function(){};
while(w = l[i++]){ r.push(each(slot(w)[1],p(w)||w,this)) }
return r;
}

function set(word, is){
var b = this, has = b.all[word];
if(has){ return b(word, is) } // updates to in-memory items will always match exactly.
var page = b.page(word), tmp; // before we assume this is an insert tho, we need to check
var page = b.page(word=''+word), tmp; // before we assume this is an insert tho, we need to check
if(page && page.from){ // if it could be an update to an existing word from parseless.
b.get(word);
if(b.all[word]){ return b(word, is) }
Expand All @@ -100,7 +106,7 @@ function split(p, b){ // TODO: use closest hash instead of half.
//while(tmp = L[i++]){ }
var L = p.limbo = sort(p.limbo), l = L.length, i = l/2 >> 0, j = i, half = L[j], tmp;
//console.timeEnd();
var next = {limbo: [], first: half.substring(), size: 0, substring: sub, toString: to, book: b, get: b}, nl = next.limbo;
var next = {limbo: [], first: half.substring(), size: 0, substring: sub, toString: to, book: b, get: b, read: list}, nl = next.limbo;
nl.toString = join;
//console.time();
while(tmp = L[i++]){
Expand Down Expand Up @@ -131,9 +137,11 @@ function heal(l, s){ var i, e;
function size(t){ return (t||'').length||1 } // bits/numbers less size? Bug or feature?
function subt(i,j){ return this.word }
//function tot(){ return this.text = this.text || "'"+(this.word)+"'"+(this.is)+"'" }
function tot(){ var tmp;
function tot(){ var tmp = {};
//if((tmp = this.page) && tmp.saving){ delete tmp.book.all[this.word]; } // TODO: BUG! Book can't know about RAD, this was from RAD, so this MIGHT be correct but we need to refactor. Make sure to add tests that will re-trigger this.
return this.text = this.text || ":"+B.encode(this.word)+":"+B.encode(this.is)+":";
tmp[this.word] = this.is;
return this.text = this.text || B.encode(tmp,'|',':').slice(1,-1);
//return this.text = this.text || "'"+(this.word)+"'"+(this.is)+"'";
}
function sub(i,j){ return (this.first||this.word||B.decode((from(this)||'')[0]||'')).substring(i,j) }
Expand Down
68 changes: 35 additions & 33 deletions lib/radisk3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

var has = (sT.RAD.has || (sT.RAD.has = {}))[opt.file];
if(has){ return has }
var r = function rad(word, is, reply){
var r = function rad(word, is, reply){ r.word = word;
if(!b){ start(word, is, reply); return r }
if(is === undefined || 'function' == typeof is){ // THIS IS A READ:
var page = b.page(word);
if(page.from){
return is(null, page);
}
read(word, is, page); // get from disk
return
if(page.from){ return is && is(page, null), r }
return read(word, is, page), r; // get from disk
}
//console.log("OFF");return;
// ON WRITE:
Expand All @@ -25,15 +22,16 @@
write(word, reply);
return r;
}, /** @param b the book */ b;
r.then = function(cb, p){ return p = (new Promise(function(yes, no){ r(r.word, yes) })), cb? p.then(cb) : p }
r.read = r.results = function(cb){ return (new Promise(async function(yes, no){ yes((await r(r.word)).read(cb)) })) }

async function read(word, reply, page){
async function read(word, reply, page){ // TODO: this function doesn't do much, inline it???
if(!reply){ return }
var p = page || b.page(word);
reply = reply.call ? reply : () => { };
log(`read() ${word.slice(0, 40)}`);
get(p, function(err, disk){
if(err){ log("ERR! in read() get() cb", err); reply(err); return }
if(err){ log("ERR! in read() get() cb", err); reply(p.no, err); return }
p.from = disk || p.from;
reply(null, p, b);
reply(p, null, b);
})
}

Expand Down Expand Up @@ -68,7 +66,7 @@
RAD.put(file, data, function(err, ok){
delete put[file];
cb && cb(err, ok);
});
}, opt);
};
function get(file, cb){
var tmp;
Expand All @@ -78,9 +76,9 @@
if(tmp = put[file = fname(file)]){ cb(u, tmp.data); return }
if(tmp = get[file]){ tmp.push(cb); return } get[file] = [cb];
RAD.get(file, function(err, data){
tmp = get[file]; delete get[file];
tmp = get[file]||''; delete get[file];
var i = -1, f; while (f = tmp[++i]){ f(err, data) } // TODO: BUG! CPU SCHEDULE?
});
}, opt);
};

function start(word, is, reply){
Expand All @@ -90,7 +88,7 @@
if(b){ r(word, is, reply); return }
b = r.book = Book();
if((d = Book.slot(d)).length){ b.list = d } // TODO: BUG! Add some other sort of corrupted/error check here?
watch(b).parse = function(t){ return ('string' == typeof t)? Book.decode(Book.slot(t)[0]) : t } // TODO: This was ugly temporary, but is necessary, and is logically correct, but is there a cleaner, nicer, less assumptiony way to do it?
watch(b).parse = function(t){ return ('string' == typeof t)? Book.decode(Book.slot(t)[0]) : t } // TODO: This was ugly temporary, but is necessary, and is logically correct, but is there a cleaner, nicer, less assumptiony way to do it? // TODO: SOLUTION?! I think this needs to be in Book, not RAD.
r(word, is, reply);
})
}
Expand All @@ -116,7 +114,8 @@
}

function ename(t){ return encodeURIComponent(t).replace(/\*/g, '%2A').slice(0, 250) }
function fname(p){ return opt.file + '/' + ename(p.substring()) }
//function fname(p){ return opt.file + '/' + ename(p.substring()) }
function fname(p){ return ename(p.substring()) }


function valid(word, is, reply){
Expand All @@ -131,6 +130,7 @@

return r;
}), MAX = 1000/* 300000000 */;
sT.each = sT.each || function(l,f){l.forEach(f)};

try { module.exports = RAD } catch (e){ }

Expand Down Expand Up @@ -169,13 +169,13 @@
if(!fs){ return }

var sT = setTimeout, RAD = sT.RAD;
RAD.put = function(file, data, cb){
fs.writeFile(file, data, cb);
RAD.put = function(file, data, cb, opt){
fs.writeFile(opt.file+'/'+file, data, cb);
}
RAD.get = function(file, cb){
fs.readFile(file, function(err, data){
if(err && 'ENOENT' === (err.code || '').toUpperCase()){ return cb() }
cb(err, data.toString());
RAD.get = function(file, cb, opt){
fs.readFile(opt.file+'/'+file, function(err, data){
if(err && 'ENOENT' === (err.code||'').toUpperCase()){ return cb() }
cb(err, (data||'').toString()||data);
});
}
}());
Expand All @@ -187,28 +187,30 @@
if(!lS){ return }

var sT = setTimeout, RAD = sT.RAD;
RAD.put = function(file, data, cb){
RAD.put = function(file, data, cb, opt){
setTimeout(function(){
lS[file] = data;
lS[opt.file+'/'+file] = data;
cb(null, 1);
},9);
},1);
}
RAD.get = function(file, cb){
RAD.get = function(file, cb, opt){
setTimeout(function(){
cb(null, lS[file]);
},9);
cb(null, lS[opt.file+'/'+file]);
},1);
}
}());

;(function(){ return;
var get;
try { get = fetch } catch (e){ };
try { get = fetch } catch (e){ console.log("WARNING! need `npm install [email protected]`"); get = fetch = require('node-fetch') };
if(!get){ return }

var sT = setTimeout, RAD = sT.RAD;
RAD.put = function(file, data, cb){ cb(401) }
RAD.get = async function(file, cb){
var t = (await (await fetch('http://localhost:8765/gun/'+file)).text());
var sT = setTimeout, RAD = sT.RAD, put = RAD.put, get = RAD.get;
RAD.put = function(file, data, cb, opt){ put && put(file, data, cb, opt);
cb(401)
}
RAD.get = async function(file, cb, opt){ get && get(file, cb, opt);
var t = (await (await fetch('http://localhost:8766/gun/1data/'+file)).text());
if('404' == t){ cb(); return }
cb(null, t);
}
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d7f1947

Please sign in to comment.