Comparing version 0.0.30 to 0.0.31
@@ -15,8 +15,45 @@ import * as env from "./env"; | ||
export class PrimaryIterator { | ||
export class PrimaryIterator<T extends PrimaryValue> { | ||
db: DBI64<T>; | ||
i: i32; | ||
_value: T | null; | ||
_primary: u64; | ||
constructor( | ||
public i: i32, | ||
public primary: u64, | ||
) {} | ||
db: DBI64<T>, | ||
i: i32, | ||
_primary: u64, | ||
) { | ||
this.db = db; | ||
this.i = i; | ||
this._value = null; | ||
this._primary = _primary; | ||
} | ||
get primary(): u64 { | ||
if (!this.isOk()) { | ||
return 0; | ||
} | ||
if (this._primary != UNKNOWN_PRIMARY_KEY) { | ||
return this._primary; | ||
} | ||
let value = this.value; | ||
this._primary = value!.getPrimaryValue(); | ||
return this._primary; | ||
} | ||
get value(): T | null { | ||
if (this._value) { | ||
return this._value; | ||
} | ||
if (!this.isOk()) { | ||
return null; | ||
} | ||
this._value = this.db.getEx(this.i) | ||
return this._value; | ||
} | ||
isOk(): bool { | ||
@@ -31,3 +68,3 @@ return this.i >= 0; | ||
export class DBI64 { | ||
export class DBI64<T extends PrimaryValue> { | ||
constructor( | ||
@@ -39,10 +76,12 @@ public code: u64, | ||
store(id: u64, data: u8[], payer: u64): PrimaryIterator { | ||
store(id: u64, value: T, payer: u64): PrimaryIterator<T> { | ||
let data = value.pack(); | ||
let data_ptr = data.dataStart; | ||
let i = env.db_store_i64(this.scope, this.table, payer, id, data_ptr, data.length ); | ||
return new PrimaryIterator(i, id); | ||
return new PrimaryIterator<T>(this, i, id); | ||
} | ||
// export declare function db_update_i64(iterator: i32, payer: u64, data: usize, len: usize): void | ||
update(iterator: PrimaryIterator, payer: u64, data: u8[]): void { | ||
update(iterator: PrimaryIterator<T>, payer: u64, value: T): void { | ||
let data = value.pack(); | ||
let data_ptr = data.dataStart; | ||
@@ -53,59 +92,65 @@ env.db_update_i64(iterator.i, payer, data_ptr, data.length); | ||
// export declare function db_remove_i64(iterator: i32): void | ||
remove(iterator: i32): void { | ||
env.db_remove_i64(iterator); | ||
remove(iterator: PrimaryIterator<T>): void { | ||
env.db_remove_i64(iterator.i); | ||
} | ||
// export declare function db_get_i64(iterator: i32, data: usize, len: usize): i32 | ||
get(iterator: PrimaryIterator): u8[] { | ||
let size = env.db_get_i64(iterator.i, 0, 0); | ||
get(iterator: PrimaryIterator<T>): T | null { | ||
return iterator.value; | ||
} | ||
getEx(iterator: i32): T | null { | ||
let size = env.db_get_i64(iterator, 0, 0); | ||
if (size == 0) { | ||
return []; | ||
return null; | ||
} | ||
let arr = new Array<u8>(size); | ||
let ptr = arr.dataStart; | ||
env.db_get_i64(iterator.i, ptr, size); | ||
return arr; | ||
env.db_get_i64(iterator, ptr, size); | ||
let ret = instantiate<T>(); | ||
ret.unpack(arr); | ||
return ret; | ||
} | ||
// export declare function db_next_i64(iterator: i32, primary_ptr: usize): i32 | ||
next(iterator: i32): PrimaryIterator { | ||
next(iterator: PrimaryIterator<T>): PrimaryIterator<T> { | ||
let primary_ptr = __alloc(sizeof<u64>()); | ||
let itNext = env.db_next_i64(iterator, primary_ptr); | ||
return new PrimaryIterator(itNext, load<u64>(primary_ptr)); | ||
let itNext = env.db_next_i64(iterator.i, primary_ptr); | ||
return new PrimaryIterator(this, itNext, load<u64>(primary_ptr)); | ||
} | ||
// export declare function db_previous_i64(iterator: i32, primary_ptr: usize): i32 | ||
previous(iterator: i32): PrimaryIterator { | ||
previous(iterator: PrimaryIterator<T>): PrimaryIterator<T> { | ||
let primary_ptr = __alloc(sizeof<u64>()); | ||
let itNext = env.db_previous_i64(iterator, primary_ptr); | ||
return new PrimaryIterator(itNext, load<u64>(primary_ptr)); | ||
let itNext = env.db_previous_i64(iterator.i, primary_ptr); | ||
return new PrimaryIterator(this, itNext, load<u64>(primary_ptr)); | ||
} | ||
// export declare function db_find_i64(code: u64, scope: u64, table: u64, id: u64): i32 | ||
find(id: u64): PrimaryIterator { | ||
find(id: u64): PrimaryIterator<T> { | ||
let i = env.db_find_i64(this.code, this.scope, this.table, id); | ||
if (i >= 0) { | ||
return new PrimaryIterator(i, id); | ||
return new PrimaryIterator(this, i, id); | ||
} | ||
return new PrimaryIterator(i, id); | ||
return new PrimaryIterator(this, i, id); | ||
} | ||
// export declare function db_lowerbound_i64(code: u64, scope: u64, table: u64, id: u64): i32 | ||
lowerBound(id: u64): PrimaryIterator { | ||
lowerBound(id: u64): PrimaryIterator<T> { | ||
let i = env.db_lowerbound_i64(this.code, this.scope, this.table, id); | ||
return new PrimaryIterator(i, UNKNOWN_PRIMARY_KEY); | ||
return new PrimaryIterator(this, i, UNKNOWN_PRIMARY_KEY); | ||
} | ||
// export declare function db_upperbound_i64(code: u64, scope: u64, table: u64, id: u64): i32 | ||
upperBound(id: u64): PrimaryIterator { | ||
upperBound(id: u64): PrimaryIterator<T> { | ||
let i = env.db_upperbound_i64(this.code, this.scope, this.table, id); | ||
return new PrimaryIterator(i, UNKNOWN_PRIMARY_KEY); | ||
return new PrimaryIterator(this, i, UNKNOWN_PRIMARY_KEY); | ||
} | ||
// export declare function db_end_i64(code: u64, scope: u64, table: u64): i32 | ||
end(): PrimaryIterator { | ||
end(): PrimaryIterator<T> { | ||
let i = env.db_end_i64(this.code, this.scope, this.table); | ||
return new PrimaryIterator(i, UNKNOWN_PRIMARY_KEY); | ||
return new PrimaryIterator(this, i, UNKNOWN_PRIMARY_KEY); | ||
} | ||
} | ||
@@ -17,3 +17,3 @@ import { IDXDB, SecondaryValue, SecondaryIterator } from "./idxdb"; | ||
export class MultiIndex<T extends MultiIndexValue> { | ||
db: DBI64; | ||
db: DBI64<T>; | ||
idxdbs: Array<IDXDB>; | ||
@@ -27,3 +27,3 @@ nextPrimaryKey: u64 = unsetNextPrimaryKey; | ||
set(value: T, payer: Name): PrimaryIterator { | ||
set(value: T, payer: Name): PrimaryIterator<T> { | ||
let it = this.find(value.getPrimaryValue()); | ||
@@ -38,4 +38,4 @@ if (it.isOk()) { | ||
store(value: T, payer: Name): PrimaryIterator { | ||
const it = this.db.store(value.getPrimaryValue(), value.pack(), payer.N); | ||
store(value: T, payer: Name): PrimaryIterator<T> { | ||
const it = this.db.store(value.getPrimaryValue(), value, payer.N); | ||
for (let i=0; i<this.idxdbs.length; i++) { | ||
@@ -53,13 +53,12 @@ this.idxdbs[i].storeEx(value.getPrimaryValue(), value.getSecondaryValue(i), payer.N); | ||
update(it: PrimaryIterator, value: T, payer: Name): void { | ||
update(it: PrimaryIterator<T>, value: T, payer: Name): void { | ||
check(it.isOk(), "update:bad iterator"); | ||
let primary = value.getPrimaryValue(); | ||
if (it.primary == UNKNOWN_PRIMARY_KEY) { | ||
let it2 = this.db.find(primary); | ||
check(it2.i == it.i, "primary key can't be changed during update!"); | ||
it.primary = primary; | ||
} else { | ||
check(primary == it.primary, "primary key can't be changed during update!"); | ||
check(primary == it.primary, "primary key can't be changed during update!"); | ||
//update value in iterator | ||
if (changetype<usize>(it.value) != changetype<usize>(value)) { | ||
it.value!.unpack(value.pack()); | ||
} | ||
this.db.update(it, payer.N, value.pack()); | ||
this.db.update(it, payer.N, value); | ||
for (let i=0; i<this.idxdbs.length; i++) { | ||
@@ -79,3 +78,3 @@ let ret = this.idxdbs[i].findPrimaryEx(primary); | ||
remove(iterator: PrimaryIterator): void { | ||
remove(iterator: PrimaryIterator<T>): void { | ||
let value = this.get(iterator); | ||
@@ -89,3 +88,3 @@ let primary = value.getPrimaryValue(); | ||
check(it.isOk(), "primary value not found!"); | ||
this.db.remove(it.i); | ||
this.db.remove(it); | ||
for (let i=0; i<this.idxdbs.length; i++) { | ||
@@ -99,6 +98,9 @@ let ret = this.idxdbs[i].findPrimaryEx(primary); | ||
get(iterator: PrimaryIterator): T { | ||
let data = this.db.get(iterator); | ||
let ret = instantiate<T>(); | ||
ret.unpack(data); | ||
get(iterator: PrimaryIterator<T>): T { | ||
let ret = this.db.get(iterator); | ||
if (ret) { | ||
return ret; | ||
} | ||
ret = instantiate<T>(); | ||
return ret; | ||
@@ -114,20 +116,18 @@ } | ||
let data = this.db.get(iterator); | ||
let ret = instantiate<T>(); | ||
ret.unpack(data); | ||
return ret; | ||
return data; | ||
} | ||
next(iterator: PrimaryIterator): PrimaryIterator { | ||
return this.db.next(iterator.i); | ||
next(iterator: PrimaryIterator<T>): PrimaryIterator<T> { | ||
return this.db.next(iterator); | ||
} | ||
previous(iterator: PrimaryIterator): PrimaryIterator { | ||
return this.db.previous(iterator.i); | ||
previous(iterator: PrimaryIterator<T>): PrimaryIterator<T> { | ||
return this.db.previous(iterator); | ||
} | ||
find(id: u64): PrimaryIterator { | ||
find(id: u64): PrimaryIterator<T> { | ||
return this.db.find(id); | ||
} | ||
requireFind(id: u64, findError: string = `Could not find item with id ${id}`): PrimaryIterator { | ||
requireFind(id: u64, findError: string = `Could not find item with id ${id}`): PrimaryIterator<T> { | ||
let itr = this.find(id); | ||
@@ -138,3 +138,3 @@ check(itr.isOk(), findError); | ||
requireNotFind(id: u64, notFindError: string = `Item with id ${id} exists`): PrimaryIterator { | ||
requireNotFind(id: u64, notFindError: string = `Item with id ${id} exists`): PrimaryIterator<T> { | ||
let itr = this.find(id); | ||
@@ -145,15 +145,15 @@ check(!itr.isOk(), notFindError); | ||
lowerBound(id: u64): PrimaryIterator { | ||
lowerBound(id: u64): PrimaryIterator<T> { | ||
return this.db.lowerBound(id); | ||
} | ||
upperBound(id: u64): PrimaryIterator { | ||
upperBound(id: u64): PrimaryIterator<T> { | ||
return this.db.upperBound(id); | ||
} | ||
begin(): PrimaryIterator { | ||
begin(): PrimaryIterator<T> { | ||
return this.lowerBound(u64.MIN_VALUE) | ||
} | ||
end(): PrimaryIterator { | ||
end(): PrimaryIterator<T> { | ||
return this.db.end(); | ||
@@ -173,3 +173,3 @@ } | ||
value.setSecondaryValue(it.dbIndex, idxValue); | ||
this.db.update(primaryIt, payer.N, value.pack()); | ||
this.db.update(primaryIt, payer.N, value); | ||
this.idxdbs[it.dbIndex].updateEx(it, idxValue, payer.N); | ||
@@ -176,0 +176,0 @@ } |
@@ -24,3 +24,3 @@ import { MultiIndex, MultiIndexValue } from "./mi"; | ||
if (it.isOk()) { | ||
return this.mi.get(it); | ||
return it.value; | ||
} | ||
@@ -34,3 +34,3 @@ return null; | ||
if (it.isOk()) { | ||
return this.mi.get(it); | ||
return it.value!; | ||
} | ||
@@ -37,0 +37,0 @@ return instantiate<T>(); |
{ | ||
"name": "as-chain", | ||
"version": "0.0.30", | ||
"version": "0.0.31", | ||
"description": "chain module for assemblyscript", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
@@ -9,3 +9,3 @@ declare function __alloc(size: usize, id?: u32): usize; | ||
declare function packed(_?: string): any | ||
declare function table(_?: any, __?: string): any | ||
declare function table(_?: any, __?: string, ___?: string, ____?: string): any | ||
declare function variant(_?: any, __?: any): any | ||
@@ -12,0 +12,0 @@ declare function serializer(_?: any): any |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1324680
3404