-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRADM.js
195 lines (170 loc) · 5.08 KB
/
RADM.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const mysql = require('mysql');
const crypto = require('crypto');
class RADM {
/**
* @constructor
*/
constructor() {
this.storage = {};
this.CHUNKSIZE = 1024; // size of the chunks that a stored (max chunksize is 1024)
this.conn = mysql.createConnection({ host: "localhost", user: "root", password: "", database: "radm_testing" });
}
/**
* @public
*/
dispose() {
this.conn.end();
}
// Private functions //
/**
* @param {String} str
* @returns {String|Boolean} cleanedString or false if a string is not supplied
* @private
*/
_cleanString(str) {
if (typeof str == "string") {
return str.trim().toLowerCase();
} else {
return str;
}
}
/**
* @param {String} tableName
* @returns {Boolean} true if table exists or false if not
* @private
*/
_checkIfTableExists(tableName) {
tableName = this._cleanString(tableName);
for (key in this.storage) {
if (key == tableName) {
return true;
}
}
return false;
}
/**
* @param {String} tableName
* @param {String} keyToCheck
* @returns {Boolean} true if key exists in table - false otherwise
* @private
*/
_checkIfKeyExistsInTable(tableName, keyToCheck) {
tableName = this._cleanString(tableName);
keyToCheck = this._cleanString(keyToCheck);
for (keys in Object.keys(this.storage[tableName])) {
if (keys == keyToCheck) {
return true;
}
}
return false;
}
// Public functions //
/**
* @param {String} tableName
* @returns {Boolean} returns true if table is created - false if table exists already or tablename isn't supplied
* @public
*/
addTable(tableName) {
tableName = this._cleanString(tableName);
if (tableName == undefined || tableName == "") {
return false;
} else if (this._checkIfTableExists(tableName)) {
return false;
} else {
this.storage[tableName] = {};
return true;
}
}
/**
* @param {String} tableName
* @param {String} documentKey
* @param {String|Int|Boolean} DocumentValue
* @returns {Boolean} returns true if document is created - otherwise false
* @public
*/
addDocument(tableName, documentKey, documentValue) {
tableName = this._cleanString(tableName);
documentKey = this._cleanString(documentKey);
documentValue = this._cleanString(documentValue);
if (this._checkIfTableExists(tableName)) {
if (this._checkIfKeyExistsInTable(tableName, documentKey)) {
return false;
}
this.storage[tableName][documentKey] = documentValue;
return true;
}
}
// Snapshotting functions //
/**
* @returns {String} returns snapshot id (UUID)
* @private
*/
_generateSnapshotId() {
return crypto.randomUUID();
}
/**
* @param {String} chunk
* @param {String} snapshotId
* @returns {Boolean} True if chunk was stored - false otherwise
* @private
*/
_storeChunk(chunk, snapshotId) {
this.conn.query("INSERT INTO chunks (chunkdata, chunkid) VALUES (?, ?)", [chunk, snapshotId], (err, _) => {
if (err) return false;
return true;
});
}
/**
* @param {String} data
* @returns {Boolean} true if all data is chunked and stored
* @private
*/
_splitIntoChunksAndStore(data) {
let chunk = "";
let first = true;
let snapshotId = this._generateSnapshotId();
for (let i = 0; i < data.length; i++) {
chunk += data[i];
if (i % this.CHUNKSIZE == 0) {
if (first) {
first = false;
continue;
}
this._storeChunk(chunk, snapshotId);
chunk = ""
}
}
this._storeChunk(chunk, snapshotId);
return true;
}
/**
* @returns {boolean} True if databased was snapshotted - false otherwise
* @public
*/
snapShot() {
let data = JSON.stringify(this.storage);
if (this._splitIntoChunksAndStore(data)) {
return true;
}
return false;
}
/**
* @param {string} chunkId
* @returns {Promise} returns promise, resolve data or reject error
* @public
*/
restoreStoragebyChunkId(chunkId) {
this.conn.query("SELECT chunkdata FROM chunks WHERE chunkid = ? ORDER BY id ASC", [chunkId], (err, result) => {
if(err) return err;
let finalChunk = "";
for (const row in result) {
if (Object.hasOwnProperty.call(result, row)) {
const element = result[row]["chunkdata"];
finalChunk += element;
}
}
this.storage = JSON.parse(finalChunk);
});
}
}
module.exports = RADM;