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

Commit 5cceac3

Browse files
author
Clément Le Provost
committed
Improve lazy instantiation of local index
- Race conditions are now avoided by synchronizing the instantiation. - All methods now use the getter instead of the field.
1 parent ba02944 commit 5cceac3

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,23 @@ public void setDelayBetweenSyncs(long duration, @NonNull TimeUnit unit) {
216216
/**
217217
* Lazy instantiate the local index.
218218
*/
219-
protected void ensureLocalIndex()
219+
private synchronized void ensureLocalIndex()
220220
{
221221
if (localIndex == null) {
222222
localIndex = new LocalIndex(getClient().getRootDataDir().getAbsolutePath(), getClient().getApplicationID(), getIndexName());
223223
}
224224
}
225225

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+
226236
private File getTempDir()
227237
{
228238
// TODO: Use better value
@@ -481,11 +491,10 @@ public void run()
481491
stats.fileCount = objectFiles.size();
482492

483493
// Build the index.
484-
ensureLocalIndex();
485494
String[] objectFilePaths = new String[objectFiles.size()];
486495
for (int i = 0; i < objectFiles.size(); ++i)
487496
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 */);
489498
if (status != 200) {
490499
throw new AlgoliaException("Build index failed", status);
491500
}
@@ -840,8 +849,7 @@ JSONObject run() throws AlgoliaException {
840849
private JSONObject _searchOffline(@NonNull Query query) throws AlgoliaException
841850
{
842851
try {
843-
ensureLocalIndex();
844-
SearchResults searchResults = localIndex.search(query.build());
852+
SearchResults searchResults = getLocalIndex().search(query.build());
845853
if (searchResults.getStatusCode() == 200) {
846854
String jsonString = new String(searchResults.getData(), "UTF-8");
847855
JSONObject json = new JSONObject(jsonString);
@@ -971,7 +979,6 @@ private JSONObject _multipleQueriesOffline(@NonNull List<Query> queries, String
971979
// TODO: Move to `LocalIndex` to factorize implementation between platforms?
972980
try {
973981
JSONArray results = new JSONArray();
974-
ensureLocalIndex();
975982
boolean shouldProcess = true;
976983
for (Query query: queries) {
977984
// Implement the "stop if enough matches" strategy.
@@ -1070,8 +1077,7 @@ JSONObject run() throws AlgoliaException {
10701077
private JSONObject _browseMirror(@NonNull Query query) throws AlgoliaException
10711078
{
10721079
try {
1073-
ensureLocalIndex();
1074-
SearchResults searchResults = localIndex.browse(query.build());
1080+
SearchResults searchResults = getLocalIndex().browse(query.build());
10751081
if (searchResults.getStatusCode() == 200) {
10761082
String jsonString = new String(searchResults.getData(), "UTF-8");
10771083
JSONObject json = new JSONObject(jsonString);

0 commit comments

Comments
 (0)