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 persistence plugin #88

Merged
merged 3 commits into from
Jul 1, 2024

Conversation

jaipaljadeja
Copy link
Member

add persistence plugin to persist the indexer’s state between restarts.

@jaipaljadeja jaipaljadeja requested a review from fracek June 29, 2024 08:42
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.

Looks good. It needs tests to make sure everything is working fine but I couldn't catch anything wrong with it.

@jaipaljadeja jaipaljadeja requested a review from fracek June 30, 2024 18:00

describe("Sqlite Persistence", () => {
let db: Database<sqlite3.Database, sqlite3.Statement>;
let store: SqlitePersistence;
Copy link
Contributor

Choose a reason for hiding this comment

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

Should be called persistence :)

});

indexer.hooks.hook("connect:before", async ({ request }) => {
const store = new SqlitePersistence(db);
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 it's possible to instantiate the persistence once and share the instance between the hooks (like you do with db).


indexer.hooks.hook("sink:flush", async ({ endCursor }) => {
const store = new SqlitePersistence(db);
if (endCursor) store.put(endCursor);
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
if (endCursor) store.put(endCursor);
if (endCursor) {
store.put(endCursor);
}

import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { SqlitePersistence } from "./persistence";

describe("Sqlite Persistence", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

One issue I have with these tests is that they're very dependent on the order in which they're run.

I would change them to have only 2 cases:

Test 1: assert there's no data, then insert value, check that value was stored, update value, check that value was updated

Test 2: assert there's no data, insert value, check value was stored, delete value, check there's no data.

For each test, you should create a db/persistence instance specific to that test. That way if in the future we run tests in parallel or if vitest changes how they order tests, test won't break.


type SqliteArgs = ISqlite.Config;

export function sqlitePersistence<TFilter, TBlock, TRet>(args: SqliteArgs) {
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW now that we have a mock stream, we can test plugins end to end.

Add a tests where you setup a mock indexer with the sqlitePersistence plugin, run it over a sequence of messages and then assert that the stored cursor is the last endCursor in your stream.

id TEXT NOT NULL PRIMARY KEY,
order_key INTEGER NOT NULL,
unique_key TEXT,
UNIQUE(order_key, unique_key)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think UNIQUE is needed. We can safely remove it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not needed because we have id in this table.

@jaipaljadeja jaipaljadeja force-pushed the feat/persistence-plugin branch from 27d3bde to e56cb30 Compare July 1, 2024 17:12
@jaipaljadeja jaipaljadeja force-pushed the feat/persistence-plugin branch from e56cb30 to 9c93e23 Compare July 1, 2024 18:20
@jaipaljadeja jaipaljadeja requested a review from fracek July 1, 2024 18:38
@fracek fracek merged commit b07fa77 into apibara:develop Jul 1, 2024
1 check passed
@jaipaljadeja jaipaljadeja deleted the feat/persistence-plugin 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