AbstractBinaryStore implements BinaryStore { @Override public BinaryValue storeValue( InputStream stream, String hint, boolean markAsUnused ) throws BinaryStoreException { return storeValue(stream, markAsUnused); }  static int bestBufferSize( long fileSize ); @Override long getMinimumBinarySizeInBytes(); @Override void setMinimumBinarySizeInBytes( long minSizeInBytes ); @Override void setTextExtractors( TextExtractors textExtractors ); @Override void setMimeTypeDetector( MimeTypeDetector mimeTypeDetector ); @Override final String getText( BinaryValue binary ); @Override String getMimeType( BinaryValue binary,
                               String name ); @Override boolean hasBinary( BinaryKey key ); abstract void storeExtractedText( BinaryValue source,
                                             String extractedText ); abstract String getExtractedText( BinaryValue source ); @Override BinaryValue storeValue( InputStream stream, String hint, boolean markAsUnused ); @Override void start(); @Override void shutdown();  }
@Test public void shouldCleanupUnunsedValues() throws Exception { getBinaryStore().storeValue(new ByteArrayInputStream(IN_MEMORY_BINARY), false); List<BinaryKey> keys = new ArrayList<BinaryKey>(); keys.add(IN_MEMORY_KEY); getBinaryStore().markAsUnused(keys); Thread.sleep(100); getBinaryStore().removeValuesUnusedLongerThan(1, TimeUnit.MILLISECONDS); try { getBinaryStore().getInputStream(IN_MEMORY_KEY); fail("Key was not removed"); } catch (BinaryStoreException ex) { } }
@Test @FixFor( "MODE-2302" ) public void shouldMarkBinariesAsUsed() throws Exception { BinaryStore binaryStore = getBinaryStore(); binaryStore.storeValue(new ByteArrayInputStream(IN_MEMORY_BINARY), false); binaryStore.markAsUnused(Arrays.asList(IN_MEMORY_KEY)); Thread.sleep(100); binaryStore.markAsUsed(Arrays.asList(IN_MEMORY_KEY)); Thread.sleep(2); binaryStore.removeValuesUnusedLongerThan(1, TimeUnit.MILLISECONDS); InputStream is = binaryStore.getInputStream(IN_MEMORY_KEY); assertNotNull(is); }
@Test @FixFor("MODE-2547") public void shouldStoreBinariesConcurrently() throws Exception { int binariesCount = 11; List<byte[]> bytesToStore = new ArrayList<>(binariesCount); for (int i = 0; i < binariesCount; i++) { byte[] data = new byte[LARGE_BINARY_SIZE]; RANDOM.nextBytes(data); bytesToStore.add(data); } ExecutorService executorService = Executors.newFixedThreadPool(binariesCount); try { final CyclicBarrier barrier = new CyclicBarrier(binariesCount + 1); List<Future<BinaryValue>> results = new ArrayList<>(binariesCount); for (final byte[] data : bytesToStore) { Future<BinaryValue> result = executorService.submit(() -> { barrier.await(); return getBinaryStore().storeValue(new ByteArrayInputStream(data), false); }); results.add(result); } barrier.await(); List<BinaryValue> storedBinaries = new ArrayList<>(binariesCount); for (Future<BinaryValue> result : results) { storedBinaries.add(result.get(2, TimeUnit.SECONDS)); } for (int i = 0; i < binariesCount; i++) { byte[] expectedBytes = bytesToStore.get(i); byte[] actualBytes = IoUtil.readBytes(storedBinaries.get(i).getStream()); Assert.assertArrayEquals("Invalid binary found", expectedBytes, actualBytes); } } finally { executorService.shutdownNow(); } }