MCPcopy Create free account
hub / github.com/F-Stack/f-stack / rdbSaveKeyValuePair

Function rdbSaveKeyValuePair

app/redis-6.2.6/src/rdb.c:1081–1121  ·  view source on GitHub ↗

Save a key-value pair, with expire time, type, key, value. * On error -1 is returned. * On success if the key was actually saved 1 is returned. */

Source from the content-addressed store, hash-verified

1079 * On error -1 is returned.
1080 * On success if the key was actually saved 1 is returned. */
1081int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime) {
1082 int savelru = server.maxmemory_policy & MAXMEMORY_FLAG_LRU;
1083 int savelfu = server.maxmemory_policy & MAXMEMORY_FLAG_LFU;
1084
1085 /* Save the expire time */
1086 if (expiretime != -1) {
1087 if (rdbSaveType(rdb,RDB_OPCODE_EXPIRETIME_MS) == -1) return -1;
1088 if (rdbSaveMillisecondTime(rdb,expiretime) == -1) return -1;
1089 }
1090
1091 /* Save the LRU info. */
1092 if (savelru) {
1093 uint64_t idletime = estimateObjectIdleTime(val);
1094 idletime /= 1000; /* Using seconds is enough and requires less space.*/
1095 if (rdbSaveType(rdb,RDB_OPCODE_IDLE) == -1) return -1;
1096 if (rdbSaveLen(rdb,idletime) == -1) return -1;
1097 }
1098
1099 /* Save the LFU info. */
1100 if (savelfu) {
1101 uint8_t buf[1];
1102 buf[0] = LFUDecrAndReturn(val);
1103 /* We can encode this in exactly two bytes: the opcode and an 8
1104 * bit counter, since the frequency is logarithmic with a 0-255 range.
1105 * Note that we do not store the halving time because to reset it
1106 * a single time when loading does not affect the frequency much. */
1107 if (rdbSaveType(rdb,RDB_OPCODE_FREQ) == -1) return -1;
1108 if (rdbWriteRaw(rdb,buf,1) == -1) return -1;
1109 }
1110
1111 /* Save type, key, value */
1112 if (rdbSaveObjectType(rdb,val) == -1) return -1;
1113 if (rdbSaveStringObject(rdb,key) == -1) return -1;
1114 if (rdbSaveObject(rdb,val,key) == -1) return -1;
1115
1116 /* Delay return if required (for testing) */
1117 if (server.rdb_key_save_delay)
1118 debugDelay(server.rdb_key_save_delay);
1119
1120 return 1;
1121}
1122
1123/* Save an AUX field. */
1124ssize_t rdbSaveAuxField(rio *rdb, void *key, size_t keylen, void *val, size_t vallen) {

Callers 1

rdbSaveRioFunction · 0.85

Calls 10

rdbSaveTypeFunction · 0.85
rdbSaveMillisecondTimeFunction · 0.85
estimateObjectIdleTimeFunction · 0.85
rdbSaveLenFunction · 0.85
LFUDecrAndReturnFunction · 0.85
rdbWriteRawFunction · 0.85
rdbSaveObjectTypeFunction · 0.85
rdbSaveStringObjectFunction · 0.85
rdbSaveObjectFunction · 0.85
debugDelayFunction · 0.85

Tested by

no test coverage detected