-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snapshot.ts
54 lines (54 loc) · 1.52 KB
/
Snapshot.ts
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
/**
* Copyright (c) 2018-present, heineiuo.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { SequenceNumber } from "./Format.ts";
import { assert } from "./DBHelper.ts";
export class Snapshot {
constructor(sn: SequenceNumber) {
this._sequenceNumber = sn;
}
_sequenceNumber: SequenceNumber;
_next!: Snapshot;
_prev!: Snapshot;
get sequenceNumber(): SequenceNumber {
return this._sequenceNumber;
}
}
export class SnapshotList {
constructor() {
this._head = new Snapshot(0n);
this._head._next = this._head;
this._head._prev = this._head;
}
_head: Snapshot;
isEmpty(): boolean {
return this._head._next === this._head;
}
newest(): Snapshot {
assert(!this.isEmpty());
return this._head._prev;
}
oldest(): Snapshot {
assert(!this.isEmpty());
return this._head._next;
}
// insert before _head
insert(sn: SequenceNumber): Snapshot {
assert(this.isEmpty() || this.newest()._sequenceNumber <= sn);
const snapshot = new Snapshot(sn);
snapshot._next = this._head;
snapshot._prev = this._head._prev;
snapshot._prev._next = snapshot;
snapshot._next._prev = snapshot;
return snapshot;
}
delete(snapshot: Snapshot): void {
const next = snapshot._next;
const prev = snapshot._prev;
next._prev = prev;
prev._next = next;
}
}