From 4085a4053cdfb57d059fc7ba5e4a47881c2fcf21 Mon Sep 17 00:00:00 2001 From: Mark Nadal Date: Mon, 22 Apr 2019 17:44:38 -0700 Subject: [PATCH] Reverse RAD! --- lib/radisk.js | 15 ++++++++------- lib/radix.js | 1 + lib/rindexed.js | 3 +++ lib/store.js | 1 + package.json | 2 +- src/adapters/mesh.js | 25 +++++++++++++++---------- src/map.js | 2 +- src/type.js | 36 +++++++++++++++--------------------- test/rad/rad.js | 34 ++++++++++++++++++++++++++++++++++ 9 files changed, 79 insertions(+), 40 deletions(-) diff --git a/lib/radisk.js b/lib/radisk.js index 47c0984c..376b07ea 100644 --- a/lib/radisk.js +++ b/lib/radisk.js @@ -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); }); } }()); diff --git a/lib/radix.js b/lib/radix.js index b9d4c4c7..2eb7b311 100644 --- a/lib/radix.js +++ b/lib/radix.js @@ -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; diff --git a/lib/rindexed.js b/lib/rindexed.js index 005e2977..fcccd9a1 100644 --- a/lib/rindexed.js +++ b/lib/rindexed.js @@ -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) } diff --git a/lib/store.js b/lib/store.js index 4613cc26..471867bf 100644 --- a/lib/store.js +++ b/lib/store.js @@ -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){ diff --git a/package.json b/package.json index ef840765..3aa3fbbf 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/adapters/mesh.js b/src/adapters/mesh.js index 0f984bf0..df4f2dcc 100644 --- a/src/adapters/mesh.js +++ b/src/adapters/mesh.js @@ -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){} - + \ No newline at end of file diff --git a/src/map.js b/src/map.js index cb60a711..4ffada80 100644 --- a/src/map.js +++ b/src/map.js @@ -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; diff --git a/src/type.js b/src/type.js index bab8fd65..82be63d4 100644 --- a/src/type.js +++ b/src/type.js @@ -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; diff --git a/test/rad/rad.js b/test/rad/rad.js index ee46a444..da72c2d4 100644 --- a/test/rad/rad.js +++ b/test/rad/rad.js @@ -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 = {};