@@ -561,13 +561,7 @@ public void run()
561
561
stats .fileCount = objectFiles .size ();
562
562
563
563
// 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 ()]));
571
565
572
566
// Update statistics.
573
567
long afterBuildTime = System .currentTimeMillis ();
@@ -612,7 +606,7 @@ public void run()
612
606
}
613
607
614
608
// ----------------------------------------------------------------------
615
- // Bootstrapping
609
+ // Manual build
616
610
// ----------------------------------------------------------------------
617
611
618
612
/**
@@ -627,90 +621,59 @@ public boolean hasOfflineData() {
627
621
}
628
622
629
623
/**
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.
634
625
*
635
626
* @param settingsFile Absolute path to the file containing the index settings, in JSON format.
636
627
* @param objectFiles Absolute path(s) to the file(s) containing the objects. Each file must contain an array of
637
628
* 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.
657
631
*
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.
662
634
*/
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
665
638
@ 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 );
672
641
}
673
- });
642
+ }. start ( );
674
643
}
675
644
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 );
692
647
}
693
648
694
649
/**
695
650
* Replace the local mirror with local data stored on the filesystem.
696
651
*
697
- * **Note:** This method will *always* replace the local mirror with the specified data.
698
- *
699
652
* @param resources A {@link Resources} instance to read resources from.
700
653
* @param settingsResId Resource identifier of the index settings, in JSON format.
701
654
* @param objectsResIds Resource identifiers of the various objects files. Each file must contain an array of
702
655
* 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.
703
661
*/
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
706
665
@ Override
707
- public void run () {
708
- _buildOfflineFromRawResources (resources , settingsResId , objectsResIds );
666
+ protected JSONObject run () throws AlgoliaException {
667
+ return _buildOfflineFromRawResources (resources , settingsResId , objectsResIds );
709
668
}
710
- });
669
+ }. start ( );
711
670
}
712
671
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 {
714
677
// Save resources to independent files on disk.
715
678
// TODO: See if we can have the Offline Core read directly from resources or assets.
716
679
File tmpDir = new File (getClient ().getTempDir (), UUID .randomUUID ().toString ());
@@ -726,48 +689,48 @@ private void _buildOfflineFromRawResources(@NonNull final Resources resources, @
726
689
FileUtils .writeFile (objectFiles [i ], resources .openRawResource (objectsResIds [i ]));
727
690
}
728
691
// Build the index.
729
- _buildOffline (settingsFile , objectFiles );
692
+ return _buildOffline (settingsFile , objectFiles );
730
693
} 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 );
732
695
} finally {
733
696
// Delete temporary files.
734
697
FileUtils .deleteRecursive (tmpDir );
735
698
}
736
699
}
737
700
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
+ });
752
711
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 ();
757
723
}
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 );
767
731
}
768
- fireBuildDidFinish (error );
769
- }
770
- });
732
+ });
733
+ }
771
734
}
772
735
773
736
// ----------------------------------------------------------------------
0 commit comments