diff options
author | Simon Rettberg | 2017-01-13 13:06:35 +0100 |
---|---|---|
committer | Simon Rettberg | 2017-01-13 13:06:35 +0100 |
commit | 058fbe7faeedf83a47e9b675e589a6017917f96d (patch) | |
tree | 7a4643340c326e677fbbcf32fc271b9e854cbaad | |
parent | [server] Refine mail template parsing; fix too many mails being sent for inva... (diff) | |
download | tutor-module-058fbe7faeedf83a47e9b675e589a6017917f96d.tar.gz tutor-module-058fbe7faeedf83a47e9b675e589a6017917f96d.tar.xz tutor-module-058fbe7faeedf83a47e9b675e589a6017917f96d.zip |
[server] DbImageBlock: Formatting
-rw-r--r-- | dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImageBlock.java | 127 |
1 files changed, 64 insertions, 63 deletions
diff --git a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImageBlock.java b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImageBlock.java index bdaa356e..4e68da6d 100644 --- a/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImageBlock.java +++ b/dozentenmodulserver/src/main/java/org/openslx/bwlp/sat/database/mappers/DbImageBlock.java @@ -16,101 +16,103 @@ import org.openslx.filetransfer.util.ChunkStatus; import org.openslx.filetransfer.util.FileChunk; public class DbImageBlock { - + private static final Logger LOGGER = Logger.getLogger(DbImageBlock.class); - + private static AsyncThread asyncBlockUpdate = null; - private static synchronized void initAsyncThread() - { - if ( asyncBlockUpdate == null ) { + private static synchronized void initAsyncThread() { + if (asyncBlockUpdate == null) { asyncBlockUpdate = new AsyncThread(); asyncBlockUpdate.start(); } } - public static void asyncUpdate( String imageVersionId, FileChunk chunk ) throws InterruptedException - { + public static void asyncUpdate(String imageVersionId, FileChunk chunk) throws InterruptedException { initAsyncThread(); - asyncBlockUpdate.put( new ChunkUpdate( imageVersionId, chunk.range, chunk.getStatus() != ChunkStatus.COMPLETE ) ); + asyncBlockUpdate.put(new ChunkUpdate(imageVersionId, chunk.range, + chunk.getStatus() != ChunkStatus.COMPLETE)); } - private static class AsyncThread extends Thread - { - private final ArrayBlockingQueue<ChunkUpdate> queue = new ArrayBlockingQueue<>( 100 ); + private static class AsyncThread extends Thread { + private final ArrayBlockingQueue<ChunkUpdate> queue = new ArrayBlockingQueue<>(100); - public void put( ChunkUpdate chunk ) throws InterruptedException - { - queue.put( chunk ); + public void put(ChunkUpdate chunk) throws InterruptedException { + queue.put(chunk); } @Override - public void run() - { + public void run() { try { - while ( !interrupted() ) { + while (!interrupted()) { ChunkUpdate chunk = queue.take(); - Thread.sleep( 100 ); - try ( MysqlConnection connection = Database.getConnection() ) { - MysqlStatement stmt = connection.prepareStatement( "UPDATE imageblock SET ismissing = :ismissing" - + " WHERE imageversionid = :imageversionid AND startbyte = :startbyte AND blocksize = :blocksize" ); + Thread.sleep(100); + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("UPDATE imageblock SET ismissing = :ismissing" + + " WHERE imageversionid = :imageversionid AND startbyte = :startbyte AND blocksize = :blocksize"); do { - stmt.setBoolean( "ismissing", chunk.isMissing ); - stmt.setString( "imageversionid", chunk.imageVersionId ); - stmt.setLong( "startbyte", chunk.range.startOffset ); - stmt.setInt( "blocksize", chunk.range.getLength() ); + stmt.setBoolean("ismissing", chunk.isMissing); + stmt.setString("imageversionid", chunk.imageVersionId); + stmt.setLong("startbyte", chunk.range.startOffset); + stmt.setInt("blocksize", chunk.range.getLength()); stmt.executeUpdate(); chunk = queue.poll(); - } while ( chunk != null ); + } while (chunk != null); connection.commit(); - } catch ( SQLException e ) { - LOGGER.error( "Query failed in DbImageBlock.AsyncThread.run()", e ); + } catch (SQLException e) { + LOGGER.error("Query failed in DbImageBlock.AsyncThread.run()", e); continue; } - Thread.sleep( 2000 ); + Thread.sleep(2000); } - } catch ( InterruptedException e ) { - LOGGER.debug( "async thread interrupted" ); + } catch (InterruptedException e) { + LOGGER.debug("async thread interrupted"); interrupt(); } } } - private static class ChunkUpdate - { + private static class ChunkUpdate { public final String imageVersionId; public final FileRange range; public final boolean isMissing; - public ChunkUpdate( String imageVersionId, FileRange range, boolean isMissing ) - { + public ChunkUpdate(String imageVersionId, FileRange range, boolean isMissing) { this.imageVersionId = imageVersionId; this.range = range; this.isMissing = isMissing; } } - public static void insertChunkList( String imageVersionId, List<FileChunk> all, boolean missing ) throws SQLException - { - try ( MysqlConnection connection = Database.getConnection() ) { - MysqlStatement stmt = connection.prepareStatement( "INSERT IGNORE INTO imageblock" + public static void insertChunkList(String imageVersionId, List<FileChunk> all, boolean missing) + throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("INSERT IGNORE INTO imageblock" + " (imageversionid, startbyte, blocksize, blocksha1, ismissing) VALUES" - + " (:imageversionid, :startbyte, :blocksize, :blocksha1, :ismissing)" ); - stmt.setString( "imageversionid", imageVersionId ); - stmt.setBoolean( "ismissing", missing ); - for ( FileChunk chunk : all ) { - stmt.setLong( "startbyte", chunk.range.startOffset ); - stmt.setInt( "blocksize", chunk.range.getLength() ); - stmt.setBinary( "blocksha1", chunk.getSha1Sum() ); + + " (:imageversionid, :startbyte, :blocksize, :blocksha1, :ismissing)"); + stmt.setString("imageversionid", imageVersionId); + stmt.setBoolean("ismissing", missing); + for (FileChunk chunk : all) { + stmt.setLong("startbyte", chunk.range.startOffset); + stmt.setInt("blocksize", chunk.range.getLength()); + stmt.setBinary("blocksha1", chunk.getSha1Sum()); stmt.executeUpdate(); } connection.commit(); - } catch ( SQLException e ) { - LOGGER.error( "Query failed in DbImageBlock.insertChunkList()", e ); + } catch (SQLException e) { + LOGGER.error("Query failed in DbImageBlock.insertChunkList()", e); throw e; } } + /** + * Get list of block hashes for an image version id. The hashes, as usual, + * refer to 16MiB blocks. If hashes are missing, nulls will be inserted into + * the list, since otherwise there is no way to reconstruct the offset of + * the block in the file. Note however that missing hashes at the end of the + * list will not be added as nulls, so there still could be less hashes in + * the list than blocks in the file. + */ static List<ByteBuffer> getBlockHashes(MysqlConnection connection, String imageVersionId) throws SQLException { MysqlStatement stmt = connection.prepareStatement("SELECT startbyte, blocksha1 FROM imageblock" @@ -144,31 +146,30 @@ public class DbImageBlock { } } - public static List<Boolean> getMissingStatusList( String imageVersionId ) throws SQLException - { - try ( MysqlConnection connection = Database.getConnection() ) { - MysqlStatement stmt = connection.prepareStatement( "SELECT startbyte, ismissing FROM imageblock" - + " WHERE imageversionid = :imageversionid ORDER BY startbyte ASC" ); - stmt.setString( "imageversionid", imageVersionId ); + public static List<Boolean> getMissingStatusList(String imageVersionId) throws SQLException { + try (MysqlConnection connection = Database.getConnection()) { + MysqlStatement stmt = connection.prepareStatement("SELECT startbyte, ismissing FROM imageblock" + + " WHERE imageversionid = :imageversionid ORDER BY startbyte ASC"); + stmt.setString("imageversionid", imageVersionId); ResultSet rs = stmt.executeQuery(); List<Boolean> list = new ArrayList<>(); long expectedOffset = 0; - while ( rs.next() ) { - long currentOffset = rs.getLong( "startbyte" ); - if ( currentOffset < expectedOffset ) + while (rs.next()) { + long currentOffset = rs.getLong("startbyte"); + if (currentOffset < expectedOffset) continue; - while ( currentOffset > expectedOffset ) { - list.add( Boolean.TRUE ); + while (currentOffset > expectedOffset) { + list.add(Boolean.TRUE); expectedOffset += FileChunk.CHUNK_SIZE; } - if ( currentOffset == expectedOffset ) { - list.add( rs.getBoolean( "ismissing" ) ); + if (currentOffset == expectedOffset) { + list.add(rs.getBoolean("ismissing")); expectedOffset += FileChunk.CHUNK_SIZE; } } return list; - } catch ( SQLException e ) { - LOGGER.error( "Query failed in DbImageBlock.getBlockStatuses()", e ); + } catch (SQLException e) { + LOGGER.error("Query failed in DbImageBlock.getBlockStatuses()", e); throw e; } } |