1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | if (typeof b !== "function" && b !== null)
|
11 | throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
12 | extendStatics(d, b);
|
13 | function __() { this.constructor = d; }
|
14 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
15 | };
|
16 | })();
|
17 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
18 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
19 | return new (P || (P = Promise))(function (resolve, reject) {
|
20 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
21 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
22 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
23 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
24 | });
|
25 | };
|
26 | var __generator = (this && this.__generator) || function (thisArg, body) {
|
27 | var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
28 | return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
29 | function verb(n) { return function (v) { return step([n, v]); }; }
|
30 | function step(op) {
|
31 | if (f) throw new TypeError("Generator is already executing.");
|
32 | while (_) try {
|
33 | if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
34 | if (y = 0, t) op = [op[0] & 2, t.value];
|
35 | switch (op[0]) {
|
36 | case 0: case 1: t = op; break;
|
37 | case 4: _.label++; return { value: op[1], done: false };
|
38 | case 5: _.label++; y = op[1]; op = [0]; continue;
|
39 | case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
40 | default:
|
41 | if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
42 | if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
43 | if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
44 | if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
45 | if (t[2]) _.ops.pop();
|
46 | _.trys.pop(); continue;
|
47 | }
|
48 | op = body.call(thisArg, _);
|
49 | } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
50 | if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
51 | }
|
52 | };
|
53 | var __values = (this && this.__values) || function(o) {
|
54 | var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
55 | if (m) return m.call(o);
|
56 | if (o && typeof o.length === "number") return {
|
57 | next: function () {
|
58 | if (o && i >= o.length) o = void 0;
|
59 | return { value: o && o[i++], done: !o };
|
60 | }
|
61 | };
|
62 | throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
63 | };
|
64 | Object.defineProperty(exports, "__esModule", { value: true });
|
65 | exports.CheckpointDB = void 0;
|
66 | var db_1 = require("./db");
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | var CheckpointDB = (function (_super) {
|
72 | __extends(CheckpointDB, _super);
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | function CheckpointDB(leveldb) {
|
79 | var _this = _super.call(this, leveldb) || this;
|
80 |
|
81 | _this.checkpoints = [];
|
82 | return _this;
|
83 | }
|
84 | Object.defineProperty(CheckpointDB.prototype, "isCheckpoint", {
|
85 | |
86 |
|
87 |
|
88 | get: function () {
|
89 | return this.checkpoints.length > 0;
|
90 | },
|
91 | enumerable: false,
|
92 | configurable: true
|
93 | });
|
94 | |
95 |
|
96 |
|
97 |
|
98 | CheckpointDB.prototype.checkpoint = function (root) {
|
99 | this.checkpoints.push({ keyValueMap: new Map(), root: root });
|
100 | };
|
101 | |
102 |
|
103 |
|
104 | CheckpointDB.prototype.commit = function () {
|
105 | return __awaiter(this, void 0, void 0, function () {
|
106 | var keyValueMap, batchOp_1, currentKeyValueMap_1;
|
107 | return __generator(this, function (_a) {
|
108 | switch (_a.label) {
|
109 | case 0:
|
110 | keyValueMap = this.checkpoints.pop().keyValueMap;
|
111 | if (!!this.isCheckpoint) return [3 , 2];
|
112 | batchOp_1 = [];
|
113 | keyValueMap.forEach(function (value, key) {
|
114 | if (value === null) {
|
115 | batchOp_1.push({
|
116 | type: 'del',
|
117 | key: Buffer.from(key, 'binary'),
|
118 | });
|
119 | }
|
120 | else {
|
121 | batchOp_1.push({
|
122 | type: 'put',
|
123 | key: Buffer.from(key, 'binary'),
|
124 | value: value,
|
125 | });
|
126 | }
|
127 | });
|
128 | return [4 , this.batch(batchOp_1)];
|
129 | case 1:
|
130 | _a.sent();
|
131 | return [3 , 3];
|
132 | case 2:
|
133 | currentKeyValueMap_1 = this.checkpoints[this.checkpoints.length - 1].keyValueMap;
|
134 | keyValueMap.forEach(function (value, key) { return currentKeyValueMap_1.set(key, value); });
|
135 | _a.label = 3;
|
136 | case 3: return [2 ];
|
137 | }
|
138 | });
|
139 | });
|
140 | };
|
141 | |
142 |
|
143 |
|
144 | CheckpointDB.prototype.revert = function () {
|
145 | return __awaiter(this, void 0, void 0, function () {
|
146 | var root;
|
147 | return __generator(this, function (_a) {
|
148 | root = this.checkpoints.pop().root;
|
149 | return [2 , root];
|
150 | });
|
151 | });
|
152 | };
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 | CheckpointDB.prototype.get = function (key) {
|
159 | return __awaiter(this, void 0, void 0, function () {
|
160 | var index, value_1, value;
|
161 | return __generator(this, function (_a) {
|
162 | switch (_a.label) {
|
163 | case 0:
|
164 |
|
165 | for (index = this.checkpoints.length - 1; index >= 0; index--) {
|
166 | value_1 = this.checkpoints[index].keyValueMap.get(key.toString('binary'));
|
167 | if (value_1 !== undefined) {
|
168 | return [2 , value_1];
|
169 | }
|
170 | }
|
171 | return [4 , _super.prototype.get.call(this, key)];
|
172 | case 1:
|
173 | value = _a.sent();
|
174 | if (this.isCheckpoint) {
|
175 |
|
176 | this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), value);
|
177 | }
|
178 | return [2 , value];
|
179 | }
|
180 | });
|
181 | });
|
182 | };
|
183 | |
184 |
|
185 |
|
186 |
|
187 |
|
188 | CheckpointDB.prototype.put = function (key, val) {
|
189 | return __awaiter(this, void 0, void 0, function () {
|
190 | return __generator(this, function (_a) {
|
191 | switch (_a.label) {
|
192 | case 0:
|
193 | if (!this.isCheckpoint) return [3 , 1];
|
194 |
|
195 | this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), val);
|
196 | return [3 , 3];
|
197 | case 1: return [4 , _super.prototype.put.call(this, key, val)];
|
198 | case 2:
|
199 | _a.sent();
|
200 | _a.label = 3;
|
201 | case 3: return [2 ];
|
202 | }
|
203 | });
|
204 | });
|
205 | };
|
206 | |
207 |
|
208 |
|
209 |
|
210 | CheckpointDB.prototype.del = function (key) {
|
211 | return __awaiter(this, void 0, void 0, function () {
|
212 | return __generator(this, function (_a) {
|
213 | switch (_a.label) {
|
214 | case 0:
|
215 | if (!this.isCheckpoint) return [3 , 1];
|
216 |
|
217 | this.checkpoints[this.checkpoints.length - 1].keyValueMap.set(key.toString('binary'), null);
|
218 | return [3 , 3];
|
219 | case 1:
|
220 |
|
221 | return [4 , this._leveldb.del(key, db_1.ENCODING_OPTS)];
|
222 | case 2:
|
223 |
|
224 | _a.sent();
|
225 | _a.label = 3;
|
226 | case 3: return [2 ];
|
227 | }
|
228 | });
|
229 | });
|
230 | };
|
231 | |
232 |
|
233 |
|
234 |
|
235 | CheckpointDB.prototype.batch = function (opStack) {
|
236 | return __awaiter(this, void 0, void 0, function () {
|
237 | var opStack_1, opStack_1_1, op, e_1_1;
|
238 | var e_1, _a;
|
239 | return __generator(this, function (_b) {
|
240 | switch (_b.label) {
|
241 | case 0:
|
242 | if (!this.isCheckpoint) return [3 , 11];
|
243 | _b.label = 1;
|
244 | case 1:
|
245 | _b.trys.push([1, 8, 9, 10]);
|
246 | opStack_1 = __values(opStack), opStack_1_1 = opStack_1.next();
|
247 | _b.label = 2;
|
248 | case 2:
|
249 | if (!!opStack_1_1.done) return [3 , 7];
|
250 | op = opStack_1_1.value;
|
251 | if (!(op.type === 'put')) return [3 , 4];
|
252 | return [4 , this.put(op.key, op.value)];
|
253 | case 3:
|
254 | _b.sent();
|
255 | return [3 , 6];
|
256 | case 4:
|
257 | if (!(op.type === 'del')) return [3 , 6];
|
258 | return [4 , this.del(op.key)];
|
259 | case 5:
|
260 | _b.sent();
|
261 | _b.label = 6;
|
262 | case 6:
|
263 | opStack_1_1 = opStack_1.next();
|
264 | return [3 , 2];
|
265 | case 7: return [3 , 10];
|
266 | case 8:
|
267 | e_1_1 = _b.sent();
|
268 | e_1 = { error: e_1_1 };
|
269 | return [3 , 10];
|
270 | case 9:
|
271 | try {
|
272 | if (opStack_1_1 && !opStack_1_1.done && (_a = opStack_1.return)) _a.call(opStack_1);
|
273 | }
|
274 | finally { if (e_1) throw e_1.error; }
|
275 | return [7 ];
|
276 | case 10: return [3 , 13];
|
277 | case 11: return [4 , _super.prototype.batch.call(this, opStack)];
|
278 | case 12:
|
279 | _b.sent();
|
280 | _b.label = 13;
|
281 | case 13: return [2 ];
|
282 | }
|
283 | });
|
284 | });
|
285 | };
|
286 | return CheckpointDB;
|
287 | }(db_1.DB));
|
288 | exports.CheckpointDB = CheckpointDB;
|
289 |
|
\ | No newline at end of file |