OnDiskSnapshotsStore implements SnapshotsStore { @Override public @Nullable ExtendedSnapshot getLatestSnapshot() throws StorageException { checkInitialized(); try { SnapshotMetadata metadata = dbi.withHandle(new HandleCallback<SnapshotMetadata>() { @Override public SnapshotMetadata withHandle(Handle handle) throws Exception { SnapshotsDAO dao = handle.attach(SnapshotsDAO.class); return dao.getLatestSnapshot(); } }); if (metadata == null) { return null; } Path snapshot = Paths.get(snapshotsDirectory, metadata.getFilename()); if (Files.notExists(snapshot)) { throw new StorageException("snapshot not found", new FileNotFoundException(snapshot.toAbsolutePath().toString())); } return new SnapshotFileReader(snapshotsDirectory, metadata.getFilename(), metadata.getLastTerm(), metadata.getLastIndex()); } catch (StorageException e) { throw e; } catch (CallbackFailedException e) { throw new StorageException("fail get latest snapshot", e.getCause()); } catch (Exception e) { throw new StorageException("fail get latest snapshot", e); } } OnDiskSnapshotsStore(DBI dbi, String snapshotsDirectory); void initialize(); void teardown(); @Override ExtendedSnapshotWriter newSnapshotWriter(); @SuppressWarnings("ConstantConditions") @Override void storeSnapshot(ExtendedSnapshotWriter snapshotWriter); @Override @Nullable ExtendedSnapshot getLatestSnapshot(); List<SnapshotMetadata> getAllSnapshotsFromLatestToOldest(); void pruneSnapshots(final int numSnapshotsToKeep); void reconcileSnapshots();  }
@Test public void shouldReturnNullIfNoSnapshotsExist() throws StorageException { Snapshot snapshot = snapshotsStore.getLatestSnapshot(); assertThat(snapshot, nullValue()); }
@Test public void shouldReturnTheLatestSnapshotIfSnapshotsExist() throws StorageException, IOException, InterruptedException { createSnapshots(10); long finalSnapshotTerm = 10; long finalSnapshotIndex = 10; byte[] finalSnapshotData = Integer.toString(10).getBytes(Charsets.US_ASCII.toString()); ExtendedSnapshot snapshot = snapshotsStore.getLatestSnapshot(); if (snapshot == null) { throw new IllegalStateException("a snapshot should exist"); } assertThat(snapshot.getTerm(), equalTo(finalSnapshotTerm)); assertThat(snapshot.getIndex(), equalTo(finalSnapshotIndex)); InputStream is = null; try { is = snapshot.getSnapshotInputStream(); int bytesAvailable = is.available(); byte[] buffer = new byte[bytesAvailable]; int read = is.read(buffer); assertThat(read, equalTo(bytesAvailable)); assertThat(Arrays.equals(buffer, finalSnapshotData), is(true)); } finally { Closeables.close(is, true); } }
@Test public void shouldThrowStorageExceptionIfDatabaseCallFailsInCallToGetLatestSnapshot() throws Exception { throwWhenDAOMethodCalled(new SnapshotStoreMethodCallable() { @Override public void setup(OnDiskSnapshotsStore onDiskSnapshotsStore, SnapshotsDAO mockDAO, Throwable toThrow) throws Exception { when(mockDAO.getLatestSnapshot()).thenThrow(toThrow); } @Override public void runThrowingMethod(OnDiskSnapshotsStore onDiskSnapshotsStore) throws Exception { onDiskSnapshotsStore.getLatestSnapshot(); } }); }
@Test public void shouldThrowStorageExceptionIfExceptionThrownInCallToGetLatestSnapshot() throws Exception { throwOnDBIOpen(new SnapshotStoreMethodSimpleCallable() { @Override public void setup(OnDiskSnapshotsStore onDiskSnapshotsStore) throws Exception { } @Override public void runThrowingMethod(OnDiskSnapshotsStore onDiskSnapshotsStore) throws Exception { onDiskSnapshotsStore.getLatestSnapshot(); } }); }