コード例 #1
0
  @Test
  public void open_ignores_rollback() {
    File f = TT.tempDbFile();
    WriteAheadLog wal = new WriteAheadLog(f.getPath());
    wal.walPutLong(1L, 11L);
    wal.commit();
    wal.walPutLong(2L, 33L);
    wal.rollback();
    wal.walPutLong(3L, 33L);
    wal.commit();
    wal.seal();
    wal.close();

    wal = new WriteAheadLog(f.getPath());
    wal.open(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeLong, 1L, 11L},
            new Object[] {WALSequence.commit},
            // 2L is ignored, rollback section is skipped on hard replay
            new Object[] {WALSequence.writeLong, 3L, 33L},
            new Object[] {WALSequence.commit}));
    wal.destroyWalFiles();
    wal.close();

    f.delete();
  }
コード例 #2
0
  @Test
  public void preallocate() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.open(WriteAheadLog.NOREPLAY);
    wal.startNextFile();

    wal.walPutPreallocate(111111L);
    wal.seal();

    final AtomicInteger c = new AtomicInteger();

    wal.replayWAL(
        new WriteAheadLog.WALReplay() {
          @Override
          public void beforeReplayStart() {}

          @Override
          public void afterReplayFinished() {}

          @Override
          public void writeLong(long offset, long value) {
            fail();
          }

          @Override
          public void writeRecord(long recid, long walId, Volume vol, long volOffset, int length) {
            fail();
          }

          @Override
          public void writeByteArray(
              long offset, long walId, Volume vol, long volOffset, int length) {
            fail();
          }

          @Override
          public void commit() {
            fail();
          }

          @Override
          public void rollback() {
            fail();
          }

          @Override
          public void writeTombstone(long recid) {
            fail();
          }

          @Override
          public void writePreallocate(long recid) {
            c.incrementAndGet();
            assertEquals(111111L, recid);
          }
        });
    assertEquals(1, c.get());
  }
コード例 #3
0
  @Test
  public void commit() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.open(WriteAheadLog.NOREPLAY);
    wal.walPutLong(111L, 1111L);
    wal.commit();
    wal.seal();

    wal.replayWAL(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeLong, 111L, 1111L},
            new Object[] {WALSequence.commit}));
  }
コード例 #4
0
  @Test
  public void skip_rollback_last_rollback() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.walPutLong(1L, 11L);
    wal.commit();
    long o1 = wal.fileOffset;
    wal.walPutLong(2L, 33L);
    wal.commit();
    long o2 = wal.fileOffset;
    wal.walPutLong(3L, 33L);
    wal.rollback();
    wal.seal();

    assertEquals(o1, wal.skipRollbacks(o1));
    assertEquals(0, wal.skipRollbacks(o2));
  }
コード例 #5
0
  @Test
  public void rollback() {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.open(WriteAheadLog.NOREPLAY);
    wal.startNextFile();

    wal.walPutLong(111L, 1000);
    wal.rollback();
    wal.seal();

    wal.replayWAL(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeLong, 111L, 1000L},
            new Object[] {WALSequence.rollback}));
  }
コード例 #6
0
  @Test
  public void empty_commit() {
    String f = TT.tempDbFile().getPath();
    WriteAheadLog wal = new WriteAheadLog(f);

    byte[] b = TT.randomByteArray(1024);
    wal.walPutRecord(33L, b, 0, b.length);
    wal.commit();
    wal.commit();
    wal.seal();
    wal.close();

    wal = new WriteAheadLog(f);
    wal.open(
        new WALSequence(
            new Object[] {WALSequence.beforeReplayStart},
            new Object[] {WALSequence.writeRecord, 33L, 16L, b},
            new Object[] {WALSequence.commit},
            new Object[] {WALSequence.commit}));
  }
コード例 #7
0
  void testRecord(final long recid, final byte[] data) {
    WriteAheadLog wal = new WriteAheadLog(null);
    wal.open(WriteAheadLog.NOREPLAY);
    wal.startNextFile();

    final AtomicBoolean called = new AtomicBoolean();

    final long pointer = wal.walPutRecord(recid, data, 0, data == null ? 0 : data.length);

    for (int i = 0; i < 1; i++) {
      byte[] val = wal.walGetRecord(pointer, recid);

      if (data == null) assertNull(val);
      else assertTrue(Arrays.equals(data, val));
      wal.seal();
    }

    WriteAheadLog.WALReplay r =
        new WriteAheadLog.WALReplay() {
          @Override
          public void beforeReplayStart() {}

          @Override
          public void afterReplayFinished() {}

          @Override
          public void writeLong(long offset, long value) {
            fail();
          }

          @Override
          public void writeRecord(long recid2, long walId, Volume vol, long volOffset, int length) {

            assertFalse(called.getAndSet(true));

            assertEquals(recid, recid2);
            if (data == null) {
              assertNull(vol);
              assertEquals(walId, 0);
              assertEquals(volOffset, 0);
              assertEquals(length, 0);
            } else {
              byte[] data = new byte[length];
              vol.getData(volOffset, data, 0, data.length);
              assertTrue(Arrays.equals(data, data));
              assertEquals(pointer, walId);
            }
          }

          @Override
          public void writeByteArray(
              long offset2, long walId, Volume vol, long volOffset, int length) {
            fail();
          }

          @Override
          public void commit() {
            fail();
          }

          @Override
          public void rollback() {
            fail();
          }

          @Override
          public void writeTombstone(long recid) {
            fail();
          }

          @Override
          public void writePreallocate(long recid) {
            fail();
          }
        };

    wal.replayWAL(r);

    assertTrue(called.get());
  }