Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for all hash field expiration commands #2742

Open
wants to merge 7 commits into
base: v5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/client/lib/commands/HEXPIRE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HEXPIRE from './HEXPIRE';

describe('HEXPIRE', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HEXPIRE.transformArguments('key', 'field', 1),
['HEXPIRE', 'key', '1', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HEXPIRE.transformArguments('key', ['field1', 'field2'], 1),
['HEXPIRE', 'key', '1', '2', 'field1', 'field2']
);
});

it('with set option', () => {
assert.deepEqual(
HEXPIRE.transformArguments('key', 'field1', 1, 'NX'),
['HEXPIRE', 'key', '1', 'NX', '1', 'field1']
);
});
});

testUtils.testAll('hExpire', async client => {
assert.equal(
await client.hExpire('key', 'field', 0),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
36 changes: 36 additions & 0 deletions packages/client/lib/commands/HEXPIRE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Command, NumberReply, ArrayReply, RedisArgument } from "../RESP/types";
import { RedisVariadicArgument, pushVariadicArgument } from "./generic-transformers";

export const HASH_EXPIRATION = {
/** The field does not exist */
FIELD_NOT_EXISTS: -2,
/** Specified NX | XX | GT | LT condition not met */
CONDITION_NOT_MET: 0,
/** Expiration time was set or updated */
UPDATED: 1,
/** Field deleted because the specified expiration time is in the past */
DELETED: 2
} as const;

export type HashExpiration = typeof HASH_EXPIRATION[keyof typeof HASH_EXPIRATION];

export type HashExpirationReply = NumberReply<HashExpiration>;

export default {
FIRST_KEY_INDEX: 1,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
seconds: number,
mode?: 'NX' | 'XX' | 'GT' | 'LT',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do

interface HExpireOptions {
  mode?: 'NX' | 'XX' | 'GT' | 'LT';
}

for "future proofing" (in case there will be more optional arguments)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't really future proof, it just makes it easier if these options are shared between multiple commands. Unsure it adds that much.

) {
const args = ['HEXPIRE', key, seconds.toString()];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply>
} as const satisfies Command;
48 changes: 48 additions & 0 deletions packages/client/lib/commands/HEXPIREAT.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HEXPIREAT from './HEXPIREAT';

describe('HEXPIREAT', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string + number', () => {
assert.deepEqual(
HEXPIREAT.transformArguments('key', 'field', 1),
['HEXPIREAT', 'key', '1', '1', 'field']
);
});

it('array + number', () => {
assert.deepEqual(
HEXPIREAT.transformArguments('key', ['field1', 'field2'], 1),
['HEXPIREAT', 'key', '1', '2', 'field1', 'field2']
);
});

it('date', () => {
const d = new Date();
assert.deepEqual(
HEXPIREAT.transformArguments('key', ['field1'], d),
['HEXPIREAT', 'key', Math.floor(d.getTime() / 1000).toString(), '1', 'field1']
);
});

it('with set option', () => {
assert.deepEqual(
HEXPIREAT.transformArguments('key', 'field1', 1, 'GT'),
['HEXPIREAT', 'key', '1', 'GT', 1, 'field1']
);
});
});

testUtils.testAll('expireAt', async client => {
assert.equal(
await client.hExpireAt('key', 'field', 1),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
23 changes: 23 additions & 0 deletions packages/client/lib/commands/HEXPIREAT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisArgument, ArrayReply, Command } from '../RESP/types';
import { HashExpirationReply } from './HEXPIRE';
import { RedisVariadicArgument, pushVariadicArgument, transformEXAT } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
timestamp: number | Date,
mode?: 'NX' | 'XX' | 'GT' | 'LT'
) {
const args = ['HEXPIREAT', key, transformEXAT(timestamp)];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply>
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HEXPIRETIME.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HEXPIRETIME from './HEXPIRETIME';

describe('HEXPIRETIME', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HEXPIRETIME.transformArguments('key', 'field'),
['HEXPIRETIME', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HEXPIRETIME.transformArguments('key', ['field1', 'field2']),
['HEXPIRETIME', 'key', '2', 'field1', 'field2']
);
});
})

testUtils.testAll('hExpireTime', async client => {
assert.equal(
await client.hExpireTime('key', 'field'),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
11 changes: 11 additions & 0 deletions packages/client/lib/commands/HEXPIRETIME.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HEXPIRETIME', key], fields);
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HPERSIST.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPERSIST from './HPERSIST';

describe('HPERSIST', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HPERSIST.transformArguments('key', 'field'),
['HPERSIST', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HPERSIST.transformArguments('key', ['field1', 'field2']),
['HPERSIST', 'key', '2', 'field1', 'field2']
);
});
})

