| 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 __REDIS_RDB_H |
| 31 | #define __REDIS_RDB_H |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include "rio.h" |
| 35 | |
| 36 | /* TBD: include only necessary headers. */ |
| 37 | #include "redis.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 REDIS_RDB_VERSION 6 |
| 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 REDIS_RDB_ENC_* defines. |
| 53 | * |
| 54 | * Lenghts up to 63 are stored using a single byte, most DB keys, and may |
| 55 | * values, will fit inside. */ |
| 56 | #define REDIS_RDB_6BITLEN 0 |
| 57 | #define REDIS_RDB_14BITLEN 1 |
| 58 | #define REDIS_RDB_32BITLEN 2 |
| 59 | #define REDIS_RDB_ENCVAL 3 |
| 60 | #define REDIS_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 REDIS_RDB_ENC_INT8 0 /* 8 bit signed integer */ |
| 66 | #define REDIS_RDB_ENC_INT16 1 /* 16 bit signed integer */ |
| 67 | #define REDIS_RDB_ENC_INT32 2 /* 32 bit signed integer */ |
| 68 | #define REDIS_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 REDIS_RDB_TYPE_STRING 0 |
| 73 | #define REDIS_RDB_TYPE_LIST 1 |
| 74 | #define REDIS_RDB_TYPE_SET 2 |
| 75 | #define REDIS_RDB_TYPE_ZSET 3 |
| 76 | #define REDIS_RDB_TYPE_HASH 4 |
| 77 | |
| 78 | /* Object types for encoded objects. */ |
| 79 | #define REDIS_RDB_TYPE_HASH_ZIPMAP 9 |
| 80 | #define REDIS_RDB_TYPE_LIST_ZIPLIST 10 |
| 81 | #define REDIS_RDB_TYPE_SET_INTSET 11 |
| 82 | #define REDIS_RDB_TYPE_ZSET_ZIPLIST 12 |
| 83 | #define REDIS_RDB_TYPE_HASH_ZIPLIST 13 |
| 84 | |
| 85 | /* Test if a type is an object type. */ |
| 86 | #define rdbIsObjectType(t) ((t >= 0 && t <= 4) || (t >= 9 && t <= 13)) |
| 87 | |
| 88 | /* Special RDB opcodes (saved/loaded with rdbSaveType/rdbLoadType). */ |
| 89 | #define REDIS_RDB_OPCODE_EXPIRETIME_MS 252 |
| 90 | #define REDIS_RDB_OPCODE_EXPIRETIME 253 |
| 91 | #define REDIS_RDB_OPCODE_SELECTDB 254 |
| 92 | #define REDIS_RDB_OPCODE_EOF 255 |
| 93 | |
| 94 | int rdbSaveType(rio *rdb, unsigned char type); |
| 95 | int rdbLoadType(rio *rdb); |
| 96 | int rdbSaveTime(rio *rdb, time_t t); |
| 97 | time_t rdbLoadTime(rio *rdb); |
| 98 | int rdbSaveLen(rio *rdb, uint32_t len); |
| 99 | uint32_t rdbLoadLen(rio *rdb, int *isencoded); |
| 100 | int rdbSaveObjectType(rio *rdb, robj *o); |
| 101 | int rdbLoadObjectType(rio *rdb); |
| 102 | int rdbLoad(char *filename); |
| 103 | int rdbSaveBackground(char *filename); |
| 104 | void rdbRemoveTempFile(pid_t childpid); |
| 105 | int rdbSave(char *filename); |
| 106 | int rdbSaveObject(rio *rdb, robj *o); |
| 107 | off_t rdbSavedObjectLen(robj *o); |
| 108 | off_t rdbSavedObjectPages(robj *o); |
| 109 | robj *rdbLoadObject(int type, rio *rdb); |
| 110 | void backgroundSaveDoneHandler(int exitcode, int bysignal); |
| 111 | int rdbSaveKeyValuePair(rio *rdb, robj *key, robj *val, long long expiretime, long long now); |
| 112 | robj *rdbLoadStringObject(rio *rdb); |
| 113 | |
| 114 | #endif |