Ejemplo n.º 1
0
 /**
  * Partially roll back the current transaction.
  *
  * @param savepoint the savepoint to which should be rolled back
  * @param trimToSize if the list should be trimmed
  */
 public void rollbackTo(Savepoint savepoint, boolean trimToSize) {
   int index = savepoint == null ? 0 : savepoint.logIndex;
   while (undoLog.size() > index) {
     UndoLogRecord entry = undoLog.getLast();
     entry.undo(this);
     undoLog.removeLast(trimToSize);
   }
   if (transaction != null) {
     long savepointId = savepoint == null ? 0 : savepoint.transactionSavepoint;
     HashMap<String, MVTable> tableMap = database.getMvStore().getTables();
     Iterator<Change> it = transaction.getChanges(savepointId);
     while (it.hasNext()) {
       Change c = it.next();
       MVTable t = tableMap.get(c.mapName);
       if (t != null) {
         long key = ((ValueLong) c.key).getLong();
         ValueArray value = (ValueArray) c.value;
         short op;
         Row row;
         if (value == null) {
           op = UndoLogRecord.INSERT;
           row = t.getRow(this, key);
         } else {
           op = UndoLogRecord.DELETE;
           row = new Row(value.getList(), Row.MEMORY_CALCULATE);
         }
         row.setKey(key);
         UndoLogRecord log = new UndoLogRecord(t, op, row);
         log.undo(this);
       }
     }
   }
   if (savepoints != null) {
     String[] names = new String[savepoints.size()];
     savepoints.keySet().toArray(names);
     for (String name : names) {
       Savepoint sp = savepoints.get(name);
       int savepointIndex = sp.logIndex;
       if (savepointIndex > index) {
         savepoints.remove(name);
       }
     }
   }
 }
Ejemplo n.º 2
0
  private void testGetModifiedMaps() {
    MVStore s = MVStore.open(null);
    TransactionStore ts = new TransactionStore(s);
    Transaction tx;
    TransactionMap<String, String> m1, m2, m3;
    long sp;

    tx = ts.begin();
    m1 = tx.openMap("m1");
    m2 = tx.openMap("m2");
    m3 = tx.openMap("m3");
    assertFalse(tx.getChanges(0).hasNext());
    tx.commit();

    tx = ts.begin();
    m1 = tx.openMap("m1");
    m2 = tx.openMap("m2");
    m3 = tx.openMap("m3");
    m1.put("1", "100");
    sp = tx.setSavepoint();
    m2.put("1", "100");
    m3.put("1", "100");
    Iterator<Change> it = tx.getChanges(sp);
    assertTrue(it.hasNext());
    Change c;
    c = it.next();
    assertEquals("m3", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m2", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());

    it = tx.getChanges(0);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m3", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m2", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m1", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());

    tx.rollbackToSavepoint(sp);

    it = tx.getChanges(0);
    assertTrue(it.hasNext());
    c = it.next();
    assertEquals("m1", c.mapName);
    assertEquals("1", c.key.toString());
    assertNull(c.value);
    assertFalse(it.hasNext());

    tx.commit();

    s.close();
  }