radisk.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. ;(function(){
  2. function Radisk(opt){
  3. opt = opt || {};
  4. opt.log = opt.log || console.log;
  5. opt.file = String(opt.file || 'radata');
  6. var has = (Radisk.has || (Radisk.has = {}))[opt.file];
  7. if(has){ return has }
  8. opt.pack = opt.pack || (opt.memory? (opt.memory * 1000 * 1000) : 1399000000) * 0.3; // max_old_space_size defaults to 1400 MB.
  9. opt.until = opt.until || opt.wait || 250;
  10. opt.batch = opt.batch || (10 * 1000);
  11. opt.chunk = opt.chunk || (1024 * 1024 * 1); // 1MB
  12. opt.code = opt.code || {};
  13. opt.code.from = opt.code.from || '!';
  14. opt.jsonify = true;
  15. function ename(t){ return encodeURIComponent(t).replace(/\*/g, '%2A') }
  16. function atomic(v){ return u !== v && (!v || 'object' != typeof v) }
  17. var timediate = (typeof setImmediate === "undefined")? setTimeout : setImmediate;
  18. var puff = setTimeout.puff || timediate;
  19. var map = Gun.obj.map;
  20. var obj_empty = Gun.obj.empty;
  21. var ST = 0;
  22. if(!opt.store){
  23. return opt.log("ERROR: Radisk needs `opt.store` interface with `{get: fn, put: fn (, list: fn)}`!");
  24. }
  25. if(!opt.store.put){
  26. return opt.log("ERROR: Radisk needs `store.put` interface with `(file, data, cb)`!");
  27. }
  28. if(!opt.store.get){
  29. return opt.log("ERROR: Radisk needs `store.get` interface with `(file, cb)`!");
  30. }
  31. if(!opt.store.list){
  32. //opt.log("WARNING: `store.list` interface might be needed!");
  33. }
  34. /*
  35. Any and all storage adapters should...
  36. 1. Because writing to disk takes time, we should batch data to disk. This improves performance, and reduces potential disk corruption.
  37. 2. If a batch exceeds a certain number of writes, we should immediately write to disk when physically possible. This caps total performance, but reduces potential loss.
  38. */
  39. var r = function(key, data, cb, tag, DBG){
  40. if('function' === typeof data){
  41. var o = cb || {};
  42. cb = data;
  43. r.read(key, cb, o, DBG || tag);
  44. return;
  45. }
  46. //var tmp = (tmp = r.batch = r.batch || {})[key] = tmp[key] || {};
  47. //var tmp = (tmp = r.batch = r.batch || {})[key] = data;
  48. r.save(key, data, cb, tag, DBG);
  49. }
  50. r.save = function(key, data, cb, tag, DBG){
  51. var s = {key: key}, tags, f, d, q;
  52. s.find = function(file){ var tmp;
  53. s.file = file || (file = opt.code.from);
  54. DBG && (DBG = DBG[file] = DBG[file] || {});
  55. DBG && (DBG.sf = DBG.sf || +new Date);
  56. if(tmp = r.disk[file]){ s.mix(u, tmp); return }
  57. r.parse(file, s.mix, u, DBG);
  58. }
  59. s.mix = function(err, disk){
  60. DBG && (DBG.sml = +new Date);
  61. DBG && (DBG.sm = DBG.sm || +new Date);
  62. if(s.err = err || s.err){ cb(err); return } // TODO: HANDLE BATCH EMIT
  63. var file = s.file = (disk||'').file || s.file, tmp;
  64. if(!disk && file !== opt.code.from){ // corrupt file?
  65. r.find.bad(file); // remove from dir list
  66. r.save(key, data, cb, tag); // try again
  67. return;
  68. }
  69. (disk = r.disk[file] || (r.disk[file] = disk || Radix())).file || (disk.file = file);
  70. if(opt.compare){
  71. data = opt.compare(disk(key), data, key, file);
  72. if(u === data){ cb(err, -1); return } // TODO: HANDLE BATCH EMIT
  73. }
  74. (s.disk = disk)(key, data);
  75. if(tag){
  76. (tmp = (tmp = disk.tags || (disk.tags = {}))[tag] || (tmp[tag] = r.tags[tag] || (r.tags[tag] = {})))[file] || (tmp[file] = r.one[tag] || (r.one[tag] = cb));
  77. cb = null;
  78. }
  79. DBG && (DBG.st = DBG.st || +new Date);
  80. if(disk.Q){ cb && disk.Q.push(cb); return } disk.Q = (cb? [cb] : []);
  81. disk.to = setTimeout(s.write, opt.until);
  82. }
  83. s.write = function(){
  84. DBG && (DBG.sto = DBG.sto || +new Date);
  85. var file = f = s.file, disk = d = s.disk;
  86. q = s.q = disk.Q;
  87. tags = s.tags = disk.tags;
  88. delete disk.Q;
  89. delete r.disk[file];
  90. delete disk.tags;
  91. r.write(file, disk, s.ack, u, DBG);
  92. }
  93. s.ack = function(err, ok){
  94. DBG && (DBG.sa = DBG.sa || +new Date);
  95. DBG && (DBG.sal = q.length);
  96. var ack, tmp;
  97. // TODO!!!! CHANGE THIS INTO PUFF!!!!!!!!!!!!!!!!
  98. for(var id in r.tags){
  99. if(!r.tags.hasOwnProperty(id)){ continue } var tag = r.tags[id];
  100. if((tmp = r.disk[f]) && (tmp = tmp.tags) && tmp[tag]){ continue }
  101. ack = tag[f];
  102. delete tag[f];
  103. if(!obj_empty(tag)){ continue }
  104. delete r.tags[tag];
  105. ack && ack(err, ok);
  106. }
  107. !q && (q = '');
  108. var l = q.length, i = 0;
  109. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  110. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  111. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  112. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  113. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  114. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  115. // TODO: PERF: Why is acks so slow, what work do they do??? CHECK THIS!!
  116. var S = +new Date;
  117. for(;i < l; i++){ (ack = q[i]) && ack(err, ok) }
  118. console.STAT && console.STAT(S, +new Date - S, 'rad acks', ename(s.file));
  119. console.STAT && console.STAT(S, q.length, 'rad acks #', ename(s.file));
  120. }
  121. cb || (cb = function(err, ok){ // test delete!
  122. if(!err){ return }
  123. });
  124. r.find(key, s.find);
  125. }
  126. r.disk = {};
  127. r.one = {};
  128. r.tags = {};
  129. /*
  130. Any storage engine at some point will have to do a read in order to write.
  131. This is true of even systems that use an append only log, if they support updates.
  132. Therefore it is unavoidable that a read will have to happen,
  133. the question is just how long you delay it.
  134. */
  135. var RWC = 0;
  136. r.write = function(file, rad, cb, o, DBG){
  137. if(!rad){ cb('No radix!'); return }
  138. o = ('object' == typeof o)? o : {force: o};
  139. var f = function Fractal(){}, a, b;
  140. f.text = '';
  141. f.file = file = rad.file || (rad.file = file);
  142. if(!file){ cb('What file?'); return }
  143. f.write = function(){
  144. var text = rad.raw = f.text;
  145. r.disk[file = rad.file || f.file || file] = rad;
  146. var S = +new Date;
  147. DBG && (DBG.wd = S);
  148. r.find.add(file, function add(err){
  149. DBG && (DBG.wa = +new Date);
  150. if(err){ cb(err); return }
  151. opt.store.put(ename(file), text, function safe(err, ok){
  152. DBG && (DBG.wp = +new Date);
  153. console.STAT && console.STAT(S, ST = +new Date - S, "wrote disk", JSON.stringify(file), ++RWC, 'total all writes.');
  154. cb(err, ok || 1);
  155. if(!rad.Q){ delete r.disk[file] } // VERY IMPORTANT! Clean up memory, but not if there is already queued writes on it!
  156. });
  157. });
  158. }
  159. f.split = function(){
  160. var S = +new Date;
  161. DBG && (DBG.wf = S);
  162. f.text = '';
  163. if(!f.count){ f.count = 0;
  164. Radix.map(rad, function count(){ f.count++ }); // TODO: Perf? Any faster way to get total length?
  165. }
  166. DBG && (DBG.wfc = f.count);
  167. f.limit = Math.ceil(f.count/2);
  168. var SC = f.count;
  169. f.count = 0;
  170. DBG && (DBG.wf1 = +new Date);
  171. f.sub = Radix();
  172. Radix.map(rad, f.slice, {reverse: 1}); // IMPORTANT: DO THIS IN REVERSE, SO LAST HALF OF DATA MOVED TO NEW FILE BEFORE DROPPING FROM CURRENT FILE.
  173. DBG && (DBG.wf2 = +new Date);
  174. r.write(f.end, f.sub, f.both, o);
  175. DBG && (DBG.wf3 = +new Date);
  176. f.hub = Radix();
  177. Radix.map(rad, f.stop);
  178. DBG && (DBG.wf4 = +new Date);
  179. r.write(rad.file, f.hub, f.both, o);
  180. DBG && (DBG.wf5 = +new Date);
  181. console.STAT && console.STAT(S, +new Date - S, "rad split", ename(rad.file), SC);
  182. return true;
  183. }
  184. f.slice = function(val, key){
  185. f.sub(f.end = key, val);
  186. if(f.limit <= (++f.count)){ return true }
  187. }
  188. f.stop = function(val, key){
  189. if(key >= f.end){ return true }
  190. f.hub(key, val);
  191. }
  192. f.both = function(err, ok){
  193. DBG && (DBG.wfd = +new Date);
  194. if(b){ cb(err || b); return }
  195. if(a){ cb(err, ok); return }
  196. a = true;
  197. b = err;
  198. }
  199. f.each = function(val, key, k, pre){
  200. if(u !== val){ f.count++ }
  201. if(opt.pack <= (val||'').length){ return cb("Data too big!"), true }
  202. var enc = Radisk.encode(pre.length) +'#'+ Radisk.encode(k) + (u === val? '' : ':'+ Radisk.encode(val)) +'\n';
  203. if((opt.chunk < f.text.length + enc.length) && (1 < f.count) && !o.force){
  204. return f.split();
  205. }
  206. f.text += enc;
  207. }
  208. if(opt.jsonify){ r.write.jsonify(f, rad, cb, o, DBG); return } // temporary testing idea
  209. if(!Radix.map(rad, f.each, true)){ f.write() }
  210. }
  211. r.write.jsonify = function(f, rad, cb, o, DBG){
  212. var raw;
  213. var S = +new Date;
  214. DBG && (DBG.w = S);
  215. try{raw = JSON.stringify(rad.$);
  216. }catch(e){ cb("Cannot radisk!"); return }
  217. DBG && (DBG.ws = +new Date);
  218. console.STAT && console.STAT(S, +new Date - S, "rad stringified JSON");
  219. if(opt.chunk < raw.length && !o.force){
  220. var c = 0;
  221. Radix.map(rad, function(){
  222. if(c++){ return true } // more than 1 item
  223. });
  224. if(c > 1){
  225. return f.split();
  226. }
  227. }
  228. f.text = raw;
  229. f.write();
  230. }
  231. r.range = function(tree, o){
  232. if(!tree || !o){ return }
  233. if(u === o.start && u === o.end){ return tree }
  234. if(atomic(tree)){ return tree }
  235. var sub = Radix();
  236. Radix.map(tree, function(v,k){ sub(k,v) }, o); // ONLY PLACE THAT TAKES TREE, maybe reduce API for better perf?
  237. return sub('');
  238. }
  239. ;(function(){
  240. r.read = function(key, cb, o, DBG){
  241. o = o || {};
  242. var g = {key: key};
  243. g.find = function(file){ var tmp;
  244. g.file = file || (file = opt.code.from);
  245. DBG && (DBG = DBG[file] = DBG[file] || {});
  246. DBG && (DBG.rf = DBG.rf || +new Date);
  247. if(tmp = r.disk[g.file = file]){ g.check(u, tmp); return }
  248. r.parse(file, g.check, u, DBG);
  249. }
  250. g.get = function(err, disk, info){
  251. DBG && (DBG.rgl = +new Date);
  252. DBG && (DBG.rg = DBG.rg || +new Date);
  253. if(g.err = err || g.err){ cb(err); return }
  254. var file = g.file = (disk||'').file || g.file;
  255. if(!disk && file !== opt.code.from){ // corrupt file?
  256. r.find.bad(file); // remove from dir list
  257. r.read(key, cb, o); // try again
  258. return;
  259. }
  260. disk = r.disk[file] || (r.disk[file] = disk);
  261. if(!disk){ cb(file === opt.code.from? u : "No file!"); return }
  262. disk.file || (disk.file = file);
  263. var data = r.range(disk(key), o);
  264. DBG && (DBG.rr = +new Date);
  265. o.unit = disk.unit;
  266. o.chunks = (o.chunks || 0) + 1;
  267. o.parsed = (o.parsed || 0) + ((info||'').parsed||(o.chunks*opt.chunk));
  268. o.more = 1;
  269. o.next = u;
  270. Radix.map(r.list, function next(v,f){
  271. if(!v || file === f){ return }
  272. o.next = f;
  273. return 1;
  274. }, o.reverse? {reverse: 1, end: file} : {start: file});
  275. DBG && (DBG.rl = +new Date);
  276. if(!o.next){ o.more = 0 }
  277. if(o.next){
  278. if(!o.reverse && (key < o.next && 0 != o.next.indexOf(key)) || (u !== o.end && (o.end || '\uffff') < o.next)){ o.more = 0 }
  279. if(o.reverse && (key > o.next && 0 != key.indexOf(o.next)) || (u !== o.start && (o.start || '') > o.next)){ o.more = 0 }
  280. }
  281. if(!o.more){ cb(g.err, data, o); return }
  282. if(data){ cb(g.err, data, o) }
  283. if(o.parsed >= o.limit){ return }
  284. var S = +new Date;
  285. DBG && (DBG.rm = S);
  286. var next = o.next;
  287. timediate(function(){
  288. console.STAT && console.STAT(S, +new Date - S, 'rad more');
  289. r.parse(next, g.check);
  290. },0);
  291. }
  292. g.check = function(err, disk, info){
  293. g.get(err, disk, info);
  294. if(!disk || disk.check){ return } disk.check = 1;
  295. var S = +new Date;
  296. (info || (info = {})).file || (info.file = g.file);
  297. Radix.map(disk, function(val, key){
  298. // assume in memory for now, since both write/read already call r.find which will init it.
  299. r.find(key, function(file){
  300. if((file || (file = opt.code.from)) === info.file){ return }
  301. var id = Gun.text.random(3);
  302. puff(function(){
  303. r.save(key, val, function ack(err, ok){
  304. if(err){ r.save(key, val, ack); return } // ad infinitum???
  305. // TODO: NOTE!!! Mislocated data could be because of a synchronous `put` from the `g.get(` other than perf shouldn't we do the check first before acking?
  306. console.STAT && console.STAT("MISLOCATED DATA CORRECTED", id, ename(key), ename(info.file), ename(file));
  307. });
  308. },0);
  309. })
  310. });
  311. console.STAT && console.STAT(S, +new Date - S, "rad check");
  312. }
  313. r.find(key, g.find);
  314. }
  315. function rev(a,b){ return b }
  316. var revo = {reverse: true};
  317. }());
  318. ;(function(){
  319. /*
  320. Let us start by assuming we are the only process that is
  321. changing the directory or bucket. Not because we do not want
  322. to be multi-process/machine, but because we want to experiment
  323. with how much performance and scale we can get out of only one.
  324. Then we can work on the harder problem of being multi-process.
  325. */
  326. var RPC = 0;
  327. var Q = {}, s = String.fromCharCode(31);
  328. r.parse = function(file, cb, raw, DBG){ var q;
  329. if(!file){ return cb(); }
  330. if(q = Q[file]){ q.push(cb); return } q = Q[file] = [cb];
  331. var p = function Parse(){}, info = {file: file};
  332. (p.disk = Radix()).file = file;
  333. p.read = function(err, data){ var tmp;
  334. DBG && (DBG.rpg = +new Date);
  335. console.STAT && console.STAT(S, +new Date - S, 'read disk', JSON.stringify(file), ++RPC, 'total all parses.');
  336. delete Q[file];
  337. if((p.err = err) || (p.not = !data)){ p.map(q, p.ack); return }
  338. if('string' !== typeof data){
  339. try{
  340. if(opt.pack <= data.length){
  341. p.err = "Chunk too big!";
  342. } else {
  343. data = data.toString(); // If it crashes, it crashes here. How!?? We check size first!
  344. }
  345. }catch(e){ p.err = e }
  346. if(p.err){ p.map(q, p.ack); return }
  347. }
  348. info.parsed = data.length;
  349. DBG && (DBG.rpl = info.parsed);
  350. DBG && (DBG.rpa = q.length);
  351. S = +new Date;
  352. if(opt.jsonify || '{' === data[0]){
  353. try{
  354. var json = JSON.parse(data); // TODO: this caused a out-of-memory crash!
  355. p.disk.$ = json;
  356. console.STAT && (ST = +new Date - S) > 9 && console.STAT(S, ST, 'rad parsed JSON');
  357. DBG && (DBG.rpd = +new Date);
  358. p.map(q, p.ack); // hmmm, v8 profiler can't see into this cause of try/catch?
  359. return;
  360. }catch(e){ tmp = e }
  361. if('{' === data[0]){
  362. p.err = tmp || "JSON error!";
  363. p.map(q, p.ack);
  364. return;
  365. }
  366. }
  367. p.radec(err, data);
  368. }
  369. p.map = function(){
  370. if(!q || !q.length){ return }
  371. //var i = 0, l = q.length, ack;
  372. var S = +new Date;
  373. var err = p.err, data = p.not? u : p.disk;
  374. var i = 0, ack; while(i < 9 && (ack = q[i++])){ ack(err, data, info) } // too much?
  375. console.STAT && console.STAT(S, +new Date - S, 'rad packs', ename(file));
  376. console.STAT && console.STAT(S, i, 'rad packs #', ename(file));
  377. if(!(q = q.slice(i)).length){ return }
  378. puff(p.map, 0);
  379. }
  380. p.ack = function(cb){
  381. if(!cb){ return }
  382. if(p.err || p.not){
  383. cb(p.err, u, info);
  384. return;
  385. }
  386. cb(u, p.disk, info);
  387. }
  388. p.radec = function(err, data){
  389. S = +new Date;
  390. var tmp = p.split(data), pre = [], i, k, v;
  391. if(!tmp || 0 !== tmp[1]){
  392. p.err = "File '"+file+"' does not have root radix! ";
  393. p.map(q, p.ack);
  394. return;
  395. }
  396. while(tmp){
  397. k = v = u;
  398. i = tmp[1];
  399. tmp = p.split(tmp[2])||'';
  400. if('#' == tmp[0]){
  401. k = tmp[1];
  402. pre = pre.slice(0,i);
  403. if(i <= pre.length){
  404. pre.push(k);
  405. }
  406. }
  407. tmp = p.split(tmp[2])||'';
  408. if('\n' == tmp[0]){ continue }
  409. if('=' == tmp[0] || ':' == tmp[0]){ v = tmp[1] }
  410. if(u !== k && u !== v){ p.disk(pre.join(''), v) }
  411. tmp = p.split(tmp[2]);
  412. }
  413. console.STAT && console.STAT(S, +new Date - S, 'parsed RAD');
  414. p.map(q, p.ack);
  415. };
  416. p.split = function(t){
  417. if(!t){ return }
  418. var l = [], o = {}, i = -1, a = '', b, c;
  419. i = t.indexOf(s);
  420. if(!t[i]){ return }
  421. a = t.slice(0, i);
  422. l[0] = a;
  423. l[1] = b = Radisk.decode(t.slice(i), o);
  424. l[2] = t.slice(i + o.i);
  425. return l;
  426. }
  427. if(r.disk){ raw || (raw = (r.disk[file]||'').raw) }
  428. var S = +new Date, SM, SL;
  429. DBG && (DBG.rp = S);
  430. if(raw){ return puff(function(){ p.read(u, raw) }, 0) }
  431. opt.store.get(ename(file), p.read);
  432. // TODO: What if memory disk gets filled with updates, and we get an old one back?
  433. }
  434. }());
  435. ;(function(){
  436. var dir, f = String.fromCharCode(28), Q;
  437. r.find = function(key, cb){
  438. if(!dir){
  439. if(Q){ Q.push([key, cb]); return } Q = [[key, cb]];
  440. r.parse(f, init);
  441. return;
  442. }
  443. Radix.map(r.list = dir, function(val, key){
  444. if(!val){ return }
  445. return cb(key) || true;
  446. }, {reverse: 1, end: key}) || cb(opt.code.from);
  447. }
  448. r.find.add = function(file, cb){
  449. var has = dir(file);
  450. if(has || file === f){ cb(u, 1); return }
  451. dir(file, 1);
  452. cb.found = (cb.found || 0) + 1;
  453. r.write(f, dir, function(err, ok){
  454. if(err){ cb(err); return }
  455. cb.found = (cb.found || 0) - 1;
  456. if(0 !== cb.found){ return }
  457. cb(u, 1);
  458. }, true);
  459. }
  460. r.find.bad = function(file, cb){
  461. dir(file, 0);
  462. r.write(f, dir, cb||noop);
  463. }
  464. function init(err, disk){
  465. if(err){
  466. opt.log('list', err);
  467. setTimeout(function(){ r.parse(f, init) }, 1000);
  468. return;
  469. }
  470. if(disk){ drain(disk); return }
  471. dir = dir || disk || Radix();
  472. if(!opt.store.list){ drain(dir); return }
  473. // import directory.
  474. opt.store.list(function(file){
  475. if(!file){ drain(dir); return }
  476. r.find.add(file, noop);
  477. });
  478. }
  479. function drain(rad, tmp){
  480. dir = dir || rad;
  481. dir.file = f;
  482. tmp = Q; Q = null;
  483. Gun.list.map(tmp, function(arg){
  484. r.find(arg[0], arg[1]);
  485. });
  486. }
  487. }());
  488. try{ !Gun.window && require('./radmigtmp')(r) }catch(e){}
  489. var noop = function(){}, RAD, u;
  490. Radisk.has[opt.file] = r;
  491. return r;
  492. }
  493. ;(function(){
  494. var _ = String.fromCharCode(31), u;
  495. Radisk.encode = function(d, o, s){ s = s || _;
  496. var t = s, tmp;
  497. if(typeof d == 'string'){
  498. var i = d.indexOf(s);
  499. while(i != -1){ t += s; i = d.indexOf(s, i+1) }
  500. return t + '"' + d + s;
  501. } else
  502. if(d && d['#'] && (tmp = Gun.val.link.is(d))){
  503. return t + '#' + tmp + t;
  504. } else
  505. if(Gun.num.is(d)){
  506. return t + '+' + (d||0) + t;
  507. } else
  508. if(null === d){
  509. return t + ' ' + t;
  510. } else
  511. if(true === d){
  512. return t + '+' + t;
  513. } else
  514. if(false === d){
  515. return t + '-' + t;
  516. }// else
  517. //if(binary){}
  518. }
  519. Radisk.decode = function(t, o, s){ s = s || _;
  520. var d = '', i = -1, n = 0, c, p;
  521. if(s !== t[0]){ return }
  522. while(s === t[++i]){ ++n }
  523. p = t[c = n] || true;
  524. while(--n >= 0){ i = t.indexOf(s, i+1) }
  525. if(i == -1){ i = t.length }
  526. d = t.slice(c+1, i);
  527. if(o){ o.i = i+1 }
  528. if('"' === p){
  529. return d;
  530. } else
  531. if('#' === p){
  532. return Gun.val.link.ify(d);
  533. } else
  534. if('+' === p){
  535. if(0 === d.length){
  536. return true;
  537. }
  538. return parseFloat(d);
  539. } else
  540. if(' ' === p){
  541. return null;
  542. } else
  543. if('-' === p){
  544. return false;
  545. }
  546. }
  547. }());
  548. if(typeof window !== "undefined"){
  549. var Gun = window.Gun;
  550. var Radix = window.Radix;
  551. window.Radisk = Radisk;
  552. } else {
  553. var Gun = require('../gun');
  554. var Radix = require('./radix');
  555. //var Radix = require('./radix2'); Radisk = require('./radisk2');
  556. try{ module.exports = Radisk }catch(e){}
  557. }
  558. Radisk.Radix = Radix;
  559. }());