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

Commit 32ccf67

Browse files
author
Clément Le Provost
committed
[refact] Factorize index building between sync() and buildOffline*()
1 parent 6a465d3 commit 32ccf67

File tree

1 file changed

+63
-100
lines changed

1 file changed

+63
-100
lines changed

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

Lines changed: 63 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -561,13 +561,7 @@ public void run()
561561
stats.fileCount = objectFiles.size();
562562

563563
// Build the index.
564-
String[] objectFilePaths = new String[objectFiles.size()];
565-
for (int i = 0; i < objectFiles.size(); ++i)
566-
objectFilePaths[i] = objectFiles.get(i).getAbsolutePath();
567-
int status = getLocalIndex().build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
568-
if (status != 200) {
569-
throw new AlgoliaException("Build index failed", status);
570-
}
564+
_buildOffline(settingsFile, objectFiles.toArray(new File[objectFiles.size()]));
571565

572566
// Update statistics.
573567
long afterBuildTime = System.currentTimeMillis();
@@ -612,7 +606,7 @@ public void run()
612606
}
613607

614608
// ----------------------------------------------------------------------
615-
// Bootstrapping
609+
// Manual build
616610
// ----------------------------------------------------------------------
617611

618612
/**
@@ -627,90 +621,59 @@ public boolean hasOfflineData() {
627621
}
628622

629623
/**
630-
* Bootstrap the local mirror with local data stored on the filesystem.
631-
*
632-
* **Note:** This method will do nothing if offline data is already available, making it safe to call at every
633-
* application launch.
624+
* Replace the local mirror with local data stored on the filesystem.
634625
*
635626
* @param settingsFile Absolute path to the file containing the index settings, in JSON format.
636627
* @param objectFiles Absolute path(s) to the file(s) containing the objects. Each file must contain an array of
637628
* objects, in JSON format.
638-
*/
639-
public void bootstrapFromFiles(@NonNull final File settingsFile, @NonNull final File... objectFiles) {
640-
getClient().localBuildExecutorService.submit(new Runnable() {
641-
@Override
642-
public void run() {
643-
// Abort immediately if data already exists.
644-
if (localIndex.exists()) {
645-
return;
646-
}
647-
_buildOffline(settingsFile, objectFiles);
648-
}
649-
});
650-
}
651-
652-
/**
653-
* Bootstrap the local mirror with local data stored in raw Android resources.
654-
*
655-
* **Note:** This method will do nothing if offline data is already available, making it safe to call at every
656-
* application launch.
629+
* @param completionHandler Optional completion handler to be notified of the build's outcome.
630+
* @return A cancellable request.
657631
*
658-
* @param resources A {@link Resources} instance to read resources from.
659-
* @param settingsResId Resource identifier of the index settings, in JSON format.
660-
* @param objectsResIds Resource identifiers of the various objects files. Each file must contain an array of
661-
* objects, in JSON format.
632+
* **Note:** Cancelling the request does *not* cancel the build; it merely prevents the completion handler from
633+
* being called.
662634
*/
663-
public void bootstrapFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) {
664-
getClient().localBuildExecutorService.submit(new Runnable() {
635+
public Request buildOfflineFromFiles(@NonNull final File settingsFile, @NonNull final File[] objectFiles, @Nullable CompletionHandler completionHandler) {
636+
return getClient().new AsyncTaskRequest(completionHandler, getClient().localBuildExecutorService) {
637+
@NonNull
665638
@Override
666-
public void run() {
667-
// Abort immediately if data already exists.
668-
if (localIndex.exists()) {
669-
return;
670-
}
671-
_buildOfflineFromRawResources(resources, settingsResId, objectsResIds);
639+
protected JSONObject run() throws AlgoliaException {
640+
return _buildOffline(settingsFile, objectFiles);
672641
}
673-
});
642+
}.start();
674643
}
675644

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-
});
645+
public Request buildOfflineFromFiles(@NonNull final File settingsFile, @NonNull final File... objectFiles) {
646+
return buildOfflineFromFiles(settingsFile, objectFiles, null);
692647
}
693648

