// a persistent map using a clever combination of persisting and logging // and with case-insensitive keys // (well, only logging as of now) // Note: don't put in static initializer (programID not set yet) static class PersistentCIMap extends AbstractMap { new Map m; File file; PersistentCIMap(S fileName) { this(getProgramFile(fileName)); } PersistentCIMap(S progID, S fileName) { this(getProgramFile(progID, fileName)); } *(File *file) { for (S s : scanLog(file)) pcall { L l = cast unstructure(s); if (eq(l.get(0), "add")) m.put(toLowerCase((S) l.get(1)), (B) l.get(2)); else if (eq(l.get(0), "remove")) m.remove(toLowerCase((S) l.get(1))); else print("Unknown command in log: " + l.get(0)); } } // just delegates public int size() { ret m.size(); } public B get(O a) { ret m.get(toLowerCase((S) a)); } public boolean containsKey(O a) { ret m.containsKey(toLowerCase((S) a)); } // TODO: calling remove() in the iterator will have unpersisted // effects. public Set> entrySet() { ret m.entrySet(); } // delegates with logging public B put(S a, B b) { a = toLowerCase(a); B c = m.get(a); if (neq(b, c)) { m.put(a, b); logQuoted(file, structure(litlist("add", a, b))); } ret c; } public B remove(O a) { a = toLowerCase((S) a); B result = m.remove(a); logQuoted(file, structure(litlist("remove", a))); ret result; } }