testUtils.testAll('hPersist', async client => {
assert.equal(
await client.hPersist('key', 'field'),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
23 changes: 23 additions & 0 deletions packages/client/lib/commands/HPERSIST.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export const HASH_FIELD_PERSIST = {
/** The expiration time was removed */
EXPIRATION_REMOVED: 1,
/** The field has no expiration time */
NO_EXPIRATION_TIME: -1,
/** The field does not exist */
FIELD_NOT_EXISTS: -2,
} as const;

export type HashFieldPersist = typeof HASH_FIELD_PERSIST[keyof typeof HASH_FIELD_PERSIST];

export type HashFieldPersistReply = NumberReply<HashFieldPersist>;

export default {
FIRST_KEY_INDEX: 1,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HPERSIST', key], fields);
},
transformReply: undefined as unknown as () => ArrayReply<HashFieldPersistReply>
} as const satisfies Command;
40 changes: 40 additions & 0 deletions packages/client/lib/commands/HPEXPIRE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPEXPIRE from './HPEXPIRE';

describe('HPEXPIRE', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HPEXPIRE.transformArguments('key', 'field', 1),
['HPEXPIRE', 'key', '1', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HPEXPIRE.transformArguments('key', ['field1', 'field2'], 1),
['HPEXPIRE', 'key', '1', '2', 'field1', 'field2']
);
});

it('with set option', () => {
assert.deepEqual(
HPEXPIRE.transformArguments('key', ['field1'], 1, 'NX'),
['HPEXPIRE', 'key', '1', 'NX', '1', 'field1']
);
});
});

testUtils.testAll('hpRxpire', async client => {
assert.equal(
await client.hpExpire('key', 'field', 0),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
22 changes: 22 additions & 0 deletions packages/client/lib/commands/HPEXPIRE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { RedisArgument, ArrayReply, Command } from '../RESP/types';
import { HashExpirationReply } from './HEXPIRE';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
ms: number,
mode?: 'NX' | 'XX' | 'GT' | 'LT',
) {
const args = ['HPEXPIRE', key, ms.toString()];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply>
} as const satisfies Command;
48 changes: 48 additions & 0 deletions packages/client/lib/commands/HPEXPIREAT.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPEXPIREAT from './HPEXPIREAT';

describe('HPEXPIREAT', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string + number', () => {
assert.deepEqual(
HPEXPIREAT.transformArguments('key', 'field', 1),
['HPEXPIREAT', 'key', '1', '1', 'field']
);
});

it('array + number', () => {
assert.deepEqual(
HPEXPIREAT.transformArguments('key', ['field1', 'field2'], 1),
['HPEXPIREAT', 'key', '1', '2', 'field1', 'field2']
);
});

it('date', () => {
const d = new Date();
assert.deepEqual(
HPEXPIREAT.transformArguments('key', ['field1'], d),
['HPEXPIREAT', 'key', d.getTime().toString(), '1', 'field1']
);
});

it('with set option', () => {
assert.deepEqual(
HPEXPIREAT.transformArguments('key', ['field1'], 1, 'XX'),
['HPEXPIREAT', 'key', '1', 'XX', '1', 'field1']
);
});
});

testUtils.testAll('hpExpireAt', async client => {
assert.equal(
await client.hpExpireAt('key', 'field', 1),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
23 changes: 23 additions & 0 deletions packages/client/lib/commands/HPEXPIREAT.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { RedisArgument, ArrayReply, Command } from '../RESP/types';
import { HashExpirationReply } from './HEXPIRE';
import { RedisVariadicArgument, pushVariadicArgument, transformEXAT } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(
key: RedisArgument,
fields: RedisVariadicArgument,
timestamp: number | Date,
mode?: 'NX' | 'XX' | 'GT' | 'LT'
) {
const args = ['HPEXPIREAT', key, transformEXAT(timestamp)];

if (mode) {
args.push(mode);
}

return pushVariadicArgument(args, fields);
},
transformReply: undefined as unknown as () => ArrayReply<HashExpirationReply>
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HPEXPIRETIME.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPEXPIRETIME from './HPEXPIRETIME';

describe('HPEXPIRETIME', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HPEXPIRETIME.transformArguments('key', 'field'),
['HPEXPIRETIME', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HPEXPIRETIME.transformArguments('key', ['field1', 'field2']),
['HPEXPIRETIME', 'key', '2', 'field1', 'field2']
);
});
});

testUtils.testAll('hpExpireTime', async client => {
assert.equal(
await client.hpExpireTime('key', 'field'),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
18 changes: 18 additions & 0 deletions packages/client/lib/commands/HPEXPIRETIME.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export const HASH_FIELD_EXPIRETIME = {
/** The field does not exist */
FIELD_NOT_EXISTS: -2,
/** The field has no expiration time */
NO_EXPIRATION_TIME: -1,
} as const;

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HPEXPIRETIME', key], fields);
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HPTTL.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HPTTL from './HPTTL';

describe('HPTTL', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HPTTL.transformArguments('key', 'field'),
['HPTTL', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HPTTL.transformArguments('key', ['field1', 'field2']),
['HPTTL', 'key', '2', 'field1', 'field2']
);
});
});

