]>
git.saurik.com Git - redis.git/blob - src/diskstore.c
de0e9c3ee0899e27b56905ae944f89729d3a4e4b
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.
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:
8 * /0b/ee/0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
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).
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.
19 * The actaul implementation of this disk store is highly related to the
20 * filesystem implementation. This implementation may be replaced by
21 * a B+TREE implementation in future implementations.
23 * Data ok every key is serialized using the same format used for .rdb
24 * serialization. Everything is serialized on every entry: key name,
25 * ttl information in case of keys with an associated expire time, and the
26 * serialized value itself.
28 * Because the format is the same of the .rdb files it is trivial to create
29 * an .rdb file starting from this format just by mean of scanning the
30 * directories and concatenating entries, with the sole addition of an
31 * .rdb header at the start and the end-of-db opcode at the end.
33 * -------------------------------------------------------------------------
35 * Copyright (c) 2010-2011, Salvatore Sanfilippo <antirez at gmail dot com>
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright notice,
42 * this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of Redis nor the names of its contributors may be used
47 * to endorse or promote products derived from this software without
48 * specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
54 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
71 char *path
= server
.diskstore_path
;
73 if ((retval
= stat(path
,&sb
) == -1) && errno
!= ENOENT
) {
74 redisLog(REDIS_WARNING
, "Error opening disk store at %s: %s",
75 path
, strerror(errno
));
79 /* Directory already in place. Assume everything is ok. */
80 if (retval
== 0 && S_ISDIR(sb
.st_mode
)) return REDIS_OK
;
82 /* File exists but it's not a directory */
83 if (retval
== 0 && !S_ISDIR(sb
.st_mode
)) {
84 redisLog(REDIS_WARNING
,"Disk store at %s is not a directory", path
);
88 /* New disk store, create the directory structure now, as creating
89 * them in a lazy way is not a good idea, after very few insertions
90 * we'll need most of the 65536 directories anyway. */
91 if (mkdir(path
) == -1) {
92 redisLog(REDIS_WARNING
,"Disk store init failed creating dir %s: %s",
93 path
, strerror(errno
));
103 int dsSet(redisDb
*db
, robj
*key
, robj
*val
) {
106 robj
*dsGet(redisDb
*db
, robj
*key
) {
109 int dsExists(redisDb
*db
, robj
*key
) {