Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 1x 49x 2x 2x 17x 17x 17x 2x 2x 2x 2x | import {Dictionary} from "../../dictionary";
import {Logger} from "../../logger";
import {DataType} from "./data-type";
import {DatabaseValue} from "./database-value";
export class Database extends Dictionary<string, DatabaseValue> {
protected logger: Logger = new Logger(module.id);
public exists(key: string): boolean {
const item = this.get(key);
return Boolean(item);
}
public get(key: string): any {
const item = super.get(key);
Iif (item && item.expiredAt && parseInt(
item.expiredAt,
10
) < new Date().getTime()) {
this.remove(key);
return null;
}
return item;
}
public putIfPresent(key: string, value: DatabaseValue): DatabaseValue {
let oldKey = this.get(key);
if (oldKey) {
oldKey = this.put(
key,
value
);
}
return oldKey;
}
public putIfAbsent(key: string, value: DatabaseValue): DatabaseValue {
let oldValue = this.get(key);
if (!oldValue) {
oldValue = this.put(
key,
value
);
}
return oldValue;
}
// Tslint:disable-next-line
public merge(key: string, value: DatabaseValue, callback: (oldVal: DatabaseValue, newVal: DatabaseValue) => DatabaseValue): DatabaseValue {
this.logger.debug(`Keys: ${value.getSortedSet().keys()}, Values: ${value.getSortedSet().values()}`);
const vs = value.getSortedSet();
const args : string[] = [];
vs.toArray().forEach((element) => {
args.push(element.value.toString());
});
this.logger.debug(
`merge(${key}, with [%j])`,
...args
);
const oldValue: DatabaseValue = this.get(key),
newValue: DatabaseValue = oldValue == null
? value
: callback(
oldValue,
value
);
if (newValue == null) {
this.remove(key);
} else {
this.put(
key,
newValue
);
}
return newValue;
}
public getOrDefault(key: string, defaultValue: DatabaseValue): DatabaseValue {
// Only return the value. Don't add it to the database yet
let value = this.get(key);
Eif (!value) {
value = defaultValue;
}
return value;
}
public isType(key: string, type: DataType): boolean {
this.logger.debug(
`isType(${key}, ${type})`,
key,
type
);
if (Object.values(DataType).indexOf(type) === -1) {
throw new Error(`Invalid type ${type}`);
}
const value = this.get(key);
if (value) {
return value.getType() === type;
}
// If the type does not exist then it is OK to use type
return true;
}
public rename(from: string, to: string): boolean {
const value = this.remove(from);
if (value != null) {
this.put(
to,
value
);
return true;
}
return false;
}
}
|