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