Commit f4daed73 authored by Mark Friedman's avatar Mark Friedman

Store .blk files in Blobstore

parent c9889701
...@@ -922,6 +922,8 @@ public class ObjectifyStorageIo implements StorageIo { ...@@ -922,6 +922,8 @@ public class ObjectifyStorageIo implements StorageIo {
if (useBlobstore) { if (useBlobstore) {
fd.isBlob = true; fd.isBlob = true;
fd.blobstorePath = uploadToBlobstore(content); fd.blobstorePath = uploadToBlobstore(content);
// If the content was previously stored in the datastore, clear it out.
fd.content = null;
} else { } else {
fd.content = content; fd.content = content;
} }
...@@ -984,8 +986,11 @@ public class ObjectifyStorageIo implements StorageIo { ...@@ -984,8 +986,11 @@ public class ObjectifyStorageIo implements StorageIo {
return blobstoreFile.getFullPath(); return blobstoreFile.getFullPath();
} }
private boolean useBlobstoreForFile(String fileName) { @VisibleForTesting
return fileName.contains("assets/") || fileName.endsWith(".apk"); boolean useBlobstoreForFile(String fileName) {
return fileName.contains("assets/")
|| fileName.endsWith(".apk")
|| (fileName.contains("src/") && fileName.endsWith(".blk"));
} }
@Override @Override
......
...@@ -16,7 +16,7 @@ import java.io.UnsupportedEncodingException; ...@@ -16,7 +16,7 @@ import java.io.UnsupportedEncodingException;
import java.util.Arrays; import java.util.Arrays;
/** /**
* Tests for {@link Objectifystorage}. * Tests for {@link ObjectifyStorageIo}.
* *
* @author sharon@google.com (Sharon Perl) * @author sharon@google.com (Sharon Perl)
*/ */
...@@ -44,6 +44,8 @@ public class ObjectifyStorageIoTest extends LocalDatastoreTestCase { ...@@ -44,6 +44,8 @@ public class ObjectifyStorageIoTest extends LocalDatastoreTestCase {
private static final byte[] ASSET_FILE_CONTENT1 = { (byte) 0, (byte) 1, (byte) 32, (byte) 255}; private static final byte[] ASSET_FILE_CONTENT1 = { (byte) 0, (byte) 1, (byte) 32, (byte) 255};
private static final String APK_FILE_NAME1 = "/ode/build/Android/HelloPurr.apk"; private static final String APK_FILE_NAME1 = "/ode/build/Android/HelloPurr.apk";
private static final byte[] APK_FILE_CONTENT = { (byte) 0, (byte) 1, (byte) 32, (byte) 255}; private static final byte[] APK_FILE_CONTENT = { (byte) 0, (byte) 1, (byte) 32, (byte) 255};
private static final String BLOCK_FILE_NAME = "src/blocks.blk";
private static final byte[] BLOCK_FILE_CONTENT = {(byte) 0, (byte) 1, (byte) 32, (byte) 255};
private ObjectifyStorageIo storage; private ObjectifyStorageIo storage;
private Project project; private Project project;
...@@ -382,28 +384,64 @@ public class ObjectifyStorageIoTest extends LocalDatastoreTestCase { ...@@ -382,28 +384,64 @@ public class ObjectifyStorageIoTest extends LocalDatastoreTestCase {
FORM_QUALIFIED_NAME); FORM_QUALIFIED_NAME);
storage.addSourceFilesToProject(USER_ID, projectId, false, ASSET_FILE_NAME1); storage.addSourceFilesToProject(USER_ID, projectId, false, ASSET_FILE_NAME1);
storage.uploadRawFile(projectId, ASSET_FILE_NAME1, USER_ID, ASSET_FILE_CONTENT1); storage.uploadRawFile(projectId, ASSET_FILE_NAME1, USER_ID, ASSET_FILE_CONTENT1);
storage.addSourceFilesToProject(USER_ID, projectId, false, BLOCK_FILE_NAME);
storage.uploadRawFile(projectId, BLOCK_FILE_NAME, USER_ID, BLOCK_FILE_CONTENT);
storage.addOutputFilesToProject(USER_ID, projectId, APK_FILE_NAME1); storage.addOutputFilesToProject(USER_ID, projectId, APK_FILE_NAME1);
storage.uploadRawFile(projectId, APK_FILE_NAME1, USER_ID, APK_FILE_CONTENT); storage.uploadRawFile(projectId, APK_FILE_NAME1, USER_ID, APK_FILE_CONTENT);
assertTrue(storage.getProjectSourceFiles(USER_ID, projectId).contains(ASSET_FILE_NAME1)); assertTrue(storage.getProjectSourceFiles(USER_ID, projectId).contains(ASSET_FILE_NAME1));
assertTrue(storage.getProjectOutputFiles(USER_ID, projectId).contains(APK_FILE_NAME1)); assertTrue(storage.getProjectOutputFiles(USER_ID, projectId).contains(APK_FILE_NAME1));
assertTrue(java.util.Arrays.equals(ASSET_FILE_CONTENT1, assertTrue(Arrays.equals(ASSET_FILE_CONTENT1,
storage.downloadRawFile(USER_ID, projectId, ASSET_FILE_NAME1))); storage.downloadRawFile(USER_ID, projectId, ASSET_FILE_NAME1)));
assertTrue(java.util.Arrays.equals(APK_FILE_CONTENT, assertTrue(Arrays.equals(APK_FILE_CONTENT,
storage.downloadRawFile(USER_ID, projectId, APK_FILE_NAME1))); storage.downloadRawFile(USER_ID, projectId, APK_FILE_NAME1)));
ObjectifyStorageIo objstorage = (ObjectifyStorageIo) storage; assertTrue(Arrays.equals(BLOCK_FILE_CONTENT,
assertTrue(objstorage.isBlobFile(projectId, ASSET_FILE_NAME1)); storage.downloadRawFile(USER_ID, projectId, BLOCK_FILE_NAME)));
assertTrue(objstorage.isBlobFile(projectId, APK_FILE_NAME1)); assertTrue(storage.isBlobFile(projectId, ASSET_FILE_NAME1));
assertTrue(storage.isBlobFile(projectId, APK_FILE_NAME1));
assertTrue(storage.isBlobFile(projectId, BLOCK_FILE_NAME));
storage.removeSourceFilesFromProject(USER_ID, projectId, false, ASSET_FILE_NAME1); storage.removeSourceFilesFromProject(USER_ID, projectId, false, ASSET_FILE_NAME1);
storage.removeOutputFilesFromProject(USER_ID, projectId, APK_FILE_NAME1); storage.removeOutputFilesFromProject(USER_ID, projectId, APK_FILE_NAME1);
storage.removeSourceFilesFromProject(USER_ID, projectId, false, BLOCK_FILE_NAME);
assertFalse(storage.getProjectSourceFiles(USER_ID, projectId).contains(ASSET_FILE_NAME1)); assertFalse(storage.getProjectSourceFiles(USER_ID, projectId).contains(ASSET_FILE_NAME1));
assertFalse(storage.getProjectOutputFiles(USER_ID, projectId).contains(APK_FILE_NAME1)); assertFalse(storage.getProjectOutputFiles(USER_ID, projectId).contains(APK_FILE_NAME1));
assertFalse(storage.getProjectOutputFiles(USER_ID, projectId).contains(BLOCK_FILE_NAME));
// TODO(sharon): would be good to check that blobrefs are deleted from blobstore too // TODO(sharon): would be good to check that blobrefs are deleted from blobstore too
// TODO(sharon): should test large blob files (e.g., >2MB (chunk size), >4MB (row size)); // TODO(sharon): should test large blob files (e.g., >2MB (chunk size), >4MB (row size));
} }
public void testOldBlockFilesInDatastoreStillWork() {
// Create new storage object that forces storage in the datastore
ObjectifyStorageIo oldStyleStorage = new ObjectifyStorageIo() {
@Override
boolean useBlobstoreForFile(String fileName) {
return false;
}
};
final String USER_ID = "1310";
oldStyleStorage.getUser(USER_ID);
long projectId = createProject(
USER_ID, PROJECT_NAME, YoungAndroidProjectNode.YOUNG_ANDROID_PROJECT_TYPE,
FORM_QUALIFIED_NAME);
oldStyleStorage.addSourceFilesToProject(USER_ID, projectId, false, BLOCK_FILE_NAME);
oldStyleStorage.uploadRawFile(projectId, BLOCK_FILE_NAME, USER_ID, BLOCK_FILE_CONTENT);
assertTrue(Arrays.equals(BLOCK_FILE_CONTENT,
oldStyleStorage.downloadRawFile(
USER_ID, projectId, BLOCK_FILE_NAME)));
assertFalse(oldStyleStorage.isBlobFile(projectId, BLOCK_FILE_NAME));
// Test that we can still get the content with an ordinary storage object
assertTrue(Arrays.equals(BLOCK_FILE_CONTENT,
storage.downloadRawFile(
USER_ID, projectId, BLOCK_FILE_NAME)));
// Test that ordinary storage objects will still store the data in blobstore
storage.uploadRawFile(projectId, BLOCK_FILE_NAME, USER_ID, BLOCK_FILE_CONTENT);
assertTrue(storage.isBlobFile(projectId, BLOCK_FILE_NAME));
}
public void testGetProject() { public void testGetProject() {
final String USER_ID = "1400"; final String USER_ID = "1400";
storage.getUser(USER_ID); storage.getUser(USER_ID);
...@@ -463,9 +501,14 @@ public class ObjectifyStorageIoTest extends LocalDatastoreTestCase { ...@@ -463,9 +501,14 @@ public class ObjectifyStorageIoTest extends LocalDatastoreTestCase {
} }
private long createProject(String userId, String name, String type, String fileName) { private long createProject(String userId, String name, String type, String fileName) {
return createProject(userId, name, type, fileName, storage);
}
private long createProject(String userId, String name, String type, String fileName,
ObjectifyStorageIo storageIo) {
Project project = new Project(name); Project project = new Project(name);
project.setProjectType(type); project.setProjectType(type);
project.addTextFile(new TextFile(fileName, "")); project.addTextFile(new TextFile(fileName, ""));
return storage.createProject(userId, project, SETTINGS); return storageIo.createProject(userId, project, SETTINGS);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment