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 7 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|000000 => if the two MSB are 00 the len is the 6 bits of this byte 48 * 01|000000 00000000 => 01, the len is 14 byes, 6 bits + 8 bits of next byte 49 * 10|000000 [32 bit integer] => if it's 01, a full 32 bit len will follow 50 * 11|000000 this means: specially encoded object will follow. The six bits 51 * number specify the kind of object that follows. 52 * See the RDB_ENC_* defines. 53 * 54 * Lengths up to 63 are stored using a single byte, most DB keys, and may 55 * values, will fit inside. */ 56 #define RDB_6BITLEN 0 57 #define RDB_14BITLEN 1 58 #define RDB_32BITLEN 2 59 #define RDB_ENCVAL 3 60 #define RDB_LENERR UINT_MAX 61 62 /* When a length of a string object stored on disk has the first two bits 63 * set, the remaining two bits specify a special encoding for the object 64 * accordingly to the following defines: */ 65 #define RDB_ENC_INT8 0 /* 8 bit signed integer */ 66 #define RDB_ENC_INT16 1 /* 16 bit signed integer */ 67 #define RDB_ENC_INT32 2 /* 32 bit signed integer */ 68 #define RDB_ENC_LZF 3 /* string compressed with FASTLZ */ 69 70 /* Dup object types to RDB object types. Only reason is readability (are we 71 * dealing with RDB types or with in-memory object types?). */ 72 #define RDB_TYPE_STRING 0 73 #define RDB_TYPE_LIST 1 74 #define RDB_TYPE_SET 2 75 #define RDB_TYPE_ZSET 3 76 #define RDB_TYPE_HASH 4 77 /* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */ 78 79 /* Object types for encoded objects. */ 80 #define RDB_TYPE_HASH_ZIPMAP 9 81 #define RDB_TYPE_LIST_ZIPLIST 10 82 #define RDB_TYPE_SET_INTSET 11 83 #define RDB_TYPE_ZSET_ZIPLIST 12 84 #define RDB_TYPE_HASH_ZIPLIST 13 85 #define RDB_TYPE_LIST_QUICKLIST 14 86 /* NOTE: WHEN ADDING NEW RDB TYPE, UPDATE rdbIsObjectType() BELOW */ 87 88 /* Test if a type is an object type. */ 89 #define rdbIsObjectType(t) ((t >= 0 && t <= 4) || (t >= 9 && t <= 14)) 90 91 /* Special RDB opcodes (saved/loaded with rdbSaveType/rdbLoadType). */ 92 #define RDB_OPCODE_AUX 250 93 #define RDB_OPCODE_RESIZEDB 251 94 #define RDB_OPCODE_EXPIRETIME_MS 252 95 #define RDB_OPCODE_EXPIRETIME 253 96 #define RDB_OPCODE_SELECTDB 254 97 #define RDB_OPCODE_EOF 255 98 99 int rdbSaveType(rio *rdb, unsigned char type); 100 int rdbLoadType(rio *rdb); 101 int rdbSaveTime(rio *rdb, time_t t); 102 time_t rdbLoadTime(rio *rdb); 103 int rdbSaveLen(rio *rdb, uint32_t len); 104 uint32_t rdbLoadLen(rio *rdb, int *isencoded); 105 int rdbSaveObjectType(rio *rdb, robj *o); 106 int rdbLoadObjectType(rio *rdb); 107 int rdbLoad(char *filename); 108 int rdbSaveBackground(char *filename); 109 int rdbSaveToSlavesSockets(void); 110 void rdbRemoveTempFile(pid_t childpid); 111 int rdbSave(char *filename); 112 ssize_t rdbSaveObject(rio *rdb, robj *o); 113 size_t rdbSavedObjectLen(robj *o); 114 robj *rdbLoadObject(int type, rio *rdb); 115 void backgroundSaveDoneHandler(int exitcode, int bysignal); 116 int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime, long long now); 117 robj *rdbLoadStringObject(rio *rdb); 118 119 #endif 120