-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreader.js
More file actions
174 lines (149 loc) · 3.86 KB
/
reader.js
File metadata and controls
174 lines (149 loc) · 3.86 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
/*jshint multistr: true ,node: true*/
"use strict";
let
fs = require('fs');
let
OPEN = '{',
CLOSE = '}';
class CharReader {
constructor(filepath) {
this.filepath = filepath;
this.offset = 0;
this.position = -1;
this.eof = false;
this.buffer = new Buffer(this.MAX_BUFFER);
this.str = '';
this.fd = fs.openSync(this.filepath, 'r');
}
isEof() {
return this.eof;
}
fillBuffer() {
let length = fs.readSync(this.fd, this.buffer, this.offset, this.MAX_BUFFER, null);
if(length == 0) {
this.eof = true;
}
return length;
}
getNextChar() {
if(this.eof) {
return false;
}
this.position++;
let char = this.buffer[this.position];
if(char === 0 || char === undefined) {
// we have empty buffer
this.fillBuffer();
this.position = -1;
return this.getNextChar();
}
char = String.fromCharCode(char);
return char;
}
}
CharReader.prototype.MAX_BUFFER = 1024;
class LineReader extends CharReader {
constructor(filepath) {
super(filepath);
}
getNextLine(trim = true) {
let line = '';
while(!this.eof) {
let char = this.getNextChar();
if(char == '\n') {
if(trim) {
line = line.trim();
}
return line;
}
line += char;
}
}
}
/**
*
* @param {JSON} json
* @returns {Promise}
*/
function onJson(json) {
}
/**
*
* @param {number} totalObjects
*/
function onEnd(totalObjects) {
}
function callback(onJSON, onEnd) {
}
class JsonReader extends CharReader {
constructor(filepath) {
super(filepath);
this.json = '';
this.counter = 0;
this.totalObjects = 0;
this.regOpen = new RegExp(OPEN, "g");
this.regClose = new RegExp(CLOSE, "g");
}
reset() {
this.json = '';
this.counter = 0;
}
count() {
let open = (this.json.match(this.regOpen) || []).length;
let close = (this.json.match(this.regClose) || []).length;
return open > 0 && open == close;
}
printUsage() {
const used = process.memoryUsage().heapUsed / 1024 / 1024;
console.log(`\n\nThe script uses approximately ${Math.round(used * 100) / 100} MB`);
}
jsonFound() {
return this.counter != 0;
}
isValid() {
return this.counter == 0 && this.json != '' && this.count();
}
/**
*
* @param {onJson} onJson
* @param {onEnd} onEnd
*/
read(onJson, onEnd) {
while(!this.eof) {
let char = this.getNextChar();
if(char == OPEN) {
this.counter++;
}
// if we have started a valide json
if(this.jsonFound()) {
this.json += char;
}
if(char == CLOSE) {
this.counter--;
}
// if we have got a copmlete valid json
if(this.isValid()) {
try {
this.json = JSON.parse(this.json);
} catch (error) {
this.reset();
return;
}
onJson(this.json)
.then(() => {
this.totalObjects++;
this.reset();
this.read(onJson, onEnd);
}).catch(() => {
this.reset();
this.read(onJson, onEnd);
});
return;
}
}
if(this.eof && onEnd) {
onEnd(this.totalObjects);
}
}
}
module.exports = JsonReader;