@@ -644,7 +644,7 @@ public void run() {
644
644
if (localIndex .exists ()) {
645
645
return ;
646
646
}
647
- _bootstrap (settingsFile , objectFiles );
647
+ _buildOffline (settingsFile , objectFiles );
648
648
}
649
649
});
650
650
}
@@ -668,33 +668,74 @@ public void run() {
668
668
if (localIndex .exists ()) {
669
669
return ;
670
670
}
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 );
693
709
}
694
710
});
695
711
}
696
712
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 ) {
698
739
// Notify listeners.
699
740
getClient ().mainHandler .post (new Runnable () {
700
741
@ Override
@@ -716,7 +757,7 @@ public void run() {
716
757
public void run () {
717
758
Throwable error = null ;
718
759
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 );
720
761
}
721
762
fireBootstrapDidFinish (error );
722
763
}
0 commit comments