Skip to content

Commit cb2a95e

Browse files
committed
add hash-field-expiration option
1 parent f8b6960 commit cb2a95e

File tree

6 files changed

+256
-200
lines changed

6 files changed

+256
-200
lines changed

kvrocks.conf

+5
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ json-max-nesting-depth 1024
329329
# Default: json
330330
json-storage-format json
331331

332+
# Whether to enable hash field expiration feature.
333+
# NOTE: This option only affects newly hash object
334+
# Default: no
335+
hash-field-expiration no
336+
332337
################################## TLS ###################################
333338

334339
# By default, TLS/SSL is disabled, i.e. `tls-port` is set to 0.

src/commands/cmd_hash.cc

+4
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,10 @@ class CommandFieldExpireBase : public Commander {
467467
}
468468

469469
Status expireFieldExecute(Server *srv, Connection *conn, std::string *output) {
470+
if (!srv->storage->GetConfig()->hash_field_expiration) {
471+
return {Status::RedisExecErr, "field expiration feature is disabled"};
472+
}
473+
470474
std::vector<int8_t> ret;
471475
redis::Hash hash_db(srv->storage, conn->GetNamespace());
472476
auto s = hash_db.ExpireFields(args_[1], expire_, fields_, field_expire_type_, &ret);

src/config/config.cc

+1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Config::Config() {
190190
{"json-max-nesting-depth", false, new IntField(&json_max_nesting_depth, 1024, 0, INT_MAX)},
191191
{"json-storage-format", false,
192192
new EnumField<JsonStorageFormat>(&json_storage_format, json_storage_formats, JsonStorageFormat::JSON)},
193+
{"hash-field-expiration", false, new YesNoField(&hash_field_expiration, false)},
193194

194195
/* rocksdb options */
195196
{"rocksdb.compression", false,

src/config/config.h

+3
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ struct Config {
167167
int json_max_nesting_depth = 1024;
168168
JsonStorageFormat json_storage_format = JsonStorageFormat::JSON;
169169

170+
// hash
171+
bool hash_field_expiration = false;
172+
170173
struct RocksDB {
171174
int block_size;
172175
bool cache_index_and_filter_blocks;

src/types/redis_hash.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ rocksdb::Status Hash::MSet(const Slice &user_key, const std::vector<FieldValue>
301301
rocksdb::Status s = GetMetadata(GetOptions{}, ns_key, &metadata);
302302
if (!s.ok() && !s.IsNotFound()) return s;
303303

304-
// For avoid affect existing data,
305-
// we only encode ttl of field on new hash object.
306-
if (s.IsNotFound()) {
304+
// For avoid affect existing data, we only encode ttl of field
305+
// on new hash object when hash_field_expiration option is yes.
306+
if (s.IsNotFound() && storage_->GetConfig()->hash_field_expiration) {
307307
metadata.field_encoding = HashSubkeyEncoding::VALUE_WITH_TTL;
308308
}
309309

@@ -517,7 +517,8 @@ rocksdb::Status Hash::ExpireFields(const Slice &user_key, uint64_t expire_ms, co
517517

518518
// we don't support encode ttl on existing hash object
519519
if (!metadata.IsFieldExpirationEnabled()) {
520-
return rocksdb::Status::NotSupported("can't expire fields on hash object whose field expiration feature is disabled");
520+
return rocksdb::Status::NotSupported(
521+
"can't expire fields on hash object whose field expiration feature is disabled");
521522
}
522523

523524
rocksdb::ReadOptions read_options = storage_->DefaultMultiGetOptions();

0 commit comments

Comments
 (0)