forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyValueStorageBase.ts
More file actions
128 lines (111 loc) · 3.14 KB
/
KeyValueStorageBase.ts
File metadata and controls
128 lines (111 loc) · 3.14 KB
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
118
119
120
121
122
123
124
125
126
127
128
import { IStorage } from './IStorage';
import { IStorageItem } from './IStorageItem';
export abstract class KeyValueStorageBase implements IStorage {
private maxItems: number;
private items: number[];
private lastTimestamp: number = 0;
constructor(maxItems) {
this.maxItems = maxItems;
}
public save(value: any, single?: boolean): number {
if (!value) {
return null;
}
this.ensureIndex();
const items = this.items;
const timestamp = Math.max(Date.now(), this.lastTimestamp + 1);
const key = this.getKey(timestamp);
const json = JSON.stringify(value);
try {
this.write(key, json);
this.lastTimestamp = timestamp;
if (items.push(timestamp) > this.maxItems) {
this.delete(this.getKey(items.shift()));
}
} catch (e) {
return null;
}
return timestamp;
}
public get(limit?: number): IStorageItem[] {
this.ensureIndex();
return this.items.slice(0, limit)
.map((timestamp) => {
// Read and parse item for this timestamp
const key = this.getKey(timestamp);
try {
const json = this.read(key);
const value = JSON.parse(json, parseDate);
return { timestamp, value };
} catch (error) {
// Something went wrong - try to delete the cause.
this.safeDelete(key);
return null;
}
})
.filter((item) => item != null);
}
public remove(timestamp: number): void {
this.ensureIndex();
const items = this.items;
const index = items.indexOf(timestamp);
if (index >= 0) {
const key = this.getKey(timestamp);
this.safeDelete(key);
items.splice(index, 1);
}
}
public clear(): void {
this.items.forEach((item) => this.safeDelete(this.getKey(item)));
this.items = [];
}
protected abstract write(key: string, value: string): void;
protected abstract read(key: string): string;
protected abstract readAllKeys(): string[];
protected abstract delete(key: string);
protected abstract getKey(timestamp: number): string;
protected abstract getTimestamp(key: string): number;
private ensureIndex() {
if (!this.items) {
this.items = this.createIndex();
this.lastTimestamp = Math.max(0, ...this.items) + 1;
}
}
private safeDelete(key: string): void {
try {
this.delete(key);
} catch (error) {
}
}
private createIndex() {
try {
const keys = this.readAllKeys();
return keys.map((key) => {
try {
const timestamp = this.getTimestamp(key);
if (!timestamp) {
this.safeDelete(key);
return null;
}
return timestamp;
} catch (error) {
this.safeDelete(key);
return null;
}
}).filter((timestamp) => timestamp != null)
.sort((a, b) => a - b);
} catch (error) {
return [];
}
}
}
function parseDate(key, value) {
const dateRegx = /\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\.\d+([+-][0-2]\d:[0-5]\d|Z)/g;
if (typeof value === 'string') {
const a = dateRegx.exec(value);
if (a) {
return new Date(value);
}
}
return value;
}