store.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. opt.store = opt.store || (!Gun.window && require('./rfs')(opt));
  10. var rad = Radisk(opt), esc = String.fromCharCode(27);
  11. root.on('put', function(msg){
  12. this.to.next(msg);
  13. var id = msg['#'] || Gun.text.random(3), track = !msg['@'], acks = track? 0 : u; // only ack non-acks.
  14. var got = (msg._||empty).rad, now = Gun.state();
  15. var S = (+new Date); // STATS!
  16. Gun.graph.is(msg.put, null, function(val, key, node, soul){
  17. if(!track && got){
  18. var at = (root.next||empty)[soul];
  19. if(!at){ return }
  20. if(u !== got['.']){ at = (at.next||empty)[key] }
  21. if(!at){ return }
  22. at.rad = now;
  23. return;
  24. }
  25. if(track){ ++acks }
  26. //console.log('put:', soul, key, val);
  27. val = Radisk.encode(val, null, esc)+'>'+Radisk.encode(Gun.state.is(node, key), null, esc);
  28. rad(soul+esc+key, val, (track? ack : u));
  29. });
  30. //console.log(+new Date - S, 'put loop');
  31. function ack(err, ok){
  32. acks--;
  33. if(ack.err){ return }
  34. if(ack.err = err){
  35. try{opt.store.stats.put.err = err}catch(e){} // STATS!
  36. root.on('in', {'@': id, err: err});
  37. return;
  38. }
  39. if(acks){ return }
  40. try{opt.store.stats.put.time[statp % 50] = (+new Date) - S; ++statp;
  41. opt.store.stats.put.count++;
  42. }catch(e){} // STATS!
  43. //console.log(+new Date - S, 'put'); S = +new Date;
  44. root.on('in', {'@': id, ok: 1});
  45. //console.log(+new Date - S, 'put sent');
  46. }
  47. });
  48. root.on('get', function(msg){
  49. this.to.next(msg);
  50. var id = msg['#'], get = msg.get, soul = msg.get['#'], has = msg.get['.']||'', o = {}, graph, lex, key, tmp, force;
  51. if('string' == typeof soul){
  52. key = soul;
  53. } else
  54. if(soul){
  55. if(u !== (tmp = soul['*'])){ o.limit = force = 1 }
  56. if(u !== soul['>']){ o.start = soul['>'] }
  57. if(u !== soul['<']){ o.end = soul['<'] }
  58. key = force? (''+tmp) : tmp || soul['='];
  59. force = null;
  60. }
  61. if(key && !o.limit){ // a soul.has must be on a soul, and not during soul*
  62. if('string' == typeof has){
  63. key = key+esc+(o.atom = has);
  64. } else
  65. if(has){
  66. if(u !== has['>']){ o.start = has['>']; o.limit = 1 }
  67. if(u !== has['<']){ o.end = has['<']; o.limit = 1 }
  68. if(u !== (tmp = has['*'])){ o.limit = force = 1 }
  69. if(key){ key = key+esc + (force? (''+(tmp||'')) : tmp || (o.atom = has['='] || '')) }
  70. }
  71. }
  72. if((tmp = get['%']) || o.limit){
  73. o.limit = (tmp <= (o.pack || (1000 * 100)))? tmp : 1;
  74. }
  75. if(has['-'] || (soul||{})['-']){ o.reverse = true }
  76. if(tmp = (root.next||empty)[soul]){
  77. if(tmp && tmp.rad){ return }
  78. if(o.atom){ tmp = (tmp.next||empty)[o.atom] }
  79. if(tmp && tmp.rad){ return }
  80. }
  81. var S = (+new Date); // STATS!
  82. rad(key||'', function(err, data, o){
  83. try{opt.store.stats.get.time[statg % 50] = (+new Date) - S; ++statg;
  84. opt.store.stats.get.count++;
  85. if(err){ opt.store.stats.get.err = err }
  86. }catch(e){} // STATS!
  87. //if(u === data && o.chunks > 1){ return } // if we already sent a chunk, ignore ending empty responses. // this causes tests to fail.
  88. //console.log(+new Date - S, 'got'); S = +new Date;
  89. if(data){
  90. if(typeof data !== 'string'){
  91. if(o.atom){
  92. data = u;
  93. } else {
  94. Radix.map(data, each)
  95. }
  96. }
  97. if(!graph && data){ each(data, '') }
  98. }
  99. //console.log(+new Date - S, 'got prep'); S = +new Date;
  100. root.on('in', {'@': id, put: graph, '%': o.more? 1 : u, err: err? err : u, _: each});
  101. //console.log(+new Date - S, 'got sent');
  102. }, o);
  103. //console.log(+new Date - S, 'get call');
  104. function each(val, has, a,b){
  105. if(!val){ return }
  106. has = (key+has).split(esc);
  107. var soul = has.slice(0,1)[0];
  108. has = has.slice(-1)[0];
  109. o.count = (o.count || 0) + val.length;
  110. var tmp = val.lastIndexOf('>');
  111. var state = Radisk.decode(val.slice(tmp+1), null, esc);
  112. val = Radisk.decode(val.slice(0,tmp), null, esc);
  113. (graph = graph || {})[soul] = Gun.state.ify(graph[soul], has, state, val, soul);
  114. if(o.limit && o.limit <= o.count){ return true }
  115. }
  116. each.rad = get;
  117. });
  118. opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // STATS!
  119. var statg = 0, statp = 0; // STATS!
  120. });