Skip to content

Commit 5d351cc

Browse files
committed
Rudimentary checkpoint support for MutableReference
1 parent 39949fa commit 5d351cc

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

src/data/collections/mutable/MutableReference.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class MutableReference<T> extends BaseCollection<T> {
1717
_value?: T;
1818

1919
constructor(config?: CollectionConfig) {
20-
super([RefUpdateOp.className], config);
21-
20+
super([RefUpdateOp.className], {supportsCheckpoints: true, ...config});
21+
2222
this.setRandomId();
2323
}
2424

@@ -101,6 +101,20 @@ class MutableReference<T> extends BaseCollection<T> {
101101
init(): void {
102102

103103
}
104+
105+
exportMutableState() {
106+
return {
107+
_sequence: this._sequence,
108+
_timestamp: this._timestamp,
109+
_value: this._value
110+
};
111+
}
112+
113+
importMutableState(state: any) {
114+
this._sequence = state._sequence;
115+
this._timestamp = state._timestamp;
116+
this._value = state._value;
117+
}
104118

105119
async validate(references: Map<Hash, HashedObject>) {
106120

src/data/collections/mutable/MutableSet.ts

+12
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,18 @@ class MutableSet<T> extends BaseCollection<T> implements Collection<T> {
206206
init(): void {
207207

208208
}
209+
210+
exportMutableState() {
211+
return {
212+
_elements: this._elements,
213+
_currentAddOpRefs: this._currentAddOpRefs
214+
};
215+
}
216+
217+
importMutableState(state: any): void {
218+
this._elements = new Map(Object.entries(state._elements));
219+
this._currentAddOpRefs = new Map(Object.entries(state._currentAddOpRefs));
220+
}
209221

210222
async validate(references: Map<Hash, HashedObject>) {
211223

test/storage/store.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { SomethingMutable, SomeMutation } from '../data//types/SomethingMutable'
77
import { describeProxy } from 'config';
88
import { HistoryFragment } from 'data/history/HistoryFragment';
99
import { OpHeader } from 'data/history/OpHeader';
10+
import { MutableReference } from 'index';
1011

1112
//import { SQLiteBackend } from '@hyper-hyper-space/sqlite';
1213

@@ -138,6 +139,12 @@ describeProxy('[STR] Storage', () => {
138139
store.close();
139140
});*/
140141

142+
test('[STR19] Validate save/load checkpoint cycle for MutableReference with memory store', async () => {
143+
let store = new Store(new MemoryBackend('test-storage-backend'));
144+
await testCheckpointSaveLoadCycle(store);
145+
store.close();
146+
})
147+
141148
});
142149

143150
async function testLoadStoreCycle(store: Store) {
@@ -223,6 +230,27 @@ async function testMutationOps(store: Store) {
223230
}
224231
}
225232

233+
async function testCheckpointSaveLoadCycle(store: Store) {
234+
let sm = new MutableReference();
235+
await sm.setValue('hello');
236+
await store.save(sm);
237+
await sm.saveCheckpoint();
238+
const lastCheckpoint = await store.loadLastCheckpoint(sm.hash());
239+
expect(sm.getValue()).toEqual('hello');
240+
241+
await sm.setValue('world');
242+
await sm.setValue('!');
243+
await store.save(sm);
244+
expect(sm.getValue()).toEqual('!');
245+
246+
await sm.restoreCheckpoint(lastCheckpoint!);
247+
expect(sm.getValue()).toEqual('hello');
248+
await store.save(sm);
249+
250+
await sm.loadAllChanges();
251+
expect(sm.getValue()).toEqual('!');
252+
}
253+
226254
async function testMutationOpAutoLoad(store: Store) {
227255
let sm = new SomethingMutable();
228256

0 commit comments

Comments
 (0)