-
Notifications
You must be signed in to change notification settings - Fork 130
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
[OTE-753] add new persistent cache table #2175
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
indexer/packages/postgres/__tests__/stores/persistent-cache-table.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { PersistentCacheFromDatabase } from '../../src/types'; | ||
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers'; | ||
import { defaultKV, defaultKV2 } from '../helpers/constants'; | ||
import * as PersistentCacheTable from '../../src/stores/persistent-cache-table'; | ||
|
||
describe('Persistent cache store', () => { | ||
beforeAll(async () => { | ||
await migrate(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await clearData(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await teardown(); | ||
}); | ||
|
||
it('Successfully creates a key value pair', async () => { | ||
await PersistentCacheTable.create(defaultKV); | ||
}); | ||
|
||
it('Successfully upserts a kv pair multiple times', async () => { | ||
const newKv = { | ||
...defaultKV, | ||
value: 'someOtherValue', | ||
}; | ||
await PersistentCacheTable.upsert(newKv); | ||
let kv: PersistentCacheFromDatabase | undefined = await PersistentCacheTable.findById( | ||
defaultKV.key, | ||
); | ||
expect(kv).toEqual(expect.objectContaining(newKv)); | ||
|
||
const newKv2 = { | ||
...defaultKV, | ||
value: 'someOtherValue2', | ||
}; | ||
await PersistentCacheTable.upsert(newKv2); | ||
kv = await PersistentCacheTable.findById(defaultKV.key); | ||
|
||
expect(kv).toEqual(expect.objectContaining(newKv2)); | ||
}); | ||
|
||
it('Successfully finds all kv pairs', async () => { | ||
await Promise.all([ | ||
PersistentCacheTable.create(defaultKV), | ||
PersistentCacheTable.create(defaultKV2), | ||
]); | ||
|
||
const kvs: PersistentCacheFromDatabase[] = await PersistentCacheTable.findAll( | ||
{}, | ||
[], | ||
{ readReplica: true }, | ||
); | ||
|
||
expect(kvs.length).toEqual(2); | ||
expect(kvs).toEqual(expect.arrayContaining([ | ||
expect.objectContaining(defaultKV), | ||
expect.objectContaining(defaultKV2), | ||
])); | ||
}); | ||
|
||
it('Successfully finds a kv pair', async () => { | ||
await PersistentCacheTable.create(defaultKV); | ||
|
||
const kv: PersistentCacheFromDatabase | undefined = await PersistentCacheTable.findById( | ||
defaultKV.key, | ||
); | ||
|
||
expect(kv).toEqual(expect.objectContaining(defaultKV)); | ||
}); | ||
}); |
12 changes: 12 additions & 0 deletions
12
...ostgres/src/db/migrations/migration_files/20240829171730_create_persistent_cache_table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import * as Knex from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.createTable('persistent_cache', (table) => { | ||
table.string('key').primary().notNullable(); | ||
table.string('value').notNullable(); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('persistent_cache'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
indexer/packages/postgres/src/models/persistent-cache-model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import UpsertQueryBuilder from '../query-builders/upsert'; | ||
import BaseModel from './base-model'; | ||
|
||
export default class PersistentCacheModel extends BaseModel { | ||
static get tableName() { | ||
return 'persistent_cache'; | ||
} | ||
|
||
static get idColumn() { | ||
return 'key'; | ||
} | ||
|
||
static relationMappings = {}; | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: ['key', 'value'], | ||
properties: { | ||
key: { type: 'string' }, | ||
value: { type: 'string' }, | ||
}, | ||
}; | ||
} | ||
|
||
QueryBuilderType!: UpsertQueryBuilder<this>; | ||
|
||
key!: string; | ||
|
||
value!: string; | ||
} |
94 changes: 94 additions & 0 deletions
94
indexer/packages/postgres/src/stores/persistent-cache-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { QueryBuilder } from 'objection'; | ||
|
||
import { DEFAULT_POSTGRES_OPTIONS } from '../constants'; | ||
import { setupBaseQuery, verifyAllRequiredFields } from '../helpers/stores-helpers'; | ||
import Transaction from '../helpers/transaction'; | ||
import PersistentCacheModel from '../models/persistent-cache-model'; | ||
import { | ||
Options, | ||
Ordering, | ||
QueryableField, | ||
QueryConfig, | ||
PersistentCacheColumns, | ||
PersistentCacheCreateObject, | ||
PersistentCacheFromDatabase, | ||
PersistentCacheQueryConfig, | ||
} from '../types'; | ||
|
||
export async function findAll( | ||
{ | ||
key, | ||
limit, | ||
}: PersistentCacheQueryConfig, | ||
requiredFields: QueryableField[], | ||
options: Options = DEFAULT_POSTGRES_OPTIONS, | ||
): Promise<PersistentCacheFromDatabase[]> { | ||
verifyAllRequiredFields( | ||
{ | ||
key, | ||
limit, | ||
} as QueryConfig, | ||
requiredFields, | ||
); | ||
|
||
let baseQuery: QueryBuilder<PersistentCacheModel> = setupBaseQuery<PersistentCacheModel>( | ||
PersistentCacheModel, | ||
options, | ||
); | ||
|
||
if (key) { | ||
baseQuery = baseQuery.where(PersistentCacheColumns.key, key); | ||
} | ||
|
||
if (options.orderBy !== undefined) { | ||
for (const [column, order] of options.orderBy) { | ||
baseQuery = baseQuery.orderBy( | ||
column, | ||
order, | ||
); | ||
} | ||
} else { | ||
baseQuery = baseQuery.orderBy( | ||
PersistentCacheColumns.key, | ||
Ordering.ASC, | ||
); | ||
} | ||
|
||
if (limit) { | ||
baseQuery = baseQuery.limit(limit); | ||
} | ||
|
||
return baseQuery.returning('*'); | ||
} | ||
|
||
export async function create( | ||
kvToCreate: PersistentCacheCreateObject, | ||
options: Options = { txId: undefined }, | ||
): Promise<PersistentCacheFromDatabase> { | ||
return PersistentCacheModel.query( | ||
Transaction.get(options.txId), | ||
).insert(kvToCreate).returning('*'); | ||
} | ||
|
||
export async function upsert( | ||
kvToUpsert: PersistentCacheCreateObject, | ||
options: Options = { txId: undefined }, | ||
): Promise<PersistentCacheFromDatabase> { | ||
const kvs: PersistentCacheModel[] = await PersistentCacheModel.query( | ||
Transaction.get(options.txId), | ||
).upsert(kvToUpsert).returning('*'); | ||
// should only ever be one key value pair | ||
return kvs[0]; | ||
} | ||
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. nit: whitespace |
||
export async function findById( | ||
kv: string, | ||
options: Options = DEFAULT_POSTGRES_OPTIONS, | ||
): Promise<PersistentCacheFromDatabase | undefined> { | ||
const baseQuery: QueryBuilder<PersistentCacheModel> = setupBaseQuery<PersistentCacheModel>( | ||
PersistentCacheModel, | ||
options, | ||
); | ||
return baseQuery | ||
.findById(kv) | ||
.returning('*'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
indexer/packages/postgres/src/types/persistent-cache-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface PersistentCacheCreateObject { | ||
key: string, | ||
value: string, | ||
} | ||
|
||
export enum PersistentCacheColumns { | ||
key = 'key', | ||
value = 'value', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
LGTM! Consider adding a timestamp column.
The function correctly defines the schema for the
persistent_cache
table. However, it might be beneficial to add a timestamp column to track when entries are created or updated.Consider adding the following lines to the table definition:
Committable suggestion