]> git.saurik.com Git - redis.git/blob - src/diskstore.c
touched key for WATCH refactored into a more general thing that can be used also...
[redis.git] / src / diskstore.c
1 /* diskstore.c implements a very simple disk backed key-value store used
2 * by Redis for the "disk" backend. This implementation uses the filesystem
3 * to store key/value pairs. Every file represents a given key.
4 *
5 * The key path is calculated using the SHA1 of the key name. For instance
6 * the key "foo" is stored as a file name called:
7 *
8 * /0b/ee/0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
9 *
10 * The couples of characters from the hex output of SHA1 are also used
11 * to locate two two levels of directories to store the file (as most
12 * filesystems are not able to handle too many files in a single dir).
13 *
14 * In the end there are 65536 final directories (256 directories inside
15 * every 256 top level directories), so that with 1 billion of files every
16 * directory will contain in the average 15258 entires, that is ok with
17 * most filesystems implementation.
18 *
19 * Note that since Redis supports multiple databases, the actual key name
20 * is:
21 *
22 * /0b/ee/<dbid>_0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
23 *
24 * so for instance if the key is inside DB 0:
25 *
26 * /0b/ee/0_0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
27 *
28 * The actaul implementation of this disk store is highly dependant to the
29 * filesystem implementation itself. This implementation may be replaced by
30 * a B+TREE implementation in future implementations.
31 *
32 * Data ok every key is serialized using the same format used for .rdb
33 * serialization. Everything is serialized on every entry: key name,
34 * ttl information in case of keys with an associated expire time, and the
35 * serialized value itself.
36 *
37 * Because the format is the same of the .rdb files it is trivial to create
38 * an .rdb file starting from this format just by mean of scanning the
39 * directories and concatenating entries, with the sole addition of an
40 * .rdb header at the start and the end-of-db opcode at the end.
41 *
42 * -------------------------------------------------------------------------
43 *
44 * Copyright (c) 2010-2011, Salvatore Sanfilippo <antirez at gmail dot com>
45 * All rights reserved.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions are met:
49 *
50 * * Redistributions of source code must retain the above copyright notice,
51 * this list of conditions and the following disclaimer.
52 * * Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * * Neither the name of Redis nor the names of its contributors may be used
56 * to endorse or promote products derived from this software without
57 * specific prior written permission.
58 *
59 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
60 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
63 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
69 * POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 #include "redis.h"
73
74 #include <fcntl.h>
75 #include <sys/stat.h>
76
77 int dsOpen(void) {
78 struct stat sb;
79 int retval;
80 char *path = server.ds_path;
81
82 if ((retval = stat(path,&sb) == -1) && errno != ENOENT) {
83 redisLog(REDIS_WARNING, "Error opening disk store at %s: %s",
84 path, strerror(errno));
85 return REDIS_ERR;
86 }
87
88 /* Directory already in place. Assume everything is ok. */
89 if (retval == 0 && S_ISDIR(sb.st_mode)) return REDIS_OK;
90
91 /* File exists but it's not a directory */
92 if (retval == 0 && !S_ISDIR(sb.st_mode)) {
93 redisLog(REDIS_WARNING,"Disk store at %s is not a directory", path);
94 return REDIS_ERR;
95 }
96
97 /* New disk store, create the directory structure now, as creating
98 * them in a lazy way is not a good idea, after very few insertions
99 * we'll need most of the 65536 directories anyway. */
100 if (mkdir(path,0644) == -1) {
101 redisLog(REDIS_WARNING,"Disk store init failed creating dir %s: %s",
102 path, strerror(errno));
103 return REDIS_ERR;
104 }
105 return REDIS_OK;
106 }
107
108 int dsClose(void) {
109 return REDIS_OK;
110 }
111
112 int dsSet(redisDb *db, robj *key, robj *val) {
113 }
114
115 robj *dsGet(redisDb *db, robj *key) {
116 }
117
118 int dsDel(redisDb *db, robj *key) {
119 }
120
121 int dsExists(redisDb *db, robj *key) {
122 }
123
124 int dsFlushDb(int dbid) {
125 }