@Override protected <T> Collection<T> getAllLatestEntities(EntityType entityType, Function<byte[], T> transform) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); List<T> entities = Lists.newLinkedList(); try { PreparedStatement statement = getSelectAllLatestStatement(conn, entityType); try { ResultSet rs = statement.executeQuery(); try { while (rs.next()) { entities.add(transform.apply(rs.getBytes(1))); } } finally { rs.close(); } } finally { statement.close(); } } finally { conn.close(); } return entities; } catch (SQLException e) { throw new IOException("Exception getting all entities of type " + entityType.name().toLowerCase() + accountErrorSnippet); } }
@Override protected void deleteEntity(EntityType entityType, String entityName) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getDeleteStatementWithoutVersion(conn, entityType, entityName); try { statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception deleting all versions of type " + entityType.name().toLowerCase() + " with name " + entityName + accountErrorSnippet); } }
@Override protected byte[] getEntity(EntityType entityType, String entityName, int entityVersion) throws IOException { try { byte[] entityBytes = null; Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getSelectStatement(conn, entityType, entityName, entityVersion); try { ResultSet rs = statement.executeQuery(); try { if (rs.next()) { entityBytes = rs.getBytes(1); } } finally { rs.close(); } } finally { statement.close(); } } finally { conn.close(); } return entityBytes; } catch (SQLException e) { throw new IOException("Exception getting entity of type " + entityType.name().toLowerCase() + " of name " + entityName + " of version " + entityVersion + accountErrorSnippet); } }
@Override protected int getVersion(EntityType entityType, String entityName) throws IOException { try { Integer version = 1; Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getSelectMaxVersionStatement(conn, entityType, entityName); try { ResultSet rs = statement.executeQuery(); try { if (rs.next()) { version += rs.getInt(1); } } finally { rs.close(); } } finally { statement.close(); } } finally { conn.close(); } return version; } catch (SQLException e) { throw new IOException("Exception getting highest version of entity of type " + entityType.name().toLowerCase() + " of name " + entityName + accountErrorSnippet); } }
@Override protected void deleteEntity(EntityType entityType, String entityName, int entityVersion) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { PreparedStatement statement = getDeleteStatementWithVersion(conn, entityType, entityName, entityVersion); try { statement.executeUpdate(); } finally { statement.close(); } } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception deleting entity of type " + entityType.name().toLowerCase() + " with name " + entityName + " and version " + entityVersion + accountErrorSnippet); } }
@Override protected void writeEntity(EntityType entityType, String entityName, int version, byte[] data) throws IOException { try { Connection conn = dbConnectionPool.getConnection(); try { DBPut entityPut = new EntityDBPut(entityType, entityName, version, data); entityPut.executePut(conn); } finally { conn.close(); } } catch (SQLException e) { throw new IOException("Exception writing entity of type " + entityType.name().toLowerCase() + " with name " + entityName + accountErrorSnippet); } }