|
43 | 43 | import java.io.Writer;
|
44 | 44 | import java.util.ArrayList;
|
45 | 45 | import java.util.Arrays;
|
| 46 | +import java.util.Collections; |
46 | 47 | import java.util.Date;
|
47 | 48 | import java.util.HashSet;
|
48 | 49 | import java.util.List;
|
@@ -1249,6 +1250,117 @@ private JSONObject _browseMirror(@NonNull Query query) throws AlgoliaException
|
1249 | 1250 | // Getting individual objects
|
1250 | 1251 | // ----------------------------------------------------------------------
|
1251 | 1252 |
|
| 1253 | + /** |
| 1254 | + * Get an individual object from the online API, falling back to the local mirror in case of error (when enabled). |
| 1255 | + * |
| 1256 | + * @param objectID Identifier of the object to retrieve. |
| 1257 | + * @param attributesToRetrieve Attributes to retrieve. If `null` or if at least one item is `*`, all retrievable |
| 1258 | + * attributes will be retrieved. |
| 1259 | + * @param completionHandler The listener that will be notified of the request's outcome. |
| 1260 | + * @return A cancellable request. |
| 1261 | + */ |
| 1262 | + @Override |
| 1263 | + public Request getObjectAsync(final @NonNull String objectID, final @Nullable List<String> attributesToRetrieve, @NonNull CompletionHandler completionHandler) { |
| 1264 | + if (!mirrored) { |
| 1265 | + return super.getObjectAsync(objectID, attributesToRetrieve, completionHandler); |
| 1266 | + } else { |
| 1267 | + return new OnlineOfflineGetObjectRequest(objectID, attributesToRetrieve, completionHandler).start(); |
| 1268 | + } |
| 1269 | + } |
| 1270 | + |
| 1271 | + private class OnlineOfflineGetObjectRequest extends OnlineOfflineRequest { |
| 1272 | + private final String objectID; |
| 1273 | + private final List<String> attributesToRetrieve; |
| 1274 | + |
| 1275 | + public OnlineOfflineGetObjectRequest(@NonNull String objectID, final @Nullable List<String> attributesToRetrieve, @NonNull CompletionHandler completionHandler) { |
| 1276 | + super(completionHandler); |
| 1277 | + this.objectID = objectID; |
| 1278 | + this.attributesToRetrieve = attributesToRetrieve; |
| 1279 | + } |
| 1280 | + |
| 1281 | + @Override |
| 1282 | + protected Request startOnlineRequest(CompletionHandler completionHandler) { |
| 1283 | + return getObjectOnlineAsync(objectID, attributesToRetrieve, completionHandler); |
| 1284 | + } |
| 1285 | + |
| 1286 | + @Override |
| 1287 | + protected Request startOfflineRequest(CompletionHandler completionHandler) { |
| 1288 | + return getObjectOfflineAsync(objectID, attributesToRetrieve, completionHandler); |
| 1289 | + } |
| 1290 | + } |
| 1291 | + |
| 1292 | + /** |
| 1293 | + * Get an individual object, explicitly targeting the online API, not the offline mirror. |
| 1294 | + * |
| 1295 | + * @param objectID Identifier of the object to retrieve. |
| 1296 | + * @param attributesToRetrieve Attributes to retrieve. If `null` or if at least one item is `*`, all retrievable |
| 1297 | + * attributes will be retrieved. |
| 1298 | + * @param completionHandler The listener that will be notified of the request's outcome. |
| 1299 | + * @return A cancellable request. |
| 1300 | + */ |
| 1301 | + public Request getObjectOnlineAsync(@NonNull final String objectID, final @Nullable List<String> attributesToRetrieve, @NonNull final CompletionHandler completionHandler) { |
| 1302 | + // TODO: Cannot perform origin tagging because it could conflict with the object's attributes |
| 1303 | + return super.getObjectAsync(objectID, attributesToRetrieve, completionHandler); |
| 1304 | + } |
| 1305 | + |
| 1306 | + /** |
| 1307 | + * Get an individual object, explicitly targeting the online API, not the offline mirror. |
| 1308 | + * |
| 1309 | + * @param objectID Identifier of the object to retrieve. |
| 1310 | + * @param completionHandler The listener that will be notified of the request's outcome. |
| 1311 | + * @return A cancellable request. |
| 1312 | + */ |
| 1313 | + public Request getObjectOnlineAsync(@NonNull final String objectID, @NonNull final CompletionHandler completionHandler) { |
| 1314 | + return getObjectOnlineAsync(objectID, null, completionHandler); |
| 1315 | + } |
| 1316 | + |
| 1317 | + /** |
| 1318 | + * Get an individual object, explicitly targeting the offline mirror, not the online API. |
| 1319 | + * |
| 1320 | + * @param objectID Identifier of the object to retrieve. |
| 1321 | + * @param attributesToRetrieve Attributes to retrieve. If `null` or if at least one item is `*`, all retrievable |
| 1322 | + * attributes will be retrieved. |
| 1323 | + * @param completionHandler The listener that will be notified of the request's outcome. |
| 1324 | + * @return A cancellable request. |
| 1325 | + * @throws IllegalStateException if mirroring is not activated on this index. |
| 1326 | + */ |
| 1327 | + public Request getObjectOfflineAsync(@NonNull final String objectID, final @Nullable List<String> attributesToRetrieve, @NonNull CompletionHandler completionHandler) { |
| 1328 | + if (!mirrored) { |
| 1329 | + throw new IllegalStateException("Mirroring not activated on this index"); |
| 1330 | + } |
| 1331 | + return getClient().new AsyncTaskRequest(completionHandler, getClient().localSearchExecutorService) { |
| 1332 | + @NonNull |
| 1333 | + @Override |
| 1334 | + protected JSONObject run() throws AlgoliaException { |
| 1335 | + return _getObjectOffline(objectID, attributesToRetrieve); |
| 1336 | + } |
| 1337 | + }.start(); |
| 1338 | + } |
| 1339 | + |
| 1340 | + /** |
| 1341 | + * Get an individual object, explicitly targeting the offline mirror, not the online API. |
| 1342 | + * |
| 1343 | + * @param objectID Identifier of the object to retrieve. |
| 1344 | + * @param completionHandler The listener that will be notified of the request's outcome. |
| 1345 | + * @return A cancellable request. |
| 1346 | + * @throws IllegalStateException if mirroring is not activated on this index. |
| 1347 | + */ |
| 1348 | + public Request getObjectOfflineAsync(@NonNull final String objectID, @NonNull final CompletionHandler completionHandler) { |
| 1349 | + return getObjectOfflineAsync(objectID, null, completionHandler); |
| 1350 | + } |
| 1351 | + |
| 1352 | + private JSONObject _getObjectOffline(@NonNull final String objectID, final @Nullable List<String> attributesToRetrieve) throws AlgoliaException |
| 1353 | + { |
| 1354 | + try { |
| 1355 | + JSONObject content = _getObjectsOffline(Collections.singletonList(objectID), attributesToRetrieve); |
| 1356 | + JSONArray results = content.getJSONArray("results"); |
| 1357 | + return results.getJSONObject(0); |
| 1358 | + } |
| 1359 | + catch (JSONException e) { |
| 1360 | + throw new AlgoliaException("Invalid response returned", e); // should never happen |
| 1361 | + } |
| 1362 | + } |
| 1363 | + |
1252 | 1364 | /**
|
1253 | 1365 | * Get individual objects from the online API, falling back to the local mirror in case of error (when enabled).
|
1254 | 1366 | *
|
|
0 commit comments