694649
/**
695650
* Replace the local mirror with local data stored on the filesystem.
696651
*
697-
* **Note:** This method will *always* replace the local mirror with the specified data.
698-
*
699652
* @param resources A {@link Resources} instance to read resources from.
700653
* @param settingsResId Resource identifier of the index settings, in JSON format.
701654
* @param objectsResIds Resource identifiers of the various objects files. Each file must contain an array of
702655
* objects, in JSON format.
656+
* @param completionHandler Optional completion handler to be notified of the build's outcome.
657+
* @return A cancellable request.
658+
*
659+
* **Note:** Cancelling the request does *not* cancel the build; it merely prevents the completion handler from
660+
* being called.
703661
*/
704-
public void buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) {
705-
getClient().localBuildExecutorService.submit(new Runnable() {
662+
public Request buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int[] objectsResIds, @Nullable CompletionHandler completionHandler) {
663+
return getClient().new AsyncTaskRequest(completionHandler, getClient().localBuildExecutorService) {
664+
@NonNull
706665
@Override
707-
public void run() {
708-
_buildOfflineFromRawResources(resources, settingsResId, objectsResIds);
666+
protected JSONObject run() throws AlgoliaException {
667+
return _buildOfflineFromRawResources(resources, settingsResId, objectsResIds);
709668
}
710-
});
669+
}.start();
711670
}
712671

713-
private void _buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) {
672+
public Request buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) {
673+
return buildOfflineFromRawResources(resources, settingsResId, objectsResIds, null);
674+
}
675+
676+
private JSONObject _buildOfflineFromRawResources(@NonNull final Resources resources, @NonNull final int settingsResId, @NonNull final int... objectsResIds) throws AlgoliaException {
714677
// Save resources to independent files on disk.
715678
// TODO: See if we can have the Offline Core read directly from resources or assets.
716679
File tmpDir = new File(getClient().getTempDir(), UUID.randomUUID().toString());
@@ -726,48 +689,48 @@ private void _buildOfflineFromRawResources(@NonNull final Resources resources, @
726689
FileUtils.writeFile(objectFiles[i], resources.openRawResource(objectsResIds[i]));
727690
}
728691
// Build the index.
729-
_buildOffline(settingsFile, objectFiles);
692+
return _buildOffline(settingsFile, objectFiles);
730693
} catch (IOException e) {
731-
Log.e(MirroredIndex.class.getSimpleName(), "Failed to write build resources to disk", e);
694+
throw new AlgoliaException("Failed to write build resources to disk", e);
732695
} finally {
733696
// Delete temporary files.
734697
FileUtils.deleteRecursive(tmpDir);
735698
}
736699
}
737700

738-
/**
739-
* Build the local mirror (synchronously).
740-
*
741-
* @param settingsFile The file containing index settings.
742-
* @param objectFiles The files containing objects.
743-
*/
744-
private void _buildOffline(@NonNull File settingsFile, @NonNull File... objectFiles) {
745-
// Notify listeners.
746-
getClient().mainHandler.post(new Runnable() {
747-
@Override
748-
public void run() {
749-
fireBuildDidStart();
750-
}
751-
});
701+
private JSONObject _buildOffline(@NonNull File settingsFile, @NonNull File... objectFiles) throws AlgoliaException {
702+
AlgoliaException error = null;
703+
try {
704+
// Notify listeners.
705+
getClient().mainHandler.post(new Runnable() {
706+
@Override
707+
public void run() {
708+
fireBuildDidStart();
709+
}
710+
});
752711

753-
// Build the index.
754-
String[] objectFilePaths = new String[objectFiles.length];
755-
for (int i = 0; i < objectFiles.length; ++i) {
756-
objectFilePaths[i] = objectFiles[i].getAbsolutePath();
712+
// Build the index.
713+
String[] objectFilePaths = new String[objectFiles.length];
714+
for (int i = 0; i < objectFiles.length; ++i) {
715+
objectFilePaths[i] = objectFiles[i].getAbsolutePath();
716+
}
717+
final int status = getLocalIndex().build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
718+
if (status != 200) {
719+
error = new AlgoliaException(String.format("Failed to build local mirror \"%s\"", MirroredIndex.this.getIndexName()), status);
720+
throw error;
721+
}
722+
return new JSONObject();
757723
}
758-
final int status = localIndex.build(settingsFile.getAbsolutePath(), objectFilePaths, true /* clearIndex */, null /* deletedObjectIDs */);
759-
760-
// Notify listeners.
761-
getClient().mainHandler.post(new Runnable() {
762-
@Override
763-
public void run() {
764-
Throwable error = null;
765-
if (status != 200) {
766-
error = new AlgoliaException(String.format("Failed to build local mirror \"%s\"", MirroredIndex.this.getIndexName()), status);
724+
finally {
725+
// Notify listeners.
726+
final Throwable finalError = error;
727+
getClient().mainHandler.post(new Runnable() {
728+
@Override
729+
public void run() {
730+
fireBuildDidFinish(finalError);
767731
}
768-
fireBuildDidFinish(error);
769-
}
770-
});
732+
});
733+
}
771734
}
772735

773736
// ----------------------------------------------------------------------

0 commit comments

Comments
 (0)