1 | // a persistent map using a clever combination of persisting and logging |
2 | // (well, only logging as of now) |
3 | // Uses TreeMap as default |
4 | // Note: don't put in static initializer (programID not set yet) |
5 | static class PersistentMap<A, B> extends AbstractMap<A, B> { |
6 | Map<A, B> m = makeMap(); |
7 | File file; |
8 | S id; // unique ID of this map |
9 | |
10 | Map<A, B> makeMap() { |
11 | ret new TreeMap(); |
12 | } |
13 | |
14 | PersistentMap(S fileName) { |
15 | this(getProgramFile(fileName)); |
16 | } |
17 | |
18 | PersistentMap(S progID, S fileName) { |
19 | this(getProgramFile(progID, fileName)); |
20 | } |
21 | |
22 | *(File *file) { |
23 | for (S s : scanLog(file)) pcall { |
24 | L l = cast unstructure(s); |
25 | O cmd = l.get(0); |
26 | if (eq(cmd, "add")) |
27 | m.put((A) l.get(1), (B) l.get(2)); |
28 | else if (eq(cmd, "remove")) |
29 | m.remove((A) l.get(1)); |
30 | else if (eq(cmd, "id")) |
31 | id = (S) l.get(1); |
32 | else |
33 | print("Unknown command in log: " + l.get(0)); |
34 | } |
35 | |
36 | if (id == null) |
37 | makeID_priv(); |
38 | } |
39 | |
40 | void makeID_priv() { |
41 | id = makeRandomID(12); |
42 | logQuoted(file, structure(litlist("id", id))); |
43 | } |
44 | |
45 | // just delegates |
46 | |
47 | public synchronized int size() { |
48 | ret m.size(); |
49 | } |
50 | |
51 | public synchronized B get(O a) { |
52 | ret m.get(a); |
53 | } |
54 | |
55 | // TODO: calling remove() in the iterator will have unpersisted |
56 | // effects. |
57 | public synchronized Set<Map.Entry<A,B>> entrySet() { |
58 | ret m.entrySet(); // synchronize this? |
59 | } |
60 | |
61 | // delegates with logging |
62 | |
63 | public synchronized B put(A a, B b) { |
64 | B c = m.get(a); |
65 | if (neq(b, c)) { |
66 | m.put(a, b); |
67 | logQuoted(file, structure(litlist("add", a, b))); |
68 | } |
69 | ret c; |
70 | } |
71 | |
72 | public synchronized B remove(O a) { |
73 | B result = m.remove(a); |
74 | logQuoted(file, structure(litlist("remove", a))); |
75 | ret result; |
76 | } |
77 | |
78 | // unique id of this map |
79 | public S id() { |
80 | ret id; |
81 | } |
82 | |
83 | // clear map, make new ID |
84 | public synchronized void clear() { |
85 | m.clear(); |
86 | file.delete(); |
87 | makeID_priv(); |
88 | } |
89 | } |
Began life as a copy of #1001972
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1002063 |
Snippet name: | PersistentMap (synchronized) |
Eternal ID of this version: | #1002063/1 |
Text MD5: | 407e8d5b23f3c37cddd4a29735dbdafa |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-07-12 17:21:56 |
Source code size: | 2046 bytes / 89 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 759 / 2284 |
Referenced in: | [show references] |