2016-01-05 01:10:31 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2016-01-05 14:20:13 +01:00
|
|
|
(C) 2016 GNUnet e.V.
|
2016-01-05 01:10:31 +01:00
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
/// <reference path="../decl/urijs/URIjs.d.ts" />
|
2016-01-05 01:10:31 +01:00
|
|
|
|
|
|
|
|
2016-01-05 14:20:13 +01:00
|
|
|
/**
|
|
|
|
* Database query abstractions.
|
|
|
|
* @module Query
|
|
|
|
* @author Florian Dold
|
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
2016-01-05 01:10:31 +01:00
|
|
|
|
2016-01-05 17:11:01 +01:00
|
|
|
|
2016-01-10 20:07:42 +01:00
|
|
|
export function Query(db) {
|
2016-01-05 01:10:31 +01:00
|
|
|
return new QueryRoot(db);
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
/**
|
|
|
|
* Stream that can be filtered, reduced or joined
|
|
|
|
* with indices.
|
|
|
|
*/
|
|
|
|
export interface QueryStream<T> {
|
|
|
|
indexJoin<S>(storeName: string,
|
|
|
|
indexName: string,
|
|
|
|
keyFn: (obj: any) => any): QueryStream<[T,S]>;
|
|
|
|
filter(f: (any) => boolean): QueryStream<T>;
|
|
|
|
reduce<S>(f: (S, T) => S, acc?: S): Promise<S>;
|
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an unresolved promise together with its extracted resolve / reject
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* @returns {{resolve: any, reject: any, promise: Promise<T>}}
|
|
|
|
*/
|
|
|
|
function openPromise<T>() {
|
|
|
|
let resolve, reject;
|
|
|
|
const promise = new Promise<T>((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
|
|
|
});
|
|
|
|
return {resolve, reject, promise};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
abstract class QueryStreamBase<T> implements QueryStream<T> {
|
|
|
|
abstract subscribe(f: (isDone: boolean,
|
|
|
|
value: any,
|
|
|
|
tx: IDBTransaction) => void);
|
2016-01-05 17:11:01 +01:00
|
|
|
|
2016-01-05 14:20:13 +01:00
|
|
|
root: QueryRoot;
|
|
|
|
|
|
|
|
constructor(root: QueryRoot) {
|
|
|
|
this.root = root;
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
indexJoin<S>(storeName: string,
|
|
|
|
indexName: string,
|
|
|
|
key: any): QueryStream<[T,S]> {
|
|
|
|
this.root.addWork(null, storeName, false);
|
2016-01-05 14:20:13 +01:00
|
|
|
return new QueryStreamIndexJoin(this, storeName, indexName, key);
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
filter(f: (any) => boolean): QueryStream<T> {
|
2016-01-05 14:20:13 +01:00
|
|
|
return new QueryStreamFilter(this, f);
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
|
|
|
|
reduce(f, acc?): Promise<any> {
|
2016-01-05 01:10:31 +01:00
|
|
|
let leakedResolve;
|
|
|
|
let p = new Promise((resolve, reject) => {
|
|
|
|
leakedResolve = resolve;
|
|
|
|
});
|
|
|
|
|
2016-01-05 14:20:13 +01:00
|
|
|
this.subscribe((isDone, value) => {
|
|
|
|
if (isDone) {
|
|
|
|
leakedResolve(acc);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
acc = f(value, acc);
|
|
|
|
});
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => this.root.finish())
|
|
|
|
.then(() => p);
|
2016-01-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
class QueryStreamFilter<T> extends QueryStreamBase<T> {
|
|
|
|
s: QueryStreamBase<T>;
|
2016-01-05 14:20:13 +01:00
|
|
|
filterFn;
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
constructor(s: QueryStreamBase<T>, filterFn) {
|
2016-01-05 14:20:13 +01:00
|
|
|
super(s.root);
|
|
|
|
this.s = s;
|
|
|
|
this.filterFn = filterFn;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(f) {
|
2016-01-11 02:56:32 +01:00
|
|
|
this.s.subscribe((isDone, value, tx) => {
|
2016-01-05 14:20:13 +01:00
|
|
|
if (isDone) {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(true, undefined, tx);
|
2016-01-05 14:20:13 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.filterFn(value)) {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(false, value, tx)
|
2016-01-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
class QueryStreamIndexJoin<T> extends QueryStreamBase<T> {
|
|
|
|
s: QueryStreamBase<T>;
|
2016-01-05 14:20:13 +01:00
|
|
|
storeName;
|
|
|
|
key;
|
2016-01-05 17:11:01 +01:00
|
|
|
indexName;
|
|
|
|
|
2016-01-05 14:20:13 +01:00
|
|
|
constructor(s, storeName: string, indexName: string, key: any) {
|
|
|
|
super(s.root);
|
|
|
|
this.s = s;
|
|
|
|
this.storeName = storeName;
|
|
|
|
this.key = key;
|
2016-01-05 17:11:01 +01:00
|
|
|
this.indexName = indexName;
|
2016-01-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(f) {
|
2016-01-11 02:56:32 +01:00
|
|
|
this.s.subscribe((isDone, value, tx) => {
|
2016-01-05 14:20:13 +01:00
|
|
|
if (isDone) {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(true, undefined, tx);
|
2016-01-05 14:20:13 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
let s = tx.objectStore(this.storeName).index(this.indexName);
|
2016-01-05 17:11:01 +01:00
|
|
|
let req = s.openCursor(IDBKeyRange.only(this.key(value)));
|
2016-01-05 14:20:13 +01:00
|
|
|
req.onsuccess = () => {
|
|
|
|
let cursor = req.result;
|
|
|
|
if (cursor) {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(false, [value, cursor.value], tx);
|
2016-01-05 14:20:13 +01:00
|
|
|
cursor.continue();
|
|
|
|
} else {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(true, undefined, tx);
|
2016-01-05 14:20:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
class IterQueryStream<T> extends QueryStreamBase<T> {
|
2016-01-05 14:20:13 +01:00
|
|
|
private storeName;
|
|
|
|
private options;
|
|
|
|
|
|
|
|
constructor(qr, storeName, options?) {
|
|
|
|
super(qr);
|
|
|
|
this.options = options;
|
|
|
|
this.storeName = storeName;
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe(f) {
|
2016-01-11 02:56:32 +01:00
|
|
|
let doIt = (tx) => {
|
2016-01-05 17:11:01 +01:00
|
|
|
let s;
|
|
|
|
if (this.options && this.options.indexName) {
|
2016-01-11 02:56:32 +01:00
|
|
|
s = tx.objectStore(this.storeName)
|
|
|
|
.index(this.options.indexName);
|
2016-01-05 17:11:01 +01:00
|
|
|
} else {
|
2016-01-11 02:56:32 +01:00
|
|
|
s = tx.objectStore(this.storeName);
|
2016-01-05 17:11:01 +01:00
|
|
|
}
|
2016-01-05 14:20:13 +01:00
|
|
|
let kr = undefined;
|
|
|
|
if (this.options && ("only" in this.options)) {
|
|
|
|
kr = IDBKeyRange.only(this.options.only);
|
|
|
|
}
|
|
|
|
let req = s.openCursor(kr);
|
2016-01-05 01:10:31 +01:00
|
|
|
req.onsuccess = (e) => {
|
|
|
|
let cursor: IDBCursorWithValue = req.result;
|
|
|
|
if (cursor) {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(false, cursor.value, tx);
|
2016-01-05 01:10:31 +01:00
|
|
|
cursor.continue();
|
|
|
|
} else {
|
2016-01-11 02:56:32 +01:00
|
|
|
f(true, undefined, tx);
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
};
|
2016-01-05 17:11:01 +01:00
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
this.root.addWork(doIt, null, false);
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class QueryRoot {
|
2016-01-11 02:56:32 +01:00
|
|
|
private work = [];
|
|
|
|
private db: IDBDatabase;
|
|
|
|
private stores = new Set();
|
|
|
|
private kickoffPromise;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Some operations is a write operation,
|
|
|
|
* and we need to do a "readwrite" transaction/
|
|
|
|
*/
|
|
|
|
private hasWrite;
|
2016-01-05 01:10:31 +01:00
|
|
|
|
|
|
|
constructor(db) {
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
iter<T>(storeName): QueryStream<T> {
|
2016-01-05 14:20:13 +01:00
|
|
|
this.stores.add(storeName);
|
|
|
|
return new IterQueryStream(this, storeName);
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
iterOnly<T>(storeName, key): QueryStream<T> {
|
2016-01-05 01:10:31 +01:00
|
|
|
this.stores.add(storeName);
|
2016-01-05 14:20:13 +01:00
|
|
|
return new IterQueryStream(this, storeName, {only: key});
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
|
|
|
|
iterIndex<T>(storeName, indexName, key) {
|
2016-01-05 17:11:01 +01:00
|
|
|
this.stores.add(storeName);
|
|
|
|
return new IterQueryStream(this, storeName, {indexName: indexName});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
/**
|
|
|
|
* Put an object into the given object store.
|
|
|
|
* Overrides if an existing object with the same key exists
|
|
|
|
* in the store.
|
|
|
|
*/
|
|
|
|
put(storeName, val): QueryRoot {
|
|
|
|
let doPut = (tx: IDBTransaction) => {
|
|
|
|
tx.objectStore(storeName).put(val);
|
|
|
|
};
|
|
|
|
this.addWork(doPut, storeName, true);
|
2016-01-05 01:10:31 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add all object from an iterable to the given object store.
|
|
|
|
* Fails if the object's key is already present
|
|
|
|
* in the object store.
|
|
|
|
*/
|
2016-01-05 01:10:31 +01:00
|
|
|
putAll(storeName, iterable): QueryRoot {
|
2016-01-11 02:56:32 +01:00
|
|
|
const doPutAll = (tx: IDBTransaction) => {
|
|
|
|
for (const obj of iterable) {
|
|
|
|
tx.objectStore(storeName).put(obj);
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
};
|
|
|
|
this.addWork(doPutAll, storeName, true);
|
2016-01-05 01:10:31 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
/**
|
|
|
|
* Add an object to the given object store.
|
|
|
|
* Fails if the object's key is already present
|
|
|
|
* in the object store.
|
|
|
|
*/
|
2016-01-05 01:10:31 +01:00
|
|
|
add(storeName, val): QueryRoot {
|
2016-01-11 02:56:32 +01:00
|
|
|
const doAdd = (tx: IDBTransaction) => {
|
|
|
|
tx.objectStore(storeName).add(val);
|
|
|
|
};
|
|
|
|
this.addWork(doAdd, storeName, true);
|
2016-01-05 01:10:31 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
/**
|
|
|
|
* Get one object from a store by its key.
|
|
|
|
*/
|
2016-01-05 01:10:31 +01:00
|
|
|
get(storeName, key): Promise<any> {
|
2016-01-11 02:56:32 +01:00
|
|
|
const {resolve, promise} = openPromise();
|
|
|
|
|
|
|
|
const doGet = (tx) => {
|
|
|
|
const req = tx.objectStore(storeName).get(key);
|
2016-01-05 01:10:31 +01:00
|
|
|
req.onsuccess = (r) => {
|
2016-01-11 02:56:32 +01:00
|
|
|
resolve(req.result);
|
2016-01-05 01:10:31 +01:00
|
|
|
};
|
2016-01-11 02:56:32 +01:00
|
|
|
};
|
2016-01-05 17:11:01 +01:00
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
this.addWork(doGet, storeName, false);
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => this.finish())
|
|
|
|
.then(() => promise);
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
/**
|
|
|
|
* Finish the query, and start the query in the first place if necessary.
|
|
|
|
*/
|
2016-01-05 01:10:31 +01:00
|
|
|
finish(): Promise<void> {
|
|
|
|
if (this.kickoffPromise) {
|
|
|
|
return this.kickoffPromise;
|
|
|
|
}
|
|
|
|
this.kickoffPromise = new Promise((resolve, reject) => {
|
2016-01-11 02:56:32 +01:00
|
|
|
const mode = this.hasWrite ? "readwrite" : "readonly";
|
|
|
|
const tx = this.db.transaction(Array.from(this.stores), mode);
|
|
|
|
tx.oncomplete = () => {
|
2016-01-05 01:10:31 +01:00
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
for (let w of this.work) {
|
2016-01-11 02:56:32 +01:00
|
|
|
w(tx);
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return this.kickoffPromise;
|
|
|
|
}
|
|
|
|
|
2016-01-11 02:56:32 +01:00
|
|
|
/**
|
|
|
|
* Delete an object by from the given object store.
|
|
|
|
*/
|
2016-01-05 01:10:31 +01:00
|
|
|
delete(storeName: string, key): QueryRoot {
|
2016-01-11 02:56:32 +01:00
|
|
|
const doDelete = (tx) => {
|
|
|
|
tx.objectStore(storeName).delete(key);
|
|
|
|
};
|
|
|
|
this.addWork(doDelete, storeName, true);
|
2016-01-05 01:10:31 +01:00
|
|
|
return this;
|
|
|
|
}
|
2016-01-11 02:56:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Low-level function to add a task to the internal work queue.
|
|
|
|
*/
|
|
|
|
addWork(workFn: (IDBTransaction) => void,
|
|
|
|
storeName: string,
|
|
|
|
isWrite: boolean) {
|
|
|
|
if (storeName) {
|
|
|
|
this.stores.add(storeName);
|
|
|
|
}
|
|
|
|
if (isWrite) {
|
|
|
|
this.hasWrite = true;
|
|
|
|
}
|
|
|
|
if (workFn) {
|
|
|
|
this.work.push(workFn);
|
|
|
|
}
|
|
|
|
}
|
2016-01-05 01:10:31 +01:00
|
|
|
}
|