/*
* === FUNCTION ==================================================================
* Name: printRedisObject
* Description: added by forenroll
* file position:src/redis.c
* =================================================================================
*/
void printRedisObject (robj *o )
{
unsigned type = o->type;
unsigned encoding = o->encoding;
if(type!=REDIS_STRING)
{
printf("the robj is not string type, could not print it now\n");
return;
}
switch(encoding){
case REDIS_ENCODING_RAW:
printf("the robj is string type, and its value is %s, and its length is %d\n",o->ptr,sdslen(o->ptr));
break;
case REDIS_ENCODING_INT:
printf("the robj is long type, and its value is %ld\n",(long)o->ptr);
break;
default:
printf("could not print the robj\n");
break;
}
return;
} /* ----- end of function printRedisObject ----- */
/* Create the length prefix of a bulk reply, example: $2234 */
void addReplyBulkLen(redisClient *c, robj *obj) {
size_t len;
printRedisObject(obj);
if (obj->encoding == REDIS_ENCODING_RAW) {
len = sdslen(obj->ptr);
} else {
long n = (long)obj->ptr;
/* Compute how many bytes will take this integer as a radix 10 string */
len = 1;
if (n < 0) {
len++;
n = -n;
}
while((n = n/10) != 0) {
len++;
}
}
addReplyLongLongWithPrefix(c,len,'$');
}
typedef struct redisDb {
dict *dict; /* The keyspace for this DB */
dict *expires; /* Timeout of keys with a timeout set */
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
dict *ready_keys; /* Blocked keys that received a PUSH */
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
int id;
long long avg_ttl; /* Average TTL, just for stats */
} redisDb;
int expireIfNeeded(redisDb *db, robj *key) {
long long when = getExpire(db,key);//这个函数会查询db->expires里key的过期时间。
if (when < 0) return 0; /* No expire for this key */
/* Don't expire anything while loading. It will be done later. */
if (server.loading) return 0;
if (server.masterhost != NULL) { //当前server是slave时,也同样对待
return mstime() > when;
}
/* Return when this key has not expired */
if (mstime() <= when) return 0;
/* Delete the key */
server.stat_expiredkeys++;
propagateExpire(db,key);//这个函数的作用是写日志和通知slave。
return dbDelete(db,key);
}
dictEntry *dictFind(dict *d, const void *key)
{
dictEntry *he;
unsigned int h, idx, table;
if (d->ht[0].size == 0) return NULL; /* We don't have a table at all */
if (dictIsRehashing(d)) _dictRehashStep(d);
h = dictHashKey(d, key);
for (table = 0; table <= 1; table++) {
idx = h & d->ht[table].sizemask;
he = d->ht[table].table[idx];
while(he) {
if (dictCompareKeys(d, key, he->key))
return he;
he = he->next;
}
if (!dictIsRehashing(d)) return NULL;
}
return NULL;
}