Skip to content
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

indexer: add kv plugin #84

Merged
merged 7 commits into from
Jun 26, 2024
Merged

Conversation

jaipaljadeja
Copy link
Member

@jaipaljadeja jaipaljadeja commented Jun 21, 2024

add kv plugin, useKVStore hook and KVStore test. also update biome config to check for unused imports

@jaipaljadeja jaipaljadeja requested a review from fracek June 21, 2024 13:22
});

indexer.hooks.hook("handler:exception", async () => {
await db.exec("COMMIT TRANSACTION");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My Bad, This will be ROLLBACK TRANSACTION. i will update it tomorrow morning.

Copy link
Contributor

@fracek fracek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a promising start! I think we need to add tests to the KVStore by using the in memory sqlite database.

"handler:after": ({ output }: { output: TRet[] }) => void;
"handler:exception": ({ error }: { error: Error }) => void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this handler called anywhere? I think we should catch the handler exception and then rethrow it after calling the hook.

private _endCursor: Cursor,
) {}

async get<T extends Record<string, unknown>>(key: string): Promise<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here T can be anything that is json encodeable (e.g. I often store just a number or a string).

return row ? deserialize(row.v) : undefined;
}

async put<T extends Record<string, unknown>>(key: string, value: T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Comment on lines 80 to 95
await this._db.run(
`
UPDATE kvs
SET to_block = ?
WHERE k = ? AND to_block IS NULL
`,
[Number(this._endCursor.orderKey), key],
);

await this._db.run(
`
INSERT INTO kvs (from_block, to_block, k, v)
VALUES (?, NULL, ?, ?)
`,
[Number(this._endCursor.orderKey), key, serialize(value)],
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this will behave if the user inserts the same key twice in the same block. We need to add tests to know for sure.

@jaipaljadeja jaipaljadeja requested a review from fracek June 25, 2024 15:15
Copy link
Contributor

@fracek fracek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of small fixes and it's ready.

packages/indexer/src/indexer.ts Outdated Show resolved Hide resolved
packages/indexer/src/plugins/kv.ts Outdated Show resolved Hide resolved
@jaipaljadeja jaipaljadeja requested a review from fracek June 25, 2024 17:05
Comment on lines 15 to 23
await db.exec(`
CREATE TABLE IF NOT EXISTS kvs (
from_block INTEGER NOT NULL,
to_block INTEGER,
k TEXT NOT NULL,
v BLOB NOT NULL,
PRIMARY KEY (from_block, k)
);
`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put this logic in a static initialize(db) method on the KVStore? That way we can share the logic between tests and hooks.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I should have caught it in the previous review round!

Comment on lines 32 to 34
ctx.kv = new KVStore(db, finality, endCursor);

await db.exec("BEGIN TRANSACTION");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My advice is to put this into a function on the kv so that it can be tested. You should test both the successfull flow (begin tx -> commit) and the exception flow (begin tx -> rollback)

Suggested change
ctx.kv = new KVStore(db, finality, endCursor);
await db.exec("BEGIN TRANSACTION");
ctx.kv = new KVStore(db, finality, endCursor);
await ctx.kv.beginTransaction();

Comment on lines 38 to 41
await db.exec("COMMIT TRANSACTION");

const ctx = useIndexerContext();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await db.exec("COMMIT TRANSACTION");
const ctx = useIndexerContext();
const ctx = useIndexerContext();
ctx.kv.commitTransaction()

Comment on lines 46 to 48
await db.exec("ROLLBACK TRANSACTION");

const ctx = useIndexerContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await db.exec("ROLLBACK TRANSACTION");
const ctx = useIndexerContext();
const ctx = useIndexerContext();
ctx.kv.rollbackTransaction();

@jaipaljadeja jaipaljadeja requested a review from fracek June 26, 2024 11:16
@fracek fracek merged commit 3a0f381 into apibara:develop Jun 26, 2024
1 check passed
@jaipaljadeja jaipaljadeja deleted the feat/kv-storage branch December 9, 2024 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants