use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.
the class DefaultDiskStorageTest method testEntryIds.
@Test
public void testEntryIds() throws Exception {
DefaultDiskStorage storage = getStorageSupplier(1).get();
final byte[] value1 = new byte[101];
final byte[] value2 = new byte[102];
final byte[] value3 = new byte[103];
value1[80] = 123;
value2[80] = 45;
value3[80] = 67;
writeFileToStorage(storage, "resourceId1", value1);
writeFileToStorage(storage, "resourceId2", value2);
writeFileToStorage(storage, "resourceId3", value3);
// check that resources are retrieved by the right name, before testing getEntries
BinaryResource res1 = storage.getResource("resourceId1", null);
BinaryResource res2 = storage.getResource("resourceId2", null);
BinaryResource res3 = storage.getResource("resourceId3", null);
assertArrayEquals(value1, res1.read());
assertArrayEquals(value2, res2.read());
assertArrayEquals(value3, res3.read());
// obtain entries and sort by name
List<DiskStorage.Entry> entries = new ArrayList<>(storage.getEntries());
Collections.sort(entries, new Comparator<DiskStorage.Entry>() {
@Override
public int compare(DiskStorage.Entry lhs, DiskStorage.Entry rhs) {
return lhs.getId().compareTo(rhs.getId());
}
});
assertEquals(3, entries.size());
assertEquals("resourceId1", entries.get(0).getId());
assertEquals("resourceId2", entries.get(1).getId());
assertEquals("resourceId3", entries.get(2).getId());
assertArrayEquals(value1, entries.get(0).getResource().read());
assertArrayEquals(value2, entries.get(1).getResource().read());
assertArrayEquals(value3, entries.get(2).getResource().read());
}
use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.
the class DefaultDiskStorageTest method testBasicOperations.
@Test
public void testBasicOperations() throws Exception {
DefaultDiskStorage storage = getStorageSupplier(1).get();
final String resourceId1 = "R1";
final String resourceId2 = "R2";
// no file - get should fail
BinaryResource resource1 = storage.getResource(resourceId1, null);
Assert.assertNull(resource1);
// write out the file now
byte[] key1Contents = new byte[] { 0, 1, 2 };
writeToStorage(storage, resourceId1, key1Contents);
// get should succeed now
resource1 = storage.getResource(resourceId1, null);
Assert.assertNotNull(resource1);
File underlyingFile = ((FileBinaryResource) resource1).getFile();
Assert.assertArrayEquals(key1Contents, Files.toByteArray(underlyingFile));
// remove the file now - get should fail again
Assert.assertTrue(underlyingFile.delete());
resource1 = storage.getResource(resourceId1, null);
Assert.assertNull(resource1);
// no file
BinaryResource resource2 = storage.getResource(resourceId2, null);
Assert.assertNull(resource2);
}
use of com.facebook.binaryresource.BinaryResource in project fresco by facebook.
the class DiskStorageCache method insert.
@Override
public BinaryResource insert(CacheKey key, WriterCallback callback) throws IOException {
// Write to a temp file, then move it into place. This allows more parallelism
// when writing files.
SettableCacheEvent cacheEvent = SettableCacheEvent.obtain().setCacheKey(key);
mCacheEventListener.onWriteAttempt(cacheEvent);
String resourceId;
synchronized (mLock) {
// for multiple resource ids associated with the same image, we only write one file
resourceId = CacheKeyUtil.getFirstResourceId(key);
}
cacheEvent.setResourceId(resourceId);
try {
// getting the file is synchronized
DiskStorage.Inserter inserter = startInsert(resourceId, key);
try {
inserter.writeData(callback, key);
// Committing the file is synchronized
BinaryResource resource = endInsert(inserter, key, resourceId);
cacheEvent.setItemSize(resource.size()).setCacheSize(mCacheStats.getSize());
mCacheEventListener.onWriteSuccess(cacheEvent);
return resource;
} finally {
if (!inserter.cleanUp()) {
FLog.e(TAG, "Failed to delete temp file");
}
}
} catch (IOException ioe) {
cacheEvent.setException(ioe);
mCacheEventListener.onWriteException(cacheEvent);
FLog.e(TAG, "Failed inserting a file into the cache", ioe);
throw ioe;
} finally {
cacheEvent.recycle();
}
}
Aggregations