diff options
author | Simon Rettberg | 2015-09-01 16:26:09 +0200 |
---|---|---|
committer | Simon Rettberg | 2015-09-01 16:26:09 +0200 |
commit | 802a564dae4e41d149d307bfd1265d30e02ce792 (patch) | |
tree | 83334c36451ac0e47c8402ba1ae6df89bbb4d843 /dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java | |
parent | [client] ImageDetailsWindow: only show popup menu item if image-version is ac... (diff) | |
download | tutor-module-802a564dae4e41d149d307bfd1265d30e02ce792.tar.gz tutor-module-802a564dae4e41d149d307bfd1265d30e02ce792.tar.xz tutor-module-802a564dae4e41d149d307bfd1265d30e02ce792.zip |
[server] Store block hashes in DB
Diffstat (limited to 'dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java')
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java index c8e5fd2a..8a10fb85 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImage.java @@ -1,6 +1,7 @@ package org.openslx.bwlp.sat.database.mappers; import java.io.File; +import java.nio.ByteBuffer; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; @@ -13,6 +14,7 @@ import org.openslx.bwlp.sat.database.Database; import org.openslx.bwlp.sat.database.MysqlConnection; import org.openslx.bwlp.sat.database.MysqlStatement; import org.openslx.bwlp.sat.database.Paginator; +import org.openslx.bwlp.sat.database.models.ImageVersionMeta; import org.openslx.bwlp.sat.database.models.LocalImageVersion; import org.openslx.bwlp.sat.mail.MailGenerator; import org.openslx.bwlp.sat.permissions.User; @@ -29,6 +31,7 @@ import org.openslx.bwlp.thrift.iface.ShareMode; import org.openslx.bwlp.thrift.iface.TNotFoundException; import org.openslx.bwlp.thrift.iface.UserInfo; import org.openslx.filetransfer.util.ChunkList; +import org.openslx.filetransfer.util.FileChunk; import org.openslx.util.QuickTimer; import org.openslx.util.QuickTimer.Task; @@ -103,6 +106,7 @@ public class DbImage { rs.getLong("createtime"), rs.getLong("updatetime"), rs.getString("ownerid"), rs.getString("updaterid"), toShareMode(rs.getString("sharemode")), rs.getByte("istemplate") != 0, defaultPermissions); + image.setUserPermissions(DbImagePermissions.fromResultSetUser(rs)); User.setCombinedUserPermissions(image, user); return image; } catch (SQLException e) { @@ -536,16 +540,11 @@ public class DbImage { verStmt.setBinary("mastersha1", null); // TODO verStmt.setBinary("virtualizerconfig", machineDescription); verStmt.executeUpdate(); - // TODO: Write chunk hashes to DB - // Make this version the latest version - MysqlStatement baseStmt = connection.prepareStatement("UPDATE imagebase SET" - + " latestversionid = :imageversionid WHERE imagebaseid = :imagebaseid LIMIT 1"); - baseStmt.setString("imageversionid", imageVersionId); - baseStmt.setString("imagebaseid", imageBaseId); - baseStmt.executeUpdate(); + writeChunks(connection, imageVersionId, chunks); LocalImageVersion liv = new LocalImageVersion(imageVersionId, imageBaseId, filePath, fileSize, owner.userId, nowSecs, expireTime, true); DbLecture.autoUpdateUsedImage(connection, imageBaseId, liv); + // Make this version the latest version setLatestVersion(connection, imageBaseId, liv); connection.commit(); } catch (SQLException e) { @@ -554,6 +553,26 @@ public class DbImage { } } + private static void writeChunks(MysqlConnection connection, String imageVersionId, ChunkList chunks) + throws SQLException { + if (chunks == null || chunks.isEmpty()) + return; + for (FileChunk chunk : chunks.getAll()) { + if (chunk.getSha1Sum() == null) + return; + } + MysqlStatement stmt = connection.prepareStatement("INSERT IGNORE INTO imageblock" + + " (imageversionid, startbyte, blocksize, blocksha1, ismissing) VALUES" + + " (:imageversionid, :startbyte, :blocksize, :blocksha1, 0)"); + stmt.setString("imageversionid", imageVersionId); + for (FileChunk chunk : chunks.getAll()) { + stmt.setLong("startbyte", chunk.range.startOffset); + stmt.setInt("blocksize", chunk.range.getLength()); + stmt.setBinary("blocksha1", chunk.getSha1Sum()); + stmt.executeUpdate(); + } + } + protected static void markValid(MysqlConnection connection, boolean valid, LocalImageVersion... imageVersion) throws SQLException { if (imageVersion == null || imageVersion.length == 0) @@ -781,4 +800,35 @@ public class DbImage { } } + public static ImageVersionMeta getVersionDetails(String imageVersionId) throws SQLException, + TNotFoundException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("SELECT" + + " imageversionid, imagebaseid, virtualizerconfig FROM imageversion" + + " WHERE imageversionid = :imageversionid"); + stmt.setString("imageversionid", imageVersionId); + ResultSet rs = stmt.executeQuery(); + if (!rs.next()) + throw new TNotFoundException(); + return new ImageVersionMeta(imageVersionId, rs.getString("imagebaseid"), + rs.getBytes("virtualizerconfig"), getBlockHashes(connection, imageVersionId)); + } catch (SQLException e) { + LOGGER.error("Query failed in DbImage.getVersionDetails()", e); + throw e; + } + } + + private static List<ByteBuffer> getBlockHashes(MysqlConnection connection, String imageVersionId) + throws SQLException { + MysqlStatement stmt = connection.prepareStatement("SELECT blocksha1 FROM imageblock" + + " WHERE imageversionid = :imageversionid ORDER BY startbyte ASC"); + stmt.setString("imageversionid", imageVersionId); + ResultSet rs = stmt.executeQuery(); + List<ByteBuffer> list = new ArrayList<>(); + while (rs.next()) { + list.add(ByteBuffer.wrap(rs.getBytes("blocksha1"))); + } + return list; + } + } |