Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 46 additions & 70 deletions databases/mongodb_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
import AbstractDatabase, {type Settings} from '../lib/AbstractDatabase';
import type {BulkObject} from './cassandra_db';
import {MongoClient} from 'mongodb';
import type {Collection, Db} from 'mongodb';
import type {Collection, Db, Filter} from 'mongodb';

// Document shape stored in the ueberdb collection. _id is the user-provided string key,
// not the default ObjectId, so mongodb's generic types need this narrowing.
type UeberDoc = {_id: string; value: string};

export default class extends AbstractDatabase {
public interval: NodeJS.Timer | undefined;
public database: Db|undefined;
public client: MongoClient|undefined;
public collection: Collection|undefined;
constructor(settings:Settings) {
public database: Db | undefined;
public client: MongoClient | undefined;
public collection: Collection | undefined;
constructor(settings: Settings) {
super(settings);
this.settings = settings;

Expand All @@ -35,27 +38,11 @@ export default class extends AbstractDatabase {
if (!this.settings.collection) this.settings.collection = 'ueberdb';
}

clearPing() {
if (this.interval) {
clearInterval(this.interval[Symbol.toPrimitive]());
}
}

schedulePing() {
this.clearPing();
this.interval = setInterval(() => {
this.database!.command({
ping: 1,
});
}, 10000);
}

init(callback:Function) {

MongoClient.connect(this.settings.url!).then((v)=>{
init(callback: Function) {
MongoClient.connect(this.settings.url!)
.then((v) => {
this.client = v;
this.database = v.db(this.settings.database);
this.schedulePing();
this.collection = this.database.collection(this.settings.collection!);
callback(null);
})
Expand All @@ -68,39 +55,32 @@ export default class extends AbstractDatabase {

get(key:string, callback:Function) {
// @ts-ignore
this.collection!.findOne({_id: key})
.then((v)=>{
callback(null, v&&v.value);
}).catch(v=> {
console.log(v)
callback(v);
})

this.schedulePing();
this.collection!.findOne({ _id: key })
.then((v) => {
callback(null, v && v.value);
})
.catch((v) => {
console.log(v);
callback(v);
});
}

findKeys(key:string, notKey:string, callback:Function) {
const selector = {
$and: [
{_id: {$regex: `${key.replace(/\*/g, '')}`}},
],
findKeys(key: string, notKey: string, callback: Function) {
const escape = (s: string) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
const selector: Filter<UeberDoc> = {
$and: [{ _id: { $regex: `^${escape(key)}$` } }],
};

if (notKey) {
// @ts-ignore
selector.$and.push({_id: {$not: {$regex: `${notKey.replace(/\*/g, '')}`}}});
selector.$and!.push({ _id: { $not: { $regex: `^${escape(notKey)}$` } } });
}

// @ts-ignore
this.collection!.find(selector).map((i: any) => i._id)
.toArray()
.then(r =>{
callback(null, r);
})
.catch(v=>callback(v));


this.schedulePing();
(this.collection as Collection<UeberDoc> | undefined)!
.find(selector)
.map((i) => i._id)
.toArray()
.then((r) => callback(null, r))
.catch((v) => callback(v));
}

set(key:string, value:string, callback:Function) {
Expand All @@ -112,21 +92,17 @@ export default class extends AbstractDatabase {
.then(()=>callback(null))
.catch(v=>callback(v));
}

this.schedulePing();
}

remove(key:string, callback:Function) {
// @ts-ignore
this.collection!.deleteOne({_id: key}, )
.then(r =>callback(null,r) )
.catch(v=>callback(v));

this.schedulePing();
this.collection!.deleteOne({ _id: key })
.then((r) => callback(null, r))
.catch((v) => callback(v));
}

doBulk(bulk:BulkObject[], callback:Function) {
const bulkMongo = this.collection!.initializeOrderedBulkOp();
doBulk(bulk: BulkObject[], callback: Function) {
const bulkMongo = this.collection!.initializeUnorderedBulkOp();

for (const i in bulk) {
if (bulk[i].type === 'set') {
Expand All @@ -136,17 +112,17 @@ export default class extends AbstractDatabase {
}
}

bulkMongo.execute().then((res:any) => {
callback(null, res);
}).catch((error:any) => {
callback(error);
});

this.schedulePing();
bulkMongo
.execute()
.then((res: any) => {
callback(null, res);
})
.catch((error: any) => {
callback(error);
});
}

close(callback:any) {
this.clearPing();
this.client!.close().then(r =>callback(r));
close(callback: any) {
this.client!.close().then((r) => callback(r));
}
}
101 changes: 62 additions & 39 deletions databases/postgres_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ export default class extends AbstractDatabase {
});
}

get(key:string, callback: (err: Error | null, value: any)=>{}) {
this.db.query('SELECT value FROM store WHERE key=$1', [key], (err, results) => {
let value = null;
get(key: string, callback: (err: Error | null, value: any) => {}) {
this.db.query(
{ name: 'ueberdb_get', text: 'SELECT value FROM store WHERE key=$1', values: [key] },
(err, results) => {
let value = null;

if (!err && results.rows.length === 1) {
value = results.rows[0].value;
}

if (!err && results.rows.length === 1) {
value = results.rows[0].value;
callback(err, value);
}

callback(err, value);
});
);
}

findKeys(key:string, notKey:string, callback: (err: Error | null, value: any)=>{}) {
Expand Down Expand Up @@ -187,50 +190,70 @@ export default class extends AbstractDatabase {
const val = '' as any;
callback(Error('Your Key can only be 100 chars'), val);
} else if (this.upsertStatement != null) {
this.db.query(this.upsertStatement, [key, value], callback);
const name = this.upsertStatement.startsWith('INSERT INTO store(key, value) VALUES')
? 'ueberdb_set_native'
: 'ueberdb_set_function';
this.db.query({ name, text: this.upsertStatement, values: [key, value] }, callback);
}
}

remove(key:string, callback:()=>{}) {
this.db.query('DELETE FROM store WHERE key=$1', [key], callback);
remove(key: string, callback: () => {}) {
this.db.query(
{ name: 'ueberdb_remove', text: 'DELETE FROM store WHERE key=$1', values: [key] },
callback
);
}

doBulk(bulk:BulkObject[], callback:()=>{}) {
const replaceVALs = [];
let removeSQL = 'DELETE FROM store WHERE key IN (';
const removeVALs: string[] = [];
doBulk(bulk: BulkObject[], callback: () => {}) {
if (!this.upsertStatement) {
return;
}

let removeCount = 0;
const setOps: Array<[string, string]> = [];
const removeKeys: string[] = [];

for (const i in bulk) {
if (bulk[i].type === 'set') {
replaceVALs.push([bulk[i].key, bulk[i].value]);
} else if (bulk[i].type === 'remove') {
if (removeCount !== 0) removeSQL += ',';
removeCount += 1;
for (const op of bulk) {
if (op.type === 'set') setOps.push([op.key, op.value!]);
else if (op.type === 'remove') removeKeys.push(op.key);
}

removeSQL += `$${removeCount}`;
removeVALs.push(bulk[i].key);
const isNativeUpsert = this.upsertStatement.startsWith('INSERT INTO store(key, value) VALUES');
// async.parallel expects (err?: Error | null) on its callbacks; pg.query callbacks supply
// (err: Error). Wrap each query so the error type assigns cleanly without an `any` cast.
type AsyncTaskCb = (err?: Error | null) => void;
const tasks: Array<(cb: AsyncTaskCb) => void> = [];

if (setOps.length > 0) {
if (isNativeUpsert && setOps.length > 1) {
// Build a single multi-row VALUES list with positional params.
const valuesSql: string[] = [];
const params: string[] = [];
let i = 1;
for (const [k, v] of setOps) {
valuesSql.push(`($${i++},$${i++})`);
params.push(k, v);
}
const sql =
`INSERT INTO store(key, value) VALUES ${valuesSql.join(',')} ` +
`ON CONFLICT (key) DO UPDATE SET value = excluded.value`;
tasks.push((cb) => { this.db.query(sql, params, (err) => cb(err)); });
} else {
// Fallback: per-row via the existing upsertStatement (function-based, or single-row native).
for (const [k, v] of setOps) {
tasks.push((cb) => {
this.db.query(this.upsertStatement as string, [k, v], (err) => cb(err));
});
}
}
}

removeSQL += ');';

if (!this.upsertStatement) {
return;
if (removeKeys.length > 0) {
const placeholders = removeKeys.map((_, idx) => `$${idx + 1}`).join(',');
const sql = `DELETE FROM store WHERE key IN (${placeholders})`;
tasks.push((cb) => { this.db.query(sql, removeKeys, (err) => cb(err)); });
}


const functions:any = replaceVALs.map((v) => (cb:()=>{}) => this.db.query(this.upsertStatement as string, v as string[], cb));

const removeFunction = (callback: ()=>{}) => {
if (!(removeVALs.length < 1)) {
this.db.query(removeSQL, removeVALs, callback);
} else { callback(); }
};
functions.push(removeFunction);

async.parallel(functions, callback);
async.parallel(tasks, callback);
}

close(callback:()=>{}) {
Expand Down
Loading