mirror of
https://github.com/amark/gun.git
synced 2026-03-16 13:25:16 +00:00
RAD improvements!!!
This commit is contained in:
@@ -44,7 +44,6 @@
|
||||
return cb(u, val);
|
||||
}
|
||||
}
|
||||
//console.log("READ FROM DISK");
|
||||
return r.read(key, cb);
|
||||
}
|
||||
r.batch(key, val);
|
||||
@@ -71,7 +70,7 @@
|
||||
r.batch.ed = 0;
|
||||
r.save(batch, function(err, ok){
|
||||
if(++i > 1){ return }
|
||||
if(err){ Gun.log(err) }
|
||||
if(err){ Gun.log('err', err) }
|
||||
Gun.obj.map(batch.acks, function(cb){ cb(err, ok) });
|
||||
thrash.at = null;
|
||||
thrash.ing = false;
|
||||
@@ -139,7 +138,7 @@
|
||||
f.each = function(val, key, k, pre){
|
||||
f.count++;
|
||||
var enc = Radisk.encode(pre.length) +'#'+ Radisk.encode(k) + (u === val? '' : '='+ Radisk.encode(val)) +'\n';
|
||||
if(opt.chunk < f.text.length + enc.length){
|
||||
if((opt.chunk < f.text.length + enc.length) && !(1 >= rad.count)){
|
||||
f.text = '';
|
||||
f.limit = Math.ceil(f.count/2);
|
||||
f.count = 0;
|
||||
@@ -171,15 +170,8 @@
|
||||
if(!Radix.map(rad, f.each, true)){ f.write() }
|
||||
}
|
||||
|
||||
r.read = function(key, cb){
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
// TODO: BUG!!! If a node spans multiple file chunks, it won't return all!
|
||||
if(RAD){ // cache
|
||||
r.read = function(key, cb, next){
|
||||
if(RAD && !next){ // cache
|
||||
var val = RAD(key);
|
||||
if(u !== val){
|
||||
return cb(u, val);
|
||||
@@ -188,26 +180,32 @@
|
||||
var g = function Get(){}, tmp;
|
||||
g.lex = function(file){
|
||||
file = (u === file)? u : decodeURIComponent(file);
|
||||
if(!file || file > key){
|
||||
if(!file || file > (next || key)){
|
||||
if(next){ g.file = file }
|
||||
if(tmp = q[g.file]){
|
||||
tmp.push({key: key, ack: cb});
|
||||
tmp.push({key: key, ack: cb, file: g.file});
|
||||
return true;
|
||||
}
|
||||
q[g.file] = [{key: key, ack: cb}];
|
||||
q[g.file] = [{key: key, ack: cb, file: g.file}];
|
||||
r.parse(g.file, g.it);
|
||||
return true;
|
||||
}
|
||||
g.file = file;
|
||||
}
|
||||
g.it = function(err, disk){
|
||||
if(g.err = err){ Gun.log(err) }
|
||||
if(g.err = err){ Gun.log('err', err) }
|
||||
if(disk){ RAD = disk }
|
||||
disk = q[g.file]; Gun.obj.del(q, g.file);
|
||||
Gun.obj.map(disk, g.ack);
|
||||
}
|
||||
g.ack = function(as){
|
||||
if(!as.ack){ return }
|
||||
as.ack(g.err, (RAD || noop)(as.key));
|
||||
var tmp = as.key, data = (RAD || noop)(tmp), last = (RAD||{}).last;
|
||||
if(data){ as.ack(g.err, data) }
|
||||
else if(!as.file){ return as.ack(g.err, u) }
|
||||
if(!last || last === tmp){ return } // is this correct?
|
||||
if(last > tmp && 0 > last.indexOf(tmp)){ return }
|
||||
r.read(tmp, as.ack, as.file);
|
||||
}
|
||||
opt.store.list(g.lex);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
|
||||
function Radix(){
|
||||
var radix = function(key, val, t){
|
||||
key = ''+key;
|
||||
if(!t && u !== val){
|
||||
radix.last = (key < radix.last)? radix.last : key;
|
||||
radix.count = (radix.count || 0) + 1;
|
||||
}
|
||||
t = t || radix[_] || (radix[_] = {});
|
||||
var i = 0, l = key.length-1, k = key[i], at, tmp;
|
||||
while(!(at = t[k]) && i < l){
|
||||
|
||||
23
lib/store.js
23
lib/store.js
@@ -1,4 +1,6 @@
|
||||
if(typeof window === "undefined"){
|
||||
if(typeof window !== "undefined"){
|
||||
var Gun = window.Gun;
|
||||
} else {
|
||||
var Gun = require('../gun');
|
||||
}
|
||||
|
||||
@@ -18,10 +20,11 @@ Gun.on('opt', function(ctx){
|
||||
opt.store = opt.store || (!opt.window && require('./rfs')(opt));
|
||||
var rad = Radisk(opt);
|
||||
|
||||
ctx.on('put', function(at){
|
||||
this.to.next(at);
|
||||
var id = at['#'], track = !at['@'], acks = track? 0 : u; // only ack non-acks.
|
||||
Gun.graph.is(at.put, null, function(val, key, node, soul){
|
||||
ctx.on('put', function(msg){
|
||||
this.to.next(msg);
|
||||
var id = msg['#'], track = !msg['@'], acks = track? 0 : u; // only ack non-acks.
|
||||
if(msg.rad && !track){ return } // don't save our own acks
|
||||
Gun.graph.is(msg.put, null, function(val, key, node, soul){
|
||||
if(track){ ++acks }
|
||||
val = Radisk.encode(val)+'>'+Radisk.encode(Gun.state.is(node, key));
|
||||
rad(soul+'.'+key, val, (track? ack : u));
|
||||
@@ -30,7 +33,7 @@ Gun.on('opt', function(ctx){
|
||||
acks--;
|
||||
if(ack.err){ return }
|
||||
if(ack.err = err){
|
||||
ctx.on('in', {'@': id, err: Gun.log(err)});
|
||||
ctx.on('in', {'@': id, err: err});
|
||||
return;
|
||||
}
|
||||
if(acks){ return }
|
||||
@@ -38,15 +41,15 @@ Gun.on('opt', function(ctx){
|
||||
}
|
||||
});
|
||||
|
||||
ctx.on('get', function(at){
|
||||
this.to.next(at);
|
||||
var id = at['#'], soul = at.get['#'], key = at.get['.']||'', tmp = soul+'.'+key, node;
|
||||
ctx.on('get', function(msg){
|
||||
this.to.next(msg);
|
||||
var id = msg['#'], soul = msg.get['#'], key = msg.get['.']||'', tmp = soul+'.'+key, node;
|
||||
rad(tmp, function(err, val){
|
||||
if(val){
|
||||
Radix.map(val, each);
|
||||
if(!node){ each(val, key) }
|
||||
}
|
||||
ctx.on('in', {'@': id, put: Gun.graph.node(node), err: err? err : u});
|
||||
ctx.on('in', {'@': id, '#': key, put: Gun.graph.node(node), err: err? err : u, rad: Radix});
|
||||
});
|
||||
function each(val, key){
|
||||
tmp = val.lastIndexOf('>');
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
if(typeof window === "undefined"){ //Not in the browser, Include from node
|
||||
if(typeof window !== "undefined"){
|
||||
var Gun = window.Gun;
|
||||
} else {
|
||||
var Gun = require('../gun');
|
||||
}
|
||||
|
||||
@@ -60,7 +62,7 @@ if(typeof window === "undefined"){ //Not in the browser, Include from node
|
||||
function travel(cb, opt, b, gun){
|
||||
var root = gun.back(-1), tmp;
|
||||
(opt = Gun.num.is(opt)? {last: opt} : opt || {}).seen = opt.seen || {};
|
||||
var t = now();
|
||||
var t = now(opt.start);
|
||||
gun.on(function(data, key, msg, eve){
|
||||
var at = msg.$._, id = at.link || at.soul || Gun.node.soul(data);
|
||||
if(!id){ return }
|
||||
|
||||
@@ -6,29 +6,44 @@
|
||||
<script src="../lib/radisk.js"></script>
|
||||
<script src="../lib/store.js"></script>
|
||||
|
||||
<button onclick="var i = 0; window.TO = setInterval(function(){ gun.get(Gun.text.random(3)).put({a: ++i}) }, 0);">start</button>
|
||||
<button onclick="clearTimeout(window.TO);">end</button>
|
||||
<br/><br/>
|
||||
<button onclick="gun.get('a').put({b: Gun.text.random(900)});setTimeout(function(){gun.get('x').put({y: Gun.text.random(900)});},1000);">write</button>
|
||||
<button id='read' onclick="console.debug.i=1;gun.get('a').once(d => console.log(1, d));gun.get('x').once(d => console.log(2, d));">read</button>
|
||||
|
||||
<script>
|
||||
localStorage.clear();
|
||||
//localStorage.clear();
|
||||
|
||||
function Store(opt){
|
||||
console.log(opt);
|
||||
opt = opt || {};
|
||||
opt.file = String(opt.file || 'radata');
|
||||
var storage = {};
|
||||
var store = function Store(){};
|
||||
var storage = window.storage = localStorage;//{};
|
||||
var store = function Store(){}, u;
|
||||
store.put = function(file, data, cb){
|
||||
storage[file] = data
|
||||
cb(undefined, 1)
|
||||
//console.log("put", file, data);
|
||||
setTimeout(function(){
|
||||
storage[file] = data;
|
||||
cb(null, 1);
|
||||
}, 25);
|
||||
};
|
||||
store.get = function(file, cb){
|
||||
var temp = storage[file] || undefined
|
||||
cb(temp)
|
||||
//console.log("get", file);
|
||||
setTimeout(function(){
|
||||
var tmp = storage[file] || u;
|
||||
cb(null, tmp);
|
||||
}, 10);
|
||||
};
|
||||
store.list = function(cb, match){
|
||||
var arr = Object.entries(storage)[0];
|
||||
Gun.obj.map(arr, cb) || cb();
|
||||
//console.log('list');
|
||||
setTimeout(function(){
|
||||
Gun.obj.map(Object.keys(storage), cb) || cb();
|
||||
}, 5);
|
||||
};
|
||||
return store;
|
||||
}
|
||||
|
||||
var gun = Gun({localStorage: false, store: Store()});
|
||||
var gun = Gun({localStorage: false, store: Store(), chunk: 1024, batch: 100});
|
||||
|
||||
//setTimeout(function(){ $('#read').trigger('click') });
|
||||
</script>
|
||||
Reference in New Issue
Block a user