ueberdb2 4.1.8 → 4.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/databases/cassandra_db.js +233 -235
- package/dist/databases/couch_db.js +171 -173
- package/dist/databases/dirty_db.js +73 -76
- package/dist/databases/dirty_git_db.js +57 -75
- package/dist/databases/elasticsearch_db.js +241 -267
- package/dist/databases/memory_db.js +35 -37
- package/dist/databases/mock_db.js +36 -38
- package/dist/databases/mongodb_db.js +131 -133
- package/dist/databases/mssql_db.js +183 -185
- package/dist/databases/mysql_db.js +166 -168
- package/dist/databases/postgres_db.js +188 -190
- package/dist/databases/postgrespool_db.js +10 -10
- package/dist/databases/redis_db.js +118 -120
- package/dist/databases/rethink_db.js +119 -121
- package/dist/databases/sqlite_db.js +135 -137
- package/dist/index.js +195 -213
- package/lib/AbstractDatabase.ts +79 -0
- package/lib/CacheAndBufferLayer.ts +665 -0
- package/lib/logging.ts +32 -0
- package/package.json +16 -12
- package/dist/lib/AbstractDatabase.js +0 -38
- package/dist/lib/CacheAndBufferLayer.js +0 -657
- package/dist/lib/logging.js +0 -34
- package/dist/test/lib/databases.js +0 -74
- package/dist/test/test.js +0 -327
- package/dist/test/test_bulk.js +0 -74
- package/dist/test/test_elasticsearch.js +0 -157
- package/dist/test/test_findKeys.js +0 -69
- package/dist/test/test_flush.js +0 -83
- package/dist/test/test_getSub.js +0 -57
- package/dist/test/test_lru.js +0 -155
- package/dist/test/test_memory.js +0 -59
- package/dist/test/test_metrics.js +0 -772
- package/dist/test/test_mysql.js +0 -90
- package/dist/test/test_postgres.js +0 -40
- package/dist/test/test_setSub.js +0 -48
- package/dist/test/test_tojson.js +0 -62
|
@@ -1,237 +1,235 @@
|
|
|
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
|
-
* @param {String
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
if (!settings.
|
|
39
|
-
throw new Error('The Cassandra
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
this.settings =
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if
|
|
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
|
-
* @param {String}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
if (
|
|
98
|
-
return callback(
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
111
|
-
* @param {
|
|
112
|
-
* @param {String}
|
|
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
|
-
if (
|
|
148
|
-
return callback(
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
*
|
|
168
|
-
*
|
|
169
|
-
* @param {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
*
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
close(callback) {
|
|
234
|
-
this.pool.shutdown(callback);
|
|
235
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
+
var cassandraDriver = require('cassandra-driver');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const Database = class Cassandra_db extends AbstractDatabase {
|
|
20
|
+
client;
|
|
21
|
+
pool;
|
|
22
|
+
/**
|
|
23
|
+
* @param {Object} settings The required settings object to initiate the Cassandra database
|
|
24
|
+
* @param {String[]} settings.clientOptions See
|
|
25
|
+
* http://www.datastax.com/drivers/nodejs/2.0/global.html#ClientOptions for a full set of
|
|
26
|
+
* options that can be used
|
|
27
|
+
* @param {String} settings.columnFamily The column family that should be used to store data. The
|
|
28
|
+
* column family will be created if it doesn't exist
|
|
29
|
+
* @param {Function} [settings.logger] Function that will be used to pass on log events emitted by
|
|
30
|
+
* the Cassandra driver. See https://github.com/datastax/nodejs-driver#logging for more
|
|
31
|
+
* information
|
|
32
|
+
*/
|
|
33
|
+
constructor(settings) {
|
|
34
|
+
super();
|
|
35
|
+
if (!settings.clientOptions) {
|
|
36
|
+
throw new Error('The Cassandra client options should be defined');
|
|
37
|
+
}
|
|
38
|
+
if (!settings.columnFamily) {
|
|
39
|
+
throw new Error('The Cassandra column family should be defined');
|
|
40
|
+
}
|
|
41
|
+
this.settings = { database: settings.database };
|
|
42
|
+
this.settings.clientOptions = settings.clientOptions;
|
|
43
|
+
this.settings.columnFamily = settings.columnFamily;
|
|
44
|
+
this.settings.logger = settings.logger;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Initializes the Cassandra client, connects to Cassandra and creates the CF if it didn't exist
|
|
48
|
+
* already
|
|
49
|
+
*
|
|
50
|
+
* @param {Function} callback Standard callback method.
|
|
51
|
+
* @param {Error} callback.err An error object (if any.)
|
|
52
|
+
*/
|
|
53
|
+
init(callback) {
|
|
54
|
+
// Create a client
|
|
55
|
+
this.client = new cassandraDriver.Client(this.settings.clientOptions);
|
|
56
|
+
// Pass on log messages if a logger has been configured
|
|
57
|
+
if (this.settings.logger) {
|
|
58
|
+
this.client.on('log', this.settings.logger);
|
|
59
|
+
}
|
|
60
|
+
// Check whether our column family already exists and create it if necessary
|
|
61
|
+
this.client.execute('SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = ?', [this.settings.clientOptions.keyspace], (err, result) => {
|
|
62
|
+
if (err) {
|
|
63
|
+
return callback(err);
|
|
64
|
+
}
|
|
65
|
+
let isDefined = false;
|
|
66
|
+
const length = result.rows.length;
|
|
67
|
+
for (let i = 0; i < length; i++) {
|
|
68
|
+
if (result.rows[i].columnfamily_name === this.settings.columnFamily) {
|
|
69
|
+
isDefined = true;
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (isDefined) {
|
|
74
|
+
return callback(null);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
const cql = `CREATE COLUMNFAMILY "${this.settings.columnFamily}" ` +
|
|
78
|
+
'(key text PRIMARY KEY, data text)';
|
|
79
|
+
this.client && this.client.execute(cql, callback);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Gets a value from Cassandra
|
|
85
|
+
*
|
|
86
|
+
* @param {String} key The key for which the value should be retrieved
|
|
87
|
+
* @param {Function} callback Standard callback method
|
|
88
|
+
* @param {Error} callback.err An error object, if any
|
|
89
|
+
* @param {String} callback.value The value for the given key (if any)
|
|
90
|
+
*/
|
|
91
|
+
get(key, callback) {
|
|
92
|
+
const cql = `SELECT data FROM "${this.settings.columnFamily}" WHERE key = ?`;
|
|
93
|
+
this.client && this.client.execute(cql, [key], (err, result) => {
|
|
94
|
+
if (err) {
|
|
95
|
+
return callback(err);
|
|
96
|
+
}
|
|
97
|
+
if (!result.rows || result.rows.length === 0) {
|
|
98
|
+
return callback(null, null);
|
|
99
|
+
}
|
|
100
|
+
return callback(null, result.rows[0].data);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Cassandra has no native `findKeys` method. This function implements a naive filter by
|
|
105
|
+
* retrieving *all* the keys and filtering those. This should obviously be used with the utmost
|
|
106
|
+
* care and is probably not something you want to run in production.
|
|
107
|
+
*
|
|
108
|
+
* @param {String} key The filter for keys that should match
|
|
109
|
+
* @param {String} [notKey] The filter for keys that shouldn't match
|
|
110
|
+
* @param {Function} callback Standard callback method
|
|
111
|
+
* @param {Error} callback.err An error object, if any
|
|
112
|
+
* @param {String[]} callback.keys An array of keys that match the specified filters
|
|
113
|
+
*/
|
|
114
|
+
findKeys(key, notKey, callback) {
|
|
115
|
+
let cql = null;
|
|
116
|
+
if (!notKey) {
|
|
117
|
+
// Get all the keys
|
|
118
|
+
cql = `SELECT key FROM "${this.settings.columnFamily}"`;
|
|
119
|
+
this.client && this.client.execute(cql, (err, result) => {
|
|
120
|
+
if (err) {
|
|
121
|
+
return callback(err);
|
|
122
|
+
}
|
|
123
|
+
// Construct a regular expression based on the given key
|
|
124
|
+
const regex = new RegExp(`^${key.replace(/\*/g, '.*')}$`);
|
|
125
|
+
const keys = [];
|
|
126
|
+
result.rows.forEach((row) => {
|
|
127
|
+
if (regex.test(row.key)) {
|
|
128
|
+
keys.push(row.key);
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
return callback(null, keys);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
else if (notKey === '*:*:*') {
|
|
135
|
+
// restrict key to format 'text:*'
|
|
136
|
+
const matches = /^([^:]+):\*$/.exec(key);
|
|
137
|
+
if (matches) {
|
|
138
|
+
// Get the 'text' bit out of the key and get all those keys from a special column.
|
|
139
|
+
// We can retrieve them from this column as we're duplicating them on .set/.remove
|
|
140
|
+
cql = `SELECT * from "${this.settings.columnFamily}" WHERE key = ?`;
|
|
141
|
+
this.client &&
|
|
142
|
+
this.client
|
|
143
|
+
.execute(cql, [`ueberdb:keys:${matches[1]}`], (err, result) => {
|
|
144
|
+
if (err) {
|
|
145
|
+
return callback(err);
|
|
146
|
+
}
|
|
147
|
+
if (!result.rows || result.rows.length === 0) {
|
|
148
|
+
return callback(null, []);
|
|
149
|
+
}
|
|
150
|
+
const keys = result.rows.map((row) => row.data);
|
|
151
|
+
return callback(null, keys);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
const msg = 'Cassandra db only supports key patterns like pad:* when notKey is set to *:*:*';
|
|
156
|
+
return callback(new Error(msg), null);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
return callback(new Error('Cassandra db currently only supports *:*:* as notKey'), null);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Sets a value for a key
|
|
165
|
+
*
|
|
166
|
+
* @param {String} key The key to set
|
|
167
|
+
* @param {String} value The value associated to this key
|
|
168
|
+
* @param {Function} callback Standard callback method
|
|
169
|
+
* @param {Error} callback.err An error object, if any
|
|
170
|
+
*/
|
|
171
|
+
set(key, value, callback) {
|
|
172
|
+
this.doBulk([{ type: 'set', key, value }], callback);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Removes a key and it's value from the column family
|
|
176
|
+
*
|
|
177
|
+
* @param {String} key The key to remove
|
|
178
|
+
* @param {Function} callback Standard callback method
|
|
179
|
+
* @param {Error} callback.err An error object, if any
|
|
180
|
+
*/
|
|
181
|
+
remove(key, callback) {
|
|
182
|
+
this.doBulk([{ type: 'remove', key }], callback);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Performs multiple operations in one action
|
|
186
|
+
*
|
|
187
|
+
* @param {Object[]} bulk The set of operations that should be performed
|
|
188
|
+
* @param {Function} callback Standard callback method
|
|
189
|
+
* @param {Error} callback.err An error object, if any
|
|
190
|
+
*/
|
|
191
|
+
doBulk(bulk, callback) {
|
|
192
|
+
const queries = [];
|
|
193
|
+
bulk.forEach((operation) => {
|
|
194
|
+
// We support finding keys of the form `test:*`. If anything matches, we will try and save
|
|
195
|
+
// this
|
|
196
|
+
const matches = /^([^:]+):([^:]+)$/.exec(operation.key);
|
|
197
|
+
if (operation.type === 'set') {
|
|
198
|
+
queries.push({
|
|
199
|
+
query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
|
|
200
|
+
params: [operation.value, operation.key],
|
|
201
|
+
});
|
|
202
|
+
if (matches) {
|
|
203
|
+
queries.push({
|
|
204
|
+
query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
|
|
205
|
+
params: ['1', `ueberdb:keys:${matches[1]}`],
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else if (operation.type === 'remove') {
|
|
210
|
+
queries.push({
|
|
211
|
+
query: `DELETE FROM "${this.settings.columnFamily}" WHERE key=?`,
|
|
212
|
+
params: [operation.key],
|
|
213
|
+
});
|
|
214
|
+
if (matches) {
|
|
215
|
+
queries.push({
|
|
216
|
+
query: `DELETE FROM "${this.settings.columnFamily}" WHERE key = ?`,
|
|
217
|
+
params: [`ueberdb:keys:${matches[1]}`],
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
this.client && this.client.batch(queries, { prepare: true }, callback);
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Closes the Cassandra connection
|
|
226
|
+
*
|
|
227
|
+
* @param {Function} callback Standard callback method
|
|
228
|
+
* @param {Error} callback.err Error object in case something goes wrong
|
|
229
|
+
*/
|
|
230
|
+
close(callback) {
|
|
231
|
+
this.pool.shutdown(callback);
|
|
232
|
+
}
|
|
236
233
|
};
|
|
234
|
+
|
|
237
235
|
exports.Database = Database;
|