Skip to content

Commit

Permalink
Merge branch 'master' into parser
Browse files Browse the repository at this point in the history
  • Loading branch information
leibale committed Apr 25, 2022
2 parents 8ec4855 + b1a0b48 commit 6b2de37
Show file tree
Hide file tree
Showing 21 changed files with 379 additions and 10 deletions.
18 changes: 18 additions & 0 deletions examples/topk.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ async function topK() {
// ]
console.log(top10);

// List out the top 10 with their counts (requires RedisBloom >=2.2.9)
const top10WithCounts = await client.topK.listWithCount('mytopk');
console.log('The top 10 with counts:');
console.log(top10WithCounts);
// top10WithCounts looks like this:
// [
// { item: 'suze', count: 42363 },
// { item: 'lance', count: 41982 },
// { item: 'simon', count: 41831 },
// { item: 'steve', count: 39237 },
// { item: 'guy', count: 39078 },
// { item: 'kyleb', count: 37338 },
// { item: 'leibale', count: 34230 },
// { item: 'kyleo', count: 33812 },
// { item: 'alex', count: 33679 },
// { item: 'nava', count: 32663 }
// ]

// Check if a few team members are in the top 10 with TOPK.QUERY:
const [ steve, suze, leibale, frederick ] = await client.topK.query('mytopk', [
'steve',
Expand Down
2 changes: 2 additions & 0 deletions packages/bloom/lib/commands/top-k/LIST.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;

export function transformArguments(key: string): Array<string> {
return ['TOPK.LIST', key];
}
Expand Down
30 changes: 30 additions & 0 deletions packages/bloom/lib/commands/top-k/LIST_WITHCOUNT.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../../test-utils';
import { transformArguments } from './LIST_WITHCOUNT';

describe('TOPK LIST WITHCOUNT', () => {
testUtils.isVersionGreaterThanHook([2, 2, 9]);

it('transformArguments', () => {
assert.deepEqual(
transformArguments('key'),
['TOPK.LIST', 'key', 'WITHCOUNT']
);
});

testUtils.testWithClient('client.topK.listWithCount', async client => {
const [,, list] = await Promise.all([
client.topK.reserve('key', 3),
client.topK.add('key', 'item'),
client.topK.listWithCount('key')
]);

assert.deepEqual(
list,
[{
item: 'item',
count: 1
}]
);
}, GLOBAL.SERVERS.OPEN);
});
26 changes: 26 additions & 0 deletions packages/bloom/lib/commands/top-k/LIST_WITHCOUNT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;

export function transformArguments(key: string): Array<string> {
return ['TOPK.LIST', key, 'WITHCOUNT'];
}

type ListWithCountRawReply = Array<string | number>;

type ListWithCountReply = Array<{
item: string,
count: number
}>;

export function transformReply(rawReply: ListWithCountRawReply): ListWithCountReply {
const reply: ListWithCountReply = [];
for (let i = 0; i < rawReply.length; i++) {
reply.push({
item: rawReply[i] as string,
count: rawReply[++i] as number
});
}

return reply;
}
2 changes: 2 additions & 0 deletions packages/bloom/lib/commands/top-k/RESERVE.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;

interface ReserveOptions {
width: number;
depth: number;
Expand Down
3 changes: 3 additions & 0 deletions packages/bloom/lib/commands/top-k/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as ADD from './ADD';
import * as COUNT from './COUNT';
import * as INCRBY from './INCRBY';
import * as INFO from './INFO';
import * as LIST_WITHCOUNT from './LIST_WITHCOUNT';
import * as LIST from './LIST';
import * as QUERY from './QUERY';
import * as RESERVE from './RESERVE';
Expand All @@ -15,6 +16,8 @@ export default {
incrBy: INCRBY,
INFO,
info: INFO,
LIST_WITHCOUNT,
listWithCount: LIST_WITHCOUNT,
LIST,
list: LIST,
QUERY,
Expand Down
12 changes: 12 additions & 0 deletions packages/client/lib/cluster/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import * as BITFIELD from '../commands/BITFIELD';
import * as BITOP from '../commands/BITOP';
import * as BITPOS from '../commands/BITPOS';
import * as BLMOVE from '../commands/BLMOVE';
import * as BLMPOP from '../commands/BLMPOP';
import * as BLPOP from '../commands/BLPOP';
import * as BRPOP from '../commands/BRPOP';
import * as BRPOPLPUSH from '../commands/BRPOPLPUSH';
import * as BZMPOP from '../commands/BZMPOP';
import * as BZPOPMAX from '../commands/BZPOPMAX';
import * as BZPOPMIN from '../commands/BZPOPMIN';
import * as COPY from '../commands/COPY';
Expand Down Expand Up @@ -59,6 +61,7 @@ import * as LINDEX from '../commands/LINDEX';
import * as LINSERT from '../commands/LINSERT';
import * as LLEN from '../commands/LLEN';
import * as LMOVE from '../commands/LMOVE';
import * as LMPOP from '../commands/LMPOP';
import * as LPOP_COUNT from '../commands/LPOP_COUNT';
import * as LPOP from '../commands/LPOP';
import * as LPOS_COUNT from '../commands/LPOS_COUNT';
Expand Down Expand Up @@ -161,6 +164,7 @@ import * as ZINTER from '../commands/ZINTER';
import * as ZINTERCARD from '../commands/ZINTERCARD';
import * as ZINTERSTORE from '../commands/ZINTERSTORE';
import * as ZLEXCOUNT from '../commands/ZLEXCOUNT';
import * as ZMPOP from '../commands/ZMPOP';
import * as ZMSCORE from '../commands/ZMSCORE';
import * as ZPOPMAX_COUNT from '../commands/ZPOPMAX_COUNT';
import * as ZPOPMAX from '../commands/ZPOPMAX';
Expand Down Expand Up @@ -202,12 +206,16 @@ export default {
bitPos: BITPOS,
BLMOVE,
blMove: BLMOVE,
BLMPOP,
blmPop: BLMPOP,
BLPOP,
blPop: BLPOP,
BRPOP,
brPop: BRPOP,
BRPOPLPUSH,
brPopLPush: BRPOPLPUSH,
BZMPOP,
bzmPop: BZMPOP,
BZPOPMAX,
bzPopMax: BZPOPMAX,
BZPOPMIN,
Expand Down Expand Up @@ -308,6 +316,8 @@ export default {
lLen: LLEN,
LMOVE,
lMove: LMOVE,
LMPOP,
lmPop: LMPOP,
LPOP_COUNT,
lPopCount: LPOP_COUNT,
LPOP,
Expand Down Expand Up @@ -512,6 +522,8 @@ export default {
zInterStore: ZINTERSTORE,
ZLEXCOUNT,
zLexCount: ZLEXCOUNT,
ZMPOP,
zmPop: ZMPOP,
ZMSCORE,
zmScore: ZMSCORE,
ZPOPMAX_COUNT,
Expand Down
6 changes: 3 additions & 3 deletions packages/client/lib/commands/BLMOVE.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { LMoveSide } from './LMOVE';
import { ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export function transformArguments(
source: RedisCommandArgument,
destination: RedisCommandArgument,
sourceDirection: LMoveSide,
destinationDirection: LMoveSide,
sourceDirection: ListSide,
destinationDirection: ListSide,
timeout: number
): RedisCommandArguments {
return [
Expand Down
32 changes: 32 additions & 0 deletions packages/client/lib/commands/BLMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BLMPOP';

describe('BLMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', 'LEFT'),
['BLMPOP', '0', '1', 'key', 'LEFT']
);
});

it('with COUNT', () => {
assert.deepEqual(
transformArguments(0, 'key', 'LEFT', {
COUNT: 2
}),
['BLMPOP', '0', '1', 'key', 'LEFT', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.blmPop', async client => {
assert.deepEqual(
await client.blmPop(1, 'key', 'RIGHT'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
20 changes: 20 additions & 0 deletions packages/client/lib/commands/BLMPOP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { transformLMPopArguments, LMPopOptions, ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 3;

export function transformArguments(
timeout: number,
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: ListSide,
options?: LMPopOptions
): RedisCommandArguments {
return transformLMPopArguments(
['BLMPOP', timeout.toString()],
keys,
side,
options
);
}

export { transformReply } from './LMPOP';
2 changes: 1 addition & 1 deletion packages/client/lib/commands/BLPOP.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('BLPOP', () => {
'key',
1
),
cluster.lPush('key', 'element'),
cluster.lPush('key', 'element')
]);

assert.deepEqual(
Expand Down
32 changes: 32 additions & 0 deletions packages/client/lib/commands/BZMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './BZMPOP';

describe('BZMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments(0, 'key', 'MIN'),
['BZMPOP', '0', '1', 'key', 'MIN']
);
});

it('with COUNT', () => {
assert.deepEqual(
transformArguments(0, 'key', 'MIN', {
COUNT: 2
}),
['BZMPOP', '0', '1', 'key', 'MIN', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.bzmPop', async client => {
assert.deepEqual(
await client.bzmPop(1, 'key', 'MAX'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
20 changes: 20 additions & 0 deletions packages/client/lib/commands/BZMPOP.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { SortedSetSide, transformZMPopArguments, ZMPopOptions } from './generic-transformers';

export const FIRST_KEY_INDEX = 3;

export function transformArguments(
timeout: number,
keys: RedisCommandArgument | Array<RedisCommandArgument>,
side: SortedSetSide,
options?: ZMPopOptions
): RedisCommandArguments {
return transformZMPopArguments(
['BZMPOP', timeout.toString()],
keys,
side,
options
);
}

export { transformReply } from './ZMPOP';
2 changes: 1 addition & 1 deletion packages/client/lib/commands/BZPOPMAX.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('BZPOPMAX', () => {
client.bzPopMax(
commandOptions({ isolated: true }),
'key',
0
1
),
client.zAdd('key', [{
value: '1',
Expand Down
2 changes: 1 addition & 1 deletion packages/client/lib/commands/BZPOPMIN.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('BZPOPMIN', () => {
client.bzPopMin(
commandOptions({ isolated: true }),
'key',
0
1
),
client.zAdd('key', [{
value: '1',
Expand Down
7 changes: 3 additions & 4 deletions packages/client/lib/commands/LMOVE.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { RedisCommandArgument, RedisCommandArguments } from '.';
import { ListSide } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export type LMoveSide = 'LEFT' | 'RIGHT';

export function transformArguments(
source: RedisCommandArgument,
destination: RedisCommandArgument,
sourceSide: LMoveSide,
destinationSide: LMoveSide
sourceSide: ListSide,
destinationSide: ListSide
): RedisCommandArguments {
return [
'LMOVE',
Expand Down
32 changes: 32 additions & 0 deletions packages/client/lib/commands/LMPOP.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './LMPOP';

describe('LMPOP', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

describe('transformArguments', () => {
it('simple', () => {
assert.deepEqual(
transformArguments('key', 'LEFT'),
['LMPOP', '1', 'key', 'LEFT']
);
});

it('with COUNT', () => {
assert.deepEqual(
transformArguments('key', 'LEFT', {
COUNT: 2
}),
['LMPOP', '1', 'key', 'LEFT', 'COUNT', '2']
);
});
});

testUtils.testWithClient('client.lmPop', async client => {
assert.deepEqual(
await client.lmPop('key', 'RIGHT'),
null
);
}, GLOBAL.SERVERS.OPEN);
});
Loading

0 comments on commit 6b2de37

Please sign in to comment.