tmp.test.sql.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. (function(){
  2. if(!this.Gun){ return }
  3. function Test(o){
  4. var test = this;
  5. if(!(test instanceof Test)){ return new Test(o) }
  6. test._ = {};
  7. test._.stack = [];
  8. return test;
  9. }
  10. Test.chain = Test.prototype;
  11. Test.chain.run = function(fn){
  12. var test = this;
  13. var e = test._.i, i = 0;
  14. var stack = test._.stack;
  15. stack.push(fn);
  16. //var to = setInterval(function(){ if(++i >= e){ return clearTimeout(to) }
  17. var start = Gun.time.is();
  18. while(++i <= e){
  19. Gun.list.map(stack, function(fn){ (fn||function(){})(i) })
  20. }
  21. console.log((Gun.time.is() - start) / 1000);
  22. //},0);
  23. return test;
  24. }
  25. Test.chain.it = function(i){
  26. var test = this;
  27. test._.i = i || 1000;
  28. return test;
  29. }
  30. Test.chain.gen = function(fn){
  31. var test = this;
  32. test._.stack.push(fn);
  33. return test;
  34. }
  35. var gun = window.gun = Gun();
  36. window.SPAM = function(read){ // TODO: BUG? gun-sid in transport layer not correct?
  37. //localStorage.clear();
  38. var start = Gun.time.is();
  39. /*var mark = {
  40. name: "Mark Nadal"
  41. };
  42. var amber = {
  43. name: "Amber Nadal",
  44. spouse: mark
  45. }
  46. mark.spouse = amber;
  47. amber.pet = {
  48. name: "Fluffy",
  49. slave: mark
  50. }
  51. Test().it(read).gen(function(i){
  52. Gun.ify(mark);
  53. }).run();return;*/
  54. Test().it(read).gen(function(i){
  55. gun.get('users').path(i).path('where').put({lat: Math.random(), lng: Math.random(),i:i});
  56. }).run();return;
  57. Test().it(read === true? 1 : read || 1000).gen(function(i){
  58. if(read === true){
  59. gun.get('users').map().path('where').on(function(node){
  60. console.log("node:", node);
  61. if(node.i === (1000)){
  62. console.log("total:", Gun.time.is() - start);
  63. start = Gun.time.is();
  64. }
  65. });
  66. return;
  67. }
  68. // PUT, GET, PATH, ON
  69. var now = Gun.time.is();
  70. /*var obj = {'i': i, 'v': Gun.text.random(100)};
  71. gun.put(obj, function(err, ok){
  72. //console.log("put done", i, 'in', Gun.time.is() - now);//, i % 1000);
  73. if(i % (1000) === 0){
  74. console.log("total:", Gun.time.is() - start);
  75. start = Gun.time.is();
  76. }
  77. });return;*/
  78. gun.get('users').path(i).path('where').put({
  79. lat: Math.random(),lng: Math.random(),i: i
  80. //lol: i / 2
  81. }, function(err, ok){
  82. console.log("put done", i, 'in', Gun.time.is() - now);//, i % 1000);
  83. if(i % (1000) === 0){
  84. console.log("total:", Gun.time.is() - start);
  85. start = Gun.time.is();
  86. }
  87. });return;
  88. }).run(function(){});
  89. }
  90. //window.SPAM(1000000);
  91. }());
  92. /* EXTRA GUN UTILITY FUNCTIONS I MIGHT WANT TO KEEP
  93. (function(){
  94. Gun().get('chat').path('messages').since(Gun().get('me').path('last')).map().val(function(msg){
  95. });
  96. Gun().get('chat').path('messages').last(100).map().val(function(msg){
  97. });
  98. var peers = [
  99. peer1,
  100. peer2
  101. ];
  102. Gun.on('put').event(function(graph, cb, opt){
  103. Gun.is.graph(graph, function(node, soul){
  104. localStorage[soul] = node;
  105. });
  106. });
  107. Gun.on('put').event(function(graph, cb, opt){
  108. Peers(opt.peers).send({
  109. id: MsgID,
  110. value: data,
  111. from: myPeerID
  112. }, cb);
  113. });
  114. Gun.on('get').event(function(lex, cb, opt){
  115. Peers(opt.peers || peers).send({
  116. '#': MsgID,
  117. '$': lex,
  118. '~': myPeerID
  119. }, cb);
  120. });
  121. Peers.server(function(req, res){
  122. if(Msg.IDs[req.id]){ return } // throttle
  123. // auth
  124. Peers(peers).send(req); // relay
  125. // auth
  126. if(req.rid){ return } // ignore
  127. if(req.put && opt.everything || graph[for soul in req.body]){ // process
  128. Gun.put(gun, req.body, REPLY);
  129. }
  130. });
  131. // TODO: MARK / JESSE need to solve infinite circular loop on get flushing and put flushing.
  132. GUN = {'#': 'soul', '.': 'field', '=': 'value', '>': 'state'}
  133. MSG = {'#': 'id', '$': 'body', '@': 'to'}
  134. Gun.wire = function(data){
  135. }
  136. Gun.get.wire = function(lex, cb, opt){ return Gun.text.is(lex)? Gun.get.wire.from(lex, cb, opt) : Gun.get.wire.to(lex, cb, opt) }
  137. Gun.get.wire.to = function(lex, cb, opt){
  138. var t = '';
  139. Gun.obj.map(lex, function(v,f){
  140. if(!v){ return }
  141. Gun.list.map(Gun.list.is(v)? v : [v], function(v){
  142. t += f + "'" + Gun.put.wire.ify(v) + "'";
  143. });
  144. });
  145. return t + '?';
  146. }
  147. Gun.get.wire.from = function(t, cb, opt){
  148. if(!t){ return null }
  149. var a = Gun.put.wire.from.parse(t), lex = {};
  150. Gun.list.map([Gun._.soul, Gun._.field, Gun._.value, Gun._.state], function(sym, i){
  151. if(!(i = a.indexOf(sym) + 1)){ return }
  152. lex[sym] = Gun.put.wire.type(a[i]);
  153. });
  154. return lex;
  155. }
  156. // #soul.field
  157. // "#soul.field=value>state"
  158. // #messages>>1234567890 //{soul: 'messages', state: {'>': 1234567890}}
  159. // #id$"msg"~who@to
  160. Gun.put.wire = function(n, cb, opt){ return Gun.text.is(n)? Gun.put.wire.from(n, cb, opt) : Gun.put.wire.to(n, cb, opt) }
  161. Gun.put.wire.ify = function(s){ var tmp;
  162. if(Infinity === s || -Infinity === s){ return s }
  163. if(tmp = Gun.is.rel(s)){ return '#' + JSON.stringify(tmp) }
  164. return JSON.stringify(s)
  165. }
  166. Gun.put.wire.type = function(s){ var tmp;
  167. if(Gun._.soul === s.charAt(0)){ return Gun.is.rel.ify(JSON.parse(s.slice(1))) }
  168. if(String(Infinity) === s){ return Infinity }
  169. if(String(-Infinity) === s){ return -Infinity }
  170. return JSON.parse(s)
  171. }
  172. Gun.put.wire.to = function(n, cb, opt){ var t, b;
  173. if(!n || !(t = Gun.is.node.soul(n))){ return null }
  174. cb = cb || function(){};
  175. t = (b = "#'" + Gun.put.wire.ify(t) + "'");
  176. var val = function(v,f, nv,nf){
  177. var w = '', s = Gun.is.node.state(n,f), sw = '';
  178. if(!s){ return }
  179. w += ".'" + Gun.put.wire.ify(f) + "'";
  180. console.log("yeah value?", v, Gun.put.wire.ify(v));
  181. w += "='" + Gun.put.wire.ify(v) + "'";
  182. if(s !== Gun.is.node.state(n,nf)){
  183. w += ">'" + Gun.put.wire.ify(s) + "'";
  184. } else {
  185. sw = ">'" + Gun.put.wire.ify(s) + "'";
  186. }
  187. t += w;
  188. w = b + w + sw;
  189. cb(null, w);
  190. }
  191. var next = function(v,f){ // TODO: BUG! Missing adding meta data.
  192. if(Gun._.meta === f){ return }
  193. if(next.f){
  194. val(next.v, next.f, v,f);
  195. }
  196. next.f = f;
  197. next.v = v;
  198. }
  199. Gun.obj.map(n, next);
  200. next();
  201. return t;
  202. }
  203. Gun.put.wire.from = function(t, cb, opt){
  204. if(!t){ return null }
  205. var a = Gun.put.wire.from.parse(t);
  206. Gun.list.map(a, function(v, i){
  207. if(Gun._.soul === v){
  208. Gun.is.node.soul.ify(n, Gun.put.wire.type(a[i]));
  209. return;
  210. }
  211. if(Gun._.field === v){
  212. var val = a.indexOf(Gun._.value,i), state = a.indexOf(Gun._.state,i);
  213. Gun.is.node.state.ify([n], Gun.put.wire.type(a[i]), Gun.put.wire.type(a[val+1]), Gun.put.wire.type(a[state+1]));
  214. return;
  215. }
  216. })
  217. return n;
  218. }
  219. Gun.put.wire.from.parse = function(t){
  220. var a = [], s = -1, e = 0, end = 1, n = {};
  221. while((e = t.indexOf("'", s + 1)) >= 0){
  222. if(s === e || '\\' === t.charAt(e-1)){}else{
  223. a.push(t.slice(s + 1,e));
  224. s = e;
  225. }
  226. }
  227. return a;
  228. }
  229. }());
  230. */
  231. ;(function(){ // make as separate module!
  232. function SQL(){}
  233. SQL.select = function(sel){
  234. this._.sql.select = sel;
  235. return this;
  236. }
  237. SQL.from = function(from){
  238. this._.sql.from = from;
  239. //this.get(from).map();
  240. return this;
  241. }
  242. SQL.where = function(where){
  243. this._.sql.where = where;
  244. return this;
  245. }
  246. Gun.chain.sql = function(sql){
  247. var gun = this;//.chain();
  248. sql = gun._.sql = sql || {};
  249. gun.select = SQL.select;
  250. gun.from = SQL.from;
  251. gun.where = SQL.where;
  252. return gun;
  253. }
  254. Gun.on('chain').event(function(gun, at){
  255. console.log("sql stuff?", gun._, at.node);
  256. var query = gun._.sql;
  257. if(!query){ return }
  258. var node = at.node;
  259. });
  260. }());