this.write_list = []
this.write_list_len = 0
this.write_count = 0
+ //this.serial = 0
}
async open(path) {
// hold mutex whilst calling this function
let [ptr, len] = ptr_len
+ let value
if (Object.prototype.hasOwnProperty.call(this.read_cache, ptr)) {
//console.log('hit', ptr)
let cached_value = this.read_cache[ptr]
cached_value.stale_count = this.read_timeout
- return cached_value.value
+ value = cached_value.value
}
-
- let buffer = Buffer.alloc(len)
- assert(
- (await this.log.read(buffer, 0, len, ptr)).bytesRead === len
- )
- let value = JSON.parse(buffer.toString('utf-8'))
-
- if (this.read_timeout) //{
+ else {
+ let buffer = Buffer.alloc(len)
+ assert(
+ (await this.log.read(buffer, 0, len, ptr)).bytesRead === len
+ )
+ value = JSON.parse(buffer.toString('utf-8'))
+
+ if (!this.read_timeout)
+ return value
//console.log('add', ptr)
this.read_cache[ptr] = new CachedValue(0, value, this.read_timeout)
- //}
+ }
- return value
+ // value is from cache, copy it
+ assert(typeof value === 'object' && value !== null)
+ let new_value
+ if (value instanceof Array) {
+ new_value = []
+ for (let i = 0; i < value.length; ++i)
+ new_value.push(value[i])
+ }
+ else {
+ new_value = {}
+ for (let i in value)
+ new_value[i] = value[i]
+ }
+ return new_value
}
async write(value) {
async Transaction() {
await this.mutex.acquire()
+ //let transaction = new Transaction(this, this.value)
+ //transaction.serial = this.serial
+ //this.serial += 1
+ //console.log('create transaction', transaction.serial)
+ //return transaction
return new Transaction(this, this.value)
}