Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

89
LINES

< > BotCompany Repo | #1002063 // PersistentMap (synchronized)

JavaX fragment (include)

// a persistent map using a clever combination of persisting and logging
// (well, only logging as of now)
// Uses TreeMap as default
// Note: don't put in static initializer (programID not set yet)
static class PersistentMap<A, B> extends AbstractMap<A, B> {
  Map<A, B> m = makeMap();
  File file;
  S id; // unique ID of this map
  
  Map<A, B> makeMap() {
    ret new TreeMap();
  }
  
  PersistentMap(S fileName) {
    this(getProgramFile(fileName));
  }
  
  PersistentMap(S progID, S fileName) {
    this(getProgramFile(progID, fileName));
  }
  
  *(File *file) {
    for (S s : scanLog(file)) pcall {
      L l = cast unstructure(s);
      O cmd = l.get(0);
      if (eq(cmd, "add"))
        m.put((A) l.get(1), (B) l.get(2));
      else if (eq(cmd, "remove"))
        m.remove((A) l.get(1));
      else if (eq(cmd, "id"))
        id = (S) l.get(1);
      else
        print("Unknown command in log: " + l.get(0));
    }
    
    if (id == null)
      makeID_priv();
  }
  
  void makeID_priv() {
    id = makeRandomID(12);
    logQuoted(file, structure(litlist("id", id)));
  }
  
  // just delegates
  
  public synchronized int size() {
    ret m.size();
  }
  
  public synchronized B get(O a) {
    ret m.get(a);
  }
  
  // TODO: calling remove() in the iterator will have unpersisted
  // effects.
  public synchronized Set<Map.Entry<A,B>> entrySet() {
    ret m.entrySet(); // synchronize this?
  }
  
  // delegates with logging

  public synchronized B put(A a, B b) {
    B c = m.get(a);
    if (neq(b, c)) {
      m.put(a, b);
      logQuoted(file, structure(litlist("add", a, b)));
    }
    ret c;
  }
  
  public synchronized B remove(O a) {
    B result = m.remove(a);
    logQuoted(file, structure(litlist("remove", a)));
    ret result;
  }
  
  // unique id of this map
  public S id() {
    ret id;
  }
  
  // clear map, make new ID
  public synchronized void clear() {
    m.clear();
    file.delete();
    makeID_priv();
  }
}

Author comment

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: 660 / 2166
Referenced in: [show references]