Skip to content

Commit e57ca82

Browse files
committed
move info commands to discussed format
we will always return a typemapped resp3 format, even in resp2. we will not massage the field names to remove the spaces. + handles type mapping of double in resp2 for the one case.
1 parent 142dfb4 commit e57ca82

File tree

6 files changed

+57
-204
lines changed

6 files changed

+57
-204
lines changed
Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RedisArgument, Command, UnwrapReply, NullReply, NumberReply, TuplesToMapReply, Resp2Reply, SimpleStringReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, UnwrapReply, NullReply, NumberReply, TuplesToMapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
2+
import { transformInfoV2Reply } from '.';
23

34
export type BfInfoReplyMap = TuplesToMapReply<[
45
[SimpleStringReply<'Capacity'>, NumberReply],
@@ -23,41 +24,9 @@ export default {
2324
return ['BF.INFO', key];
2425
},
2526
transformReply: {
26-
2(reply: UnwrapReply<Resp2Reply<BfInfoReplyMap>>): BfInfoReply {
27-
return {
28-
capacity: reply[1],
29-
size: reply[3],
30-
numberOfFilters: reply[5],
31-
numberOfInsertedItems: reply[7],
32-
expansionRate: reply[9]
33-
};
27+
2: (reply: UnwrapReply<Resp2Reply<BfInfoReplyMap>>, _, typeMapping?: TypeMapping): BfInfoReplyMap => {
28+
return transformInfoV2Reply<BfInfoReplyMap>(reply, typeMapping);
3429
},
35-
3(reply: UnwrapReply<BfInfoReplyMap>): BfInfoReply {
36-
if (reply instanceof Map) {
37-
return {
38-
capacity: reply.get('Capacity') as NumberReply,
39-
size: reply.get('Size') as NumberReply,
40-
numberOfFilters: reply.get('Number of filters') as NumberReply,
41-
numberOfInsertedItems: reply.get('Number of items inserted') as NumberReply,
42-
expansionRate: reply.get('Expansion rate') as NullReply | NumberReply
43-
}
44-
} else if (reply instanceof Array) {
45-
return {
46-
capacity: reply[1],
47-
size: reply[3],
48-
numberOfFilters: reply[5],
49-
numberOfInsertedItems: reply[7],
50-
expansionRate: reply[9]
51-
}
52-
} else {
53-
return {
54-
capacity: reply["Capacity"],
55-
size: reply["Size"],
56-
numberOfFilters: reply["Number of filters"],
57-
numberOfInsertedItems: reply["Number of items inserted"],
58-
expansionRate: reply["Expansion rate"]
59-
};
60-
}
61-
}
30+
3: undefined as unknown as () => BfInfoReplyMap
6231
}
6332
} as const satisfies Command;

packages/bloom/lib/commands/bloom/index.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import type { RedisCommands } from '@redis/client/dist/lib/RESP/types';
1+
import type { RedisCommands, TypeMapping } from '@redis/client/dist/lib/RESP/types';
2+
23
import ADD from './ADD';
34
import CARD from './CARD';
45
import EXISTS from './EXISTS';
@@ -9,6 +10,7 @@ import MADD from './MADD';
910
import MEXISTS from './MEXISTS';
1011
import RESERVE from './RESERVE';
1112
import SCANDUMP from './SCANDUMP';
13+
import { RESP_TYPES } from '@redis/client';
1214

1315
export default {
1416
ADD,
@@ -32,3 +34,31 @@ export default {
3234
SCANDUMP,
3335
scanDump: SCANDUMP
3436
} as const satisfies RedisCommands;
37+
38+
export function transformInfoV2Reply<T>(reply: Array<any>, typeMapping?: TypeMapping): T {
39+
const mapType = typeMapping ? typeMapping[RESP_TYPES.MAP] : undefined;
40+
41+
switch (mapType) {
42+
case Array: {
43+
return reply as unknown as T;
44+
}
45+
case Map: {
46+
const ret = new Map<string, any>();
47+
48+
for (let i = 0; i < reply.length; i += 2) {
49+
ret.set(reply[i].toString(), reply[i + 1]);
50+
}
51+
52+
return ret as unknown as T;
53+
}
54+
default: {
55+
const ret = Object.create(null);
56+
57+
for (let i = 0; i < reply.length; i += 2) {
58+
ret[reply[i].toString()] = reply[i + 1];
59+
}
60+
61+
return ret as unknown as T;
62+
}
63+
}
64+
}
Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RedisArgument, TuplesToMapReply, NumberReply, UnwrapReply, Resp2Reply, Command, SimpleStringReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, TuplesToMapReply, NumberReply, UnwrapReply, Resp2Reply, Command, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
2+
import { transformInfoV2Reply } from '../bloom';
23

34
export type CmsInfoReplyMap = TuplesToMapReply<[
45
[SimpleStringReply<'width'>, NumberReply],
@@ -19,34 +20,9 @@ export default {
1920
return ['CMS.INFO', key];
2021
},
2122
transformReply: {
22-
2(reply: UnwrapReply<Resp2Reply<CmsInfoReplyMap>>): CmsInfoReply {
23-
return {
24-
width: reply[1],
25-
depth: reply[3],
26-
count: reply[5]
27-
};
23+
2: (reply: UnwrapReply<Resp2Reply<CmsInfoReplyMap>>, _, typeMapping?: TypeMapping): CmsInfoReply => {
24+
return transformInfoV2Reply<CmsInfoReply>(reply, typeMapping);
2825
},
29-
3(reply: UnwrapReply<CmsInfoReplyMap>): CmsInfoReply {
30-
if (reply instanceof Map) {
31-
return {
32-
width: reply.get('width') as NumberReply,
33-
depth: reply.get('depth') as NumberReply,
34-
count: reply.get('count') as NumberReply,
35-
}
36-
37-
} else if (reply instanceof Array) {
38-
return {
39-
width: reply[1],
40-
depth: reply[3],
41-
count: reply[5]
42-
}
43-
} else {
44-
return {
45-
width: reply['width'],
46-
depth: reply['depth'],
47-
count: reply['count']
48-
};
49-
}
50-
}
26+
3: undefined as unknown as () => CmsInfoReply
5127
}
5228
} as const satisfies Command;
Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RedisArgument, Command, NumberReply, TuplesToMapReply, UnwrapReply, Resp2Reply, SimpleStringReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, NumberReply, TuplesToMapReply, UnwrapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
2+
import { transformInfoV2Reply } from '../bloom';
23

34
export type CfInfoReplyMap = TuplesToMapReply<[
45
[SimpleStringReply<'Size'>, NumberReply],
@@ -29,53 +30,9 @@ export default {
2930
return ['CF.INFO', key];
3031
},
3132
transformReply: {
32-
2(reply: UnwrapReply<Resp2Reply<CfInfoReplyMap>>): CfInfoReply {
33-
return {
34-
size: reply[1],
35-
numberOfBuckets: reply[3],
36-
numberOfFilters: reply[5],
37-
numberOfInsertedItems: reply[7],
38-
numberOfDeletedItems: reply[9],
39-
bucketSize: reply[11],
40-
expansionRate: reply[13],
41-
maxIteration: reply[15]
42-
};
33+
2: (reply: UnwrapReply<Resp2Reply<CfInfoReplyMap>>, _, typeMapping?: TypeMapping): CfInfoReply => {
34+
return transformInfoV2Reply<CfInfoReply>(reply, typeMapping);
4335
},
44-
3: (reply: UnwrapReply<CfInfoReplyMap>): CfInfoReply => {
45-
if (reply instanceof Map) {
46-
return {
47-
size: reply.get('Size') as NumberReply,
48-
numberOfBuckets: reply.get('Number of buckets') as NumberReply,
49-
numberOfFilters: reply.get('Number of filters') as NumberReply,
50-
numberOfInsertedItems: reply.get('Number of items inserted') as NumberReply,
51-
numberOfDeletedItems: reply.get('Number of items deleted') as NumberReply,
52-
bucketSize: reply.get('Bucket size') as NumberReply,
53-
expansionRate: reply.get('Expansion rate') as NumberReply,
54-
maxIteration: reply.get('Max iterations') as NumberReply
55-
}
56-
} else if (reply instanceof Array) {
57-
return {
58-
size: reply[1],
59-
numberOfBuckets: reply[3],
60-
numberOfFilters: reply[5],
61-
numberOfInsertedItems: reply[7],
62-
numberOfDeletedItems: reply[9],
63-
bucketSize: reply[11],
64-
expansionRate: reply[13],
65-
maxIteration: reply[15]
66-
}
67-
} else {
68-
return {
69-
size: reply['Size'],
70-
numberOfBuckets: reply['Number of buckets'],
71-
numberOfFilters: reply['Number of filters'],
72-
numberOfInsertedItems: reply['Number of items inserted'],
73-
numberOfDeletedItems: reply['Number of items deleted'],
74-
bucketSize: reply['Bucket size'],
75-
expansionRate: reply['Expansion rate'],
76-
maxIteration: reply['Max iterations']
77-
};
78-
}
79-
}
36+
3: undefined as unknown as () => CfInfoReply
8037
}
8138
} as const satisfies Command;
Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { RedisArgument, Command, NumberReply, TuplesToMapReply, UnwrapReply, Resp2Reply, SimpleStringReply } from '@redis/client/dist/lib/RESP/types';
1+
import { RedisArgument, Command, NumberReply, TuplesToMapReply, UnwrapReply, Resp2Reply, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
2+
import { transformInfoV2Reply } from '../bloom';
23

34
export type TdInfoReplyMap = TuplesToMapReply<[
45
[SimpleStringReply<'Compression'>, NumberReply],
@@ -31,57 +32,9 @@ export default {
3132
return ['TDIGEST.INFO', key];
3233
},
3334
transformReply: {
34-
2(reply: UnwrapReply<Resp2Reply<TdInfoReplyMap>>): TdInfoReply {
35-
return {
36-
compression: reply[1],
37-
capacity: reply[3],
38-
mergedNodes: reply[5],
39-
unmergedNodes: reply[7],
40-
mergedWeight: reply[9],
41-
unmergedWeight: reply[11],
42-
observations: reply[13],
43-
totalCompression: reply[15],
44-
memoryUsage: reply[17]
45-
};
35+
2: (reply: UnwrapReply<Resp2Reply<TdInfoReplyMap>>, _, typeMapping?: TypeMapping): TdInfoReply => {
36+
return transformInfoV2Reply<TdInfoReply>(reply, typeMapping);
4637
},
47-
3(reply: UnwrapReply<TdInfoReplyMap>): TdInfoReply {
48-
if (reply instanceof Map) {
49-
return {
50-
compression: reply.get('Compression') as NumberReply,
51-
capacity: reply.get('Capacity') as NumberReply,
52-
mergedNodes: reply.get('Merged nodes') as NumberReply,
53-
unmergedNodes: reply.get('Unmerged nodes') as NumberReply,
54-
mergedWeight: reply.get('Merged weight') as NumberReply,
55-
unmergedWeight: reply.get('Unmerged weight') as NumberReply,
56-
observations: reply.get('Observations') as NumberReply,
57-
totalCompression: reply.get('Total compressions') as NumberReply,
58-
memoryUsage: reply.get('Memory usage') as NumberReply
59-
};
60-
} else if (reply instanceof Array) {
61-
return {
62-
compression: reply[1],
63-
capacity: reply[3],
64-
mergedNodes: reply[5],
65-
unmergedNodes: reply[7],
66-
mergedWeight: reply[9],
67-
unmergedWeight: reply[11],
68-
observations: reply[13],
69-
totalCompression: reply[15],
70-
memoryUsage: reply[17]
71-
};
72-
} else {
73-
return {
74-
compression: reply['Compression'],
75-
capacity: reply['Capacity'],
76-
mergedNodes: reply['Merged nodes'],
77-
unmergedNodes: reply['Unmerged nodes'],
78-
mergedWeight: reply['Merged weight'],
79-
unmergedWeight: reply['Unmerged weight'],
80-
observations: reply['Observations'],
81-
totalCompression: reply['Total compressions'],
82-
memoryUsage: reply['Memory usage']
83-
};
84-
}
85-
}
38+
3: undefined as unknown as () => TdInfoReply
8639
}
8740
} as const satisfies Command;
Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { transformDoubleReply } from '@redis/client/dist/lib/commands/generic-transformers';
22
import { RedisArgument, TuplesToMapReply, NumberReply, DoubleReply, UnwrapReply, Resp2Reply, Command, SimpleStringReply, TypeMapping } from '@redis/client/dist/lib/RESP/types';
3+
import { transformInfoV2Reply } from '../bloom';
34

45
export type TopKInfoReplyMap = TuplesToMapReply<[
56
[SimpleStringReply<'k'>, NumberReply],
@@ -8,51 +9,18 @@ export type TopKInfoReplyMap = TuplesToMapReply<[
89
[SimpleStringReply<'decay'>, DoubleReply]
910
]>;
1011

11-
export type TkInfoReply = {
12-
k: NumberReply;
13-
width: NumberReply;
14-
depth: NumberReply;
15-
decay: DoubleReply;
16-
}
17-
1812
export default {
1913
FIRST_KEY_INDEX: 1,
2014
IS_READ_ONLY: true,
2115
transformArguments(key: RedisArgument) {
2216
return ['TOPK.INFO', key];
2317
},
2418
transformReply: {
25-
2: (reply: UnwrapReply<Resp2Reply<TopKInfoReplyMap>>, preserve?: any, typeMapping?: TypeMapping): TkInfoReply => {
26-
return {
27-
k: reply[1],
28-
width: reply[3],
29-
depth: reply[5],
30-
decay: transformDoubleReply[2](reply[7], preserve, typeMapping)
31-
};
19+
2: (reply: UnwrapReply<Resp2Reply<TopKInfoReplyMap>>, preserve?: any, typeMapping?: TypeMapping): TopKInfoReplyMap => {
20+
reply[7] = transformDoubleReply[2](reply[7], preserve, typeMapping) as any;
21+
22+
return transformInfoV2Reply<TopKInfoReplyMap>(reply, typeMapping);
3223
},
33-
3: (reply: UnwrapReply<TopKInfoReplyMap>): TkInfoReply => {
34-
if (reply instanceof Map) {
35-
return {
36-
k: reply.get('k') as NumberReply,
37-
width: reply.get('width') as NumberReply,
38-
depth: reply.get('depth') as NumberReply,
39-
decay: reply.get('decay') as DoubleReply
40-
};
41-
} else if (reply instanceof Array) {
42-
return {
43-
k: reply[1],
44-
width: reply[3],
45-
depth: reply[5],
46-
decay: reply[7]
47-
};
48-
} else {
49-
return {
50-
k: reply['k'],
51-
width: reply['width'],
52-
depth: reply['depth'],
53-
decay: reply['decay']
54-
};
55-
}
56-
}
24+
3: undefined as unknown as () => TopKInfoReplyMap
5725
}
5826
} as const satisfies Command

0 commit comments

Comments
 (0)