|
23 | 23 |
|
24 | 24 | package com.algolia.search.saas;
|
25 | 25 |
|
| 26 | +import android.content.res.Resources; |
26 | 27 | import android.os.Looper;
|
27 | 28 | import android.support.annotation.NonNull;
|
28 | 29 | import android.support.annotation.Nullable;
|
@@ -829,6 +830,100 @@ public boolean hasOfflineData() {
|
829 | 830 | return localIndex.exists();
|
830 | 831 | }
|
831 | 832 |
|
| 833 | + // ---------------------------------------------------------------------- |
| 834 | + // Manual build |
| 835 | + // ---------------------------------------------------------------------- |
| 836 | + |
| 837 | + /** |
| 838 | + * Build the index from local data stored on the filesystem. |
| 839 | + * |
| 840 | + * @param settingsFile Absolute path to the file containing the index settings, in JSON format. |
| 841 | + * @param objectFiles Absolute path(s) to the file(s) containing the objects. Each file must contain an array of |
| 842 | + * objects, in JSON format. |
| 843 | + * @param completionHandler Optional completion handler to be notified of the build's outcome. |
| 844 | + * @return A cancellable request. |
| 845 | + * |
| 846 | + * **Note:** Cancelling the request does *not* cancel the build; it merely prevents the completion handler from |
| 847 | + * being called. |
| 848 | + */ |
| 849 | + public Request buildFromFiles(@NonNull final File settingsFile, @NonNull final File[] objectFiles, @Nullable CompletionHandler completionHandler) { |
| 850 | + return getClient().new AsyncTaskRequest(completionHandler, getClient().localBuildExecutorService) { |
| 851 | + @NonNull |
| 852 | + @Override |
| 853 | + protected JSONObject run() throws AlgoliaException { |
| 854 | + return _build(settingsFile, objectFiles); |
| 855 | + } |
| 856 | + }.start(); |
| 857 | + } |
| 858 | + |
| 859 | + public Request buildFromFiles(@NonNull final File settingsFile, @NonNull final File... objectFiles) { |
| 860 | + return buildFromFiles(settingsFile, objectFiles, null); |
| 861 | + } |
| 862 | + |
| 863 | + /** |
| 864 | + * Build the index from local data stored in raw resources. |
| 865 | + * |
| 866 | + * @param resources A {@link Resources} instance to read resources from. |
| 867 | + * @param settingsResId Resource identifier of the index settings, in JSON format. |
| 868 | + * @param objectsResIds Resource identifiers of the various objects files. Each file must contain an array of |
| 869 | + * objects, in JSON format. |
| 870 | + * @param completionHandler Optional completion handler to be notified of the build's outcome. |
| 871 | + * @return A cancellable request. |
| 872 | + * |
| 873 | + * **Note:** Cancelling the request does *not* cancel the build; it merely prevents the completion handler from |
| 874 | + * being called. |
| 875 | + */ |
| 876 | + public Request buildFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int[] objectsResIds, @Nullable CompletionHandler completionHandler) { |
| 877 | + return getClient().new AsyncTaskRequest(completionHandler, getClient().localBuildExecutorService) { |
| 878 | + @NonNull |
| 879 | + @Override |
| 880 | + protected JSONObject run() throws AlgoliaException { |
| 881 | + return _buildFromRawResources(resources, settingsResId, objectsResIds); |
| 882 | + } |
| 883 | + }.start(); |
| 884 | + } |
| 885 | + |
| 886 | + public Request buildFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) { |
| 887 | + return buildFromRawResources(resources, settingsResId, objectsResIds, null); |
| 888 | + } |
| 889 | + |
| 890 | + private JSONObject _buildFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) throws AlgoliaException { |
| 891 | + // Save resources to independent files on disk. |
| 892 | + File tmpDir = new File(getClient().getTempDir(), UUID.randomUUID().toString()); |
| 893 | + try { |
| 894 | + tmpDir.mkdirs(); |
| 895 | + // Settings. |
| 896 | + File settingsFile = new File(tmpDir, "settings.json"); |
| 897 | + FileUtils.writeFile(settingsFile, resources.openRawResource(settingsResId)); |
| 898 | + // Objects. |
| 899 | + File[] objectFiles = new File[objectsResIds.length]; |
| 900 | + for (int i = 0; i < objectsResIds.length; ++i) { |
| 901 | + objectFiles[i] = new File(tmpDir, "objects#" + Integer.toString(objectsResIds[i]) + ".json"); |
| 902 | + FileUtils.writeFile(objectFiles[i], resources.openRawResource(objectsResIds[i])); |
| 903 | + } |
| 904 | + // Build the index. |
| 905 | + return _build(settingsFile, objectFiles); |
| 906 | + } catch (IOException e) { |
| 907 | + throw new AlgoliaException("Failed to write build resources to disk", e); |
| 908 | + } finally { |
| 909 | + // Delete temporary files. |
| 910 | + FileUtils.deleteRecursive(tmpDir); |
| 911 | + } |
| 912 | + } |
| 913 | + |
| 914 | + private JSONObject _build(@NonNull File settingsFile, @NonNull File... objectFiles) throws AlgoliaException { |
| 915 | + AlgoliaException error = null; |
| 916 | + String[] objectFilePaths = new String[objectFiles.length]; |
| 917 | + for (int i = 0; i < objectFiles.length; ++i) { |
| 918 | + objectFilePaths[i] = objectFiles[i].getAbsolutePath(); |
| 919 | + } |
| 920 | + final int status = localIndex.build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */); |
| 921 | + if (status != 200) { |
| 922 | + throw new AlgoliaException(String.format("Failed to build local index \"%s\"", OfflineIndex.this.getName()), status); |
| 923 | + } |
| 924 | + return new JSONObject(); |
| 925 | + } |
| 926 | + |
832 | 927 | // ----------------------------------------------------------------------
|
833 | 928 | // Helpers
|
834 | 929 | // ----------------------------------------------------------------------
|
|
0 commit comments