Skip to content

Commit

Permalink
fix: keys contained values, kv map included deleted keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Dec 29, 2023
1 parent de0c093 commit acd31a4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
18 changes: 9 additions & 9 deletions src/PgSortKeyCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,14 +385,14 @@ export class PgSortKeyCache<V> implements SortKeyCache<V> {
text: `WITH latest_values AS (SELECT DISTINCT ON (key) key, sort_key, value
FROM "${this.schemaName}"."${this.tableName}"
WHERE sort_key <= $1
AND value IS NOT NULL
AND ($2::text IS NULL OR key >= $2)
AND ($3::text IS NULL OR key < $3)
order by key ${order}, sort_key desc
LIMIT $4::bigint)
select key, value
order by key ${order}, sort_key desc)
select key
from latest_values
order by key ${order};`,
WHERE value IS NOT NULL
order by key ${order}
LIMIT $4::bigint;`,
values: [sortKey, options?.gte, options?.lt, options?.limit],
rowMode: 'array'
});
Expand All @@ -413,14 +413,14 @@ export class PgSortKeyCache<V> implements SortKeyCache<V> {
WITH latest_values AS (SELECT DISTINCT ON (key) key, sort_key, value
FROM "${this.schemaName}"."${this.tableName}"
WHERE sort_key <= $1
AND value IS NOT NULL
AND ($2::text IS NULL OR key >= $2)
AND ($3::text IS NULL OR key < $3)
order by key ${order}, sort_key desc
LIMIT $4::bigint)
order by key ${order}, sort_key desc)
select key, value
from latest_values
order by key ${order};`,
WHERE value IS NOT NULL
order by key ${order}
LIMIT $4::bigint;`,
[sortKey, options?.gte, options?.lt, options?.limit]
);
const kv = new Map(result.rows.map((i): [string, V] => [i.key, i.value]));
Expand Down
35 changes: 32 additions & 3 deletions src/__tests__/pg-sort-key-cache-commit-rollback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('Postgres cache batch', () => {

it('keys order natural and reversed', async () => {
await sut.open();
const sortKey = 348;
let sortKey = 348;

await sut.begin();

Expand All @@ -62,6 +62,7 @@ describe('Postgres cache batch', () => {
expect(naturalOrder.reverse()).toEqual(reverseOrder);

await sut.commit();
sortKey++;

await sut.begin();
await sut.del(new CacheKey('user.12', getSortKey(sortKey)));
Expand Down Expand Up @@ -113,7 +114,7 @@ describe('Postgres cache batch', () => {

it('multiple operations', async () => {
await sut.open();
const sortKey = 395;
let sortKey = 395;

await sut.begin();
await sut.put(new CacheKey('key.one', getSortKey(sortKey)), 111);
Expand Down Expand Up @@ -152,7 +153,35 @@ describe('Postgres cache batch', () => {
);
expect(rollbackKeys).toEqual(['key.two', 'key.one']);

await sut.drop();
await sut.close();
});

it('delete and fetch kv map', async () => {
await sut.open();
let sortKey = 405;

await sut.begin();
await sut.put(new CacheKey('message', getSortKey(sortKey)), 'world');
await sut.put(new CacheKey('change', getSortKey(sortKey)), 'it');
await sut.commit();
sortKey++;

await sut.begin();
await sut.del(new CacheKey('message', getSortKey(sortKey)));
await sut.commit();
sortKey++;

await sut.begin();
const transactionKeys = await sut.keys(getSortKey(sortKey));
expect(transactionKeys).toEqual(["change", "key.one", "key.two", "user.11", "user.13", "user.14", "user.15"]);

const kv = await sut.kvMap(getSortKey(sortKey));
expect(kv.get('message')).toBeFalsy();
expect(kv.get('change')).toBeTruthy();

await sut.commit();

await sut.drop();
await sut.close();
})
});

0 comments on commit acd31a4

Please sign in to comment.