@@ -216,13 +216,23 @@ public void setDelayBetweenSyncs(long duration, @NonNull TimeUnit unit) {
216
216
/**
217
217
* Lazy instantiate the local index.
218
218
*/
219
- protected void ensureLocalIndex ()
219
+ private synchronized void ensureLocalIndex ()
220
220
{
221
221
if (localIndex == null ) {
222
222
localIndex = new LocalIndex (getClient ().getRootDataDir ().getAbsolutePath (), getClient ().getApplicationID (), getIndexName ());
223
223
}
224
224
}
225
225
226
+ /**
227
+ * Get the local index, lazy instantiating it if needed.
228
+ *
229
+ * @return The local index.
230
+ */
231
+ protected LocalIndex getLocalIndex () {
232
+ ensureLocalIndex ();
233
+ return localIndex ;
234
+ }
235
+
226
236
private File getTempDir ()
227
237
{
228
238
// TODO: Use better value
@@ -481,11 +491,10 @@ public void run()
481
491
stats .fileCount = objectFiles .size ();
482
492
483
493
// Build the index.
484
- ensureLocalIndex ();
485
494
String [] objectFilePaths = new String [objectFiles .size ()];
486
495
for (int i = 0 ; i < objectFiles .size (); ++i )
487
496
objectFilePaths [i ] = objectFiles .get (i ).getAbsolutePath ();
488
- int status = localIndex .build (settingsFile .getAbsolutePath (), objectFilePaths , true /* clearIndex */ );
497
+ int status = getLocalIndex () .build (settingsFile .getAbsolutePath (), objectFilePaths , true /* clearIndex */ );
489
498
if (status != 200 ) {
490
499
throw new AlgoliaException ("Build index failed" , status );
491
500
}
@@ -840,8 +849,7 @@ JSONObject run() throws AlgoliaException {
840
849
private JSONObject _searchOffline (@ NonNull Query query ) throws AlgoliaException
841
850
{
842
851
try {
843
- ensureLocalIndex ();
844
- SearchResults searchResults = localIndex .search (query .build ());
852
+ SearchResults searchResults = getLocalIndex ().search (query .build ());
845
853
if (searchResults .getStatusCode () == 200 ) {
846
854
String jsonString = new String (searchResults .getData (), "UTF-8" );
847
855
JSONObject json = new JSONObject (jsonString );
@@ -971,7 +979,6 @@ private JSONObject _multipleQueriesOffline(@NonNull List<Query> queries, String
971
979
// TODO: Move to `LocalIndex` to factorize implementation between platforms?
972
980
try {
973
981
JSONArray results = new JSONArray ();
974
- ensureLocalIndex ();
975
982
boolean shouldProcess = true ;
976
983
for (Query query : queries ) {
977
984
// Implement the "stop if enough matches" strategy.
@@ -1070,8 +1077,7 @@ JSONObject run() throws AlgoliaException {
1070
1077
private JSONObject _browseMirror (@ NonNull Query query ) throws AlgoliaException
1071
1078
{
1072
1079
try {
1073
- ensureLocalIndex ();
1074
- SearchResults searchResults = localIndex .browse (query .build ());
1080
+ SearchResults searchResults = getLocalIndex ().browse (query .build ());
1075
1081
if (searchResults .getStatusCode () == 200 ) {
1076
1082
String jsonString = new String (searchResults .getData (), "UTF-8" );
1077
1083
JSONObject json = new JSONObject (jsonString );
0 commit comments