Browse Source

added prefix DB support

pull/2/head
asiekierka 6 years ago
parent
commit
5243caeefb
5 changed files with 60 additions and 50 deletions
  1. 4
    3
      app.js
  2. 7
    5
      cachedb.js
  3. 2
    1
      config-default.json
  4. 38
    34
      imagedb.js
  5. 9
    7
      userdb.js

+ 4
- 3
app.js View File

@@ -363,9 +363,10 @@ console.log("Connecting to database...")
363 363
 var client = redis.createClient(config.database.port, config.database.host);
364 364
 if(config.database.password != "")
365 365
   client.auth(config.database.password);
366
-imageDB.connect(client);
367
-cacheDB.connect(client);
368
-userDB.connect(client, config.salt);
366
+client.debug_mode = true;
367
+imageDB.connect(client,config);
368
+cacheDB.connect(client,config);
369
+userDB.connect(client,config);
369 370
 
370 371
 console.log("(Re)Creating users...");
371 372
 _.each(config.users, function(value,key) {

+ 7
- 5
cachedb.js View File

@@ -5,16 +5,18 @@ var CacheDB = {}
5 5
   , crypto = require('crypto');
6 6
 
7 7
 var client = null;
8
+var config = null;
9
+var prefix = "";
8 10
 
9
-CacheDB.connect = function(cli) { client = cli; }
11
+CacheDB.connect = function(cli,conf) { client = cli; config=conf; prefix=conf.database.prefix; }
10 12
 
11 13
 CacheDB.get = function(query,callback) {
12
-  client.get("cache:"+this.hash(query),function(err,out) {
14
+  client.get(prefix+"cache:"+this.hash(query),function(err,out) {
13 15
     callback(err,JSON.parse(out));
14 16
   });
15 17
 }
16 18
 
17
-CacheDB.exists = function(query,callback) { return client.exists("cache:"+this.hash(query),callback); }
19
+CacheDB.exists = function(query,callback) { return client.exists(prefix+"cache:"+this.hash(query),callback); }
18 20
 
19 21
 CacheDB.hash = function(data) {
20 22
   return crypto.createHash('md5').update(data).digest("hex");
@@ -24,8 +26,8 @@ CacheDB.set = function(query,data,ttl,callback) {
24 26
   if(data == null) callback(new Error("Invalid query data!"));
25 27
   else if(query == null) callback(new Error("Invalid query name!"));
26 28
   else async.series([
27
-    _.bind(client.set,client,"cache:"+this.hash(query),JSON.stringify(data)),
28
-    _.bind(client.expire,client,"cache:"+this.hash(query),ttl || 60)
29
+    _.bind(client.set,client,prefix+"cache:"+this.hash(query),JSON.stringify(data)),
30
+    _.bind(client.expire,client,prefix+"cache:"+this.hash(query),ttl || 60)
29 31
   ], callback);
30 32
 }
31 33
 

+ 2
- 1
config-default.json View File

@@ -5,7 +5,8 @@
5 5
   "database": {
6 6
     "host": "localhost",
7 7
     "port": 6379,
8
-    "password": ""
8
+    "password": "",
9
+    "prefix": ""
9 10
   },
10 11
   "salt": "",
11 12
   "pageSize": 15,

+ 38
- 34
imagedb.js View File

@@ -6,14 +6,18 @@ var ImageDB = {}
6 6
 
7 7
 var client = null
8 8
   , CURRENT_VERSION = 2
9
-  , cloudTags = ["tag", "author", "uploader"];
9
+  , cloudTags = ["tag", "author", "uploader"]
10
+  , config = null
11
+  , prefix = "";
10 12
 
11
-ImageDB.connect = function(cli) {
13
+ImageDB.connect = function(cli,conf) {
12 14
   client = cli;
15
+  config = conf;
16
+  prefix = conf.database.prefix;
13 17
 }
14 18
 ImageDB.log = function(t) { console.log("[ImageDB] "+t); }
15 19
 ImageDB.getVersion = function(cb) {
16
-  client.get("db_version",function(err, out) {
20
+  client.get(prefix+"db_version",function(err, out) {
17 21
     if(err) cb(1);
18 22
     else cb((parseInt(out)>0) ? parseInt(out) : 1);
19 23
   });
@@ -26,7 +30,7 @@ ImageDB.updateDatabase = function(callback) {
26 30
       self.log("Updating DB from version 1 to 2");
27 31
       self.updateCloud(function(){
28 32
         self.log("Done updating!");
29
-        client.set("db_version", 2, _.bind(self.updateDatabase, self));
33
+        client.set(prefix+"db_version", 2, _.bind(self.updateDatabase, self));
30 34
       });
31 35
     }
32 36
     else if(version == CURRENT_VERSION) { self.log("Latest DB version, nothing to do..."); if(_.isFunction(callback)) callback(); }
@@ -43,7 +47,7 @@ ImageDB.updateCloud = function(callback) {
43 47
   }, callback);
44 48
 }
45 49
 ImageDB.getCloud = function(name, cb) {
46
-  client.zrange("cloud:"+name,0,-1,'WITHSCORES',function(err, data) {
50
+  client.zrange(prefix+"cloud:"+name,0,-1,'WITHSCORES',function(err, data) {
47 51
     if(err) throw err;
48 52
     var fixedData = {};
49 53
     while(data.length > 0) {
@@ -55,10 +59,10 @@ ImageDB.getCloud = function(name, cb) {
55 59
 }
56 60
 ImageDB.generateCloud = function(name, cb) {
57 61
   this.log("(Re)generating tag cloud "+name+"...");
58
-  client.smembers(name+"s", function(err, out) {
62
+  client.smembers(prefix+name+"s", function(err, out) {
59 63
     if(err) throw err;
60 64
     var counts = async.map(out, function(tagName, callback) {
61
-      client.scard(name+":"+tagName, function(err, out) { // Count
65
+      client.scard(prefix+name+":"+tagName, function(err, out) { // Count
62 66
         callback(err, {"name": tagName, "count": out});
63 67
       });
64 68
     }, function(err, out) {
@@ -66,35 +70,35 @@ ImageDB.generateCloud = function(name, cb) {
66 70
       var output = {};
67 71
       async.eachSeries(out, function(tag, callback) {
68 72
         if(tag.count == 0 || tag.name == "") callback();
69
-        else client.zadd("cloud:"+name, tag.count, tag.name, callback);
73
+        else client.zadd(prefix+"cloud:"+name, tag.count, tag.name, callback);
70 74
       }, cb);
71 75
     });
72 76
   });
73 77
 }
74 78
 
75 79
 ImageDB.get = function(id,callback) {
76
-  client.get("data:"+id,function(err,out) {
80
+  client.get(prefix+"data:"+id,function(err,out) {
77 81
     if(err) throw err;
78 82
     callback(JSON.parse(out));
79 83
   });
80 84
 }
81 85
 ImageDB.getWithError = function(id,callback) {
82
-  client.get("data:"+id,function(err,out) {
86
+  client.get(prefix+"data:"+id,function(err,out) {
83 87
     callback(err,JSON.parse(out));
84 88
   });
85 89
 }
86 90
 
87
-ImageDB.exists = function(id,callback) { return client.exists(client,"data:"+id,callback); }
91
+ImageDB.exists = function(id,callback) { return client.exists(client,prefix+"data:"+id,callback); }
88 92
 
89 93
 ImageDB.listFields = function(name,callback) {
90
-  client.smembers(name, function(err,m) {
94
+  client.smembers(prefix+name, function(err,m) {
91 95
     if(err) throw err;
92 96
     if(_.isNull(m)) callback(new Array());
93 97
     else callback(m.sort());
94 98
   });
95 99
 }
96 100
 ImageDB.listImages = function(name,callback) {
97
-  client.smembers(name, function(err,m) {
101
+  client.smembers(prefix+name, function(err,m) {
98 102
     if(err) throw err;
99 103
     if(_.isNull(m)) callback(new Array());
100 104
     else callback(_.sortBy(m,function(num){ return 0-num; }));
@@ -108,7 +112,7 @@ ImageDB.imagesBy = function(key,val,callback){
108 112
   this.listImages(key+":"+val,callback);
109 113
 }
110 114
 ImageDB.imagesByNum = function(key,min,max,callback){
111
-  client.zrangebyscore("size:"+key,min,max,function(err,m) {
115
+  client.zrangebyscore(prefix+"size:"+key,min,max,function(err,m) {
112 116
     if(err) throw err;
113 117
     if(_.isNull(m)) callback(new Array());
114 118
     else callback(_.sortBy(m,function(num){ return 0-num; }));
@@ -131,12 +135,12 @@ ImageDB.range = function(arr2,s,l,callback) {
131 135
 }
132 136
 
133 137
 ImageDB.count = function(name,callback) {
134
-  client.setnx("counter:"+name,0);
135
-  client.incr("counter:"+name,callback);
138
+  client.setnx(prefix+"counter:"+name,0);
139
+  client.incr(prefix+"counter:"+name,callback);
136 140
 }
137 141
 ImageDB.uncount = function(name,callback) {
138
-  client.setnx("counter:"+name,0);
139
-  client.decr("counter:"+name,callback);
142
+  client.setnx(prefix+"counter:"+name,0);
143
+  client.decr(prefix+"counter:"+name,callback);
140 144
 }
141 145
 ImageDB.add = function(data,callback) {
142 146
   this.count("imageId",function(count) {
@@ -146,7 +150,7 @@ ImageDB.add = function(data,callback) {
146 150
   });
147 151
 }
148 152
 ImageDB.hashed = function(hash,callback) {
149
-  client.sismember("hashes",hash,function(err,ishashed) {
153
+  client.sismember(prefix+"hashes",hash,function(err,ishashed) {
150 154
     callback(ishashed);
151 155
   });
152 156
 }
@@ -154,20 +158,20 @@ ImageDB.hashed = function(hash,callback) {
154 158
 
155 159
 ImageDB.addField = function(id,key,val,callback) {
156 160
   async.parallel([
157
-    _.bind(client.sadd,client,key+"s",val),
158
-    _.bind(client.sadd,client,key+":"+val,id),
161
+    _.bind(client.sadd,client,prefix+key+"s",val),
162
+    _.bind(client.sadd,client,prefix+key+":"+val,id),
159 163
     ], callback);
160 164
 }
161 165
 ImageDB.delField = function(id,key,val,callback) {
162 166
   async.series([
163
-    _.bind(client.srem,client,key+":"+val,id),
167
+    _.bind(client.srem,client,prefix+key+":"+val,id),
164 168
     function(cb) {
165
-      client.scard(key+":"+val,function(err,card) {
169
+      client.scard(prefix+key+":"+val,function(err,card) {
166 170
         if(err) throw err;
167 171
         if(card==0)
168 172
           async.parallel([
169
-            _.bind(client.del,client,key+":"+val),
170
-            _.bind(client.srem,client,key+"s",val)
173
+            _.bind(client.del,client,prefix+key+":"+val),
174
+            _.bind(client.srem,client,prefix+key+"s",val)
171 175
           ],cb);
172 176
         else cb();
173 177
       });
@@ -176,10 +180,10 @@ ImageDB.delField = function(id,key,val,callback) {
176 180
 }
177 181
 
178 182
 ImageDB.addSize = function(id,key,val,callback) {
179
-  client.zadd("size:"+key,val,id,callback);
183
+  client.zadd(prefix+"size:"+key,val,id,callback);
180 184
 }
181 185
 ImageDB.delSize = function(id,key,callback) {
182
-  client.zrem("size:"+key,id,callback);
186
+  client.zrem(prefix+"size:"+key,id,callback);
183 187
 }
184 188
 
185 189
 ImageDB.setTags = function(id,tags,callback) {
@@ -196,7 +200,7 @@ ImageDB.unsetTags = function(id,tags,callback) {
196 200
 ImageDB.setSearchData = function(id, data, callback) {
197 201
   var self = this;
198 202
   async.parallel([
199
-    _.bind(client.sadd,client,"hashes",data.hash),
203
+    _.bind(client.sadd,client,prefix+"hashes",data.hash),
200 204
     _.bind(self.setTags,self,id,data.tags),
201 205
     _.bind(self.addField,self,id,"author",data.author),
202 206
     _.bind(self.addField,self,id,"uploader",data.uploader),
@@ -207,7 +211,7 @@ ImageDB.setSearchData = function(id, data, callback) {
207 211
 ImageDB.unsetSearchData = function(id, data, callback) {
208 212
   var self = this;
209 213
   async.series([
210
-    _.bind(client.srem,client,"hashes",data.hash),
214
+    _.bind(client.srem,client,prefix+"hashes",data.hash),
211 215
     _.bind(self.unsetTags,self,id,data.tags),
212 216
     function(cb) { cb(); },
213 217
     _.bind(self.delField,self,id,"author",data.author),
@@ -232,11 +236,11 @@ ImageDB.set = function(id,data,callback,noHashCheck) {
232 236
   var self = this;
233 237
   this.hashed(data.hash,function(ishashed) {
234 238
     if(noHashCheck || !ishashed)
235
-      client.exists("data:"+id,function(err,exists) {
239
+      client.exists(prefix+"data:"+id,function(err,exists) {
236 240
         var s = function() {
237 241
           async.parallel([
238
-            _.bind(client.set,client,"data:"+id,JSON.stringify(data)),
239
-            _.bind(client.sadd,client,"images",id),
242
+            _.bind(client.set,client,prefix+"data:"+id,JSON.stringify(data)),
243
+            _.bind(client.sadd,client,prefix+"images",id),
240 244
             _.bind(self.setSearchData,self,id,data),
241 245
             _.bind(self.updateCloud,self)
242 246
           ], callback);
@@ -253,8 +257,8 @@ ImageDB.unset = function(id,callback,dontTouchData) {
253 257
   this.get(id,function(data) {
254 258
     if(data == null) callback();
255 259
     async.parallel([
256
-      function(callback) { if(!dontTouchData) client.del("data:"+id,callback); else callback(); },
257
-      function(callback) { if(!dontTouchData) client.srem("images",id,callback); else callback(); },
260
+      function(callback) { if(!dontTouchData) client.del(prefix+"data:"+id,callback); else callback(); },
261
+      function(callback) { if(!dontTouchData) client.srem(prefix+"images",id,callback); else callback(); },
258 262
       _.bind(self.unsetSearchData,self,id,data),
259 263
       _.bind(self.updateCloud,self)
260 264
     ],callback);

+ 9
- 7
userdb.js View File

@@ -5,17 +5,19 @@ var UserDB = {}
5 5
   , crypto = require('crypto');
6 6
 
7 7
 var client = null
8
-  , salt = "DummyDummyDummyDummy";
8
+  , salt = "DummyDummyDummyDummy"
9
+  , config = null
10
+  , prefix = "";
9 11
 
10
-UserDB.connect = function(cli,s) { client = cli; salt = s; }
12
+UserDB.connect = function(cli,conf) { client = cli; config=conf; salt=conf.salt; prefix=conf.database.prefix; }
11 13
 
12 14
 UserDB.get = function(nick,callback) {
13
-  client.get("user:"+nick,function(err,out) {
15
+  client.get(prefix+"user:"+nick,function(err,out) {
14 16
     callback(err,JSON.parse(out));
15 17
   });
16 18
 }
17 19
 
18
-UserDB.exists = function(nick,callback) { return client.exists("user:"+nick,callback); }
20
+UserDB.exists = function(nick,callback) { return client.exists(prefix+"user:"+nick,callback); }
19 21
 
20 22
 UserDB.hash = function(pass,iter) {
21 23
   var iters = 32000 || iter;
@@ -32,11 +34,11 @@ UserDB.addUser = function(data,callback) {
32 34
     if(err) callback(err);
33 35
     //else if(does) callback(new Error("User exists!")); * We overwrite users for now. HACK! TODO!
34 36
     else async.series([
35
-      _.bind(client.set,client,"user:"+data.user,JSON.stringify(data)),
36
-      _.bind(client.sadd,client,"users",data.user)
37
+      _.bind(client.set,client,prefix+"user:"+data.user,JSON.stringify(data)),
38
+      _.bind(client.sadd,client,prefix+"users",data.user)
37 39
      ], callback);
38 40
   });
39 41
 }
40
-UserDB.list = function(callback) { client.smembers("users",callback); }
42
+UserDB.list = function(callback) { client.smembers(prefix+"users",callback); }
41 43
 
42 44
 exports.UserDB = UserDB;

Loading…
Cancel
Save