diff --git a/src/PgSortKeyCache.ts b/src/PgSortKeyCache.ts index 8261523..480a48c 100644 --- a/src/PgSortKeyCache.ts +++ b/src/PgSortKeyCache.ts @@ -385,14 +385,14 @@ export class PgSortKeyCache implements SortKeyCache { 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' }); @@ -413,14 +413,14 @@ export class PgSortKeyCache implements SortKeyCache { 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])); diff --git a/src/__tests__/pg-sort-key-cache-commit-rollback.test.ts b/src/__tests__/pg-sort-key-cache-commit-rollback.test.ts index 686b42a..85fef8e 100644 --- a/src/__tests__/pg-sort-key-cache-commit-rollback.test.ts +++ b/src/__tests__/pg-sort-key-cache-commit-rollback.test.ts @@ -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(); @@ -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))); @@ -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); @@ -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(); + }) });