-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackLog.js
151 lines (128 loc) · 3.87 KB
/
BackLog.js
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const QDB = require("qdatabase");
const Qulity = require("qulity");
const Defaults = new Qulity.Collection({
"Persistent Path": "BackLog/Persistent.qdb",
"Connection": {}
});
class BackLog {
/**
* An off-load key/value-based cache.
* @param {String} Identifier A storage identifier for this instance.
*/
constructor (Identifier) {
if (typeof Identifier !== "string") throw new Error("BackLog identifiers should be a type of String.");
/**
* Identifier of this BackLog map.
* @name BackLog#Identifier
* @type {String}
* @readonly
*/
Object.defineProperty(this, "Identifier", {
enumerable: true,
value: Identifier
});
/**
* Active cache which will get pushed to persistent disk on exit.
* @name BackLog#Cache
* @type {Map}
* @readonly
*/
Object.defineProperty(this, "Cache", {
enumerable: true,
value: new Qulity.Collection()
});
/**
* A defer Promise that resolves once this BackLog is done loading.
* @name BackLog#Ready
* @type {Promise}
* @readonly
*/
Object.defineProperty(this, "Ready", {
value: new Promise(Resolve => {
setImmediate(() => {
this.PullPersistent();
process.on("beforeExit", this.PushPersistent);
Resolve();
});
})
});
/**
* Settings of this BackLog.
* @name BackLog#Settings
* @type {Map}
* @private
*/
Object.defineProperty(this, "Settings", {
value: Defaults.Clone()
});
}
/**
* Returns a new QDB Connection.
* @returns {Connection}
* @private
*/
QDBConnection () {
const Path = this.Settings.get("Persistent Path");
return new QDB.Connection(Path, {
...this.Settings.get("Connection") || {},
Table: this.Identifier
});
}
/**
* Pulls the items from persistent memory into this BackLog.
* @returns {BackLog}
* @private
*/
PullPersistent () {
const Connection = this.QDBConnection();
Connection.Each((V, K) => this.Cache.set(K, V));
Connection.Erase(...Connection.Indexes);
Connection.Disconnect();
return this;
}
/**
* Moves the current items from this BackLog instance into
* persistent memory, provided by the identifier of the BackLog.
* @returns {BackLog}
* @private
*/
PushPersistent () {
const Connection = this.QDBConnection();
for (const [Key, Val] of this.Cache)
Connection.Set(Key, Val);
Connection.Disconnect();
return this;
}
/**
* Modifies the settings of this BackLog instance.
* @param {Object|*} Key Either a list of key/values or just a key.
* @param {*} Val If a singular key was provided, a value for this option.
* @returns {Collection}
*/
Config (Key, Val) {
if (typeof Key === "object") {
for (const K of Key)
this.Settings.set(K, Key[K]);
return this.Settings;
}
this.Settings.set(Key, Val);
return this.Settings;
}
/**
* Modifies the default settings of BackLog.
* @param {Object|*} Key Either a list of key/values or just a key.
* @param {*} Val If a singular key was provided, a value for this default option.
* @returns {Collection}
* @static
*/
static Default (Key, Val) {
if (typeof Key === "object") {
for (const K of Key)
Defaults.set(K, Key[K])
return Defaults;
}
Defaults.set(Key, Val);
return Defaults;
}
}
module.exports = BackLog;