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

[OTE-753] add new persistent cache table #2175

Merged
merged 4 commits into from
Aug 30, 2024

Conversation

jerryfan01234
Copy link
Contributor

@jerryfan01234 jerryfan01234 commented Aug 29, 2024

Changelist

[Describe or list the changes made in this PR]

Test Plan

[Describe how this PR was tested (if applicable)]

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Introduced a persistent caching system, allowing for efficient storage and retrieval of key-value pairs.
    • Added new constants and interfaces for managing persistent cache entries.
    • Implemented a new database migration for the persistent_cache table.
    • Enhanced query configuration options for persistent cache queries.
  • Bug Fixes

    • Improved testing coverage for the persistent cache functionalities, ensuring reliability.
  • Documentation

    • Updated types and interfaces to enhance clarity and usability in the persistent cache management.

Copy link

linear bot commented Aug 29, 2024

Copy link
Contributor

coderabbitai bot commented Aug 29, 2024

Walkthrough

The changes introduce a persistent caching system within a PostgreSQL database context. New types and interfaces are defined for managing cache entries, including a migration for creating a persistent_cache table. Unit tests are added to validate the functionality of the cache system, which includes operations for creating, updating, and retrieving cache entries. The overall structure of the module is expanded to support these new caching capabilities.

Changes

File Path Change Summary
.../helpers/constants.ts Added PersistentCacheCreateObject type and two constants, defaultKV and defaultKV2.
.../stores/persistent-cache-table.test.ts Introduced unit tests for PersistentCacheTable, covering creation, upsert, and retrieval.
.../migrations/20240828130730_create_persistent_cache_table.ts Created migration script for persistent_cache table with key and value columns.
.../db-helpers.ts Added persistent_cache to layer1Tables array.
.../index.ts Exported PersistentCacheModel and PersistentCacheTable.
.../models/persistent-cache-model.ts Defined PersistentCacheModel class with properties and schema validation for cache entries.
.../stores/persistent-cache-table.ts Implemented functions for managing cache entries: findAll, create, upsert, and findById.
.../db-model-types.ts Introduced PersistentCacheFromDatabase interface for cache representation.
.../index.ts Added export statement for persistent-cache-types.
.../persistent-cache-types.ts Defined PersistentCacheCreateObject interface and PersistentCacheColumns enum.
.../query-types.ts Modified QueryableField enum to include KEY and added PersistentCacheQueryConfig interface.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CacheTable
    participant Database

    User->>CacheTable: create(key, value)
    CacheTable->>Database: INSERT INTO persistent_cache (key, value)
    Database-->>CacheTable: Confirmation

    User->>CacheTable: upsert(key, value)
    CacheTable->>Database: UPSERT INTO persistent_cache (key, value)
    Database-->>CacheTable: Confirmation

    User->>CacheTable: findAll()
    CacheTable->>Database: SELECT * FROM persistent_cache
    Database-->>CacheTable: Return all entries
    CacheTable-->>User: List of cache entries
Loading

Poem

🐇
In the cache where data hops,
Key and value, never stops.
With a table built so fine,
Persistent treasures, all align.
Upsert, find, and create anew,
Hooray for caching, cheers to you!
🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (5)
indexer/packages/postgres/src/types/persistent-cache-types.ts (2)

1-4: Add comments for better readability.

Consider adding comments to the PersistentCacheCreateObject interface to describe the purpose of each property.

export interface PersistentCacheCreateObject {
+  // The key for the cache entry
  key: string,
+  // The value for the cache entry
  value: string,
}

6-9: Add comments for better readability.

Consider adding comments to the PersistentCacheColumns enum to describe the purpose of each column.

export enum PersistentCacheColumns {
+  // The key column for the cache table
  key = 'key',
+  // The value column for the cache table
  value = 'value',
}
indexer/packages/postgres/src/db/migrations/migration_files/20240828130730_create_persistent_cache_table.ts (2)

3-8: Add comments for better readability.

Consider adding comments to the up function to describe the purpose of each column and the table.

export async function up(knex: Knex): Promise<void> {
+  // Create the persistent_cache table
  return knex.schema.createTable('persistent_cache', (table) => {
+    // The primary key for the cache entry
    table.string('key').primary().notNullable();
+    // The value for the cache entry
    table.text('value').notNullable();
  });
}

10-12: Add comments for better readability.

Consider adding comments to the down function to describe the purpose of dropping the table.

export async function down(knex: Knex): Promise<void> {
+  // Drop the persistent_cache table
  return knex.schema.dropTable('persistent_cache');
}
indexer/packages/postgres/src/models/persistent-cache-model.ts (1)

15-23: Add comments for better readability.

Consider adding comments to the jsonSchema getter to describe the purpose of each property.

static get jsonSchema() {
  return {
    type: 'object',
    required: ['key', 'value'],
    properties: {
+      // The key for the cache entry
      key: { type: 'string' },
+      // The value for the cache entry
      value: { type: 'string' },
    },
  };
}
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 61fa593 and bc2edc6.

Files selected for processing (11)
  • indexer/packages/postgres/tests/helpers/constants.ts (2 hunks)
  • indexer/packages/postgres/tests/stores/persistent-cache-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240828130730_create_persistent_cache_table.ts (1 hunks)
  • indexer/packages/postgres/src/helpers/db-helpers.ts (1 hunks)
  • indexer/packages/postgres/src/index.ts (2 hunks)
  • indexer/packages/postgres/src/models/persistent-cache-model.ts (1 hunks)
  • indexer/packages/postgres/src/stores/persistent-cache-table.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/index.ts (1 hunks)
  • indexer/packages/postgres/src/types/persistent-cache-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/query-types.ts (2 hunks)
