-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorphoDBcreator.js
More file actions
241 lines (189 loc) · 5.76 KB
/
morphoDBcreator.js
File metadata and controls
241 lines (189 loc) · 5.76 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict'
const hrstart = process.hrtime()
const fs = require('fs')
const readline = require('readline')
const zlib = require('zlib')
const path = require('path')
let byteCount = 0
let fileSize = 0
/*
This function (createHashIndex) was borrowed from https://gist.github.com/alexhawkins/48d7fd31af6ed00e5c60
Thanks to alexhawkins
*/
function createHashIndex (key, max) {
let hash = 0
for (var i = 0; i < key.length; i++) {
hash = (hash << 5) - hash + key.charCodeAt(i)
hash = hash >>> 0 // convert to 32bit unsigned integer
}
return Math.abs(hash % max)
}
function guessEncoding (path) {
const BOM_0 = 0xff
const BOM_1 = 0xfe
try {
const fd = fs.openSync(path, 'r')
const bf = Buffer.alloc(2)
fs.readSync(fd, bf, 0, 2, 0)
fs.closeSync(fd)
return bf[0] === BOM_0 && bf[1] === BOM_1 ? 'utf16le' : 'utf8'
} catch (e) {
console.error(`Error: ${e.message}.`)
return null
}
}
function updateProgressBar () {
if (!fileSize || !byteCount) return
const readPercent = Math.ceil(byteCount / fileSize * 100)
if (readPercent > 100) readPercent = 100
const barsNumber = Math.floor(readPercent / 2)
const padsNumber = 50 - barsNumber
readline.cursorTo(process.stdout, 0)
readline.clearLine(process.stdout, 0)
if (readPercent) {
process.stdout.write(
`${'█'.repeat(barsNumber)}${' '.repeat(padsNumber)} ${readPercent}%`
)
}
}
const tab = Object.create(null)
tab.data = Object.create(null)
const arr1 = []
const arr2 = []
const ignored = []
function readfile (inputfile, encoding) {
let lineCount = 0
return new Promise((resolve, reject) => {
const updater = setInterval(updateProgressBar, 100)
readline
.createInterface({
input: fs.createReadStream(inputfile, encoding),
terminal: false,
historySize: 0,
output: null,
crlfDelay: Infinity
})
.on('line', line => {
byteCount += Buffer.byteLength(line, encoding) + 1
lineCount++
if (lineCount === 1) line = line.replace(/^\uFEFF/, '')
const arr = line.trim().split('\t').map(el => el.trim()).filter(Boolean)
if (arr.length === 2) {
const [word, data] = arr
const key = word.toLowerCase()
if (tab.data[key] === undefined) {
tab.data[key] = `${word}\t${data}`
} else {
tab.data[key] += `\r${word}\t${data}`
}
} else {
ignored.push(`Line ${lineCount}: ${line}`)
}
})
.on('close', () => {
clearInterval(updater)
byteCount = fileSize
updateProgressBar()
console.log('\n\n')
resolve()
})
.on('error', err => {
reject(err)
})
})
}
async function main () {
try {
fileSize = fs.statSync(process.argv[2])['size']
const encoding = guessEncoding(process.argv[2])
console.log('Reading input file:')
await readfile(process.argv[2], encoding)
process.stdout.write('Creating database...')
for (const k in tab.data) {
const unique = [...new Set(tab.data[k].split('\r'))]
arr1.push(`${k}\t${unique.join('\r')}`)
}
delete tab.data
arr2.length = arr1.length
arr2.fill('')
for (const v of arr1) {
const i = createHashIndex(v.split('\t')[0], arr1.length)
arr2[i] = `${arr2[i]}\n${v}`.trim()
}
arr1.length = 0
const arr3 = []
const arr4 = []
let offset = 0
const res = path.parse(process.argv[2])
const ranges_file = `${res.name}.ranges.dat`
const blocks_file = `${res.name}.blocks.dat`
fs.writeFileSync(blocks_file, '', { flag: 'w' })
for (let i = 0; i < arr2.length; i++) {
arr3.push(arr2[i])
if (arr3.length === 100) {
const buf1 = zlib.deflateRawSync(arr3.join('\x00'))
fs.writeFileSync(blocks_file, buf1, { flag: 'a' })
const length = buf1.byteLength
arr3.length = 0
arr4.push(`${offset},${length}`)
offset += length
}
}
if (arr3.length > 0) {
const buf1 = zlib.deflateRawSync(arr3.join('\x00'))
fs.writeFileSync(blocks_file, buf1, { flag: 'a' })
const length = buf1.byteLength
arr3.length = 0
arr4.push(`${offset},${length}`)
}
let HashTableRowSize = 0
for (const v of arr4) {
const buf = Buffer.from(v, 'utf8')
if (buf.byteLength > HashTableRowSize) {
HashTableRowSize = buf.byteLength
}
}
{
const buf1 = Buffer.from(`${arr4.length}\t${HashTableRowSize}\t${arr2.length.toString()}`)
const buf2 = Buffer.alloc(64)
buf1.copy(buf2)
fs.writeFileSync(ranges_file, buf2, { flag: 'w' })
}
for (const v of arr4) {
const buf1 = Buffer.alloc(HashTableRowSize)
const buf2 = Buffer.from(v, 'utf8')
buf2.copy(buf1)
fs.writeFileSync(ranges_file, buf1, { flag: 'a' })
}
const count = arr2.length
arr2.length = 0
if (ignored.length > 0) {
fs.writeFileSync('ignored.txt', '', { encoding: 'utf8', flag: 'w' })
} else {
try {
fs.unlinkSync('ignored.txt')
} catch(e) {
}
}
for (let v of ignored) {
fs.writeFileSync('ignored.txt', v + '\n', { encoding: 'utf8', flag: 'a' })
}
process.stdout.write('\rCreating database... Done\n')
console.log('Added: ' + count)
console.log('Ignored: ' + ignored.length)
const hrend = process.hrtime(hrstart)
console.log(
`\n\nExecution time: ${hrend[0]}.${Math.floor(hrend[1] / 1000000)}\n`
)
process.stdout.write('Memory Usage: ')
console.log(process.memoryUsage().rss / 1024 / 1024)
} catch (err) {
console.log(err)
}
}
if (process.argv.length === 3 || fileExists(process.argv[2])) {
main()
} else {
console.log('Invalid command line.')
process.exit()
}