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

Commit abdc474

Browse files
author
Clément Le Provost
committed
[offline] Add methods to build offline mirror regardless of current state
1 parent d768701 commit abdc474

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

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

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ public void run() {
644644
if (localIndex.exists()) {
645645
return;
646646
}
647-
_bootstrap(settingsFile, objectFiles);
647+
_buildOffline(settingsFile, objectFiles);
648648
}
649649
});
650650
}
@@ -668,33 +668,74 @@ public void run() {
668668
if (localIndex.exists()) {
669669
return;
670670
}
671-
// Save resources to independent files on disk.
672-
// TODO: See if we can have the Offline Core read directly from resources or assets.
673-
File tmpDir = new File(getClient().getTempDir(), UUID.randomUUID().toString());
674-
try {
675-
tmpDir.mkdirs();
676-
// Settings.
677-
File settingsFile = new File(tmpDir, "settings.json");
678-
FileUtils.writeFile(settingsFile, resources.openRawResource(settingsResId));
679-
// Objects.
680-
File[] objectFiles = new File[objectsResIds.length];
681-
for (int i = 0; i < objectsResIds.length; ++i) {
682-
objectFiles[i] = new File(tmpDir, "objects#" + Integer.toString(objectsResIds[i]) + ".json");
683-
FileUtils.writeFile(objectFiles[i], resources.openRawResource(objectsResIds[i]));
684-
}
685-
// Build the index.
686-
_bootstrap(settingsFile, objectFiles);
687-
} catch (IOException e) {
688-
Log.e(MirroredIndex.class.getSimpleName(), "Failed to write bootstrap resources to disk", e);
689-
} finally {
690-
// Delete temporary files.
691-
FileUtils.deleteRecursive(tmpDir);
692-
}
671+
_buildOfflineFromRawResources(resources, settingsResId, objectsResIds);
672+
}
673+
});
674+
}
675+
676+
/**
677+
* Replace the local mirror with local data stored on the filesystem.
678+
*
679+
* **Note:** This method will *always* replace the local mirror with the specified data.
680+
*
681+
* @param settingsFile Absolute path to the file containing the index settings, in JSON format.
682+
* @param objectFiles Absolute path(s) to the file(s) containing the objects. Each file must contain an array of
683+
* objects, in JSON format.
684+
*/
685+
public void buildOfflineFromFiles(@NonNull final File settingsFile, @NonNull final File... objectFiles) {
686+
getClient().localBuildExecutorService.submit(new Runnable() {
687+
@Override
688+
public void run() {
689+
_buildOffline(settingsFile, objectFiles);
690+
}
691+
});
692+
}
693+
694+
/**
695+
* Replace the local mirror with local data stored on the filesystem.
696+
*
697+
* **Note:** This method will *always* replace the local mirror with the specified data.
698+
*
699+
* @param resources A {@link Resources} instance to read resources from.
700+
* @param settingsResId Resource identifier of the index settings, in JSON format.
701+
* @param objectsResIds Resource identifiers of the various objects files. Each file must contain an array of
702+
* objects, in JSON format.
703+
*/
704+
public void buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) {
705+
getClient().localBuildExecutorService.submit(new Runnable() {
706+
@Override
707+
public void run() {
708+
_buildOfflineFromRawResources(resources, settingsResId, objectsResIds);
693709
}
694710
});
695711
}
696712

697-
private void _bootstrap(@NonNull File settingsFile, @NonNull File... objectFiles) {
713+
private void _buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) {
714+
// Save resources to independent files on disk.
715+
// TODO: See if we can have the Offline Core read directly from resources or assets.
716+
File tmpDir = new File(getClient().getTempDir(), UUID.randomUUID().toString());
717+
try {
718+
tmpDir.mkdirs();
719+
// Settings.
720+
File settingsFile = new File(tmpDir, "settings.json");
721+
FileUtils.writeFile(settingsFile, resources.openRawResource(settingsResId));
722+
// Objects.
723+
File[] objectFiles = new File[objectsResIds.length];
724+
for (int i = 0; i < objectsResIds.length; ++i) {
725+
objectFiles[i] = new File(tmpDir, "objects#" + Integer.toString(objectsResIds[i]) + ".json");
726+
FileUtils.writeFile(objectFiles[i], resources.openRawResource(objectsResIds[i]));
727+
}
728+
// Build the index.
729+
_buildOffline(settingsFile, objectFiles);
730+
} catch (IOException e) {
731+
Log.e(MirroredIndex.class.getSimpleName(), "Failed to write bootstrap resources to disk", e);
732+
} finally {
733+
// Delete temporary files.
734+
FileUtils.deleteRecursive(tmpDir);
735+
}
736+
}
737+
738+
private void _buildOffline(@NonNull File settingsFile, @NonNull File... objectFiles) {
698739
// Notify listeners.
699740
getClient().mainHandler.post(new Runnable() {
700741
@Override
@@ -716,7 +757,7 @@ public void run() {
716757
public void run() {
717758
Throwable error = null;
718759
if (status != 200) {
719-
error = new AlgoliaException(String.format("Failed to bootstrap index \"%s\"", MirroredIndex.this.getIndexName()), status);
760+
error = new AlgoliaException(String.format("Failed to build local mirror \"%s\"", MirroredIndex.this.getIndexName()), status);
720761
}
721762
fireBootstrapDidFinish(error);
722763
}

0 commit comments

Comments
 (0)