From cc70f09431deb7937e01cc6583884fb5067a2994 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 18 Apr 2016 15:18:05 +0200 Subject: More additions for central image store --- .../org/openslx/filetransfer/util/HashChecker.java | 24 +++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src/main/java/org/openslx/filetransfer/util/HashChecker.java') diff --git a/src/main/java/org/openslx/filetransfer/util/HashChecker.java b/src/main/java/org/openslx/filetransfer/util/HashChecker.java index 5b647aa..d9db7df 100644 --- a/src/main/java/org/openslx/filetransfer/util/HashChecker.java +++ b/src/main/java/org/openslx/filetransfer/util/HashChecker.java @@ -75,7 +75,16 @@ public class HashChecker } } - public void queue( FileChunk chunk, byte[] data, HashCheckCallback callback ) throws InterruptedException + /** + * Queue the given chunk for hashing. The chunk should be in pending state. + * + * @param chunk chunk to hash + * @param data binary data of this chunk + * @param callback callback to call when hashing is done + * @return true if the chunk was handled, false if the queue was full and rejected the chunk. + * @throws InterruptedException + */ + public boolean queue( FileChunk chunk, byte[] data, HashCheckCallback callback, boolean blocking ) throws InterruptedException { byte[] sha1Sum = chunk.getSha1Sum(); if ( sha1Sum == null ) @@ -84,7 +93,7 @@ public class HashChecker synchronized ( threads ) { if ( invalid ) { execCallback( task, HashResult.FAILURE ); - return; + return true; } if ( queue.remainingCapacity() <= 1 && threads.size() < Runtime.getRuntime().availableProcessors() ) { try { @@ -95,8 +104,18 @@ public class HashChecker LOGGER.warn( "Could not create additional hash checking thread", e ); } } + } + ChunkStatus old = chunk.getStatus(); + chunk.setStatus( ChunkStatus.HASHING ); + if ( blocking ) { queue.put( task ); + } else { + if ( !queue.offer( task ) ) { + chunk.setStatus( old ); + return false; + } } + return true; } // ############################################################# \\ @@ -171,7 +190,6 @@ public class HashChecker this.data = data; this.chunk = chunk; this.callback = callback; - chunk.setStatus( ChunkStatus.HASHING ); } } -- cgit v1.2.3-55-g7522