-
-
Notifications
You must be signed in to change notification settings - Fork 405
feat: support single db batch operation #8268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: unstable
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import {Type} from "@chainsafe/ssz"; | |
import {ChainForkConfig} from "@lodestar/config"; | ||
import {BUCKET_LENGTH} from "./const.js"; | ||
import {KeyValue} from "./controller/index.js"; | ||
import {Db, DbReqOpts, FilterOptions} from "./controller/interface.js"; | ||
import {Db, DbBatch, DbReqOpts, FilterOptions} from "./controller/interface.js"; | ||
import {encodeKey} from "./util.js"; | ||
|
||
type Id = Uint8Array | string | number | bigint; | ||
|
@@ -141,6 +141,30 @@ export abstract class PrefixedRepository<P, I extends Id, T> { | |
await this.db.batchDelete(keys.flat(), this.dbReqOpts); | ||
} | ||
|
||
async batch(prefix: P, batch: DbBatch<I, T>): Promise<void> { | ||
const batchWithKeys = []; | ||
for (const b of batch) { | ||
if (b.type === "del") { | ||
batchWithKeys.push({type: b.type, key: this.wrapKey(this.encodeKeyRaw(prefix, b.key))}); | ||
} else { | ||
batchWithKeys.push({ | ||
type: b.type, | ||
key: this.wrapKey(this.encodeKeyRaw(prefix, b.key)), | ||
value: this.encodeValue(b.value), | ||
}); | ||
} | ||
} | ||
await this.db.batch(batchWithKeys, this.dbReqOpts); | ||
} | ||
|
||
async batchBinary(prefix: P, batch: DbBatch<I, Uint8Array>): Promise<void> { | ||
const batchWithKeys = []; | ||
for (const b of batch) { | ||
batchWithKeys.push({...b, key: this.wrapKey(this.encodeKeyRaw(prefix, b.key))}); | ||
} | ||
await this.db.batch(batchWithKeys, this.dbReqOpts); | ||
} | ||
Comment on lines
+160
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This async batchBinary(prefix: P, batch: DbBatch<I, Uint8Array>): Promise<void> {
const batchWithKeys: DbBatch<Uint8Array, Uint8Array> = batch.map((b) => ({
...b,
key: this.wrapKey(this.encodeKeyRaw(prefix, b.key)),
}));
await this.db.batch(batchWithKeys, this.dbReqOpts);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer using |
||
|
||
async *valuesStream(prefix: P | P[]): AsyncIterable<T> { | ||
for (const p of Array.isArray(prefix) ? prefix : [prefix]) { | ||
for await (const vb of this.db.valuesStream({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import {Type} from "@chainsafe/ssz"; | |
import {ChainForkConfig} from "@lodestar/config"; | ||
import {BUCKET_LENGTH} from "./const.js"; | ||
import {FilterOptions, KeyValue} from "./controller/index.js"; | ||
import {Db, DbReqOpts} from "./controller/interface.js"; | ||
import {Db, DbBatch, DbReqOpts} from "./controller/interface.js"; | ||
import {encodeKey as _encodeKey} from "./util.js"; | ||
|
||
export type Id = Uint8Array | string | number | bigint; | ||
|
@@ -130,6 +130,26 @@ export abstract class Repository<I extends Id, T> { | |
); | ||
} | ||
|
||
async batch(batch: DbBatch<I, T>): Promise<void> { | ||
const batchWithKeys: DbBatch<Uint8Array, Uint8Array> = []; | ||
for (const b of batch) { | ||
if (b.type === "del") { | ||
batchWithKeys.push({...b, key: this.encodeKey(b.key)}); | ||
} else { | ||
batchWithKeys.push({...b, key: this.encodeKey(b.key), value: this.encodeValue(b.value)}); | ||
} | ||
} | ||
await this.db.batch(batchWithKeys, this.dbReqOpts); | ||
} | ||
Comment on lines
+133
to
+143
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The async batch(batch: DbBatch<I, T>): Promise<void> {
const batchWithKeys: DbBatch<Uint8Array, Uint8Array> = batch.map((b) => {
if (b.type === "del") {
return {type: "del", key: this.encodeKey(b.key)};
} else {
return {type: "put", key: this.encodeKey(b.key), value: this.encodeValue(b.value)};
}
});
await this.db.batch(batchWithKeys, this.dbReqOpts);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer using |
||
|
||
async batchBinary(batch: DbBatch<I, Uint8Array>): Promise<void> { | ||
const batchWithKeys: DbBatch<Uint8Array, Uint8Array> = []; | ||
for (const b of batch) { | ||
batchWithKeys.push({...b, key: this.encodeKey(b.key)}); | ||
} | ||
await this.db.batch(batchWithKeys, this.dbReqOpts); | ||
} | ||
Comment on lines
+145
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This async batchBinary(batch: DbBatch<I, Uint8Array>): Promise<void> {
const batchWithKeys: DbBatch<Uint8Array, Uint8Array> = batch.map((b) => ({
...b,
key: this.encodeKey(b.key),
}));
await this.db.batch(batchWithKeys, this.dbReqOpts);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We prefer using |
||
|
||
async batchAdd(values: T[]): Promise<void> { | ||
// handle single value in batchPut | ||
await this.batchPut( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
export type {Db, DbReqOpts, DatabaseController, FilterOptions, KeyValue} from "./interface.js"; | ||
export type { | ||
Db, | ||
DbReqOpts, | ||
DatabaseController, | ||
FilterOptions, | ||
KeyValue, | ||
DbBatch, | ||
DbBatchOperation, | ||
} from "./interface.js"; | ||
export {LevelDbController} from "./level.js"; | ||
export type {LevelDbControllerMetrics} from "./metrics.js"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
for
loop withif/else
can be simplified by using themap
function on thebatch
array. This would make the code more concise and functional. Also, it's good practice to typebatchWithKeys
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We prefer using
for off
instead ofmap
iterators because of performance.