Redis rio

Redis rio的代码很少,基本上包了一个虚基类:

struct _rio {
    void (*update_cksum)(struct _rio *, const void *buf, size_t len);

    /* The current checksum */
    uint64_t cksum;

    /* Backend-specific vars. */
    union {
        struct {
            sds ptr;
            off_t pos;
        } buffer;
        struct {
            FILE *fp;
            off_t buffered; /* Bytes written since last fsync. */
            off_t autosync;   // buffered超过这个值自动更新,默认为0
        } file;
    } io;
};

API如下:

static inline size_t rioWrite(rio *r, const void *buf, size_t len) ;  // 调用 rio->write,同时更新checksum
static inline size_t rioRead(rio *r, void *buf, size_t len) // 调用 rio->read,同时更新checksum
static inline off_t rioTell(rio *r);  //直接调用rio->tell

void rioInitWithFile(rio *r, FILE *fp);  // 初始化rio->io->file
void rioInitWithBuffer(rio *r, sds s);  // 初始化rio->io->buffer

//  以下几个是High Level Interface,用于写AOF文件
size_t rioWriteBulkCount(rio *r, char prefix, int count);
size_t rioWriteBulkString(rio *r, const char *buf, size_t len);
size_t rioWriteBulkLongLong(rio *r, long long l);
size_t rioWriteBulkDouble(rio *r, double d);


// 更新crc64 checksum

void rioGenericUpdateChecksum(rio *r, const void *buf, size_t len);


// 设置自动sync的字节数

void rioSetAutoSync(rio *r, off_t bytes);


最后嗨提供了两种rio类型:

static const rio rioBufferIO = { 
    rioBufferRead,
    rioBufferWrite,
    rioBufferTell,
    NULL,           /* update_checksum */
    0,              /* current checksum */
    { { NULL, 0 } } /* union for io-specific vars */
};

static const rio rioFileIO = { 
    rioFileRead,
    rioFileWrite,
    rioFileTell,
    NULL,           /* update_checksum */
    0,              /* current checksum */
    { { NULL, 0 } } /* union for io-specific vars */
};

你可能感兴趣的:(redis,源码)