testUtils.testAll('hpTTL', async client => {
assert.equal(
await client.hpTTL('key', 'field'),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
11 changes: 11 additions & 0 deletions packages/client/lib/commands/HPTTL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HPTTL', key], fields);
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
} as const satisfies Command;
33 changes: 33 additions & 0 deletions packages/client/lib/commands/HTTL.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { strict as assert } from 'node:assert';
import testUtils, { GLOBAL } from '../test-utils';
import HTTL from './HTTL';

describe('HTTL', () => {
testUtils.isVersionGreaterThanHook([7, 4]);

describe('transformArguments', () => {
it('string', () => {
assert.deepEqual(
HTTL.transformArguments('key', 'field'),
['HTTL', 'key', '1', 'field']
);
});

it('array', () => {
assert.deepEqual(
HTTL.transformArguments('key', ['field1', 'field2']),
['HTTL', 'key', '2', 'field1', 'field2']
);
});
});

testUtils.testAll('hTTL', async client => {
assert.equal(
await client.hTTL('key', 'field'),
[-2]
);
}, {
client: GLOBAL.SERVERS.OPEN,
cluster: GLOBAL.CLUSTERS.OPEN
});
});
11 changes: 11 additions & 0 deletions packages/client/lib/commands/HTTL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RedisArgument, ArrayReply, NumberReply, Command } from '../RESP/types';
import { RedisVariadicArgument, pushVariadicArgument } from './generic-transformers';

export default {
FIRST_KEY_INDEX: 1,
IS_READ_ONLY: true,
transformArguments(key: RedisArgument, fields: RedisVariadicArgument) {
return pushVariadicArgument(['HTTL', key], fields);
},
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reply is array of

Integer reply: -2 if no such field exists in the provided hash key, or the provided key does not exist.
Integer reply: -1 if the field exists but has no associated expiration set.
Integer reply: the TTL in seconds.

should we create an "enum" (not the TS one) for the -2 and -1 values? IDK if I like having them anymore..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an enum can have value to refrer to negative values by name. but isn't that what I did before ala

export const HASH_FIELD_EXPIRETIME = {
  /** The field does not exist /
  FIELD_NOT_EXISTS: -2,
  /* The field has no expiration time */
  NO_EXPIRATION_TIME: -1,
} as const;

} as const satisfies Command;
27 changes: 27 additions & 0 deletions packages/client/lib/commands/index.ts
Original file line number Diff line number Diff line change
@@ -133,13 +133,22 @@ import FUNCTION_STATS from './FUNCTION_STATS';
import HDEL from './HDEL';
import HELLO from './HELLO';
import HEXISTS from './HEXISTS';
import HEXPIRE from './HEXPIRE';
import HEXPIREAT from './HEXPIREAT';
import HEXPIRETIME from './HEXPIRETIME';
import HGET from './HGET';
import HGETALL from './HGETALL';
import HINCRBY from './HINCRBY';
import HINCRBYFLOAT from './HINCRBYFLOAT';
import HKEYS from './HKEYS';
import HLEN from './HLEN';
import HMGET from './HMGET';
import HPERSIST from './HPERSIST';
import HPEXPIRE from './HPEXPIRE';
import HPEXPIREAT from './HPEXPIREAT';
import HPEXPIRETIME from './HPEXPIRETIME';
import HPTTL from './HPTTL';
import HTTL from './HTTL';
import HRANDFIELD_COUNT_WITHVALUES from './HRANDFIELD_COUNT_WITHVALUES';
import HRANDFIELD_COUNT from './HRANDFIELD_COUNT';
import HRANDFIELD from './HRANDFIELD';
@@ -601,6 +610,12 @@ export default {
hello: HELLO,
HEXISTS,
hExists: HEXISTS,
HEXPIRE,
hExpire: HEXPIRE,
HEXPIREAT,
hExpireAt: HEXPIREAT,
HEXPIRETIME,
hExpireTime: HEXPIRETIME,
HGET,
hGet: HGET,
HGETALL,
@@ -615,6 +630,18 @@ export default {
hLen: HLEN,
HMGET,
hmGet: HMGET,
HPERSIST,
hPersist: HPERSIST,
HPEXPIRE,
hpExpire: HPEXPIRE,
HPEXPIREAT,
hpExpireAt: HPEXPIRE,
HPEXPIRETIME,
hpExpireTime: HPEXPIRETIME,
HPTTL,
hpTTL: HPTTL,
HTTL,
hTTL: HTTL,
HRANDFIELD_COUNT_WITHVALUES,
hRandFieldCountWithValues: HRANDFIELD_COUNT_WITHVALUES,
HRANDFIELD_COUNT,