Skip to content

Remove ParseFile in memory cache #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions Parse/src/main/java/com/parse/ParseFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ public Task<Void> then(Task<State> task) throws Exception {
state = task.getResult();
// Since we have successfully uploaded the file, we do not need to hold the file pointer
// anymore.
data = null;
file = null;
return task.makeVoid();
}
Expand Down Expand Up @@ -424,31 +425,18 @@ public byte[] getData() throws ParseException {
* @return A Task that is resolved when the data has been fetched.
*/
public Task<byte[]> getDataInBackground(final ProgressCallback progressCallback) {
// If data is already available, just return immediately.
if (data != null) {
// in-memory
return Task.forResult(data);
}

final Task<Void>.TaskCompletionSource cts = Task.create();
currentTasks.add(cts);

return taskQueue.enqueue(new Continuation<Void, Task<byte[]>>() {
@Override
public Task<byte[]> then(Task<Void> toAwait) throws Exception {
// If data is already available, just return immediately.
if (data != null) {
// in-memory
return Task.forResult(data);
}

return fetchInBackground(progressCallback, toAwait, cts.getTask()).onSuccess(new Continuation<File, byte[]>() {
@Override
public byte[] then(Task<File> task) throws Exception {
File file = task.getResult();
try {
data = ParseFileUtils.readFileToByteArray(file);
return data;
return ParseFileUtils.readFileToByteArray(file);
} catch (IOException e) {
// do nothing
}
Expand Down Expand Up @@ -550,7 +538,7 @@ public Task<File> then(Task<File> task) throws Exception {
* @return A Task that is resolved when the data has been fetched.
*/
public Task<File> getFileInBackground() {
return getFileInBackground((ProgressCallback)null);
return getFileInBackground((ProgressCallback) null);
}

/**
Expand Down
56 changes: 51 additions & 5 deletions Parse/src/test/java/com/parse/ParseFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,31 +58,29 @@ public void testConstructor() throws Exception {
String contentType = "content_type";
File file = temporaryFolder.newFile(name);

// TODO(mengyan): After we have proper staging strategy, we should verify the staging file's
// content is the same with the original file.

ParseFile parseFile = new ParseFile(name, data, contentType);
assertEquals("name", parseFile.getName());
assertEquals("hello", new String(parseFile.getData()));
assertEquals("content_type", parseFile.getState().mimeType());
assertTrue(parseFile.isDirty());

parseFile = new ParseFile(data);
assertEquals("file", parseFile.getName()); // Default
assertEquals("hello", new String(parseFile.getData()));
assertEquals(null, parseFile.getState().mimeType());
assertTrue(parseFile.isDirty());

parseFile = new ParseFile(name, data);
assertEquals("name", parseFile.getName());
assertEquals("hello", new String(parseFile.getData()));
assertEquals(null, parseFile.getState().mimeType());
assertTrue(parseFile.isDirty());

parseFile = new ParseFile(data, contentType);
assertEquals("file", parseFile.getName()); // Default
assertEquals("hello", new String(parseFile.getData()));
assertEquals("content_type", parseFile.getState().mimeType());
assertTrue(parseFile.isDirty());

// TODO(mengyan): Test file pointer in ParseFile when we have proper stage strategy
parseFile = new ParseFile(file);
assertEquals(name, parseFile.getName()); // Default
assertEquals(null, parseFile.getState().mimeType());
Expand Down Expand Up @@ -291,6 +289,22 @@ public void testGetDataAsyncSuccess() throws Exception {
assertEquals(url, stateCaptor.getValue().url());
// Verify the data we get is correct
assertArrayEquals(content.getBytes(), data);

// Make sure we always get the data from network
byte[] dataAgain = ParseTaskUtils.wait(parseFile.getDataInBackground());

// Verify controller get the correct data
ArgumentCaptor<ParseFile.State> stateCaptorAgain =
ArgumentCaptor.forClass(ParseFile.State.class);
verify(controller, times(2)).fetchAsync(
stateCaptorAgain.capture(),
anyString(),
any(ProgressCallback.class),
Matchers.<Task<Void>>any()
);
assertEquals(url, stateCaptorAgain.getValue().url());
// Verify the data we get is correct
assertArrayEquals(content.getBytes(), dataAgain);
}

@Test
Expand Down Expand Up @@ -325,6 +339,22 @@ public void testGetDataStreamAsyncSuccess() throws Exception {
assertEquals(url, stateCaptor.getValue().url());
// Verify the data we get is correct
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStream));

// Make sure we always get the data from network
InputStream dataStreamAgain = ParseTaskUtils.wait(parseFile.getDataStreamInBackground());

// Verify controller get the correct data
ArgumentCaptor<ParseFile.State> stateCaptorAgain =
ArgumentCaptor.forClass(ParseFile.State.class);
verify(controller, times(2)).fetchAsync(
stateCaptorAgain.capture(),
anyString(),
any(ProgressCallback.class),
Matchers.<Task<Void>>any()
);
assertEquals(url, stateCaptorAgain.getValue().url());
// Verify the data we get is correct
assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStreamAgain));
}

@Test
Expand Down Expand Up @@ -359,6 +389,22 @@ public void testGetFileAsyncSuccess() throws Exception {
assertEquals(url, stateCaptor.getValue().url());
// Verify the data we get is correct
assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFile));

// Make sure we always get the data from network
File fetchedFileAgain = ParseTaskUtils.wait(parseFile.getFileInBackground());

// Verify controller get the correct data
ArgumentCaptor<ParseFile.State> stateCaptorAgain =
ArgumentCaptor.forClass(ParseFile.State.class);
verify(controller, times(2)).fetchAsync(
stateCaptorAgain.capture(),
anyString(),
any(ProgressCallback.class),
Matchers.<Task<Void>>any()
);
assertEquals(url, stateCaptorAgain.getValue().url());
// Verify the data we get is correct
assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFileAgain));
}

//endregion
Expand Down