#include <stdio.h>
// 假设以下是Redis中sds.h/sdshdr的简化版本
struct sdshdr {
long len; // 已使用的空间
long free; // 未使用的空间
char buf[]; // 实际保存字符串数据的地方
};
// 假设以下是Redis中robj.h/redisObject的简化版本
struct redisObject {
int type; // 对象类型
struct sdshdr *ptr; // 指向实际数据的指针
};
// 假设以下是Redis中定义的5种数据类型的宏
#define REDIS_STRING 0
#define REDIS_LIST 1
#define REDIS_SET 2
#define REDIS_HASH 3
#define REDIS_ZSET 4
// 打印Redis对象信息的函数
void printRedisObjectInfo(struct redisObject *obj) {
switch (obj->type) {
case REDIS_STRING:
printf("Redis String: len = %ld, free = %ld, s = '%s'\n",
obj->ptr->len, obj->ptr->free, obj->ptr->buf);
break;
case REDIS_LIST:
printf("Redis List: len = %ld, free = %ld\n",
obj->ptr->len, obj->ptr->free);
break;
case REDIS_SET:
printf("Redis Set: len = %ld, free = %ld\n",
obj->ptr->len, obj->ptr->free);
break;
case REDIS_HASH:
printf("Redis Hash: len = %ld, free = %ld\n",
obj->ptr->len, obj->ptr->free);
break;
case REDIS_ZSET:
printf("Redis Sorted Set: len = %ld, free = %ld\n",
obj->ptr->len, obj->ptr->free);
break;
default:
printf("Unknown Redis Object Type: %d\n", obj->type);
break;
}
}
int main() {
// 假设这是从Redis实例中获取到的对象
struct redisObject objString = {REDIS_STRING, (struct sdshdr *)malloc(sizeof(struct sdshdr) + 10)};
struct redisObject objList = {REDIS_LIST, (struct sdshdr *)malloc(sizeof(struct sdshdr))};
struct redisObject objSet = {REDIS_SET, (struct sdshdr *)malloc(sizeof(struct sdshdr))};
struct redisObject objHash = {REDIS_HASH, (struct sdshdr *)malloc(sizeof(struct sdshdr))};
struct redisObject objZSet = {REDIS_ZSET, (struct sdshdr *)malloc(sizeof(struct sdshdr))};
// 为Redis字符串对象分配空间并初始化
objString.ptr->len = 4;
objString.ptr->free = 6; // 假设这里分配了10字节总空间,使用了4字节,剩下6字节
memcpy(objString.ptr->buf, "test", 5); // 包含结束符'\0'
//
评论已关闭