Files: 7538828ea2f1694c9e4e6f1d065f859360a1b4bc / db.js
1535 bytesRaw
1 | |
2 | var urls = require('./urls') |
3 | var config = require('./config') |
4 | var Sublevel = require('level-sublevel/bytewise') |
5 | var retrive = require('./') |
6 | var cont = require('cont') |
7 | var crypto = require('crypto') |
8 | |
9 | var db = Sublevel(require('level')(config.db, {valueEncoding: 'json'})) |
10 | var rawdb = db.sublevel('raw') |
11 | var sample = db.sublevel('sample') |
12 | |
13 | function hash (data) { |
14 | return crypto.createHash('sha256').update(JSON.stringify(data, null, 2)).digest('hex') |
15 | } |
16 | |
17 | db.update = function (cb) { |
18 | cont.para( |
19 | Object.keys(urls).map(function (name) { |
20 | return function (cb) { |
21 | retrive(name, function (err, data) { |
22 | if(err) return cb(err) |
23 | var text = JSON.stringify(data, null, 2) |
24 | var id = hash(text) |
25 | rawdb.get(id, function (err) { |
26 | return cb( |
27 | null, |
28 | err ? { key: id, value: data, type: 'put', prefix: rawdb } : null |
29 | ) |
30 | }) |
31 | }) |
32 | } |
33 | }) |
34 | ) (function (err, batch) { |
35 | if(err) return cb(err) |
36 | var ts = Date.now() |
37 | batch.filter(Boolean).forEach(function (e) { |
38 | console.log(e) |
39 | batch.push({key: [e.value.name, ts], value: e.key, type: 'put', prefix: sample}) |
40 | batch.push({key: e.value.name, value: e.key, type: 'put', prefix: db}) |
41 | }) |
42 | if(!batch.length) return cb(null, []) |
43 | console.error(batch) |
44 | batch = batch.filter(Boolean) |
45 | db.batch(batch, function (err) { |
46 | cb(null, batch.map(function (e) { |
47 | return {key: e.key, value: e.value} |
48 | })) |
49 | }) |
50 | }) |
51 | } |
52 | |
53 | module.exports = db |
54 | |
55 | |
56 | |
57 |
Built with git-ssb-web