Skip to content
This repository was archived by the owner on Jan 31, 2022. It is now read-only.

Commit 6ec75d3

Browse files
author
Clément Le Provost
authored
Merge pull request #214 from algolia/feat/offline-improvements
New offline features
2 parents 5179929 + 4ae3579 commit 6ec75d3

File tree

10 files changed

+999
-77
lines changed

10 files changed

+999
-77
lines changed

algoliasearch/src/main/java/com/algolia/search/saas/Index.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,7 @@ public Request partialUpdateObjectsAsync(final @NonNull JSONArray partialObjects
399399
* @return A cancellable request.
400400
*/
401401
public Request getObjectAsync(final @NonNull String objectID, @NonNull CompletionHandler completionHandler) {
402-
return getClient().new AsyncTaskRequest(completionHandler) {
403-
@NonNull
404-
@Override protected JSONObject run() throws AlgoliaException {
405-
return getObject(objectID);
406-
}
407-
}.start();
402+
return getObjectAsync(objectID, null, completionHandler);
408403
}
409404

410405
/**
@@ -432,12 +427,7 @@ public Request getObjectAsync(final @NonNull String objectID, final List<String>
432427
* @return A cancellable request.
433428
*/
434429
public Request getObjectsAsync(final @NonNull List<String> objectIDs, @NonNull CompletionHandler completionHandler) {
435-
return getClient().new AsyncTaskRequest(completionHandler) {
436-
@NonNull
437-
@Override protected JSONObject run() throws AlgoliaException {
438-
return getObjects(objectIDs);
439-
}
440-
}.start();
430+
return getObjectsAsync(objectIDs, null, completionHandler);
441431
}
442432

443433
/**
@@ -742,8 +732,11 @@ protected JSONObject getObject(String objectID) throws AlgoliaException {
742732
*/
743733
protected JSONObject getObject(String objectID, List<String> attributesToRetrieve) throws AlgoliaException {
744734
try {
745-
String params = encodeAttributes(attributesToRetrieve, true);
746-
return client.getRequest("/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8") + params, false);
735+
String path = "/1/indexes/" + encodedIndexName + "/" + URLEncoder.encode(objectID, "UTF-8");
736+
if (attributesToRetrieve != null) {
737+
path += encodeAttributes(attributesToRetrieve, true); // includes the query separator (`?`)
738+
}
739+
return client.getRequest(path, false);
747740
} catch (UnsupportedEncodingException e) {
748741
throw new RuntimeException(e);
749742
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2012-2016 Algolia
3+
* http://www.algolia.com/
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.algolia.search.saas;
25+
26+
import android.support.annotation.NonNull;
27+
import android.support.annotation.Nullable;
28+
29+
/**
30+
* Listener for events related to index building.
31+
*
32+
* Notifications are sent on a per-index basis, but you may register the same listener for all indices.
33+
* Notifications are sent on the main thread.
34+
*/
35+
public interface BuildListener
36+
{
37+
/**
38+
* Build of the local index has just started.
39+
*
40+
* @param index The index being built.
41+
*/
42+
public void buildDidStart(@NonNull MirroredIndex index);
43+
44+
/**
45+
* Build of the local index has just finished.
46+
*
47+
* @param index The index having been built.
48+
* @param error `null` if success, otherwise indicates the error.
49+
*/
50+
public void buildDidFinish(@NonNull MirroredIndex index, @Nullable Throwable error);
51+
}

algoliasearch/src/offline/java/com/algolia/search/saas/FileUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import android.support.annotation.NonNull;
2727

2828
import java.io.File;
29+
import java.io.FileOutputStream;
30+
import java.io.IOException;
31+
import java.io.InputStream;
2932

3033
/**
3134
* Various filesystem-related utilities.
@@ -49,4 +52,26 @@ public static boolean deleteRecursive(@NonNull File item)
4952
ok = ok && item.delete();
5053
return ok;
5154
}
55+
56+
/**
57+
* Write a stream of bytes to a file.
58+
*
59+
* @param destinationFile The file to be written to. The parent directory must exist. If the file already exists,
60+
* it will be overwritten.
61+
* @param content The bytes to write.
62+
* @throws IOException if anything goes wrong.
63+
*/
64+
public static void writeFile(@NonNull File destinationFile, @NonNull InputStream content) throws IOException {
65+
byte[] buffer = new byte[64 * 1024]; // 64 kB buffer
66+
FileOutputStream outputStream = new FileOutputStream(destinationFile);
67+
try {
68+
int bytesRead;
69+
while ((bytesRead = content.read(buffer)) >= 0) {
70+
outputStream.write(buffer, 0, bytesRead);
71+
}
72+
} finally {
73+
content.close();
74+
outputStream.close();
75+
}
76+
}
5277
}

0 commit comments

Comments
 (0)