Skip to content

Commit 448ac9d

Browse files
authored
fix #2090 - topK.listWithCount (#2091)
1 parent e6de453 commit 448ac9d

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

packages/bloom/lib/commands/top-k/LIST.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export const FIRST_KEY_INDEX = 1;
22

3+
export const IS_READ_ONLY = true;
4+
35
export function transformArguments(key: string): Array<string> {
46
return ['TOPK.LIST', key];
57
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../../test-utils';
3+
import { transformArguments } from './LIST_WITHCOUNT';
4+
5+
describe('TOPK LIST WITHCOUNT', () => {
6+
testUtils.isVersionGreaterThanHook([2, 2, 9]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments('key'),
11+
['TOPK.LIST', 'key', 'WITHCOUNT']
12+
);
13+
});
14+
15+
testUtils.testWithClient('client.topK.listWithCount', async client => {
16+
const [,, list] = await Promise.all([
17+
client.topK.reserve('key', 3),
18+
client.topK.add('key', 'item'),
19+
client.topK.listWithCount('key')
20+
]);
21+
22+
assert.deepEqual(
23+
list,
24+
[{
25+
item: 'item',
26+
count: 1
27+
}]
28+
);
29+
}, GLOBAL.SERVERS.OPEN);
30+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const FIRST_KEY_INDEX = 1;
2+
3+
export const IS_READ_ONLY = true;
4+
5+
export function transformArguments(key: string): Array<string> {
6+
return ['TOPK.LIST', key, 'WITHCOUNT'];
7+
}
8+
9+
type ListWithCountRawReply = Array<string | number>;
10+
11+
type ListWithCountReply = Array<{
12+
item: string,
13+
count: number
14+
}>;
15+
16+
export function transformReply(rawReply: ListWithCountRawReply): ListWithCountReply {
17+
const reply: ListWithCountReply = [];
18+
for (let i = 0; i < rawReply.length; i++) {
19+
reply.push({
20+
item: rawReply[i] as string,
21+
count: rawReply[++i] as number
22+
});
23+
}
24+
25+
return reply;
26+
}

packages/bloom/lib/commands/top-k/RESERVE.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export const FIRST_KEY_INDEX = 1;
22

3+
export const IS_READ_ONLY = true;
4+
35
interface ReserveOptions {
46
width: number;
57
depth: number;

packages/bloom/lib/commands/top-k/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as ADD from './ADD';
22
import * as COUNT from './COUNT';
33
import * as INCRBY from './INCRBY';
44
import * as INFO from './INFO';
5+
import * as LIST_WITHCOUNT from './LIST_WITHCOUNT';
56
import * as LIST from './LIST';
67
import * as QUERY from './QUERY';
78
import * as RESERVE from './RESERVE';
@@ -15,6 +16,8 @@ export default {
1516
incrBy: INCRBY,
1617
INFO,
1718
info: INFO,
19+
LIST_WITHCOUNT,
20+
listWithCount: LIST_WITHCOUNT,
1821
LIST,
1922
list: LIST,
2023
QUERY,

0 commit comments

Comments
 (0)