Additional comments not posted (24)
indexer/packages/postgres/src/models/persistent-cache-model.ts (4)

1-4: LGTM!

The code changes are approved.


5-7: LGTM!

The code changes are approved.


9-11: LGTM!

The code changes are approved.


26-31: LGTM!

The code changes are approved.

indexer/packages/postgres/src/types/index.ts (1)

31-31: LGTM!

The export statement correctly integrates the new types into the existing module.

indexer/packages/postgres/__tests__/stores/persistent-cache-table.test.ts (7)

7-9: LGTM!

The beforeAll function correctly sets up the database migration.


11-13: LGTM!

The afterEach function correctly clears the database data.


15-17: LGTM!

The afterAll function correctly tears down the database setup.


19-21: LGTM!

The test correctly verifies the creation of a key-value pair.


23-42: LGTM!

The test correctly verifies the upsertion of a key-value pair multiple times.


44-61: LGTM!

The test correctly verifies finding all key-value pairs.


63-71: LGTM!

The test correctly verifies finding a key-value pair.

indexer/packages/postgres/src/stores/persistent-cache-table.ts (4)

18-62: LGTM!

The findAll function correctly retrieves all key-value pairs based on the provided query configuration.


64-71: LGTM!

The create function correctly inserts a new key-value pair into the database.


73-82: LGTM!

The upsert function correctly inserts or updates a key-value pair in the database.


83-94: LGTM!

The findById function correctly retrieves a key-value pair by its key.

indexer/packages/postgres/src/index.ts (2)

21-21: LGTM!

The addition of PersistentCacheModel export is straightforward and aligns with the PR objectives.


47-47: LGTM!

The addition of PersistentCacheTable export is straightforward and aligns with the PR objectives.

indexer/packages/postgres/src/helpers/db-helpers.ts (1)

31-31: LGTM!

The addition of persistent_cache to the layer1Tables array is straightforward and aligns with the PR objectives.

indexer/packages/postgres/src/types/db-model-types.ts (1)

276-279: LGTM!

The addition of PersistentCacheFromDatabase interface is straightforward and aligns with the PR objectives.

indexer/packages/postgres/src/types/query-types.ts (2)

91-91: LGTM!

The addition of the KEY field to the QueryableField enum is straightforward and enhances the flexibility of the enum for various query configurations.


328-330: LGTM!

The introduction of the PersistentCacheQueryConfig interface is well-structured and aligns with the existing pattern of query configuration interfaces.

indexer/packages/postgres/__tests__/helpers/constants.ts (2)

947-950: LGTM!

The addition of the defaultKV constant is straightforward and provides a default value for persistent cache entries.


952-955: LGTM!

The addition of the defaultKV2 constant is straightforward and provides a default value for persistent cache entries.

@jerryfan01234 jerryfan01234 force-pushed the affiliates-new-persistent-cache-table branch from bc2edc6 to ef21ed4 Compare August 29, 2024 22:03
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between bc2edc6 and ef21ed4.

Files selected for processing (7)
  • indexer/packages/postgres/tests/stores/persistent-cache-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240829171730_create_persistent_cache_table.ts (1 hunks)
  • indexer/packages/postgres/src/models/persistent-cache-model.ts (1 hunks)
  • indexer/packages/postgres/src/stores/persistent-cache-table.ts (1 hunks)
  • indexer/packages/postgres/src/types/db-model-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/persistent-cache-types.ts (1 hunks)
  • indexer/packages/postgres/src/types/query-types.ts (2 hunks)
Files skipped from review as they are similar to previous changes (6)
  • indexer/packages/postgres/tests/stores/persistent-cache-table.test.ts
  • indexer/packages/postgres/src/models/persistent-cache-model.ts
  • indexer/packages/postgres/src/stores/persistent-cache-table.ts
  • indexer/packages/postgres/src/types/db-model-types.ts
  • indexer/packages/postgres/src/types/persistent-cache-types.ts
  • indexer/packages/postgres/src/types/query-types.ts
Additional comments not posted (2)
indexer/packages/postgres/src/db/migrations/migration_files/20240829171730_create_persistent_cache_table.ts (2)

1-1: LGTM!

The import statement is correct and necessary for the migration functions.


10-12: LGTM!

The function correctly defines the rollback operation for the migration.

Comment on lines +3 to +8
export async function up(knex: Knex): Promise<void> {
return knex.schema.createTable('persistent_cache', (table) => {
table.string('key').primary().notNullable();
table.string('value').notNullable();
});
}
Copy link
Contributor

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:

table.timestamps(true, true);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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 up(knex: Knex): Promise<void> {
return knex.schema.createTable('persistent_cache', (table) => {
table.string('key').primary().notNullable();
table.string('value').notNullable();
table.timestamps(true, true);
});
}

).upsert(kvToUpsert).returning('*');
// should only ever be one key value pair
return kvs[0];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: whitespace

@jerryfan01234 jerryfan01234 merged commit abfec2a into main Aug 30, 2024
16 checks passed
@jerryfan01234 jerryfan01234 deleted the affiliates-new-persistent-cache-table branch August 30, 2024 17:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants