Comparing version 3.0.8 to 3.0.9
{ | ||
"name": "jembadb", | ||
"version": "3.0.8", | ||
"version": "3.0.9", | ||
"description": "Json Embeddable Appendable Database", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -319,10 +319,13 @@ 'use strict'; | ||
async close() { | ||
if (this.closed) | ||
if (this.closing || this.closed) | ||
return; | ||
this.opened = false; | ||
this.closed = true; | ||
this.closing = true; | ||
this.lock.errAll('Table closing'); | ||
await this.lock.get(); | ||
try { | ||
this.opened = false; | ||
this.closed = true; | ||
if (!this.inMemory) { | ||
@@ -329,0 +332,0 @@ while (this.savingChanges) { |
@@ -797,13 +797,9 @@ 'use strict'; | ||
where: ` | ||
let toDel = new Set(); | ||
for (const id of @all()) { | ||
const row = @row(id); | ||
@@iter(@all(), (row) => { | ||
if (row.timeBegin >= ${this.esc(delDate.getTime())}) { | ||
break; | ||
return; | ||
} else { | ||
if (row.timeEnd > 0 && row.timeEnd < ${this.esc(delDate.getTime())}) | ||
toDel.add(id); | ||
return (row.timeEnd > 0 && row.timeEnd < ${this.esc(delDate.getTime())}); | ||
} | ||
} | ||
return toDel; | ||
}); | ||
` | ||
@@ -810,0 +806,0 @@ }); |
@@ -12,3 +12,3 @@ 'use strict'; | ||
get(take = true) { | ||
return new Promise((resolve) => { | ||
return new Promise((resolve, reject) => { | ||
if (this.freed) { | ||
@@ -21,10 +21,7 @@ if (take) | ||
if (this.waitingQueue.length >= this.queueSize) | ||
throw new Error('Lock queue is too long'); | ||
this.waitingQueue.push({ | ||
onFreed: () => { | ||
resolve(); | ||
}, | ||
}); | ||
if (this.waitingQueue.length < this.queueSize) { | ||
this.waitingQueue.push({resolve, reject}); | ||
} else { | ||
reject(new Error('Lock queue is too long')); | ||
} | ||
}); | ||
@@ -35,3 +32,3 @@ } | ||
if (this.waitingQueue.length) { | ||
this.waitingQueue.shift().onFreed(); | ||
this.waitingQueue.shift().resolve(); | ||
} else { | ||
@@ -47,11 +44,16 @@ this.freed = true; | ||
free() { | ||
retAll() { | ||
while (this.waitingQueue.length) { | ||
this.waitingQueue.shift().onFreed(); | ||
this.waitingQueue.shift().resolve(); | ||
} | ||
this.freed = true; | ||
} | ||
errAll(error = 'rejected') { | ||
while (this.waitingQueue.length) { | ||
this.waitingQueue.shift().reject(new Error(error)); | ||
} | ||
} | ||
} | ||
module.exports = LockQueue; |
@@ -78,3 +78,3 @@ 'use strict'; | ||
let i = d.length - 1; | ||
//вставка | ||
//insert | ||
while (i > 0 && this.cmp(d[i], d[i - 1]) < 0) { | ||
@@ -88,3 +88,3 @@ const v = d[i]; | ||
if (d.length > 10) { | ||
//слияние | ||
//merging | ||
while (s > 0 && this.sorted[s].length >= this.sorted[s - 1].length) { | ||
@@ -183,3 +183,3 @@ const a = this.sorted.pop(); | ||
const a = this.sorted[s]; | ||
if (!a.length) // на всякий случай | ||
if (!a.length) // just in case | ||
continue; | ||
@@ -189,3 +189,3 @@ | ||
if (useFrom) { | ||
//дихотомия | ||
//dichotomy | ||
let left = 0; | ||
@@ -208,3 +208,3 @@ let right = a.length - 1; | ||
if (useTo) { | ||
//дихотомия | ||
//dichotomy | ||
let left = 0; | ||
@@ -247,2 +247,21 @@ let right = a.length - 1; | ||
reduceHash(value) { | ||
this.checkType(value); | ||
value = this.prepareValue(value); | ||
let result; | ||
if (this.hash.has(value)) { | ||
if (this.unique) { | ||
result = new Set(); | ||
result.add(this.hash.get(value)); | ||
} else { | ||
result = this.hash.get(value); | ||
} | ||
} else { | ||
result = new Set(); | ||
} | ||
return result; | ||
} | ||
min() { | ||
@@ -255,7 +274,11 @@ let result = new Set(); | ||
const a = this.sorted[s]; | ||
if (!a.length) // на всякий случай | ||
if (!a.length) // just in case | ||
continue; | ||
if (a[0] < min || min === null) { | ||
min = a[0]; | ||
id = this.hash.get(min); | ||
for (let i = 0; i < a.length; i++) { | ||
if (this.hash.has(a[i]) && (a[i] < min || min === null)) { | ||
min = a[i]; | ||
id = this.hash.get(min); | ||
break; | ||
} | ||
} | ||
@@ -281,9 +304,11 @@ } | ||
const a = this.sorted[s]; | ||
if (!a.length) // на всякий случай | ||
if (!a.length) // just in case | ||
continue; | ||
const last = a.length - 1; | ||
if (a[last] > max || max === null) { | ||
max = a[last]; | ||
id = this.hash.get(max); | ||
for (let i = a.length - 1; i >= 0; i--) { | ||
if (this.hash.has(a[i]) && (a[i] > max || max === null)) { | ||
max = a[i]; | ||
id = this.hash.get(max); | ||
break; | ||
} | ||
} | ||
@@ -290,0 +315,0 @@ } |
@@ -820,2 +820,20 @@ 'use strict'; | ||
async dirtyHash(fieldName, value) { | ||
if (this._hash.has(fieldName)) { | ||
const hash = this._hash.get(fieldName); | ||
if (!Array.isArray(value)) { | ||
return hash.reduce(value); | ||
} else { | ||
const arrSet = []; | ||
for (const v of value) { | ||
arrSet.push(hash.reduce(v)); | ||
} | ||
return utils.unionSet(arrSet); | ||
} | ||
} else { | ||
throw new Error(`Hash for field '${fieldName}' does not exist`); | ||
} | ||
} | ||
async hash(fieldName, value) { | ||
@@ -877,2 +895,11 @@ if (this._hash.has(fieldName)) { | ||
async dirtyIndexLR(fieldName, from, to) { | ||
if (this._index.has(fieldName)) { | ||
const index = this._index.get(fieldName); | ||
return index.reduce(from, to); | ||
} else { | ||
throw new Error(`Index for field '${fieldName}' does not exist`); | ||
} | ||
} | ||
async _indexReduce(fieldName, from, to, checkFuncs) { | ||
@@ -999,2 +1026,49 @@ if (this._index.has(fieldName)) { | ||
async dirtyIndexHash(fieldName, value) { | ||
if (this._index.has(fieldName)) { | ||
const index = this._index.get(fieldName); | ||
if (!Array.isArray(value)) { | ||
return index.reduceHash(value); | ||
} else { | ||
const arrSet = []; | ||
for (const v of value) { | ||
arrSet.push(index.reduceHash(v)); | ||
} | ||
return utils.unionSet(arrSet); | ||
} | ||
} else { | ||
throw new Error(`Index for field '${fieldName}' does not exist`); | ||
} | ||
} | ||
async indexHash(fieldName, value) { | ||
if (this._index.has(fieldName)) { | ||
const index = this._index.get(fieldName); | ||
const result = new Set(); | ||
if (!Array.isArray(value)) { | ||
const ids = index.reduceHash(value); | ||
for (const id of ids) { | ||
const row = await this._rowsInterface.getRow(id); | ||
if (row[fieldName] === value) | ||
result.add(id); | ||
} | ||
} else { | ||
for (const v of value) { | ||
const ids = index.reduceHash(v); | ||
for (const id of ids) { | ||
const row = await this._rowsInterface.getRow(id); | ||
if (row[fieldName] === v) | ||
result.add(id); | ||
} | ||
} | ||
} | ||
return result; | ||
} else { | ||
throw new Error(`Index for field '${fieldName}' does not exist`); | ||
} | ||
} | ||
//returns iterator, not Set | ||
@@ -1001,0 +1075,0 @@ async all() { |
@@ -124,3 +124,3 @@ 'use strict'; | ||
if (!queue) { | ||
queue = new LockQueue(100); | ||
queue = new LockQueue(1000); | ||
this.fileLockMap.set(fileName, queue); | ||
@@ -168,3 +168,3 @@ } | ||
block.addCount++; | ||
block.size += rowStr.length; | ||
block.size += JSON.stringify(id).length + rowStr.length; | ||
block.rowsLength = block.rows.size; | ||
@@ -171,0 +171,0 @@ |
194853
5091