store.js 7.5 KB

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