use of org.syncany.database.DatabaseVersion in project syncany by syncany.
the class DatabaseVersionSqlDao method createDatabaseVersionFromRow.
protected DatabaseVersion createDatabaseVersionFromRow(ResultSet resultSet, boolean excludeChunkData, int fileHistoryMaxCount) throws SQLException {
DatabaseVersionHeader databaseVersionHeader = createDatabaseVersionHeaderFromRow(resultSet);
DatabaseVersion databaseVersion = new DatabaseVersion();
databaseVersion.setHeader(databaseVersionHeader);
// Add chunk/multichunk/filecontent data
if (!excludeChunkData) {
Map<ChunkChecksum, ChunkEntry> chunks = chunkDao.getChunks(databaseVersionHeader.getVectorClock());
Map<MultiChunkId, MultiChunkEntry> multiChunks = multiChunkDao.getMultiChunks(databaseVersionHeader.getVectorClock());
Map<FileChecksum, FileContent> fileContents = fileContentDao.getFileContents(databaseVersionHeader.getVectorClock());
for (ChunkEntry chunk : chunks.values()) {
databaseVersion.addChunk(chunk);
}
for (MultiChunkEntry multiChunk : multiChunks.values()) {
databaseVersion.addMultiChunk(multiChunk);
}
for (FileContent fileContent : fileContents.values()) {
databaseVersion.addFileContent(fileContent);
}
}
// Add file histories
Map<FileHistoryId, PartialFileHistory> fileHistories = fileHistoryDao.getFileHistoriesWithFileVersions(databaseVersionHeader.getVectorClock(), fileHistoryMaxCount);
for (PartialFileHistory fileHistory : fileHistories.values()) {
databaseVersion.addFileHistory(fileHistory);
}
return databaseVersion;
}
use of org.syncany.database.DatabaseVersion in project syncany by syncany.
the class TestAssertUtil method assertDatabaseEquals.
public static void assertDatabaseEquals(MemoryDatabase expectedDatabase, MemoryDatabase actualDatabase) {
logger.log(Level.INFO, "--");
logger.log(Level.INFO, "Now comparing two databases.");
logger.log(Level.INFO, "DON'T WORRY. This can take a long time or even overload the heap space.");
List<DatabaseVersion> writtenDatabaseVersions = expectedDatabase.getDatabaseVersions();
List<DatabaseVersion> readDatabaseVersions = actualDatabase.getDatabaseVersions();
assertEquals("Different number of database versions.", writtenDatabaseVersions.size(), readDatabaseVersions.size());
for (DatabaseVersion writtenDatabaseVersion : writtenDatabaseVersions) {
DatabaseVersion readDatabaseVersion = null;
for (DatabaseVersion aReadDatabaseVersion : readDatabaseVersions) {
if (aReadDatabaseVersion.equals(writtenDatabaseVersion)) {
readDatabaseVersion = aReadDatabaseVersion;
break;
}
}
assertNotNull("Database version " + writtenDatabaseVersion + " does not exist in read database.", readDatabaseVersion);
assertDatabaseVersionEquals(writtenDatabaseVersion, readDatabaseVersion);
}
logger.log(Level.INFO, "End of comparing databases");
logger.log(Level.INFO, "--");
}
use of org.syncany.database.DatabaseVersion in project syncany by syncany.
the class DatabaseXmlParseHandler method startElement.
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
elementPath += "/" + qName;
if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion")) {
databaseVersion = new DatabaseVersion();
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/time")) {
Date timeValue = new Date(Long.parseLong(attributes.getValue("value")));
databaseVersion.setTimestamp(timeValue);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/client")) {
String clientName = attributes.getValue("name");
databaseVersion.setClient(clientName);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/vectorClock")) {
vectorClock = new VectorClock();
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/header/vectorClock/client")) {
String clientName = attributes.getValue("name");
Long clientValue = Long.parseLong(attributes.getValue("value"));
vectorClock.setClock(clientName, clientValue);
} else if (readType == DatabaseReadType.FULL) {
if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/chunks/chunk")) {
String chunkChecksumStr = attributes.getValue("checksum");
ChunkChecksum chunkChecksum = ChunkChecksum.parseChunkChecksum(chunkChecksumStr);
int chunkSize = Integer.parseInt(attributes.getValue("size"));
ChunkEntry chunkEntry = new ChunkEntry(chunkChecksum, chunkSize);
databaseVersion.addChunk(chunkEntry);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileContents/fileContent")) {
String checksumStr = attributes.getValue("checksum");
long size = Long.parseLong(attributes.getValue("size"));
fileContent = new FileContent();
fileContent.setChecksum(FileChecksum.parseFileChecksum(checksumStr));
fileContent.setSize(size);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileContents/fileContent/chunkRefs/chunkRef")) {
String chunkChecksumStr = attributes.getValue("ref");
fileContent.addChunk(ChunkChecksum.parseChunkChecksum(chunkChecksumStr));
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/multiChunks/multiChunk")) {
String multChunkIdStr = attributes.getValue("id");
MultiChunkId multiChunkId = MultiChunkId.parseMultiChunkId(multChunkIdStr);
long size = Long.parseLong(attributes.getValue("size"));
if (multiChunkId == null) {
throw new SAXException("Cannot read ID from multichunk " + multChunkIdStr);
}
multiChunk = new MultiChunkEntry(multiChunkId, size);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/multiChunks/multiChunk/chunkRefs/chunkRef")) {
String chunkChecksumStr = attributes.getValue("ref");
multiChunk.addChunk(ChunkChecksum.parseChunkChecksum(chunkChecksumStr));
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileHistories/fileHistory")) {
String fileHistoryIdStr = attributes.getValue("id");
FileHistoryId fileId = FileHistoryId.parseFileId(fileHistoryIdStr);
fileHistory = new PartialFileHistory(fileId);
} else if (elementPath.equalsIgnoreCase("/database/databaseVersions/databaseVersion/fileHistories/fileHistory/fileVersions/fileVersion")) {
String fileVersionStr = attributes.getValue("version");
String path = attributes.getValue("path");
String pathEncoded = attributes.getValue("pathEncoded");
String sizeStr = attributes.getValue("size");
String typeStr = attributes.getValue("type");
String statusStr = attributes.getValue("status");
String lastModifiedStr = attributes.getValue("lastModified");
String updatedStr = attributes.getValue("updated");
String checksumStr = attributes.getValue("checksum");
String linkTarget = attributes.getValue("linkTarget");
String dosAttributes = attributes.getValue("dosattrs");
String posixPermissions = attributes.getValue("posixperms");
if (fileVersionStr == null || (path == null && pathEncoded == null) || typeStr == null || statusStr == null || sizeStr == null || lastModifiedStr == null) {
throw new SAXException("FileVersion: Attributes missing: version, path/pathEncoded, type, status, size and last modified are mandatory");
}
// Filter it if it was purged somewhere in the future, see #58
Long fileVersionNum = Long.parseLong(fileVersionStr);
// Go add it!
FileVersion fileVersion = new FileVersion();
fileVersion.setVersion(fileVersionNum);
if (path != null) {
fileVersion.setPath(path);
} else {
try {
fileVersion.setPath(new String(Base64.decodeBase64(pathEncoded), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("Invalid Base64 encoding for filename: " + pathEncoded);
}
}
fileVersion.setType(FileType.valueOf(typeStr));
fileVersion.setStatus(FileStatus.valueOf(statusStr));
fileVersion.setSize(Long.parseLong(sizeStr));
fileVersion.setLastModified(new Date(Long.parseLong(lastModifiedStr)));
if (updatedStr != null) {
fileVersion.setUpdated(new Date(Long.parseLong(updatedStr)));
}
if (checksumStr != null) {
fileVersion.setChecksum(FileChecksum.parseFileChecksum(checksumStr));
}
if (linkTarget != null) {
fileVersion.setLinkTarget(linkTarget);
}
if (dosAttributes != null) {
fileVersion.setDosAttributes(dosAttributes);
}
if (posixPermissions != null) {
fileVersion.setPosixPermissions(posixPermissions);
}
fileHistory.addFileVersion(fileVersion);
}
}
}
use of org.syncany.database.DatabaseVersion in project syncany by syncany.
the class DatabaseXmlWriter method write.
public void write() throws XMLStreamException, IOException {
IndentXmlStreamWriter xmlOut = new IndentXmlStreamWriter(out);
xmlOut.writeStartDocument();
xmlOut.writeStartElement("database");
xmlOut.writeAttribute("version", XML_FORMAT_VERSION);
xmlOut.writeStartElement("databaseVersions");
while (databaseVersions.hasNext()) {
DatabaseVersion databaseVersion = databaseVersions.next();
// Database version
xmlOut.writeStartElement("databaseVersion");
// Header, chunks, multichunks, file contents, and file histories
writeDatabaseVersionHeader(xmlOut, databaseVersion);
writeChunks(xmlOut, databaseVersion.getChunks());
writeMultiChunks(xmlOut, databaseVersion.getMultiChunks());
writeFileContents(xmlOut, databaseVersion.getFileContents());
writeFileHistories(xmlOut, databaseVersion.getFileHistories());
// </databaserVersion>
xmlOut.writeEndElement();
}
// </databaseVersions>
xmlOut.writeEndElement();
// </database>
xmlOut.writeEndElement();
xmlOut.writeEndDocument();
xmlOut.flush();
xmlOut.close();
out.flush();
out.close();
}
use of org.syncany.database.DatabaseVersion in project syncany by syncany.
the class UpOperation method prepareResume.
private void prepareResume() throws Exception {
Collection<Long> versionsToResume = transferManager.loadPendingTransactionList();
boolean hasVersionsToResume = versionsToResume != null && versionsToResume.size() > 0;
if (hasVersionsToResume) {
logger.log(Level.INFO, "Found local transaction to resume.");
logger.log(Level.INFO, "Attempting to find transactionRemoteFile");
remoteTransactionsToResume = attemptResumeTransactions(versionsToResume);
Collection<DatabaseVersion> remoteDatabaseVersionsToResume = attemptResumeDatabaseVersions(versionsToResume);
resuming = remoteDatabaseVersionsToResume != null && remoteTransactionsToResume != null && remoteDatabaseVersionsToResume.size() == remoteTransactionsToResume.size();
if (resuming) {
databaseVersionQueue.addAll(remoteDatabaseVersionsToResume);
// Empty database version is the stopping marker
databaseVersionQueue.add(new DatabaseVersion());
transactionRemoteFileToResume = attemptResumeTransactionRemoteFile();
} else {
transferManager.clearResumableTransactions();
}
} else {
transferManager.clearResumableTransactions();
}
}
Aggregations