FileHistoryId fileId = FileHistoryId.parseFileId(fileHistoryIdStr); fileHistory = new PartialFileHistory(fileId);
private String parsePathExpression(String pathExpression, boolean isFileHistoryId) { if (pathExpression != null) { if (isFileHistoryId) { String randomFileHistoryId = FileHistoryId.secureRandomFileId().toString(); boolean isFullLength = pathExpression.length() == randomFileHistoryId.length(); if (isFullLength) { return pathExpression; } else { return pathExpression + "%"; } } else { if (pathExpression.contains("^") || pathExpression.contains("*")) { return pathExpression.replace('^', '%').replace('*', '%'); } else { return pathExpression + "%"; } } } else { return null; } } }
xmlOut.writeAttribute("id", fileHistory.getFileHistoryId().toString());
String fileContentChecksumStr = (fileVersion.getChecksum() != null) ? fileVersion.getChecksum().toString() : null; preparedStatement.setString(1, fileHistoryId.toString()); preparedStatement.setInt(2, Integer.parseInt("" + fileVersion.getVersion())); preparedStatement.setLong(3, databaseVersionId);
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (!(obj instanceof PartialFileHistory)) { return false; } PartialFileHistory other = (PartialFileHistory) obj; if (fileHistoryId == null) { if (other.fileHistoryId != null) { return false; } } else if (!fileHistoryId.equals(other.fileHistoryId)) { return false; } if (versions == null) { if (other.versions != null) { return false; } } else if (!versions.equals(other.versions)) { return false; } return true; }
protected Map<FileHistoryId, PartialFileHistory> createFileHistoriesFromResult(ResultSet resultSet) throws SQLException { Map<FileHistoryId, PartialFileHistory> fileHistories = new HashMap<FileHistoryId, PartialFileHistory>(); PartialFileHistory fileHistory = null; while (resultSet.next()) { FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet); FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); // Old history (= same filehistory identifier) if (fileHistory != null && fileHistory.getFileHistoryId().equals(fileHistoryId)) { // Same history! fileHistory.addFileVersion(lastFileVersion); } // New history! else { // Add the old history if (fileHistory != null) { fileHistories.put(fileHistory.getFileHistoryId(), fileHistory); } // Create a new one fileHistory = new PartialFileHistory(fileHistoryId); fileHistory.addFileVersion(lastFileVersion); } } // Add the last history if (fileHistory != null) { fileHistories.put(fileHistory.getFileHistoryId(), fileHistory); } return fileHistories; }
public FileVersion createFileVersionFromRow(ResultSet resultSet) throws SQLException { FileVersion fileVersion = new FileVersion(); fileVersion.setFileHistoryId(FileHistoryId.parseFileId(resultSet.getString("filehistory_id"))); fileVersion.setVersion(resultSet.getLong("version")); fileVersion.setPath(resultSet.getString("path")); fileVersion.setType(FileType.valueOf(resultSet.getString("type"))); fileVersion.setStatus(FileStatus.valueOf(resultSet.getString("status"))); fileVersion.setSize(resultSet.getLong("size")); fileVersion.setLastModified(new Date(resultSet.getTimestamp("lastmodified").getTime())); if (resultSet.getString("linktarget") != null) { fileVersion.setLinkTarget(resultSet.getString("linktarget")); } if (resultSet.getString("filecontent_checksum") != null) { FileChecksum fileChecksum = FileChecksum.parseFileChecksum(resultSet.getString("filecontent_checksum")); fileVersion.setChecksum(fileChecksum); } if (resultSet.getString("updated") != null) { fileVersion.setUpdated(new Date(resultSet.getTimestamp("updated").getTime())); } if (resultSet.getString("posixperms") != null) { fileVersion.setPosixPermissions(resultSet.getString("posixperms")); } if (resultSet.getString("dosattrs") != null) { fileVersion.setDosAttributes(resultSet.getString("dosattrs")); } return fileVersion; }
/** * Writes a list of {@link PartialFileHistory}s to the database table <i>filehistory</i> using <tt>INSERT</tt>s * and the given connection. In addition, this method also writes the corresponding {@link FileVersion}s of * each file history to the database using * {@link FileVersionSqlDao#writeFileVersions(Connection, FileHistoryId, long, Collection) FileVersionSqlDao#writeFileVersions}. * * <p><b>Note:</b> This method executes, but <b>does not commit</b> the queries. * * @param connection The connection used to execute the statements * @param databaseVersionId References the {@link PartialFileHistory} to which the list of file versions belongs * @param fileHistories List of {@link PartialFileHistory}s to be written to the database * @throws SQLException If the SQL statement fails */ public void writeFileHistories(Connection connection, long databaseVersionId, Collection<PartialFileHistory> fileHistories) throws SQLException { for (PartialFileHistory fileHistory : fileHistories) { PreparedStatement preparedStatement = getStatement(connection, "filehistory.insert.all.writeFileHistories.sql"); preparedStatement.setString(1, fileHistory.getFileHistoryId().toString()); preparedStatement.setLong(2, databaseVersionId); int affectedRows = preparedStatement.executeUpdate(); if (affectedRows == 0) { throw new SQLException("Cannot add database version header. Affected rows is zero."); } preparedStatement.close(); fileVersionDao.writeFileVersions(connection, fileHistory.getFileHistoryId(), databaseVersionId, fileHistory.getFileVersions().values()); } }
FileHistoryId restoreFileHistory = FileHistoryId.parseFileId(nonOptionArgs.get(0).toString()); operationOptions.setFileHistoryId(restoreFileHistory);
FileHistoryId fileHistoryId = FileHistoryId.parseFileId(concreteRequest.getFileHistoryId()); long version = concreteRequest.getVersion();
public FileHistoryId expandFileHistoryId(FileHistoryId fileHistoryIdPrefix) { String fileHistoryIdPrefixLikeQuery = fileHistoryIdPrefix.toString() + "%"; try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.expandFileHistoryId.sql")) { preparedStatement.setString(1, fileHistoryIdPrefixLikeQuery); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { FileHistoryId fullFileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); boolean nonUniqueResult = resultSet.next(); if (nonUniqueResult) { return null; } else { return fullFileHistoryId; } } else { return null; } } } catch (SQLException e) { throw new RuntimeException(e); } }
/** * Removes all file versions with versions <b>lower or equal</b> than the given file version. * * <p>Note that this method does not just delete the given file version, but also all of its * previous versions. */ public void removeFileVersions(Map<FileHistoryId, FileVersion> purgeFileVersions) throws SQLException { if (purgeFileVersions.size() > 0) { try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeFileVersionsByIds.sql")) { for (Map.Entry<FileHistoryId, FileVersion> purgeFileVersionEntry : purgeFileVersions.entrySet()) { FileHistoryId purgeFileHistoryId = purgeFileVersionEntry.getKey(); FileVersion purgeFileVersion = purgeFileVersionEntry.getValue(); preparedStatement.setString(1, purgeFileHistoryId.toString()); preparedStatement.setLong(2, purgeFileVersion.getVersion()); preparedStatement.addBatch(); } preparedStatement.executeBatch(); } } }
private PartialFileHistory getLastVersionByFileHistoryId(String fileHistoryId) { try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getLastVersionByFileHistoryId.sql")) { preparedStatement.setString(1, fileHistoryId); preparedStatement.setString(2, fileHistoryId); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet); FileHistoryId fileHistoryIdData = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryIdData); fileHistory.addFileVersion(lastFileVersion); return fileHistory; } else { return null; } } } catch (SQLException e) { throw new RuntimeException(e); } }
public PartialFileHistory getFileHistoryWithFileVersions(String relativePath) { try { PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoryWithFileVersions.sql"); preparedStatement.setString(1, relativePath); preparedStatement.setString(2, FileStatus.DELETED.toString()); ResultSet resultSet = preparedStatement.executeQuery(); PartialFileHistory fileHistory = null; while (resultSet.next()) { if (fileHistory == null) { FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); fileHistory = new PartialFileHistory(fileHistoryId); } FileVersion fileVersion = fileVersionDao.createFileVersionFromRow(resultSet); fileHistory.addFileVersion(fileVersion); } return fileHistory; } catch (SQLException e) { throw new RuntimeException(e); } }
public PartialFileHistory getFileHistoryWithLastVersion(String relativePath) { try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoryWithLastVersion.sql")) { preparedStatement.setString(1, relativePath); try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet); PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryId); fileHistory.addFileVersion(lastFileVersion); return fileHistory; } } return null; } catch (SQLException e) { throw new RuntimeException(e); } } }
public List<FileVersion> getFileHistory(FileHistoryId fileHistoryId) { try (PreparedStatement preparedStatement = getStatement("fileversion.select.master.getFileHistoryById.sql")) { preparedStatement.setString(1, fileHistoryId.toString()); List<FileVersion> fileTree = new ArrayList<FileVersion>(); try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { FileVersion fileVersion = createFileVersionFromRow(resultSet); fileTree.add(fileVersion); } return fileTree; } catch (SQLException e) { throw new RuntimeException(e); } } catch (SQLException e) { throw new RuntimeException(e); } }
public List<PartialFileHistory> getFileHistoriesWithLastVersion() { List<PartialFileHistory> fileHistories = new ArrayList<PartialFileHistory>(); try (PreparedStatement preparedStatement = getStatement("filehistory.select.master.getFileHistoriesWithLastVersion.sql")) { try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); FileVersion lastFileVersion = fileVersionDao.createFileVersionFromRow(resultSet); PartialFileHistory fileHistory = new PartialFileHistory(fileHistoryId); fileHistory.addFileVersion(lastFileVersion); fileHistories.add(fileHistory); } } return fileHistories; } catch (SQLException e) { throw new RuntimeException(e); } } }
private Map<FileHistoryId, List<FileVersion>> getAllVersionsInQuery(PreparedStatement preparedStatement) throws SQLException { try (ResultSet resultSet = preparedStatement.executeQuery()) { Map<FileHistoryId, List<FileVersion>> fileHistoryPurgeFileVersions = new HashMap<FileHistoryId, List<FileVersion>>(); while (resultSet.next()) { FileHistoryId fileHistoryId = FileHistoryId.parseFileId(resultSet.getString("filehistory_id")); FileVersion fileVersion = createFileVersionFromRow(resultSet); List<FileVersion> purgeFileVersions = fileHistoryPurgeFileVersions.get(fileHistoryId); if (purgeFileVersions == null) { purgeFileVersions = new ArrayList<FileVersion>(); fileHistoryPurgeFileVersions.put(fileHistoryId, purgeFileVersions); } purgeFileVersions.add(fileVersion); } return fileHistoryPurgeFileVersions; } }
public void removeSpecificFileVersions(Map<FileHistoryId, List<FileVersion>> purgeFileVersions) throws SQLException { if (purgeFileVersions.size() > 0) { try (PreparedStatement preparedStatement = getStatement(connection, "fileversion.delete.all.removeSpecificFileVersionsByIds.sql")) { for (FileHistoryId purgeFileHistoryId : purgeFileVersions.keySet()) { for (FileVersion purgeFileVersion : purgeFileVersions.get(purgeFileHistoryId)) { preparedStatement.setString(1, purgeFileHistoryId.toString()); preparedStatement.setLong(2, purgeFileVersion.getVersion()); preparedStatement.addBatch(); } } preparedStatement.executeBatch(); } } }
public static FileHistoryId secureRandomFileId() { return new FileHistoryId(ObjectId.secureRandomBytes(FILE_HISTORY_ID_LENGTH)); }