Skip to content

Commit 14b107a

Browse files
committed
fix remove insert timers
1 parent 1ca00d3 commit 14b107a

5 files changed

Lines changed: 29 additions & 14 deletions

File tree

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
# DocDB
33

44
LOG-based JavaScript DataBase (based on streaming processing).
5-
The database is currently under development, please do not use it in production. The API will be significantly changed.
6-
7-
## Note
8-
The main problem at the moment is that the recording stream should always be open (to ensure insertion performance), but in fact I can not get the document insertion event in the data file due to the complexity of the streaming architecture.
9-
Those. the insert operation occurs without real feedback from the recording stream.
5+
The API will be significantly changed.
106

117
## Install
128

core.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ var PATH = require('path'),
1111
FS = require('fs'),
1212
OBJECTSTREAM = require('@sergdudko/objectstream'),
1313
STREAM = require('stream'),
14-
ZLIB = require('zlib');
14+
ZLIB = require('zlib'),
15+
EE = require('events');;
1516

1617
let DataStore = function(config){
1718
let self = this;
@@ -20,6 +21,7 @@ let DataStore = function(config){
2021
self.config = new Object();
2122
self.config.dbname = 'docdb'; //директория с файлами данных СУБД
2223
self.config.dir = __dirname; //родительская директория для self.config.dbname
24+
self.ee = new EE(); //эмиттер внутренних событий
2325

2426
//настройки пользователя
2527
if(typeof(config) === 'object'){
@@ -57,7 +59,7 @@ let DataStore = function(config){
5759
self.methods = new Object({
5860
performance_now: function(){ //эмуляция performance.now()
5961
const nexttime = process.hrtime.bigint();
60-
const my = [parseInt(((nexttime - self.starttime) / 1000000000n).toLocaleString()), parseInt(((nexttime - self.starttime) % 1000000000n).toLocaleString())];
62+
const my = [parseInt(((nexttime - self.starttime) / 1000000n).toString()), parseInt(((nexttime - self.starttime) % 1000000n).toString())];
6163
return parseFloat(my[0]+"."+my[1]);
6264
},
6365
uuidv4: function() { //генерация uuid версии 4
@@ -190,6 +192,8 @@ let DataStore = function(config){
190192
try{
191193
if(typeof(object) === 'object'){
192194
let _id = object._id;
195+
let _id_ = object._id_;
196+
delete object._id_;
193197
let string = JSON.stringify(object); //трансформация объект->строка
194198
ZLIB.gzip(string, function(err, buffer){ //трансформация, сжатие
195199
if(err){
@@ -199,6 +203,9 @@ let DataStore = function(config){
199203
selfstream.push(buffer); //передаю сжатую строку дальше
200204
self.streams.transform['_id.key'].write({_id: _id, start: self.firstByte, end: self.firstByte + _byteLength}); //записываю в первичный ключ байты начала и конца объекта
201205
self.firstByte = self.firstByte + _byteLength; //меняю первый байт для следующей записи
206+
if(typeof(_id_) === 'string'){
207+
self.ee.emit(_id_);
208+
}
202209
return callback();
203210
});
204211
} else {
@@ -654,16 +661,21 @@ let DataStore = function(config){
654661
}
655662

656663
//запись объекта в СУБД
657-
self.methods.insertOne = function(object){
664+
self.methods.insertOne = function(_object){
665+
let object = Object.assign(_object);
658666
return new Promise(function(rs, rj){
667+
if(typeof(object._id_) !== 'undefined'){
668+
rj(new Error('Object key _id_ reserved for official purposes!'));
669+
}
670+
object._id_ = self.methods.uuidv4();
659671
if(typeof(object._id) !== 'string'){
660672
object._id = self.methods.uuidv4();
661673
}
662674
if(typeof(object) === 'object'){
675+
self.ee.once(object._id_, function(){rs(object._id);})
663676
self.streams.transform['data.store'].write(object);
664-
setTimeout(rs, 10, object._id);
665677
} else {
666-
setTimeout(rj, 0, new Error('Function insertOne require object!'));
678+
rj(new Error('Function insertOne require object!'));
667679
}
668680
});
669681
}
@@ -775,8 +787,9 @@ let DataStore = function(config){
775787
} else {
776788
self.methods.findOne({_id:index._id}, undefined, {start:index.start, end:index.end}, true).then(function(doc){ //ищу документ в бэкапе файла данных СУБД по соответствующим байтовым координатам
777789
if((typeof(doc[0]) === 'object') && (typeof(doc[0]._id) === 'string')){
790+
doc[0]._id_ = self.methods.uuidv4();
791+
self.ee.once(doc[0]._id_, function(){recursiveWrite(++i);})
778792
self.streams.transform['data.store'].write(doc[0]); //записываю документ в файл данных СУБД (первичный ключ и лог-файл)
779-
recursiveWrite(++i);
780793
} else { //документ не найден по байтовым координатам, ищу во всем файле (медленно)
781794
rj2(new Error('Doc not found.'));
782795
}

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "docdb",
3-
"version": "0.0.4",
3+
"version": "0.0.5",
44
"description": "crossplatform NoSQL JS database",
55
"main": "index.js",
66
"scripts": {
@@ -23,6 +23,7 @@
2323
"homepage": "https://github.com/siarheidudko/docdb#readme",
2424
"dependencies": {
2525
"@sergdudko/objectstream": ">=1.5.0",
26+
"events": "^3.0.0",
2627
"fs": "0.0.1-security",
2728
"path": "^0.12.7",
2829
"stream": "0.0.2",

test.core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ DataBase.then(function(db){
7373
if(i < iter){
7474
if(parseInt(i%3) === 0)
7575
testdoc._id = db.methods.uuidv4();
76-
db.methods.insertOne(testdoc).then(function(_id){
76+
db.methods.insertOne(JSON.parse(JSON.stringify(testdoc))).then(function(_id){
7777
//
7878
}).catch(console.error).finally(function(){
7979
recursiveWrite(++i);

0 commit comments

Comments
 (0)