Skip to content

Commit

Permalink
fix: fixing base randomIterate (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Farenheith committed Nov 7, 2023
1 parent cdd2153 commit b20993f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
34 changes: 14 additions & 20 deletions src/tree-key-cache-base-redis-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { KeyTreeCacheStorage } from 'tree-key-cache';
import IORedis, { Redis } from 'ioredis';
import { RedisConnection, RedisStorageValueType } from './types';
import { depaginate } from '@codibre/fluent-iterable';
import { getPageToken } from './utils';

export interface TreeKeyCacheBaseRedisStorageNonRequiredOptions {
port: number;
Expand Down Expand Up @@ -95,19 +96,16 @@ export abstract class TreeKeyCacheBaseRedisStorage<
throw new Error('getChildren not supported!');
}
const childrenKey = this.getChildrenKey(key);
return depaginate(async (cursor: string | number = 0) => {
const [strToken, results] = await this.redisChildren.sscan(
childrenKey,
cursor,
'COUNT',
COUNT_CHILDREN,
);
const nextPageToken = Number(strToken);
return {
nextPageToken: nextPageToken || undefined,
results,
};
});
return depaginate(async (cursor: string | number = 0) =>
getPageToken(
await this.redisChildren.sscan(
childrenKey,
cursor,
'COUNT',
COUNT_CHILDREN,
),
),
);
}

async getCurrentTtl(key: string) {
Expand All @@ -127,13 +125,9 @@ export abstract class TreeKeyCacheBaseRedisStorage<
)
: (cursor: string | number = 0) =>
this.redisChildren.scan(cursor, 'COUNT', COUNT_CHILDREN);
return depaginate(async (token: string | number | undefined) => {
const [nextPageToken, results] = await getNext(token);
return {
nextPageToken,
results,
};
});
return depaginate(async (token: string | number | undefined) =>
getPageToken(await getNext(token)),
);
}

async registerChild(parentKey: string | undefined, partialKey: string) {
Expand Down
7 changes: 7 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,10 @@ export function readBufferedInt(value: Buffer | null | undefined) {
value.copy(buffer);
return buffer.readInt32LE(0);
}
export function getPageToken([strToken, results]: [string, string[]]) {
const nextPageToken = Number(strToken);
return {
nextPageToken: nextPageToken || undefined,
results,
};
}
37 changes: 33 additions & 4 deletions test/unit/tree-key-cache-base-redis-storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ describe(TreeKeyCacheBaseRedisStorage.name, () => {
});
});

afterEach(async () => {
await targetBuffer['redisChildren'].flushall();
await targetBuffer['redisData'].flushall();
await targetString['redisChildren'].flushall();
await targetString['redisData'].flushall();
});

describe(proto.get.name, () => {
it('should return the saved value', async () => {
await targetBuffer['redisData'].set('my key', 'my value');
Expand Down Expand Up @@ -161,17 +168,15 @@ describe(TreeKeyCacheBaseRedisStorage.name, () => {
});

describe(proto.getChildren.name, () => {
it('should return undefined when no ttl is defined for the latest key version', async () => {
await targetString.set('my key', 'a', 123);
it('should return undefined when no ttl is defined', async () => {
await targetString.set('my key', 'b');

const result = await targetString.getCurrentTtl('my key');

expect(result).toBeUndefined();
});

it('should return registered ttl for the latest key version', async () => {
await targetString.set('my key', 'a');
it('should return registered ttl for the ', async () => {
await targetString.set('my key', 'b', 123);

const result = await targetString.getCurrentTtl('my key');
Expand Down Expand Up @@ -210,4 +215,28 @@ describe(TreeKeyCacheBaseRedisStorage.name, () => {
expect(targetBuffer['redisChildren'].sadd).toHaveCallsLike(['', 'b']);
});
});

describe(proto.randomIterate.name, () => {
it('should return all the registered keys when no parameters is passed', async () => {
await targetString['redisChildren'].set('my key 1', 'a1');
await targetString['redisChildren'].set('my key item 2', 'b1');
await targetString['redisChildren'].set('my key item 3', 'c1');

const result = await fluentAsync(targetString.randomIterate()).toArray();

expect(result).toEqual(['my key 1', 'my key item 2', 'my key item 3']);
});

it('should return all the matching registered keys when a parameters is passed', async () => {
await targetString['redisChildren'].set('my key 1', 'a1');
await targetString['redisChildren'].set('my key item 2', 'b1');
await targetString['redisChildren'].set('my key item 3', 'c1');

const result = await fluentAsync(
targetString.randomIterate('*item*'),
).toArray();

expect(result).toEqual(['my key item 2', 'my key item 3']);
});
});
});

0 comments on commit b20993f

Please sign in to comment.