-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
179 lines (176 loc) · 5.39 KB
/
db.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const MongoClient = require('mongodb').MongoClient
var db
var mongo
var config
var init = function(_config) {
config = _config
return new Promise(function(resolve) {
MongoClient.connect(_config.url, {useNewUrlParser: true}, function(err, client) {
if (err) console.log(err)
db = client.db(_config.name)
mongo = client
resolve()
})
})
}
var exit = function() {
return new Promise(function(resolve) {
mongo.close()
resolve()
})
}
var mempool = {
insert: function(item) {
return db.collection('unconfirmed').insertMany([item])
},
reset: async function() {
await db.collection('unconfirmed').deleteMany({}).catch(function(err) {
console.log('## ERR ', err)
process.exit()
})
},
sync: async function(items) {
await db.collection('unconfirmed').deleteMany({}).catch(function(err) {
console.log('## ERR ', err)
})
let index = 0
while (true) {
let chunk = items.splice(0, 1000)
if (chunk.length > 0) {
await db.collection('unconfirmed').insertMany(chunk, { ordered: false }).catch(function(err) {
// duplicates are ok because they will be ignored
if (err.code !== 11000) {
console.log('## ERR ', err, items)
process.exit()
}
})
console.log('..chunk ' + index + ' processed ...', new Date().toString())
index++
} else {
break
}
}
console.log('Mempool synchronized with ' + items.length + ' items')
}
}
var block = {
reset: async function() {
await db.collection('confirmed').deleteMany({}).catch(function(err) {
console.log('## ERR ', err)
process.exit()
})
},
replace: async function(items, block_index) {
console.log('Deleting all blocks greater than or equal to', block_index)
await db.collection('confirmed').deleteMany({
'blk.i': {
$gte: block_index
}
}).catch(function(err) {
console.log('## ERR ', err)
process.exit()
})
console.log('Updating block', block_index, 'with', items.length, 'items')
let index = 0
while (true) {
let chunk = items.slice(index, index+1000)
if (chunk.length > 0) {
await db.collection('confirmed').insertMany(chunk, { ordered: false }).catch(function(err) {
// duplicates are ok because they will be ignored
if (err.code !== 11000) {
console.log('## ERR ', err, items)
process.exit()
}
})
console.log('\tchunk ' + index + ' processed ...')
index+=1000
} else {
break
}
}
},
insert: async function(items, block_index) {
let index = 0
while (true) {
let chunk = items.slice(index, index+1000)
if (chunk.length > 0) {
try {
await db.collection('confirmed').insertMany(chunk, { ordered: false })
console.log('..chunk ' + index + ' processed ...')
} catch (e) {
// duplicates are ok because they will be ignored
if (e.code !== 11000) {
console.log('## ERR ', e, items, block_index)
process.exit()
}
}
index+=1000
} else {
break
}
}
console.log('Block ' + block_index + ' inserted ')
},
index: async function() {
console.log('* Indexing MongoDB...')
console.time('TotalIndex')
if (config.index) {
let collectionNames = Object.keys(config.index)
for(let j=0; j<collectionNames.length; j++) {
let collectionName = collectionNames[j]
let keys = config.index[collectionName].keys
let fulltext = config.index[collectionName].fulltext
if (keys) {
console.log('Indexing keys...')
for(let i=0; i<keys.length; i++) {
let o = {}
o[keys[i]] = 1
console.time('Index:' + keys[i])
try {
if (keys[i] === 'tx.h') {
await db.collection(collectionName).createIndex(o, { unique: true })
console.log('* Created unique index for ', keys[i])
} else {
await db.collection(collectionName).createIndex(o)
console.log('* Created index for ', keys[i])
}
} catch (e) {
console.log(e)
process.exit()
}
console.timeEnd('Index:' + keys[i])
}
}
if (fulltext) {
console.log('Creating full text index...')
let o = {}
fulltext.forEach(function(key) {
o[key] = 'text'
})
console.time('Fulltext search for ' + collectionName, o)
try {
await db.collection(collectionName).createIndex(o, { name: 'fulltext' })
} catch (e) {
console.log(e)
process.exit()
}
console.timeEnd('Fulltext search for ' + collectionName)
}
}
}
console.log('* Finished indexing MongoDB...')
console.timeEnd('TotalIndex')
try {
let result = await db.collection('confirmed').indexInformation({full: true})
console.log('* Confirmed Index = ', result)
result = await db.collection('unconfirmed').indexInformation({full: true})
console.log('* Unonfirmed Index = ', result)
} catch (e) {
console.log('* Error fetching index info ', e)
process.exit()
}
}
}
module.exports = {
init: init, exit: exit, block: block, mempool: mempool
}