ueberdb2 1.4.16
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/.github/workflows/npmpublish.yml +103 -0
- package/.travis.yml +46 -0
- package/CHANGELOG.md +167 -0
- package/CONTRIBUTING.md +103 -0
- package/LICENSE +202 -0
- package/README.md +356 -0
- package/SECURITY.md +5 -0
- package/databases/cassandra_db.js +250 -0
- package/databases/couch_db.js +201 -0
- package/databases/dirty_db.js +80 -0
- package/databases/dirty_git_db.js +78 -0
- package/databases/elasticsearch_db.js +288 -0
- package/databases/mock_db.js +42 -0
- package/databases/mongodb_db.js +136 -0
- package/databases/mssql_db.js +218 -0
- package/databases/mysql_db.js +178 -0
- package/databases/postgres_db.js +198 -0
- package/databases/postgrespool_db.js +11 -0
- package/databases/redis_db.js +128 -0
- package/databases/rethink_db.js +98 -0
- package/databases/sqlite_db.js +158 -0
- package/index.js +191 -0
- package/lib/AbstractDatabase.js +32 -0
- package/lib/CacheAndBufferLayer.js +610 -0
- package/package.json +122 -0
- package/test/lib/databases.js +62 -0
- package/test/lib/mysql.sql +84 -0
- package/test/test.js +312 -0
- package/test/test_bulk.js +71 -0
- package/test/test_lru.js +145 -0
- package/test/test_metrics.js +733 -0
- package/test/test_mysql.js +68 -0
- package/test/test_postgres.js +17 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 2011 Peter 'Pita' Martischka
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
19
|
+
const async = require('async');
|
|
20
|
+
const pg = require('pg');
|
|
21
|
+
|
|
22
|
+
exports.Database = class extends AbstractDatabase {
|
|
23
|
+
constructor(settings) {
|
|
24
|
+
super();
|
|
25
|
+
if (typeof settings === 'string') settings = {connectionString: settings};
|
|
26
|
+
this.settings = settings;
|
|
27
|
+
|
|
28
|
+
this.settings.cache = settings.cache || 1000;
|
|
29
|
+
this.settings.writeInterval = 100;
|
|
30
|
+
this.settings.json = true;
|
|
31
|
+
|
|
32
|
+
// Pool specific defaults
|
|
33
|
+
this.settings.max = this.settings.max || 20;
|
|
34
|
+
this.settings.min = this.settings.min || 4;
|
|
35
|
+
this.settings.idleTimeoutMillis = this.settings.idleTimeoutMillis || 1000;
|
|
36
|
+
|
|
37
|
+
this.db = new pg.Pool(this.settings);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
init(callback) {
|
|
41
|
+
const testTableExists = "SELECT 1 as exists FROM pg_tables WHERE tablename = 'store'";
|
|
42
|
+
|
|
43
|
+
const createTable = 'CREATE TABLE IF NOT EXISTS store (' +
|
|
44
|
+
'"key" character varying(100) NOT NULL, ' +
|
|
45
|
+
'"value" text NOT NULL, ' +
|
|
46
|
+
'CONSTRAINT store_pkey PRIMARY KEY (key))';
|
|
47
|
+
|
|
48
|
+
// this variable will be given a value depending on the result of the
|
|
49
|
+
// feature detection
|
|
50
|
+
this.upsertStatement = null;
|
|
51
|
+
|
|
52
|
+
/*
|
|
53
|
+
* - Detects if this Postgres version supports INSERT .. ON CONFLICT
|
|
54
|
+
* UPDATE (PostgreSQL >= 9.5 and CockroachDB)
|
|
55
|
+
* - If upsert is not supported natively, creates in the DB a pl/pgsql
|
|
56
|
+
* function that emulates it
|
|
57
|
+
* - Performs a side effect, setting this.upsertStatement to the sql
|
|
58
|
+
* statement that needs to be used, based on the detection result
|
|
59
|
+
* - calls the callback
|
|
60
|
+
*/
|
|
61
|
+
const detectUpsertMethod = (callback) => {
|
|
62
|
+
const upsertViaFunction = 'SELECT ueberdb_insert_or_update($1,$2)';
|
|
63
|
+
const upsertNatively =
|
|
64
|
+
'INSERT INTO store(key, value) VALUES ($1, $2) ' +
|
|
65
|
+
'ON CONFLICT (key) DO UPDATE SET value = excluded.value';
|
|
66
|
+
const createFunc =
|
|
67
|
+
'CREATE OR REPLACE FUNCTION ueberdb_insert_or_update(character varying, text) ' +
|
|
68
|
+
'RETURNS void AS $$ ' +
|
|
69
|
+
'BEGIN ' +
|
|
70
|
+
' IF EXISTS( SELECT * FROM store WHERE key = $1 ) THEN ' +
|
|
71
|
+
' UPDATE store SET value = $2 WHERE key = $1; ' +
|
|
72
|
+
' ELSE ' +
|
|
73
|
+
' INSERT INTO store(key,value) VALUES( $1, $2 ); ' +
|
|
74
|
+
' END IF; ' +
|
|
75
|
+
' RETURN; ' +
|
|
76
|
+
'END; ' +
|
|
77
|
+
'$$ LANGUAGE plpgsql;';
|
|
78
|
+
|
|
79
|
+
const testNativeUpsert = `EXPLAIN ${upsertNatively}`;
|
|
80
|
+
|
|
81
|
+
this.db.query(testNativeUpsert, ['test-key', 'test-value'], (err) => {
|
|
82
|
+
if (err) {
|
|
83
|
+
// the UPSERT statement failed: we will have to emulate it via
|
|
84
|
+
// an sql function
|
|
85
|
+
this.upsertStatement = upsertViaFunction;
|
|
86
|
+
|
|
87
|
+
// actually create the emulation function
|
|
88
|
+
this.db.query(createFunc, [], callback);
|
|
89
|
+
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// if we get here, the EXPLAIN UPSERT succeeded, and we can use a
|
|
94
|
+
// native UPSERT
|
|
95
|
+
this.upsertStatement = upsertNatively;
|
|
96
|
+
callback();
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
this.db.query(testTableExists, (err, result) => {
|
|
101
|
+
if (err != null) return callback(err);
|
|
102
|
+
if (result.rows.length === 0) {
|
|
103
|
+
this.db.query(createTable, (err) => {
|
|
104
|
+
if (err != null) return callback(err);
|
|
105
|
+
detectUpsertMethod(callback);
|
|
106
|
+
});
|
|
107
|
+
} else {
|
|
108
|
+
detectUpsertMethod(callback);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
get(key, callback) {
|
|
114
|
+
this.db.query('SELECT value FROM store WHERE key=$1', [key], (err, results) => {
|
|
115
|
+
let value = null;
|
|
116
|
+
|
|
117
|
+
if (!err && results.rows.length === 1) {
|
|
118
|
+
value = results.rows[0].value;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
callback(err, value);
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
findKeys(key, notKey, callback) {
|
|
126
|
+
let query = 'SELECT key FROM store WHERE key LIKE $1';
|
|
127
|
+
const params = [];
|
|
128
|
+
// desired keys are %key:%, e.g. pad:%
|
|
129
|
+
key = key.replace(/\*/g, '%');
|
|
130
|
+
params.push(key);
|
|
131
|
+
|
|
132
|
+
if (notKey != null) {
|
|
133
|
+
// not desired keys are notKey:%, e.g. %:%:%
|
|
134
|
+
notKey = notKey.replace(/\*/g, '%');
|
|
135
|
+
query += ' AND key NOT LIKE $2';
|
|
136
|
+
params.push(notKey);
|
|
137
|
+
}
|
|
138
|
+
this.db.query(query, params, (err, results) => {
|
|
139
|
+
const value = [];
|
|
140
|
+
|
|
141
|
+
if (!err && results.rows.length > 0) {
|
|
142
|
+
results.rows.forEach((val) => {
|
|
143
|
+
value.push(val.key);
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
callback(err, value);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
set(key, value, callback) {
|
|
152
|
+
if (key.length > 100) {
|
|
153
|
+
callback('Your Key can only be 100 chars');
|
|
154
|
+
} else {
|
|
155
|
+
this.db.query(this.upsertStatement, [key, value], callback);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
remove(key, callback) {
|
|
160
|
+
this.db.query('DELETE FROM store WHERE key=$1', [key], callback);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
doBulk(bulk, callback) {
|
|
164
|
+
const replaceVALs = [];
|
|
165
|
+
let removeSQL = 'DELETE FROM store WHERE key IN (';
|
|
166
|
+
const removeVALs = [];
|
|
167
|
+
|
|
168
|
+
let removeCount = 0;
|
|
169
|
+
|
|
170
|
+
for (const i in bulk) {
|
|
171
|
+
if (bulk[i].type === 'set') {
|
|
172
|
+
replaceVALs.push([bulk[i].key, bulk[i].value]);
|
|
173
|
+
} else if (bulk[i].type === 'remove') {
|
|
174
|
+
if (removeCount !== 0) removeSQL += ',';
|
|
175
|
+
removeCount += 1;
|
|
176
|
+
|
|
177
|
+
removeSQL += `$${removeCount}`;
|
|
178
|
+
removeVALs.push(bulk[i].key);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
removeSQL += ');';
|
|
183
|
+
|
|
184
|
+
const functions = replaceVALs.map((v) => (cb) => this.db.query(this.upsertStatement, v, cb));
|
|
185
|
+
|
|
186
|
+
const removeFunction = (callback) => {
|
|
187
|
+
if (!removeVALs.length < 1) this.db.query(removeSQL, removeVALs, callback);
|
|
188
|
+
else callback();
|
|
189
|
+
};
|
|
190
|
+
functions.push(removeFunction);
|
|
191
|
+
|
|
192
|
+
async.parallel(functions, callback);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
close(callback) {
|
|
196
|
+
this.db.end(callback);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const postgres = require('./postgres_db');
|
|
4
|
+
|
|
5
|
+
exports.Database = class extends postgres.Database {
|
|
6
|
+
constructor(settings) {
|
|
7
|
+
console.warn('ueberdb: The postgrespool database driver is deprecated ' +
|
|
8
|
+
'and will be removed in a future version. Use postgres instead.');
|
|
9
|
+
super(settings);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/* eslint new-cap: ["error", {"capIsNewExceptions": ["KEYS", "SMEMBERS"]}] */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 2011 Peter 'Pita' Martischka
|
|
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
|
+
|
|
20
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
21
|
+
const async = require('async');
|
|
22
|
+
const redis = require('redis');
|
|
23
|
+
|
|
24
|
+
exports.Database = class extends AbstractDatabase {
|
|
25
|
+
constructor(settings) {
|
|
26
|
+
super();
|
|
27
|
+
this.client = null;
|
|
28
|
+
this.settings = settings || {};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
auth(callback) {
|
|
32
|
+
if (!this.settings.password) return callback();
|
|
33
|
+
this.client.auth(this.settings.password, callback);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
select(callback) {
|
|
37
|
+
if (!this.settings.database) return callback();
|
|
38
|
+
this.client.select(this.settings.database, callback);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_deprecatedInit(callback) {
|
|
42
|
+
if (this.settings.socket) {
|
|
43
|
+
// Deprecated, but kept for backwards compatibility.
|
|
44
|
+
this.client = redis.createClient(this.settings.socket,
|
|
45
|
+
this.settings.client_options);
|
|
46
|
+
} else {
|
|
47
|
+
// Deprecated, but kept for backwards compatibility.
|
|
48
|
+
this.client = redis.createClient(this.settings.port,
|
|
49
|
+
this.settings.host, this.settings.client_options);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
this.client.database = this.settings.database;
|
|
53
|
+
async.waterfall([this.auth.bind(this), this.select.bind(this)], callback);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
init(callback) {
|
|
57
|
+
if (this.settings.socket || this.settings.client_options) return this._deprecatedInit(callback);
|
|
58
|
+
this.client = redis.createClient(this.settings);
|
|
59
|
+
callback();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
get(key, callback) {
|
|
63
|
+
this.client.get(key, callback);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
findKeys(key, notKey, callback) {
|
|
67
|
+
// As redis provides only limited support for getting a list of all
|
|
68
|
+
// available keys we have to limit key and notKey here.
|
|
69
|
+
// See http://redis.io/commands/keys
|
|
70
|
+
if (notKey == null) {
|
|
71
|
+
this.client.KEYS(key, callback);
|
|
72
|
+
} else if (notKey === '*:*:*') {
|
|
73
|
+
// restrict key to format "text:*"
|
|
74
|
+
const matches = /^([^:]+):\*$/.exec(key);
|
|
75
|
+
if (matches) {
|
|
76
|
+
this.client.SMEMBERS(`ueberDB:keys:${matches[1]}`, callback);
|
|
77
|
+
} else {
|
|
78
|
+
const msg = 'redis db only supports key patterns like pad:* when notKey is set to *:*:*';
|
|
79
|
+
callback(new Error(msg), null);
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
callback(new Error('redis db currently only supports *:*:* as notKey'), null);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
set(key, value, callback) {
|
|
87
|
+
const matches = /^([^:]+):([^:]+)$/.exec(key);
|
|
88
|
+
if (matches) {
|
|
89
|
+
this.client.sadd([`ueberDB:keys:${matches[1]}`, matches[0]]);
|
|
90
|
+
}
|
|
91
|
+
this.client.set(key, value, callback);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
remove(key, callback) {
|
|
95
|
+
const matches = /^([^:]+):([^:]+)$/.exec(key);
|
|
96
|
+
if (matches) {
|
|
97
|
+
this.client.srem([`ueberDB:keys:${matches[1]}`, matches[0]]);
|
|
98
|
+
}
|
|
99
|
+
this.client.del(key, callback);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
doBulk(bulk, callback) {
|
|
103
|
+
const multi = this.client.multi();
|
|
104
|
+
|
|
105
|
+
for (const {key, type, value} of bulk) {
|
|
106
|
+
const matches = /^([^:]+):([^:]+)$/.exec(key);
|
|
107
|
+
if (type === 'set') {
|
|
108
|
+
if (matches) {
|
|
109
|
+
multi.sadd([`ueberDB:keys:${matches[1]}`, matches[0]]);
|
|
110
|
+
}
|
|
111
|
+
multi.set(key, value);
|
|
112
|
+
} else if (type === 'remove') {
|
|
113
|
+
if (matches) {
|
|
114
|
+
multi.srem([`ueberDB:keys:${matches[1]}`, matches[0]]);
|
|
115
|
+
}
|
|
116
|
+
multi.del(key);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
multi.exec(callback);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
close(callback) {
|
|
124
|
+
this.client.quit(() => {
|
|
125
|
+
callback();
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 2016 Remi Arnaud
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
19
|
+
const r = require('rethinkdb');
|
|
20
|
+
const async = require('async');
|
|
21
|
+
|
|
22
|
+
exports.Database = class extends AbstractDatabase {
|
|
23
|
+
constructor(settings) {
|
|
24
|
+
super();
|
|
25
|
+
if (!settings) settings = {};
|
|
26
|
+
if (!settings.host) { settings.host = 'localhost'; }
|
|
27
|
+
if (!settings.port) { settings.port = 28015; }
|
|
28
|
+
if (!settings.db) { settings.db = 'test'; }
|
|
29
|
+
if (!settings.table) { settings.table = 'test'; }
|
|
30
|
+
|
|
31
|
+
this.host = settings.host;
|
|
32
|
+
this.db = settings.db;
|
|
33
|
+
this.port = settings.port;
|
|
34
|
+
this.table = settings.table;
|
|
35
|
+
this.connection = null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
init(callback) {
|
|
39
|
+
r.connect(this, (err, conn) => {
|
|
40
|
+
if (err) throw err;
|
|
41
|
+
this.connection = conn;
|
|
42
|
+
|
|
43
|
+
r.table(this.table).run(this.connection, (err, cursor) => {
|
|
44
|
+
if (err) {
|
|
45
|
+
// assuming table does not exists
|
|
46
|
+
r.tableCreate(this.table).run(this.connection, callback);
|
|
47
|
+
} else if (callback) { callback(null, cursor); }
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get(key, callback) {
|
|
53
|
+
r.table(this.table).get(key).run(this.connection, (err, item) => {
|
|
54
|
+
callback(err, (item ? item.content : item));
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
findKeys(key, notKey, callback) {
|
|
59
|
+
const keys = [];
|
|
60
|
+
const regex = this.createFindRegex(key, notKey);
|
|
61
|
+
r.filter((item) => {
|
|
62
|
+
if (item.id.search(regex) !== -1) {
|
|
63
|
+
keys.push(item.id);
|
|
64
|
+
}
|
|
65
|
+
}).run(this.connection, callback);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
set(key, value, callback) {
|
|
69
|
+
r.table(this.table)
|
|
70
|
+
.insert({id: key, content: value}, {conflict: 'replace'})
|
|
71
|
+
.run(this.connection, callback);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
doBulk(bulk, callback) {
|
|
75
|
+
const _in = [];
|
|
76
|
+
const _out = [];
|
|
77
|
+
|
|
78
|
+
for (const i in bulk) {
|
|
79
|
+
if (bulk[i].type === 'set') {
|
|
80
|
+
_in.push({id: bulk[i].key, content: bulk[i].value});
|
|
81
|
+
} else if (bulk[i].type === 'remove') {
|
|
82
|
+
_out.push(bulk[i].key);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async.parallel([
|
|
86
|
+
(cb) => { r.table(this.table).insert(_in, {conflict: 'replace'}).run(this.connection, cb); },
|
|
87
|
+
(cb) => { r.table(this.table).getAll(_out).delete().run(this.connection, cb); },
|
|
88
|
+
], callback);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
remove(key, callback) {
|
|
92
|
+
r.table(this.table).get(key).delete().run(this.connection, callback);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
close(callback) {
|
|
96
|
+
this.connection.close(callback);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 2011 Peter 'Pita' Martischka
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
let sqlite3;
|
|
19
|
+
try {
|
|
20
|
+
sqlite3 = require('sqlite3');
|
|
21
|
+
} catch (err) {
|
|
22
|
+
throw new Error(
|
|
23
|
+
'sqlite3 not found. It was removed from ueberdb\'s dependencies because it requires ' +
|
|
24
|
+
'compilation which fails on several systems. If you still want to use sqlite, run ' +
|
|
25
|
+
'"npm install sqlite3" in your etherpad-lite ./src directory.');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const AbstractDatabase = require('../lib/AbstractDatabase');
|
|
29
|
+
const util = require('util');
|
|
30
|
+
|
|
31
|
+
const escape = (val) => `'${val.replace(/'/g, "''")}'`;
|
|
32
|
+
|
|
33
|
+
exports.Database = class extends AbstractDatabase {
|
|
34
|
+
constructor(settings) {
|
|
35
|
+
super();
|
|
36
|
+
this.db = null;
|
|
37
|
+
|
|
38
|
+
if (!settings || !settings.filename) {
|
|
39
|
+
settings = {filename: ':memory:'};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
this.settings = settings;
|
|
43
|
+
|
|
44
|
+
// set settings for the dbWrapper
|
|
45
|
+
if (settings.filename === ':memory:') {
|
|
46
|
+
this.settings.cache = 0;
|
|
47
|
+
this.settings.writeInterval = 0;
|
|
48
|
+
this.settings.json = true;
|
|
49
|
+
} else {
|
|
50
|
+
this.settings.cache = 1000;
|
|
51
|
+
this.settings.writeInterval = 100;
|
|
52
|
+
this.settings.json = true;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _query(sql, params = []) {
|
|
57
|
+
// It is unclear how util.promisify() deals with variadic functions, so it is not used here.
|
|
58
|
+
return await new Promise((resolve, reject) => {
|
|
59
|
+
// According to sqlite3's documentation, .run() method (and maybe .all() and .get(); the
|
|
60
|
+
// documentation is unclear) might call the callback multiple times. That's OK -- ECMAScript
|
|
61
|
+
// guarantees that it is safe to call a Promise executor's resolve and reject functions
|
|
62
|
+
// multiple times. The subsequent calls are ignored, except Node.js's 'process' object emits a
|
|
63
|
+
// 'multipleResolves' event to aid in debugging.
|
|
64
|
+
this.db.all(sql, params, (err, rows) => {
|
|
65
|
+
if (err != null) return reject(err);
|
|
66
|
+
resolve(rows);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Temporary callbackified version of _query. This will be removed once all database objects are
|
|
72
|
+
// asyncified.
|
|
73
|
+
_queryCb(sql, params, callback) {
|
|
74
|
+
// It is unclear how util.callbackify() handles optional parameters, so it is not used here.
|
|
75
|
+
const p = this._query(sql, params);
|
|
76
|
+
if (callback) p.then((rows) => callback(null, rows), (err) => callback(err || new Error(err)));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
init(callback) {
|
|
80
|
+
util.callbackify(async () => {
|
|
81
|
+
this.db = await new Promise((resolve, reject) => {
|
|
82
|
+
new sqlite3.Database(this.settings.filename, function (err) {
|
|
83
|
+
if (err != null) return reject(err);
|
|
84
|
+
// The use of `this` relies on an undocumented feature of sqlite3:
|
|
85
|
+
// https://github.com/mapbox/node-sqlite3/issues/1408
|
|
86
|
+
resolve(this);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
await this._query('CREATE TABLE IF NOT EXISTS store (key TEXT PRIMARY KEY, value TEXT)');
|
|
90
|
+
})(callback);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get(key, callback) {
|
|
94
|
+
this._queryCb(
|
|
95
|
+
'SELECT value FROM store WHERE key = ?', [key],
|
|
96
|
+
(err, rows) => callback(err, err == null && rows && rows.length ? rows[0].value : null));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
findKeys(key, notKey, callback) {
|
|
100
|
+
let query = 'SELECT key FROM store WHERE key LIKE ?';
|
|
101
|
+
const params = [];
|
|
102
|
+
// desired keys are %key:%, e.g. pad:%
|
|
103
|
+
key = key.replace(/\*/g, '%');
|
|
104
|
+
params.push(key);
|
|
105
|
+
|
|
106
|
+
if (notKey != null) {
|
|
107
|
+
// not desired keys are notKey:%, e.g. %:%:%
|
|
108
|
+
notKey = notKey.replace(/\*/g, '%');
|
|
109
|
+
query += ' AND key NOT LIKE ?';
|
|
110
|
+
params.push(notKey);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
this._queryCb(query, params, (err, results) => {
|
|
114
|
+
const value = [];
|
|
115
|
+
|
|
116
|
+
if (!err && Object.keys(results).length > 0) {
|
|
117
|
+
results.forEach((val) => {
|
|
118
|
+
value.push(val.key);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
callback(err, value);
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
set(key, value, callback) {
|
|
127
|
+
this._queryCb('REPLACE INTO store VALUES (?,?)', [key, value], callback);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
remove(key, callback) {
|
|
131
|
+
this._queryCb('DELETE FROM store WHERE key = ?', [key], callback);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
doBulk(bulk, callback) {
|
|
135
|
+
let sql = 'BEGIN TRANSACTION;\n';
|
|
136
|
+
for (const i in bulk) {
|
|
137
|
+
if (bulk[i].type === 'set') {
|
|
138
|
+
sql += `REPLACE INTO store VALUES (${escape(bulk[i].key)}, ${escape(bulk[i].value)});\n`;
|
|
139
|
+
} else if (bulk[i].type === 'remove') {
|
|
140
|
+
sql += `DELETE FROM store WHERE key = ${escape(bulk[i].key)};\n`;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
sql += 'END TRANSACTION;';
|
|
144
|
+
|
|
145
|
+
this.db.exec(sql, (err) => {
|
|
146
|
+
if (err) {
|
|
147
|
+
console.error('ERROR WITH SQL: ');
|
|
148
|
+
console.error(sql);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
callback(err);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
close(callback) {
|
|
156
|
+
this.db.close(callback);
|
|
157
|
+
}
|
|
158
|
+
};
|