Reverse RAD!

This commit is contained in:
Mark Nadal 2019-04-22 17:44:38 -07:00
parent 9cb1a52fc0
commit 4085a4053c
9 changed files with 79 additions and 40 deletions

View File

@ -236,10 +236,11 @@
// if a node is requested and some of it is cached... the other parts might not be.
//}
}
var g = function Get(){}, tmp;
g.lex = function(file){
var g = function Get(){};
g.lex = function(file){ var tmp;
file = (u === file)? u : decodeURIComponent(file);
if(!file || file > (o.next || key || o.start || o.end || '')){
tmp = o.next || key || (o.reverse? o.end || '\uffff' : o.start || '');
if(!file || (o.reverse? file < tmp : file > tmp)){
if(o.next){ g.file = file }
if(tmp = Q[g.file]){
tmp.push({key: key, ack: cb, file: g.file, opt: o});
@ -272,6 +273,7 @@
o.next = as.file;
r.read(tmp, as.ack, o);
}
if(o.reverse){ g.lex.reverse = true }
r.list(g.lex);
}
}());
@ -373,9 +375,10 @@
var dir, q, f = String.fromCharCode(28), ef = ename(f);
r.list = function(cb){
if(dir){
var tmp = {reverse: (cb.reverse)? 1 : 0};
Radix.map(dir, function(val, key){
return cb(key);
}) || cb();
}, tmp) || cb();
return;
}
if(q){ return q.push(cb) } q = [cb];
@ -420,9 +423,7 @@
r.list.dir = dir = rad;
tmp = q; q = null;
Gun.list.map(tmp, function(cb){
Radix.map(dir, function(val, key){
return cb(key);
}) || cb();
r.list(cb);
});
}
}());

View File

