xref: /redis-3.2.3/src/rio.h (revision cfc08b65)
1 /*
2  * Copyright (c) 2009-2012, Pieter Noordhuis <pcnoordhuis at gmail dot com>
3  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 
32 #ifndef __REDIS_RIO_H
33 #define __REDIS_RIO_H
34 
35 #include <stdio.h>
36 #include <stdint.h>
37 #include "sds.h"
38 
39 struct _rio {
40     /* Backend functions.
41      * Since this functions do not tolerate short writes or reads the return
42      * value is simplified to: zero on error, non zero on complete success. */
43     size_t (*read)(struct _rio *, void *buf, size_t len);
44     size_t (*write)(struct _rio *, const void *buf, size_t len);
45     off_t (*tell)(struct _rio *);
46     int (*flush)(struct _rio *);
47     /* The update_cksum method if not NULL is used to compute the checksum of
48      * all the data that was read or written so far. The method should be
49      * designed so that can be called with the current checksum, and the buf
50      * and len fields pointing to the new block of data to add to the checksum
51      * computation. */
52     void (*update_cksum)(struct _rio *, const void *buf, size_t len);
53 
54     /* The current checksum */
55     uint64_t cksum;
56 
57     /* number of bytes read or written */
58     size_t processed_bytes;
59 
60     /* maximum single read or write chunk size */
61     size_t max_processing_chunk;
62 
63     /* Backend-specific vars. */
64     union {
65         /* In-memory buffer target. */
66         struct {
67             sds ptr;
68             off_t pos;
69         } buffer;
70         /* Stdio file pointer target. */
71         struct {
72             FILE *fp;
73             off_t buffered; /* Bytes written since last fsync. */
74             off_t autosync; /* fsync after 'autosync' bytes written. */
75         } file;
76         /* Multiple FDs target (used to write to N sockets). */
77         struct {
78             int *fds;       /* File descriptors. */
79             int *state;     /* Error state of each fd. 0 (if ok) or errno. */
80             int numfds;
81             off_t pos;
82             sds buf;
83         } fdset;
84     } io;
85 };
86 
87 typedef struct _rio rio;
88 
89 /* The following functions are our interface with the stream. They'll call the
90  * actual implementation of read / write / tell, and will update the checksum
91  * if needed. */
92 
rioWrite(rio * r,const void * buf,size_t len)93 static inline size_t rioWrite(rio *r, const void *buf, size_t len) {
94     while (len) {
95         size_t bytes_to_write = (r->max_processing_chunk && r->max_processing_chunk < len) ? r->max_processing_chunk : len;
96         if (r->update_cksum) r->update_cksum(r,buf,bytes_to_write);
97         if (r->write(r,buf,bytes_to_write) == 0)
98             return 0;
99         buf = (char*)buf + bytes_to_write;
100         len -= bytes_to_write;
101         r->processed_bytes += bytes_to_write;
102     }
103     return 1;
104 }
105 
rioRead(rio * r,void * buf,size_t len)106 static inline size_t rioRead(rio *r, void *buf, size_t len) {
107     while (len) {
108         size_t bytes_to_read = (r->max_processing_chunk && r->max_processing_chunk < len) ? r->max_processing_chunk : len;
109         if (r->read(r,buf,bytes_to_read) == 0)
110             return 0;
111         if (r->update_cksum) r->update_cksum(r,buf,bytes_to_read);
112         buf = (char*)buf + bytes_to_read;
113         len -= bytes_to_read;
114         r->processed_bytes += bytes_to_read;
115     }
116     return 1;
117 }
118 
rioTell(rio * r)119 static inline off_t rioTell(rio *r) {
120     return r->tell(r);
121 }
122 
rioFlush(rio * r)123 static inline int rioFlush(rio *r) {
124     return r->flush(r);
125 }
126 
127 void rioInitWithFile(rio *r, FILE *fp);
128 void rioInitWithBuffer(rio *r, sds s);
129 void rioInitWithFdset(rio *r, int *fds, int numfds);
130 
131 void rioFreeFdset(rio *r);
132 
133 size_t rioWriteBulkCount(rio *r, char prefix, int count);
134 size_t rioWriteBulkString(rio *r, const char *buf, size_t len);
135 size_t rioWriteBulkLongLong(rio *r, long long l);
136 size_t rioWriteBulkDouble(rio *r, double d);
137 
138 void rioGenericUpdateChecksum(rio *r, const void *buf, size_t len);
139 void rioSetAutoSync(rio *r, off_t bytes);
140 
141 #endif
142