private boolean haveRecordForBlob(final Blob blob) { final String fullBlobId = blob.getContentIdentity(); if (!Strings.isNullOrEmpty(fullBlobId) && !InMemoryDataRecord.isInstance(fullBlobId)) { String blobId = DataStoreBlobStore.BlobId.of(fullBlobId).getBlobId(); return cachingDataStore.exists(new DataIdentifier(blobId)); } return false; }
@Override public InputStream getInputStream(final String encodedBlobId) throws IOException { final BlobId blobId = BlobId.of(encodedBlobId); if (encodeLengthInId && blobId.hasLengthInfo() && blobId.length <= maxCachedBinarySize) { try { byte[] content = cache.get(blobId.blobId, new Callable<byte[]>() { @Override public byte[] call() throws Exception { boolean threw = true; InputStream stream = getStream(blobId.blobId); try { byte[] result = IOUtils.toByteArray(stream); threw = false; return result; } finally { Closeables.close(stream, threw); } } }); return new ByteArrayInputStream(content); } catch (ExecutionException e) { log.warn("Error occurred while loading bytes from steam while fetching for id {}", encodedBlobId, e); } } return getStream(blobId.blobId); }
@Override public InputStream getInputStream(final String encodedBlobId) throws IOException { final BlobId blobId = BlobId.of(encodedBlobId); if (encodeLengthInId && blobId.hasLengthInfo() && blobId.length <= maxCachedBinarySize) { try { byte[] content = cache.get(blobId.blobId, new Callable<byte[]>() { @Override public byte[] call() throws Exception { boolean threw = true; InputStream stream = getStream(blobId.blobId); try { byte[] result = IOUtils.toByteArray(stream); threw = false; return result; } finally { Closeables.close(stream, threw); } } }); return new ByteArrayInputStream(content); } catch (ExecutionException e) { log.warn("Error occurred while loading bytes from steam while fetching for id {}", encodedBlobId, e); } } return getStream(blobId.blobId); }
@Override public InputStream getInputStream(final String encodedBlobId) throws IOException { final BlobId blobId = BlobId.of(encodedBlobId); if (encodeLengthInId && blobId.hasLengthInfo() && blobId.length <= maxCachedBinarySize) { try { byte[] content = cache.get(blobId.blobId, new Callable<byte[]>() { @Override public byte[] call() throws Exception { boolean threw = true; InputStream stream = getStream(blobId.blobId); try { byte[] result = IOUtils.toByteArray(stream); threw = false; return result; } finally { Closeables.close(stream, threw); } } }); return new ByteArrayInputStream(content); } catch (ExecutionException e) { log.warn("Error occurred while loading bytes from steam while fetching for id {}", encodedBlobId, e); } } return getStream(blobId.blobId); }
assertEquals(testDI.toString(), BlobId.of(ds.writeBlob(new ByteArrayInputStream(data))).blobId);
@Test public void testInlineBinary() throws DataStoreException, IOException { int maxInlineSize = 300; DataStore mockedDS = mock(DataStore.class); when(mockedDS.getMinRecordLength()).thenReturn(maxInlineSize); DataStoreBlobStore ds = new DataStoreBlobStore(mockedDS); byte[] data = new byte[maxInlineSize]; new Random().nextBytes(data); DataRecord dr = ds.addRecord(new ByteArrayInputStream(data)); assertTrue(InMemoryDataRecord.isInstance(dr.getIdentifier().toString())); assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(data), dr.getStream())); assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(data), new BlobStoreInputStream(ds, dr.getIdentifier().toString(), 0))); assertEquals(dr, ds.getRecordIfStored(dr.getIdentifier())); assertEquals(dr, ds.getRecord(dr.getIdentifier())); //Check for BlobStore methods assertEquals(maxInlineSize, ds.getBlobLength(dr.getIdentifier().toString())); assertEquals(dr.getIdentifier().toString(), BlobId.of(ds.writeBlob(new ByteArrayInputStream(data))).blobId); }
@Test public void testReference() throws DataStoreException, IOException { String reference = "testReference"; String blobId = "test"; DataIdentifier testDI = new DataIdentifier(blobId); DataRecord testDR = new ByteArrayDataRecord("foo".getBytes(), testDI, reference); DataStore mockedDS = mock(DataStore.class); when(mockedDS.getRecordFromReference(reference)).thenReturn(testDR); when(mockedDS.getRecord(testDI)).thenReturn(testDR); when(mockedDS.getRecordIfStored(testDI)).thenReturn(testDR); DataStoreBlobStore ds = new DataStoreBlobStore(mockedDS); assertEquals(reference,ds.getReference(blobId)); assertEquals(blobId, BlobId.of(ds.getBlobId(reference)).blobId); assertEquals(BlobId.of(testDR).encodedValue(),ds.getBlobId(reference)); String inMemBlobId = InMemoryDataRecord.getInstance("foo".getBytes()) .getIdentifier().toString(); //For in memory record the reference should be null assertNull(ds.getReference(inMemBlobId)); }
public static BlobId of(String encodedValue) { return new BlobId(encodedValue); }
public static BlobId of(String encodedValue) { return new BlobId(encodedValue); }
public static BlobId of(String encodedValue) { return new BlobId(encodedValue); }
static BlobId of(DataRecord dr) { return new BlobId(dr); } }
@Test public void testEncodedBlobId() throws Exception{ BlobId blobId = new BlobId("abc"+BlobId.SEP+"123"); assertEquals("abc", blobId.blobId); assertEquals(123, blobId.length); blobId = new BlobId("abc"+BlobId.SEP+"abc"+BlobId.SEP+"123"); assertEquals("abc"+BlobId.SEP+"abc", blobId.blobId); assertEquals(123, blobId.length); blobId = new BlobId("abc",123); assertEquals("abc"+BlobId.SEP+"123", blobId.encodedValue()); assertTrue(BlobId.isEncoded("abc"+BlobId.SEP+"123")); assertFalse(BlobId.isEncoded("abc")); }
@Override public long getBlobLength(String encodedBlobId) throws IOException { try { checkNotNull(encodedBlobId, "BlobId must be specified"); BlobId id = BlobId.of(encodedBlobId); if (encodeLengthInId && id.hasLengthInfo()) { return id.length; } return getDataRecord(id.blobId).getLength(); } catch (DataStoreException e) { throw new IOException(e); } }
static BlobId of(DataRecord dr) { return new BlobId(dr); } }
protected String extractBlobId(String encodedBlobId) { if (encodeLengthInId) { return BlobId.of(encodedBlobId).blobId; } return encodedBlobId; }
String encodedValue() { if (hasLengthInfo()) { return blobId + SEP + String.valueOf(length); } else { return blobId; } }
protected String extractBlobId(String encodedBlobId) { if (encodeLengthInId) { return BlobId.of(encodedBlobId).blobId; } return encodedBlobId; }