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 ExHash type, support field expire #408

Closed
wants to merge 3 commits into from

Conversation

caipengbo
Copy link
Contributor

Background

I opened a Discussion #399 in which we discussed support TTL for member and I proposed a draft design. Based on this design, I extended the Hash type to support field expiration.

Design

  1. Add a new type, called ExHash
  2. Based on the coding design of Hash Type, we encode expire time in the value of subkey:

metadata:

        +----------+------------+-----------+-----------+
key =>  |  flags   |  expire    |  version  |  size     |
        | (1byte)  | (4byte)    |  (8byte)  | (8byte)   |
        +----------+------------+-----------+-----------+

subkey:

                     +---------------+
key|version|field => |     value     |
                     |     (Nbyte)   |
                     +---------------+

Encode expire in subkey's value:

                     +----------+---------------+
key|version|field => |  expire  |   value       |
                     |  (4byte) |  (Nbyte)      |
                     +----------+---------------+
  1. In addition to commands that have the same semantics as Hash, ExHash has the following special commands:
Command Syntax Description
EXHSETEX EXHSETEX key field value seconds Adds a field with a second timeout to a specified ExHash. If the key does not exist, a key for the ExHash is created. If the field already existed, this command overwrites the value of the field.
Returns OK on success. When the seconds parameter is invalid, the command returns an error.
EXHEXPIRE EXHEXPIRE key field seconds Specifies the timeout of a field in a specified ExHash. If the field timeout already existed, this command overwrites the timeout of the field.
Returns 1 on success or 0 if key or field does not exist. When the seconds parameter is invalid, the command returns an error.
EXHEXPIREAT EXHEXPIREAT key field timestamp Like EXHEXPIRE, just using unix timestamp.
Returns 1 on success or 0 if key or field does not exist. The command returns an error when the timestamp parameter is invalid.
Note: If timestamp is less than the current timestamp, the field will not set the lifetime.
EXHTTL EXHTTL key field Get the remaining expiration time of a field in a specified ExHash. Unit: Seconds.
EXHPERSIST EXHPERSIST key field Remove the expire of a field in a specified ExHash. Returns 1 when the expire is successfully removed.
Returns 0 if field has no expiration set or if ExHash or field does not exist.
EXHLEN EXHLEN key [EXACT] Get the number of fields about a specified ExHash. If there is no EXACT flag, the length returned is inaccurate (it will contain expired fields that have not been reclaimed). If EXACT flag is set, the exact length is obtained, but this is time-consuming because it will check the entire ExHash.
Return the number of fields in a ExHash. Returns 0 if key does not exist.
EXHGETALL EXHGETALL key [WITHTTL] Returns all fields and values in the ExHash table key without the optional WITHTTL parameter. When WITHTTL is added, all fields, values, and lifetime in the ExHash key are returned.
Returns the ExHash's fields and their values as a list, along with the ttl (-1 if no expire is set). If the key does not exist, the empty list is returned.

The other commands are the same as Hash. In particular, I didn't implement EXHMSET (synonymous with HMSET) because this command can be used instead of EXHSET (synonymous with HSET) to set multiple fields and values.

Implement

  1. Add a new RedisType, called kRedisExHash
  2. Modify the Hash class and add member variables support_field_expire_ to determine whether field expiration is supported. When get the value of the field, we need to check expire.
  3. In SubKeyFilter::IsKeyExpired, judge whether the Value expire. Note that we do not update the size in metadata because this accesses the Storage object and causes data race, which will affect performance. So the size in ExHash metadata is inaccurate, and it contains fields that have expired.

@caipengbo
Copy link
Contributor Author

Can deal with part of the #283

@ColinChamber
Copy link
Contributor

Cool. I have some suggestions, maybe it would be better to be more similar to the interface of Tairhash. Many people have already used it, which already been provided to a client.

@caipengbo
Copy link
Contributor Author

Thank you very much. I will take your advice seriously. @ColinChamber

@tisonkun
Copy link
Member

Closed as stale.

Codebase evolves a lot since the last activities on this patch. There're a few of conflicts. @caipengbo you're welcome to propose the changes again based on the nightly unstable branch.

@tisonkun tisonkun closed this Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants