@Override public boolean commitTx() throws Exception { if (!buff.isEmpty()) { // We first assume that all data will be persisted. So that if exception happen during persist we try to // rollback everything we had in in-memory buffer. toUndo = buff; // clearing up in-memory buffer by initializing new map. // NOTE: we want to init map here so that if no changes are made we re-use same instance of the map in next tx // NOTE: we could cache two maps and swap them to avoid creation of map instances, but code would be ugly buff = new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR); // TODO: tracking of persisted items can be optimized by returning a pair {succeededOrNot, persisted} which // tells if persisting succeeded and what was persisted (i.e. what we will have to undo in case of rollback) persist(toUndo); } return true; }
@Override public boolean commitTx() throws Exception { if (!buff.isEmpty()) { // We first assume that all data will be persisted. So that if exception happen during persist we try to // rollback everything we had in in-memory buffer. toUndo = buff; // clearing up in-memory buffer by initializing new map. // NOTE: we want to init map here so that if no changes are made we re-use same instance of the map in next tx // NOTE: we could cache two maps and swap them to avoid creation of map instances, but code would be ugly buff = new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR); // TODO: tracking of persisted items can be optimized by returning a pair {succeededOrNot, persisted} which // tells if persisting succeeded and what was persisted (i.e. what we will have to undo in case of rollback) persist(toUndo); } return true; }
@Override protected void persist(NavigableMap<byte[], NavigableMap<byte[], Update>> buff) throws Exception { // persists only first change and throws exception NavigableMap<byte[], NavigableMap<byte[], Update>> toPersist = Maps.newTreeMap(Bytes.BYTES_COMPARATOR); if (buff.size() > 0) { toPersist.put(buff.firstEntry().getKey(), buff.firstEntry().getValue()); } delegate.persist(toPersist); throw new RuntimeException("Simulating failure in the middle of persist"); }