@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(); } }); }
@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 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 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); }
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(); }
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();  }