store.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  2. Gun.on('create', function(root){
  3. if(Gun.TESTING){ root.opt.file = 'radatatest' }
  4. this.to.next(root);
  5. var opt = root.opt, empty = {}, u;
  6. if(false === opt.radisk){ return }
  7. var Radisk = (Gun.window && Gun.window.Radisk) || require('./radisk');
  8. var Radix = Radisk.Radix;
  9. var ST = 0;
  10. opt.store = opt.store || (!Gun.window && require('./rfs')(opt));
  11. var dare = Radisk(opt), esc = String.fromCharCode(27);
  12. root.on('put', function(msg){
  13. this.to.next(msg);
  14. if((msg._||'').rad){ return } // don't save what just came from a read.
  15. var id = msg['#'], put = msg.put, soul = put['#'], key = put['.'], val = put[':'], state = put['>'], tmp;
  16. var DBG = (msg._||'').DBG; DBG && (DBG.sp = DBG.sp || +new Date);
  17. var lot = (msg._||'').lot||''; count[id] = (count[id] || 0) + 1;
  18. var S = (msg._||'').RPS || ((msg._||'').RPS = +new Date);
  19. dare(soul+esc+key, {':': val, '>': state}, dare.one[id] || function(err, ok){
  20. DBG && (DBG.spd = DBG.spd || +new Date);
  21. console.STAT && console.STAT(S, +new Date - S, 'put');
  22. if(!err && count[id] !== lot.s){ console.log(err = "Disk count not same as ram count."); console.STAT && console.STAT(+new Date, lot.s - count[id], 'put ack != count') } delete count[id];
  23. if(err){ root.on('in', {'@': id, err: err, DBG: DBG}); return }
  24. root.on('in', {'@': id, ok: ok, DBG: DBG});
  25. }, id, DBG && (DBG.r = DBG.r || {}));
  26. DBG && (DBG.sps = DBG.sps || +new Date);
  27. });
  28. var count = {}, obj_empty = Gun.obj.empty;
  29. root.on('get', function(msg){
  30. this.to.next(msg);
  31. var ctx = msg._||'', DBG = ctx.DBG = msg.DBG; DBG && (DBG.sg = +new Date);
  32. var id = msg['#'], get = msg.get, soul = msg.get['#'], has = msg.get['.']||'', o = {}, graph, lex, key, tmp, force;
  33. if('string' == typeof soul){
  34. key = soul;
  35. } else
  36. if(soul){
  37. if(u !== (tmp = soul['*'])){ o.limit = force = 1 }
  38. if(u !== soul['>']){ o.start = soul['>'] }
  39. if(u !== soul['<']){ o.end = soul['<'] }
  40. key = force? (''+tmp) : tmp || soul['='];
  41. force = null;
  42. }
  43. if(key && !o.limit){ // a soul.has must be on a soul, and not during soul*
  44. if('string' == typeof has){
  45. key = key+esc+(o.atom = has);
  46. } else
  47. if(has){
  48. if(u !== has['>']){ o.start = has['>']; o.limit = 1 }
  49. if(u !== has['<']){ o.end = has['<']; o.limit = 1 }
  50. if(u !== (tmp = has['*'])){ o.limit = force = 1 }
  51. if(key){ key = key+esc + (force? (''+(tmp||'')) : tmp || (o.atom = has['='] || '')) }
  52. }
  53. }
  54. if((tmp = get['%']) || o.limit){
  55. o.limit = (tmp <= (o.pack || (1000 * 100)))? tmp : 1;
  56. }
  57. if(has['-'] || (soul||{})['-']){ o.reverse = true }
  58. if((tmp = (root.next||'')[soul]) && tmp.put){
  59. if(o.atom){
  60. tmp = (tmp.next||'')[o.atom] ;
  61. if(tmp && tmp.rad){ return }
  62. } else
  63. if(tmp && tmp.rad){ return }
  64. }
  65. var now = Gun.state();
  66. var S = (+new Date), C = 0, SPT = 0; // STATS!
  67. DBG && (DBG.sgm = S);
  68. dare(key||'', function(err, data, info){
  69. DBG && (DBG.sgr = +new Date);
  70. DBG && (DBG.sgi = info);
  71. try{opt.store.stats.get.time[statg % 50] = (+new Date) - S; ++statg;
  72. opt.store.stats.get.count++;
  73. if(err){ opt.store.stats.get.err = err }
  74. }catch(e){} // STATS!
  75. //if(u === data && info.chunks > 1){ return } // if we already sent a chunk, ignore ending empty responses. // this causes tests to fail.
  76. console.STAT && console.STAT(S, +new Date - S, 'got', JSON.stringify(key)); S = +new Date;
  77. info = info || '';
  78. var va, ve;
  79. if(info.unit && data && u !== (va = data[':']) && u !== (ve = data['>'])){ // new format
  80. var tmp = key.split(esc), so = tmp[0], ha = tmp[1];
  81. (graph = graph || {})[so] = Gun.state.ify(graph[so], ha, ve, va, so);
  82. root.$.get(so).get(ha)._.rad = now;
  83. // REMEMBER TO ADD _rad TO NODE/SOUL QUERY!
  84. } else
  85. if(data){ // old code path
  86. if(typeof data !== 'string'){
  87. if(o.atom){
  88. data = u;
  89. } else {
  90. Radix.map(data, each); // IS A RADIX TREE, NOT FUNCTION!
  91. }
  92. }
  93. if(!graph && data){ each(data, '') }
  94. // TODO: !has what about soul lookups?
  95. if(!o.atom && !has & 'string' == typeof soul && !o.limit && !o.more){
  96. root.$.get(soul)._.rad = now;
  97. }
  98. }
  99. DBG && (DBG.sgp = +new Date);
  100. // TODO: PERF NOTES! This is like 0.2s, but for each ack, or all? Can you cache these preps?
  101. // TODO: PERF NOTES! This is like 0.2s, but for each ack, or all? Can you cache these preps?
  102. // TODO: PERF NOTES! This is like 0.2s, but for each ack, or all? Can you cache these preps?
  103. // TODO: PERF NOTES! This is like 0.2s, but for each ack, or all? Can you cache these preps?
  104. // TODO: PERF NOTES! This is like 0.2s, but for each ack, or all? Can you cache these preps?
  105. // Or benchmark by reusing first start date.
  106. if(console.STAT && (ST = +new Date - S) > 9){ console.STAT(S, ST, 'got prep time'); console.STAT(S, C, 'got prep #') } SPT += ST; C = 0; S = +new Date;
  107. var faith = function(){}; faith.faith = true; faith.rad = get; // HNPERF: We're testing performance improvement by skipping going through security again, but this should be audited.
  108. root.on('in', {'@': id, put: graph, '%': info.more? 1 : u, err: err? err : u, _: faith, DBG: DBG});
  109. console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, 'got emit', Object.keys(graph||{}).length);
  110. graph = u; // each is outside our scope, we have to reset graph to nothing!
  111. }, o, DBG && (DBG.r = DBG.r || {}));
  112. DBG && (DBG.sgd = +new Date);
  113. console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, 'get call'); // TODO: Perf: this was half a second??????
  114. function each(val, has, a,b){ // TODO: THIS CODE NEEDS TO BE FASTER!!!!
  115. C++;
  116. if(!val){ return }
  117. has = (key+has).split(esc);
  118. var soul = has.slice(0,1)[0];
  119. has = has.slice(-1)[0];
  120. if(o.limit && o.limit <= o.count){ return true }
  121. var va, ve, so = soul, ha = has;
  122. //if(u !== (va = val[':']) && u !== (ve = val['>'])){ // THIS HANDLES NEW CODE!
  123. if('string' != typeof val){ // THIS HANDLES NEW CODE!
  124. va = val[':']; ve = val['>'];
  125. (graph = graph || {})[so] = Gun.state.ify(graph[so], ha, ve, va, so);
  126. //root.$.get(so).get(ha)._.rad = now;
  127. o.count = (o.count || 0) + ((va||'').length || 9);
  128. return;
  129. }
  130. o.count = (o.count || 0) + val.length;
  131. var tmp = val.lastIndexOf('>');
  132. var state = Radisk.decode(val.slice(tmp+1), null, esc);
  133. val = Radisk.decode(val.slice(0,tmp), null, esc);
  134. (graph = graph || {})[soul] = Gun.state.ify(graph[soul], has, state, val, soul);
  135. }
  136. });
  137. var val_is = Gun.val.is
  138. opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // STATS!
  139. var statg = 0, statp = 0; // STATS!
  140. });