1 /* 2 * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * * Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * * Neither the name of Redis nor the names of its contributors may be used 14 * to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #ifndef __RDB_H 31 #define __RDB_H 32 33 #include <stdio.h> 34 #include "rio.h" 35 36 /* TBD: include only necessary headers. */ 37 #include "server.h" 38 39 /* The current RDB version. When the format changes in a way that is no longer 40 * backward compatible this number gets incremented. */ 41 #define RDB_VERSION 9 42 43 /* Defines related to the dump file format. To store 32 bits lengths for short 44 * keys requires a lot of space, so we check the most significant 2 bits of 45 * the first byte to interpreter the length: 46 * 47 * 00|XXXXXX => if the two MSB are 00 the len is the 6 bits of this byte 48 * 01|XXXXXX XXXXXXXX => 01, the len is 14 byes, 6 bits + 8 bits of next byte 49 * 10|000000 [32 bit integer] => A full 32 bit len in net byte order will follow 50 * 10|000001 [64 bit integer] => A full 64 bit len in net byte order will follow 51 * 11|OBKIND this means: specially encoded object will follow. The six bits 52 * number specify the kind of object that follows. 53 * See the RDB_ENC_* defines. 54 * 55 * Lengths up to 63 are stored using a single byte, most DB keys, and may 56 * values, will fit inside. */ 57 #define RDB_6BITLEN 0 58 #define RDB_14BITLEN 1 59 #define RDB_32BITLEN 0x80 60 #define RDB_64BITLEN 0x81 61 #define RDB_ENCVAL 3 62 #define RDB_LENERR UINT64_MAX 63 64 /* When a length of a string object stored on disk has the first two bits 65 * set, the remaining six bits specify a special encoding for the object 66 * accordingly to the following defines: */ 67 #define RDB_ENC_INT8 0 /* 8 bit signed integer */ 68 #define RDB_ENC_INT16 1 /* 16 bit signed integer */ 69 #define RDB_ENC_INT32 2 /* 32 bit signed integer */ 70 #define RDB_ENC_LZF 3 /* string compressed with FASTLZ */ 71 72 /* Map object types to RDB object types. Macros starting with OBJ_ are for 73 * memory storage and may change. Instead RDB types must be fixed because 74 * we store them on disk. */ 75 #define RDB_TYPE_STRING 0 76 #define RDB_TYPE_LIST 1 77 #define RDB_TYPE_SET 2 78 #define RDB_TYPE_ZSET 3 79 #define RDB_TYPE_HASH 4 80 #define RDB_TYPE_ZSET_2 5 /* ZSET version 2 with doubles stored in binary. */ 81 #define RDB_TYPE_MODULE 6 82 #define RDB_TYPE_MODULE_2 7 /* Module value with annotations for parsing without 83 the generating module being loaded. */ 84 /* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */ 85 86 /* Object types for encoded objects. */ 87 #define RDB_TYPE_HASH_ZIPMAP 9 88 #define RDB_TYPE_LIST_ZIPLIST 10 89 #define RDB_TYPE_SET_INTSET 11 90 #define RDB_TYPE_ZSET_ZIPLIST 12 91 #define RDB_TYPE_HASH_ZIPLIST 13 92 #define RDB_TYPE_LIST_QUICKLIST 14 93 #define RDB_TYPE_STREAM_LISTPACKS 15 94 /* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */ 95 96 /* Test if a type is an object type. */ 97 #define rdbIsObjectType(t) ((t >= 0 && t <= 7) || (t >= 9 && t <= 15)) 98 99 /* Special RDB opcodes (saved/loaded with rdbSaveType/rdbLoadType). */ 100 #define RDB_OPCODE_MODULE_AUX 247 /* Module auxiliary data. */ 101 #define RDB_OPCODE_IDLE 248 /* LRU idle time. */ 102 #define RDB_OPCODE_FREQ 249 /* LFU frequency. */ 103 #define RDB_OPCODE_AUX 250 /* RDB aux field. */ 104 #define RDB_OPCODE_RESIZEDB 251 /* Hash table resize hint. */ 105 #define RDB_OPCODE_EXPIRETIME_MS 252 /* Expire time in milliseconds. */ 106 #define RDB_OPCODE_EXPIRETIME 253 /* Old expire time in seconds. */ 107 #define RDB_OPCODE_SELECTDB 254 /* DB number of the following keys. */ 108 #define RDB_OPCODE_EOF 255 /* End of the RDB file. */ 109 110 /* Module serialized values sub opcodes */ 111 #define RDB_MODULE_OPCODE_EOF 0 /* End of module value. */ 112 #define RDB_MODULE_OPCODE_SINT 1 /* Signed integer. */ 113 #define RDB_MODULE_OPCODE_UINT 2 /* Unsigned integer. */ 114 #define RDB_MODULE_OPCODE_FLOAT 3 /* Float. */ 115 #define RDB_MODULE_OPCODE_DOUBLE 4 /* Double. */ 116 #define RDB_MODULE_OPCODE_STRING 5 /* String. */ 117 118 /* rdbLoad...() functions flags. */ 119 #define RDB_LOAD_NONE 0 120 #define RDB_LOAD_ENC (1<<0) 121 #define RDB_LOAD_PLAIN (1<<1) 122 #define RDB_LOAD_SDS (1<<2) 123 124 #define RDB_SAVE_NONE 0 125 #define RDB_SAVE_AOF_PREAMBLE (1<<0) 126 127 int rdbSaveType(rio *rdb, unsigned char type); 128 int rdbLoadType(rio *rdb); 129 int rdbSaveTime(rio *rdb, time_t t); 130 time_t rdbLoadTime(rio *rdb); 131 int rdbSaveLen(rio *rdb, uint64_t len); 132 int rdbSaveMillisecondTime(rio *rdb, long long t); 133 long long rdbLoadMillisecondTime(rio *rdb, int rdbver); 134 uint64_t rdbLoadLen(rio *rdb, int *isencoded); 135 int rdbLoadLenByRef(rio *rdb, int *isencoded, uint64_t *lenptr); 136 int rdbSaveObjectType(rio *rdb, robj *o); 137 int rdbLoadObjectType(rio *rdb); 138 int rdbLoad(char *filename, rdbSaveInfo *rsi); 139 int rdbSaveBackground(char *filename, rdbSaveInfo *rsi); 140 int rdbSaveToSlavesSockets(rdbSaveInfo *rsi); 141 void rdbRemoveTempFile(pid_t childpid); 142 int rdbSave(char *filename, rdbSaveInfo *rsi); 143 ssize_t rdbSaveObject(rio *rdb, robj *o, robj *key); 144 size_t rdbSavedObjectLen(robj *o); 145 robj *rdbLoadObject(int type, rio *rdb, robj *key); 146 void backgroundSaveDoneHandler(int exitcode, int bysignal); 147 int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime); 148 ssize_t rdbSaveSingleModuleAux(rio *rdb, int when, moduleType *mt); 149 robj *rdbLoadCheckModuleValue(rio *rdb, char *modulename); 150 robj *rdbLoadStringObject(rio *rdb); 151 ssize_t rdbSaveStringObject(rio *rdb, robj *obj); 152 ssize_t rdbSaveRawString(rio *rdb, unsigned char *s, size_t len); 153 void *rdbGenericLoadStringObject(rio *rdb, int flags, size_t *lenptr); 154 int rdbSaveBinaryDoubleValue(rio *rdb, double val); 155 int rdbLoadBinaryDoubleValue(rio *rdb, double *val); 156 int rdbSaveBinaryFloatValue(rio *rdb, float val); 157 int rdbLoadBinaryFloatValue(rio *rdb, float *val); 158 int rdbLoadRio(rio *rdb, rdbSaveInfo *rsi, int loading_aof); 159 rdbSaveInfo *rdbPopulateSaveInfo(rdbSaveInfo *rsi); 160 161 #endif 162