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