@Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append(client); sb.append('/'); sb.append(vectorClock.toString()); sb.append("/T="); sb.append(date.getTime()); return sb.toString(); } }
@Override public void write(OutputNode node, VectorClock value) throws Exception { node.setValue(value.toString()); } }
/** * Note: This method selects also {@link DatabaseVersionStatus#DIRTY DIRTY}. */ public Map<MultiChunkId, MultiChunkEntry> getMultiChunks(VectorClock vectorClock) { try (PreparedStatement preparedStatement = getStatement("multichunk.select.all.getMultiChunksWithChunksForDatabaseVersion.sql")) { preparedStatement.setString(1, vectorClock.toString()); try (ResultSet resultSet = preparedStatement.executeQuery()) { return createMultiChunkEntriesWithChunks(resultSet); } } catch (SQLException e) { throw new RuntimeException(e); } }
/** * Marks the database version with the given vector clock as DIRTY, i.e. * sets the {@link DatabaseVersionStatus} to {@link DatabaseVersionStatus#DIRTY DIRTY}. * Marking a database version dirty will lead to a deletion in the next sync up * cycle. * * @param vectorClock Identifies the database version to mark dirty */ public void markDatabaseVersionDirty(VectorClock vectorClock) { try (PreparedStatement preparedStatement = getStatement("databaseversion.update.master.markDatabaseVersionDirty.sql")) { preparedStatement.setString(1, DatabaseVersionStatus.DIRTY.toString()); preparedStatement.setString(2, vectorClock.toString()); preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { throw new RuntimeException(e); } }
/** * Queries the SQL database for all chunks that <b>originally appeared</b> in the * database version identified by the given vector clock. * * <p><b>Note:</b> This method does <b>not</b> select all the chunks that are referenced * in the database version. In particular, it <b>does not return</b> chunks that appeared * in previous other database versions. * * @param vectorClock Vector clock that identifies the database version * @return Returns all chunks that originally belong to a database version */ public Map<ChunkChecksum, ChunkEntry> getChunks(VectorClock vectorClock) { try (PreparedStatement preparedStatement = getStatement("chunk.select.all.getChunksForDatabaseVersion.sql")) { preparedStatement.setString(1, vectorClock.toString()); try (ResultSet resultSet = preparedStatement.executeQuery()) { return createChunkEntries(resultSet); } } catch (SQLException e) { throw new RuntimeException(e); } }
/** * Note: Also selects versions marked as {@link DatabaseVersionStatus#DIRTY DIRTY} */ public Map<FileHistoryId, PartialFileHistory> getFileHistoriesWithFileVersions(VectorClock databaseVersionVectorClock, int maxCount) { try (PreparedStatement preparedStatement = getStatement("filehistory.select.all.getFileHistoriesWithFileVersionsByVectorClock.sql")) { preparedStatement.setString(1, databaseVersionVectorClock.toString()); if (maxCount > 0) { preparedStatement.setMaxRows(maxCount); } try (ResultSet resultSet = preparedStatement.executeQuery()) { return createFileHistoriesFromResult(resultSet); } } catch (SQLException e) { throw new RuntimeException(e); } }
/** * Queries the SQL database for all {@link FileContent}s that <b>originally appeared</b> in the * database version identified by the given vector clock. * * <p><b>Note:</b> This method does <b>not</b> select all the file contents that are referenced * in the database version. In particular, it <b>does not return</b> file contents that appeared * in previous other database versions. * * @param vectorClock Vector clock that identifies the database version * @return Returns all {@link FileContent}s that originally belong to a database version */ public Map<FileChecksum, FileContent> getFileContents(VectorClock vectorClock) { try (PreparedStatement preparedStatement = getStatement("filecontent.select.master.getFileContentsWithChunkChecksumsForDatabaseVersion.sql")) { preparedStatement.setString(1, vectorClock.toString()); try (ResultSet resultSet = preparedStatement.executeQuery()) { return createFileContents(resultSet); } } catch (SQLException e) { throw new RuntimeException(e); } }
private long writeDatabaseVersionHeaderInternal(Connection connection, DatabaseVersionHeader databaseVersionHeader) throws SQLException { try (PreparedStatement preparedStatement = connection.prepareStatement( DatabaseConnectionFactory.getStatement("databaseversion.insert.all.writeDatabaseVersion.sql"), Statement.RETURN_GENERATED_KEYS)) { preparedStatement.setString(1, DatabaseVersionStatus.MASTER.toString()); preparedStatement.setTimestamp(2, new Timestamp(databaseVersionHeader.getDate().getTime())); preparedStatement.setString(3, databaseVersionHeader.getClient()); preparedStatement.setString(4, databaseVersionHeader.getVectorClock().toString()); int affectedRows = preparedStatement.executeUpdate(); if (affectedRows == 0) { throw new SQLException("Cannot add database version header. Affected rows is zero."); } try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { if (resultSet.next()) { return resultSet.getLong(1); } else { throw new SQLException("Cannot get new database version ID"); } } } }
@Test public void testToString() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitBBB", 5L); vc1.setClock("UnitAAA", 4L); assertEquals("Expected different serialization", "(UnitAAA4,UnitBBB5)", vc1.toString()); }