]> git.saurik.com Git - redis.git/blobdiff - src/diskstore.c
diskstore directory structure creation
[redis.git] / src / diskstore.c
index de0e9c3ee0899e27b56905ae944f89729d3a4e4b..cfb8ae66040d9d2d6bbe0e6b68130e49ed0b75b2 100644 (file)
  * directory will contain in the average 15258 entires, that is ok with
  * most filesystems implementation.
  *
- * The actaul implementation of this disk store is highly related to the
- * filesystem implementation. This implementation may be replaced by
+ * Note that since Redis supports multiple databases, the actual key name
+ * is:
+ *
+ *  /0b/ee/<dbid>_0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
+ *
+ *  so for instance if the key is inside DB 0:
+ *
+ *  /0b/ee/0_0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33
+ *
+ * The actaul implementation of this disk store is highly dependant to the
+ * filesystem implementation itself. This implementation may be replaced by
  * a B+TREE implementation in future implementations.
  *
  * Data ok every key is serialized using the same format used for .rdb
 #include <fcntl.h>
 #include <sys/stat.h>
 
+int create256dir(char *prefix) {
+    char buf[1024];
+    int j;
+
+    for (j = 0; j < 256; j++) {
+        snprintf(buf,sizeof(buf),"%s%02x",prefix,j);
+        if (mkdir(buf,0755) == -1) {
+            redisLog(REDIS_WARNING,"Error creating dir %s for diskstore: %s",
+                buf,strerror(errno));
+            return REDIS_ERR;
+        }
+    }
+    return REDIS_OK;
+}
+
 int dsOpen(void) {
     struct stat sb;
-    int retval;
-    char *path = server.diskstore_path;
+    int retval, j;
+    char *path = server.ds_path;
+    char buf[1024];
 
     if ((retval = stat(path,&sb) == -1) && errno != ENOENT) {
         redisLog(REDIS_WARNING, "Error opening disk store at %s: %s",
@@ -88,11 +113,20 @@ int dsOpen(void) {
     /* New disk store, create the directory structure now, as creating
      * them in a lazy way is not a good idea, after very few insertions
      * we'll need most of the 65536 directories anyway. */
-    if (mkdir(path) == -1) {
+    if (mkdir(path,0755) == -1) {
         redisLog(REDIS_WARNING,"Disk store init failed creating dir %s: %s",
             path, strerror(errno));
         return REDIS_ERR;
     }
+    /* Create the top level 256 directories */
+    snprintf(buf,sizeof(buf),"%s/",path);
+    if (create256dir(buf) == REDIS_ERR) return REDIS_ERR;
+
+    /* For every 256 top level dir, create 256 nested dirs */
+    for (j = 0; j < 256; j++) {
+        snprintf(buf,sizeof(buf),"%s/%02x/",path,j);
+        if (create256dir(buf) == REDIS_ERR) return REDIS_ERR;
+    }
     return REDIS_OK;
 }
 
@@ -106,5 +140,11 @@ int dsSet(redisDb *db, robj *key, robj *val) {
 robj *dsGet(redisDb *db, robj *key) {
 }
 
+int dsDel(redisDb *db, robj *key) {
+}
+
 int dsExists(redisDb *db, robj *key) {
 }
+
+int dsFlushDb(int dbid) {
+}