store.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, 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. if(msg.rad && !track){ return } // don't save our own acks
  15. var start = (+new Date); // STATS!
  16. Gun.graph.is(msg.put, null, function(val, key, node, soul){
  17. if(track){ ++acks }
  18. //console.log('put:', soul, key, val);
  19. val = Radisk.encode(val, null, esc)+'>'+Radisk.encode(Gun.state.is(node, key), null, esc);
  20. rad(soul+esc+key, val, (track? ack : u));
  21. });
  22. function ack(err, ok){
  23. acks--;
  24. if(ack.err){ return }
  25. if(ack.err = err){
  26. try{opt.store.stats.put.err = err}catch(e){} // STATS!
  27. root.on('in', {'@': id, err: err});
  28. return;
  29. }
  30. if(acks){ return }
  31. try{opt.store.stats.put.time[statp % 50] = (+new Date) - start; ++statp;
  32. opt.store.stats.put.count++;
  33. }catch(e){} // STATS!
  34. //console.log("PAT!", id);
  35. root.on('in', {'@': id, ok: 1});
  36. }
  37. });
  38. root.on('get', function(msg){
  39. this.to.next(msg);
  40. var id = msg['#'], get = msg.get, soul = msg.get['#'], has = msg.get['.']||'', o = {}, graph, lex, key, tmp, force;
  41. if('string' == typeof soul){
  42. key = soul;
  43. } else
  44. if(soul){
  45. if(u !== (tmp = soul['*'])){ o.limit = force = 1 }
  46. if(u !== soul['>']){ o.start = soul['>'] }
  47. if(u !== soul['<']){ o.end = soul['<'] }
  48. key = force? (''+tmp) : tmp || soul['='];
  49. force = null;
  50. }
  51. if(key && !o.limit){ // a soul.has must be on a soul, and not during soul*
  52. if('string' == typeof has){
  53. key = key+esc+(o.atom = has);
  54. } else
  55. if(has){
  56. if(u !== has['>']){ o.start = has['>']; o.limit = 1 }
  57. if(u !== has['<']){ o.end = has['<']; o.limit = 1 }
  58. if(u !== (tmp = has['*'])){ o.limit = force = 1 }
  59. if(key){ key = key+esc + (force? (''+(tmp||'')) : tmp || (o.atom = has['='] || '')) }
  60. }
  61. }
  62. if((tmp = get['%']) || o.limit){
  63. o.limit = (tmp <= (o.pack || (1000 * 100)))? tmp : 1;
  64. }
  65. if(has['-'] || (soul||{})['-']){ o.reverse = true }
  66. //console.log("RAD get:", key, o);
  67. var start = (+new Date); // STATS! // console.log("GET!", id, JSON.stringify(key));
  68. rad(key||'', function(err, data, o){
  69. try{opt.store.stats.get.time[statg % 50] = (+new Date) - start; ++statg;
  70. opt.store.stats.get.count++;
  71. if(err){ opt.store.stats.get.err = err }
  72. }catch(e){} // STATS!
  73. //console.log("RAD gat:", err, data, o);
  74. if(data){
  75. if(typeof data !== 'string'){
  76. if(o.atom){
  77. data = u;
  78. } else {
  79. Radix.map(data, each)
  80. }
  81. }
  82. if(!graph && data){ each(data, '') }
  83. }
  84. //console.log("GOT!", id, JSON.stringify(key), ((+new Date) - start));
  85. root.on('in', {'@': id, put: graph, err: err? err : u, rad: Radix});
  86. }, o);
  87. function each(val, has, a,b){
  88. if(!val){ return }
  89. has = (key+has).split(esc);
  90. var soul = has.slice(0,1)[0];
  91. has = has.slice(-1)[0];
  92. o.count = (o.count || 0) + val.length;
  93. tmp = val.lastIndexOf('>');
  94. var state = Radisk.decode(val.slice(tmp+1), null, esc);
  95. val = Radisk.decode(val.slice(0,tmp), null, esc);
  96. (graph = graph || {})[soul] = Gun.state.ify(graph[soul], has, state, val, soul);
  97. if(o.limit && o.limit <= o.count){ return true }
  98. }
  99. });
  100. opt.store.stats = {get:{time:{}, count:0}, put: {time:{}, count:0}}; // STATS!
  101. var statg = 0, statp = 0; // STATS!
  102. });