-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcache.js
More file actions
244 lines (238 loc) · 5.16 KB
/
cache.js
File metadata and controls
244 lines (238 loc) · 5.16 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
241
242
243
244
'use strict';
/*
* @prototype
*/
const CacheBase = function(){
/*
* @param {string} data
* @param {integer} block
* @public
* @return {booolean}
*/
this.setString = function(data, block){
if(Buffer.byteLength(data) > _block_size)
return false;
if (!Number.isInteger( block ))
return false;
if(block >= _block_count)
return false;
if(typeof data !== 'string')
return false;
return _setBlock(data, block);
};
/*
* @param {integer} block
* @public
* @return {string||booolean}
*/
this.getString = function( block ){
if (Number.isInteger(block))
return _getString(block);
return false;
};
/*
* @param {integer} block
* @public
* @return {buffer||booolean}
*/
this.getBuffer = function( block ){
if (Number.isInteger(block))
return _getBuffer(block);
return false;
};
/*
* @param {integer} block_size
* @param {integer} block_count
* @public
* @return {string||booolean}
*/
this.create = function( block_size, block_count ){
return _createDB(
block_size,
block_count
);
};
/* just for testing
this.db = function(){
return _db;
}
*/
/*
* @private
* @var {buffer}
*/
let _db;
/*
* @private
* @var {boolean}
*/
let _started = false;
/*
* @private
* @var {integer}
*/
let _block_size = 0;
/*
* @private
* @var {integer}
*/
let _block_count = 0;
/*
* @private
* @var {integer}
*/
const _ENCODE = 'utf8';
/*
* @private
*/
const _checkHead = function(){
_block_size = _db.readUInt16BE(6);
_block_count = _db.readUInt16BE(8);
};
/*
* @private
*/
const _writeHead = function(){
_db.writeUInt16BE(
_block_size,
6
);
_db.writeUInt16BE(
_block_count,
8
);
};
/*
* @param {integer} block_size
* @param {integer} block_coount
* @private
* @return {boolean}
*/
const _createDB = function(block_size, block_count){
if ( _started ) // check db is defined
return false;
_block_size = parseInt(block_size+4);
_block_count = parseInt(block_count);
// buffer allocatioon
_db = Buffer.alloc(
_sizeCalc()
);
_writeHead( block_size, block_count);
_started = true;
return true;
};
/*
* @param {integer} block_size
* @param {integer} block_coount
* @private
* @return {boolean}
*/
const _sizeCalc = function(){
return 10+( ( _block_size + 4 ) * _block_count);
};
/*
* @param {integer} block
* @private
* @return {integer}
*/
const _headPosition = function(block){
return 10+( ( _block_size + 4 ) * block);
};
/*
* @param {integer} block
* @private
* @return {integer}
*/
const _blockSizePosition = function(block){
return 10+( ( _block_size + 4 ) * block)+2;
};
/*
* @param {integer} block
* @private
* @return {integer}
*/
const _blockPosition = function(block){
return 10+( ( _block_size + 4 ) * block)+4;
};
/*
* @param {integer} block
* @private
* @return {boolean}
*/
const _readBlockSize = function(block){
let size = _db.readUInt16BE(
_blockSizePosition(block)
);
return size;
};
/*
* @param {integer} block
* @param {integer} data_size
* @private
*/
const _writeBlockSize = function(block, data_size){
_db.writeUInt16BE(
data_size,
_blockSizePosition(block)
);
};
/*
* @param {string} data
* @param {integer} block
* @private
* @return {boolean}
*/
const _setBlock = function( data, block ){
let size = Buffer.byteLength(data);
let position = _blockPosition( block );
_db.write(
data,
position,
);
_writeBlockSize(
block,
size
);
return true;
};
/*
* @param {integer} block
* @private
* @return {string}
*/
const _getString = function(block){
let position =_blockPosition( block );
let end = _readBlockSize( block ) + position;
return _db.toString(
_ENCODE,
position,
end
);
};
/*
* @param {integer} block
* @private
* @return {buffer}
*/
const _getBuffer = function(block){
let position =_blockPosition( block );
let size = _readBlockSize( block );
let end = size + position;
let out = Buffer.alloc(size);
_db.copy(out, 0, position, end);
return out;
};
};
exports.Base = CacheBase;
/*
* buffer header :
* -0-5 : "bl0ck
* 6-7 : size
* 8-9 : numbers
*/
/*
* block header
* 0-1 last set date
* 2-3 current size
*
*/