space.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. ;(function(){
  2. var Gun = (typeof window !== "undefined")? window.Gun : require('../gun');
  3. var ify = Gun.node.ify, empty = {}, u;
  4. console.log("Index space is beta, API may change!");
  5. Gun.chain.space = function(key, data, opt){
  6. if(data instanceof Function){
  7. return travel(key, data, opt, this);
  8. }
  9. var gun = this;
  10. if(Gun.is(data)){
  11. data.get(function(soul){
  12. if(!soul){
  13. return cb && cb({err: "Indexspace cannot link `undefined`!"});
  14. }
  15. gun.space(key, Gun.val.link.ify(soul), opt);
  16. }, true);
  17. return gun;
  18. }
  19. var cb = (opt instanceof Function && opt), rank = (opt||empty).rank || opt, root = gun.back(-1), tmp;
  20. gun.get(function(soul){
  21. if(!soul){
  22. soul = (gun.back('opt.uuid') || Gun.text.random)(9);
  23. }
  24. /*var space = ify({}, soul), sub = space, l = 0, tmp;
  25. var atom = Gun.text.ify({get: key, put: data});
  26. Gun.list.map(index(0, key.length), function(i){
  27. sub[(tmp = key.slice(l, i))+'"'] = atom;
  28. sub = sub[tmp] = ify({}, soul+'"'+key.slice(0,i));
  29. l = i;
  30. });
  31. tmp = {}; tmp[key] = atom.put; tmp = ify(tmp, soul+'"');
  32. sub[key.slice(l, key.length)] = tmp;
  33. console.log('????', space);*/
  34. var shell = {}, l = 0, tmp;
  35. var atom = Gun.text.ify({get: key, put: data});
  36. tmp = {}; tmp[key] = data;
  37. shell.$ = ify(tmp, soul);
  38. tmp = {}; tmp[key.slice(0,l = 1)] = atom;
  39. shell[0] = ify(tmp, soul+'"');
  40. Gun.list.map(index(1, key.length), function(i){
  41. tmp = {}; tmp[key.slice(l,i)] = atom;
  42. shell[i] = ify(tmp, soul+'"'+key.slice(0,l));
  43. l = i;
  44. });
  45. tmp = {}; tmp[key.slice(l, key.length)] = atom;
  46. shell[l+1] = ify(tmp, soul+'"'+key.slice(0,l));
  47. //tmp = {}; tmp[key.slice(l, key.length)] = Gun.val.link.ify(soul); shell[l+1] = ify(tmp, soul+'"'+key.slice(0,l));
  48. //console.log('???', shell);
  49. gun.put(shell, cb, {soul: soul, shell: shell});
  50. },true);
  51. return gun;
  52. }
  53. function travel(key, cb, opt, ref){
  54. var root = ref.back(-1), tmp;
  55. opt = opt || {};
  56. opt.ack = opt.ack || {};
  57. ref.get(function(soul){
  58. ref.get(key).get(function(msg, eve){
  59. eve.off();
  60. opt.exact = true;
  61. opt.ack.key = key;
  62. opt.ack.data = msg.put;
  63. if(opt.match){ cb(opt.ack, key, msg, eve) }
  64. });
  65. //if(u !== msg.put){
  66. // cb(msg.put, msg.get, msg, eve);
  67. // return;
  68. //}
  69. opt.soul = soul;
  70. opt.start = soul+'"';
  71. opt.key = key;
  72. opt.top = index(0, opt.find);
  73. opt.low = opt.top.reverse();
  74. find(opt, cb, root);
  75. }, true);
  76. }
  77. function find(o, cb, root){
  78. var id = o.start+o.key.slice(0,o.low[0]);
  79. root.get(id).get(function(msg, eve){
  80. eve.off();
  81. o.ack.tree = {};
  82. if(u === msg.put){
  83. if(!o.exact){ return o.match = true }
  84. cb(o.ack, id, msg, eve);
  85. return;
  86. o.low = o.low.slice(1);
  87. if(!o.low.length){
  88. cb(u, o.key, msg, eve);
  89. return;
  90. }
  91. find(o, cb, root);
  92. return;
  93. }
  94. Gun.node.is(msg.put, function(v,k){
  95. if(!(k = Gun.obj.ify(v) || empty).get){ return }
  96. o.ack.tree[k.get] = k.put;
  97. });
  98. if(!o.exact){ return o.match = true }
  99. cb(o.ack, id, msg, eve);
  100. });
  101. }
  102. function index(n, m, l, k){
  103. l = l || [];
  104. if(!m){ return l }
  105. k = Math.ceil((n||1) / 10);
  106. if((n+k) >= m){ return l }
  107. l.push(n + k);
  108. return index(n + k, m, l);
  109. }
  110. }());
  111. /*
  112. gun.user('google').space('martti', "testing 123!");
  113. gun.user('google').get('search').space('ma', function(){
  114. // tree & index
  115. // UNFINISHED API!
  116. });
  117. */