Redis中涉及到多种io,如socket与file,为了统一对它们的操做,redis设计了一个抽象层,即rio,使用rio能够实现将数据写入到不一样的底层io,可是接口相同。rio的实如今rio.h与rio.c源文件中,支持内存、文件、socket集合三类底层io。redis
struct rio中声明了统一的io操做接口,而且包含一个底层io对象的union结构。使用不一样的底层io初始化rio实例后,调用rio的抽象接口即会调用对应底层io的实现。以面向对象的思想便是,rio为抽象类,它拥有三个子类:buffer、file及fdset,这三个子类实现了抽象类声明的接口。使用者可使用它们的父类rio进行编程,实现多态性。编程
如下是struct rio中抽象接口的声明(此处省略了一些其它的成员):缓存
struct _rio { /* Backend functions. * Since this functions do not tolerate short writes or reads the return * value is simplified to: zero on error, non zero on complete success. */ size_t (*read)(struct _rio *, void *buf, size_t len); size_t (*write)(struct _rio *, const void *buf, size_t len); off_t (*tell)(struct _rio *); int (*flush)(struct _rio *); /* The update_cksum method if not NULL is used to compute the checksum of * all the data that was read or written so far. The method should be * designed so that can be called with the current checksum, and the buf * and len fields pointing to the new block of data to add to the checksum * computation. */ void (*update_cksum)(struct _rio *, const void *buf, size_t len);
...
/* Backend-specific vars. */
... };
每个底层对象都须要实现它须要支持的接口,实例化rio时,rio结构中的函数指针将指向底io的实现。Redis是C语言实现,所以针对 三个底层io声明了三个对应的初始化函数:app
void rioInitWithFile(rio *r, FILE *fp); void rioInitWithBuffer(rio *r, sds s); void rioInitWithFdset(rio *r, int *fds, int numfds);
这三个函数将初始化rio实例中的函数指针为它对应的抽象接口实现,并初始化union结构指向正确的底层io对象。socket
注意:rio接口中虽然声明了write操做与read操做,可是redis中仅将它们用于单向操做,即一个rio实例或者使用write操做,或者使用read操做,同一个rio实例不能既读又写。函数
以buffer为底层io的rio实例,write操做将参数buf中的数据copy到一个sds中(redis的字符串实现)。反之,它的read操做将会从一个sds中读取数据到参数buf指向的地址中。this
抽象接口不支持seek操做,所以写操做仅能append,而读操做也只能从当前位置读数据。buffer对象的结构声明以下:spa
/* In-memory buffer target. */ struct { sds ptr; off_t pos; } buffer;
这里的pos记录了读写操做在buffer中的当前位置。设计
以file为底层io的rio实例,write操做将参数buf中的数据写入到文件中,而read操做则将file中的数据读到参数buf指向的内存地址中。file对象的抽象接口实现只须要简单的调用c语言的库函数便可。指针
一样因为抽象接口未声明seek操做,它的具体实现也没有实现seek操做。file对象的结构声明以下:
/* Stdio file pointer target. */ struct { FILE *fp; off_t buffered; /* Bytes written since last fsync. */ off_t autosync; /* fsync after 'autosync' bytes written. */ } file;
这里的buffered记录了写操做的累计数据量,而autosync为设置一个同步值,当buffered值超过autosync值后,会执行sync操做使数据同步到磁盘上,sync操做后将buffered值清零。
以fdset为底层io的rio实例能够同时将数据向多个目标写,在redis中主要用做master向它的多个slave发送同步数据,即便用fdset的write操做能够将一份数据向多个socket发送。对fdset的抽象大大地简化了redis的master向它的多个slave发送同步数据的 io操做
fdset不支持read操做。此外,它使用了相似buffer的一个sds实例做为缓存,数据首先被写入到该缓存中,当缓存中的数据超过必定数量,或者调用了flush操做,再将缓存中的数据发送到全部的socket中。fdset的结构声明以下:
/* Multiple FDs target (used to write to N sockets). */ struct { int *fds; /* File descriptors. */ int *state; /* Error state of each fd. 0 (if ok) or errno. */ int numfds; off_t pos; sds buf; } fdset;
fds即全部的目标socket的文件描述符集合,state记录了这些文件描述符的状态(是否发生写错误),numfds记录了集合的大小,buf为缓存,pos表明buf中下一个应该发送的数据的位置。