forked from ether/ueberDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloneAndAtomicLayer.js
191 lines (165 loc) · 5.03 KB
/
CloneAndAtomicLayer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* 2011 Peter 'Pita' Martischka
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var cacheAndBufferLayer = require("./CacheAndBufferLayer");
var async = require("async");
var channels = require("channels");
var defaultLogger = {debug: function(){}, info: function(){}, error: function(){}, warn: function(){}};
/**
The Constructor
*/
exports.database = function(type, dbSettings, wrapperSettings, logger)
{
if(!type)
{
type = "sqlite";
dbSettings = null;
wrapperSettings = null;
}
//saves all settings and require the db module
this.type = type;
this.db_module = require("./" + type + "_db");
this.dbSettings = dbSettings;
this.wrapperSettings = wrapperSettings;
this.logger = logger || defaultLogger;
this.channels = new channels.channels(doOperation);
}
exports.database.prototype.init = function(callback)
{
var db = new this.db_module.database(this.dbSettings);
this.db = new cacheAndBufferLayer.database(db, this.wrapperSettings, this.logger);
this.db.init(callback);
}
/**
Wrapper functions
*/
exports.database.prototype.doShutdown = function(callback)
{
this.db.doShutdown(callback);
}
exports.database.prototype.get = function (key, callback)
{
this.channels.emit(key, {"db": this.db, "type": "get", "key": key, "callback": callback});
}
exports.database.prototype.set = function (key, value, bufferCallback, writeCallback)
{
this.channels.emit(key, {"db": this.db, "type": "set", "key": key, "value": clone(value), "bufferCallback": bufferCallback, "writeCallback": writeCallback});
}
exports.database.prototype.getSub = function (key, sub, callback)
{
this.channels.emit(key, {"db": this.db, "type": "getsub", "key": key, "sub": sub, "callback": callback});
}
exports.database.prototype.setSub = function (key, sub, value, bufferCallback, writeCallback)
{
this.channels.emit(key, {"db": this.db, "type": "setsub", "key": key, "sub": sub, "value": clone(value), "bufferCallback": bufferCallback, "writeCallback": writeCallback});
}
exports.database.prototype.remove = function (key, bufferCallback, writeCallback)
{
this.channels.emit(key, {"db": this.db, "type": "remove", "key": key, "bufferCallback": bufferCallback, "writeCallback": writeCallback});
}
function doOperation (operation, callback)
{
if(operation.type == "get")
{
operation.db.get(operation.key, function(err, value)
{
//clone the value
value = clone(value);
//call the caller callback
operation.callback(err, value);
//call the queue callback
callback();
});
}
else if(operation.type == "set")
{
operation.db.set(operation.key, operation.value, function(err)
{
//call the queue callback
callback();
//call the caller callback
if(operation.bufferCallback) operation.bufferCallback(err);
}, operation.writeCallback);
}
else if(operation.type == "getsub")
{
operation.db.getSub(operation.key, operation.sub, function(err, value)
{
//clone the value
value = clone(value);
//call the caller callback
operation.callback(err, value);
//call the queue callback
callback();
});
}
else if(operation.type == "setsub")
{
operation.db.setSub(operation.key, operation.sub, operation.value, function(err)
{
//call the queue callback
callback();
//call the caller callback
if(operation.bufferCallback) operation.bufferCallback(err);
}, operation.writeCallback);
}
else if(operation.type == "remove")
{
operation.db.remove(operation.key, function(err)
{
//call the queue callback
callback();
//call the caller callback
if(operation.bufferCallback) operation.bufferCallback(err);
}, operation.writeCallback);
}
}
exports.database.prototype.close = function(callback)
{
this.db.close(callback);
}
function clone(obj)
{
// Handle the 3 simple types, and null or undefined
if (null == obj || "object" != typeof obj) return obj;
// Handle Date
if (obj instanceof Date)
{
var copy = new Date();
copy.setTime(obj.getTime());
return copy;
}
// Handle Array
if (obj instanceof Array)
{
var copy = [];
for (var i = 0, len = obj.length; i < len; ++i)
{
copy[i] = clone(obj[i]);
}
return copy;
}
// Handle Object
if (obj instanceof Object)
{
var copy = {};
for (var attr in obj)
{
if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
}
return copy;
}
throw new Error("Unable to copy obj! Its type isn't supported.");
}