@ -57,6 +57,7 @@
var keys = (t[_]||no).sort || (t[_] = function $(){ $.sort = Object.keys(t).sort(); return $ }()).sort;
//var keys = Object.keys(t).sort();
opt = (true === opt)? {branch: true} : (opt || {});
if(opt.reverse){ keys = keys.slice().reverse() }
var start = opt.start, end = opt.end;
var i = 0, l = keys.length;
for(;i < l; i++){ var key = keys[i], tree = t[key], tmp, p, pt;

View File

@ -16,6 +16,9 @@
}}catch(e){}
var store = function Store(){};
if(Store[opt.file]){ return Store[opt.file] }
Store[opt.file] = store;
store.start = function(){
var o = indexedDB.open(opt.file, 1);
o.onupgradeneeded = function(eve){ (eve.target.result).createObjectStore(opt.file) }

View File

@ -61,6 +61,7 @@ Gun.on('create', function(root){
if((tmp = get['%']) || opt.limit){
opt.limit = (tmp <= (opt.pack || (1000 * 100)))? tmp : 1;
}
if(has['-'] || (soul||{})['-']){ opt.reverse = true }
//console.log("RAD get:", key, opt);
//var start = (+new Date); // console.log("GET!", id, JSON.stringify(key));
rad(key||'', function(err, data, o){

View File

@ -1,6 +1,6 @@
{
"name": "gun",
"version": "0.2019.416",
"version": "0.2019.422",
"description": "A realtime, decentralized, offline-first, graph data synchronization engine.",
"main": "index.js",
"browser": "gun.js",

View File

@ -1,3 +1,4 @@
var Gun = require('../index');
var Type = require('../type');
@ -20,7 +21,7 @@ function Mesh(ctx){
return;
}
// add hook for AXE?
if (Gun.AXE && opt && opt.super) { Gun.AXE.say(msg, mesh.say, this); return; } // rogowski
if (Gun.AXE) { Gun.AXE.say(msg, mesh.say, this); return; }
mesh.say(msg);
}
@ -193,26 +194,30 @@ function Mesh(ctx){
tmp = tmp.id = tmp.id || Type.text.random(9);
mesh.say({dam: '?'}, opt.peers[tmp] = peer);
}
if(!tmp.hied){ ctx.on(tmp.hied = 'hi', peer) }
tmp = peer.queue; peer.queue = [];
Type.obj.map(tmp, function(msg){
mesh.say(msg, peer);
});
if(!tmp.hied){ ctx.on(tmp.hied = 'hi', peer); }
// tmp = peer.queue; peer.queue = [];
// Type.obj.map(tmp, function(msg){
// mesh.say(msg, peer);
// });
}
mesh.bye = function(peer){
Type.obj.del(opt.peers, peer.id); // assume if peer.url then reconnect
ctx.on('bye', peer);
}
mesh.hear['!'] = function(msg, peer){ opt.log('Error:', msg.err) }
mesh.hear['?'] = function(msg, peer){
if(!msg.pid){
return mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer);
// return mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer);
mesh.say({dam: '?', pid: opt.pid, '@': msg['#']}, peer);
var tmp = peer.queue; peer.queue = [];
Type.obj.map(tmp, function(msg){
mesh.say(msg, peer);
});
return;
}
peer.id = peer.id || msg.pid;
mesh.hi(peer);
}
return mesh;
}
@ -233,4 +238,4 @@ Mesh.hash = function(s){ // via SO
try{ module.exports = Mesh }catch(e){}

View File

@ -29,7 +29,7 @@ function map(msg){
function each(v,k){
if(n_ === k){ return }
var msg = this.msg, gun = msg.$, at = gun._, cat = this.at, tmp = at.lex;
if(tmp && !Gun.text.match(k, tmp['.'] || tmp['#'] || tmp)){ return } // TODO: Ugly hack!
if(tmp && !Gun.text.match(k, tmp['.'] || tmp['#'] || tmp)){ return } // review?
((tmp = gun.get(k)._).echo || (tmp.echo = {}))[cat.id] = tmp.echo[cat.id] || cat;
}
var obj_map = Gun.obj.map, noop = function(){}, event = {stun: noop, off: noop}, n_ = Gun.node._, u;

View File

@ -18,28 +18,22 @@ Type.text.random = function(l, c){
while(l > 0){ s += c.charAt(Math.floor(Math.random() * c.length)); l-- }
return s;
}
Type.text.match = function(t, o){ var r = false;
t = t || '';
o = Type.text.is(o)? {'=': o} : o || {}; // {'~', '=', '*', '<', '>', '+', '-', '?', '!'} // ignore case, exactly equal, anything after, lexically larger, lexically lesser, added in, subtacted from, questionable fuzzy match, and ends with.
if(Type.obj.has(o,'~')){ t = t.toLowerCase(); o['='] = (o['='] || o['~']).toLowerCase() }
if(Type.obj.has(o,'=')){ return t === o['='] }
if(Type.obj.has(o,'*')){ if(t.slice(0, o['*'].length) === o['*']){ r = true; t = t.slice(o['*'].length) } else { return false }}
if(Type.obj.has(o,'!')){ if(t.slice(-o['!'].length) === o['!']){ r = true } else { return false }}
if(Type.obj.has(o,'+')){
if(Type.list.map(Type.list.is(o['+'])? o['+'] : [o['+']], function(m){
if(t.indexOf(m) >= 0){ r = true } else { return true }
})){ return false }
Type.text.match = function(t, o){ var tmp, u;
if('string' !== typeof t){ return false }
if('string' == typeof o){ o = {'=': o} }
o = o || {};
tmp = (o['='] || o['*'] || o['>'] || o['<']);
if(t === tmp){ return true }
if(u !== o['=']){ return false }
tmp = (o['*'] || o['>'] || o['<']);
if(t.slice(0, (tmp||'').length) === tmp){ return true }
if(u !== o['*']){ return false }
if(u !== o['>'] && u !== o['<']){
return (t > o['>'] && t < o['<'])? true : false;
}
if(Type.obj.has(o,'-')){
if(Type.list.map(Type.list.is(o['-'])? o['-'] : [o['-']], function(m){
if(t.indexOf(m) < 0){ r = true } else { return true }
})){ return false }
}
if(Type.obj.has(o,'>')){ if(t > o['>']){ r = true } else { return false }}
if(Type.obj.has(o,'<')){ if(t < o['<']){ r = true } else { return false }}
function fuzzy(t,f){ var n = -1, i = 0, c; for(;c = f[i++];){ if(!~(n = t.indexOf(c, n+1))){ return false }} return true } // via http://stackoverflow.com/questions/9206013/javascript-fuzzy-search
if(Type.obj.has(o,'?')){ if(fuzzy(t, o['?'])){ r = true } else { return false }} // change name!
return r;
if(u !== o['>'] && t > o['>']){ return true }
if(u !== o['<'] && t < o['<']){ return true }
return false;
}
Type.list = {is: function(l){ return (l instanceof Array) }}
Type.list.slit = Array.prototype.slice;

View File

@ -31,6 +31,7 @@ Gun = root.Gun
if(Gun.window && !Gun.window.RindexedDB){ return }
var opt = {};
opt.file = 'radatatest';
var Radisk = (Gun.window && Gun.window.Radisk) || require('../../lib/radisk');
opt.store = ((Gun.window && Gun.window.RindexedDB) || require('../../lib/rfs'))(opt);
opt.chunk = 1000;
@ -110,6 +111,20 @@ var names = ["Adalard","Adora","Aia","Albertina","Alfie","Allyn","Amabil","Ammam
expect(Gun.obj.empty(all)).to.be.ok();
done();
});
it('radix reverse', function(done){
var r = Radix(), tmp;
r('alice', 1);r('bob', 2);r('carl', 3);r('dave', 4);
Radix.map(r, function(v,k, a,b){
tmp = v;
}, {reverse: 1});
expect(tmp).to.be(1);
Radix.map(r, function(v,k, a,b){
tmp = v;
});
expect(tmp).to.be(4);
done();
});
});
describe('Radisk', function(){
@ -138,6 +153,25 @@ var names = ["Adalard","Adora","Aia","Albertina","Alfie","Allyn","Amabil","Ammam
})
})
});
/*it('read contacts reverse', function(done){
var opt = {};
opt.reverse = true;
opt.end = 'nothing';
opt.start = 'marcy';
var first, last;
rad('', function(err, data){
console.log("???", err, data);
return;
Radix.map(data, function(v,k){
console.log(k, v);
//delete all[find+k];
});
//if(!Gun.obj.empty(all)){ return }
//done();
}, opt);
});
console.log("UNDO THIS RETURN!!!");return;*/
it('read contacts start end', function(done){
var opt = {};