@@ -1245,6 +1245,149 @@ private JSONObject _browseMirror(@NonNull Query query) throws AlgoliaException
1245
1245
}
1246
1246
}
1247
1247
1248
+ // ----------------------------------------------------------------------
1249
+ // Getting individual objects
1250
+ // ----------------------------------------------------------------------
1251
+
1252
+ /**
1253
+ * Get individual objects from the online API, falling back to the local mirror in case of error (when enabled).
1254
+ *
1255
+ * @param objectIDs Identifiers of objects to retrieve.
1256
+ * @param attributesToRetrieve Attributes to retrieve. If `null` or if at least one item is `*`, all retrievable
1257
+ * attributes will be retrieved.
1258
+ * @param completionHandler The listener that will be notified of the request's outcome.
1259
+ * @return A cancellable request.
1260
+ */
1261
+ @ Override
1262
+ public Request getObjectsAsync (final @ NonNull List <String > objectIDs , final @ Nullable List <String > attributesToRetrieve , @ NonNull CompletionHandler completionHandler ) {
1263
+ if (!mirrored ) {
1264
+ return super .getObjectsAsync (objectIDs , attributesToRetrieve , completionHandler );
1265
+ } else {
1266
+ return new OnlineOfflineGetObjectsRequest (objectIDs , attributesToRetrieve , completionHandler ).start ();
1267
+ }
1268
+ }
1269
+
1270
+ private class OnlineOfflineGetObjectsRequest extends OnlineOfflineRequest {
1271
+ private final List <String > objectIDs ;
1272
+ private final List <String > attributesToRetrieve ;
1273
+
1274
+ public OnlineOfflineGetObjectsRequest (@ NonNull List <String > objectIDs , final @ Nullable List <String > attributesToRetrieve , @ NonNull CompletionHandler completionHandler ) {
1275
+ super (completionHandler );
1276
+ this .objectIDs = objectIDs ;
1277
+ this .attributesToRetrieve = attributesToRetrieve ;
1278
+ }
1279
+
1280
+ @ Override
1281
+ protected Request startOnlineRequest (CompletionHandler completionHandler ) {
1282
+ return getObjectsOnlineAsync (objectIDs , attributesToRetrieve , completionHandler );
1283
+ }
1284
+
1285
+ @ Override
1286
+ protected Request startOfflineRequest (CompletionHandler completionHandler ) {
1287
+ return getObjectsOfflineAsync (objectIDs , attributesToRetrieve , completionHandler );
1288
+ }
1289
+ }
1290
+
1291
+ /**
1292
+ * Get individual objects, explicitly targeting the online API, not the offline mirror.
1293
+ *
1294
+ * @param objectIDs Identifiers of objects to retrieve.
1295
+ * @param attributesToRetrieve Attributes to retrieve. If `null` or if at least one item is `*`, all retrievable
1296
+ * attributes will be retrieved.
1297
+ * @param completionHandler The listener that will be notified of the request's outcome.
1298
+ * @return A cancellable request.
1299
+ */
1300
+ public Request getObjectsOnlineAsync (@ NonNull final List <String > objectIDs , final @ Nullable List <String > attributesToRetrieve , @ NonNull final CompletionHandler completionHandler ) {
1301
+ return getClient ().new AsyncTaskRequest (completionHandler ) {
1302
+ @ NonNull
1303
+ @ Override
1304
+ protected JSONObject run () throws AlgoliaException {
1305
+ return getObjectsOnline (objectIDs , attributesToRetrieve );
1306
+ }
1307
+ }.start ();
1308
+ }
1309
+
1310
+ /**
1311
+ * Get individual objects, explicitly targeting the online API, not the offline mirror.
1312
+ *
1313
+ * @param objectIDs Identifiers of objects to retrieve.
1314
+ * @param completionHandler The listener that will be notified of the request's outcome.
1315
+ * @return A cancellable request.
1316
+ */
1317
+ public Request getObjectsOnlineAsync (@ NonNull final List <String > objectIDs , @ NonNull final CompletionHandler completionHandler ) {
1318
+ return getObjectsOnlineAsync (objectIDs , null , completionHandler );
1319
+ }
1320
+
1321
+ private JSONObject getObjectsOnline (@ NonNull final List <String > objectIDs , final @ Nullable List <String > attributesToRetrieve ) throws AlgoliaException {
1322
+ try {
1323
+ JSONObject content = super .getObjects (objectIDs , attributesToRetrieve );
1324
+ // TODO: Factorize origin tagging
1325
+ content .put (JSON_KEY_ORIGIN , JSON_VALUE_ORIGIN_REMOTE );
1326
+ return content ;
1327
+ }
1328
+ catch (JSONException e ) {
1329
+ throw new AlgoliaException ("Failed to patch JSON result" );
1330
+ }
1331
+ }
1332
+
1333
+ /**
1334
+ * Get individual objects, explicitly targeting the offline mirror, not the online API.
1335
+ *
1336
+ * @param objectIDs Identifiers of objects to retrieve.
1337
+ * @param attributesToRetrieve Attributes to retrieve. If `null` or if at least one item is `*`, all retrievable
1338
+ * attributes will be retrieved.
1339
+ * @param completionHandler The listener that will be notified of the request's outcome.
1340
+ * @return A cancellable request.
1341
+ * @throws IllegalStateException if mirroring is not activated on this index.
1342
+ */
1343
+ public Request getObjectsOfflineAsync (@ NonNull final List <String > objectIDs , final @ Nullable List <String > attributesToRetrieve , @ NonNull CompletionHandler completionHandler ) {
1344
+ if (!mirrored ) {
1345
+ throw new IllegalStateException ("Mirroring not activated on this index" );
1346
+ }
1347
+ return getClient ().new AsyncTaskRequest (completionHandler , getClient ().localSearchExecutorService ) {
1348
+ @ NonNull
1349
+ @ Override
1350
+ protected JSONObject run () throws AlgoliaException {
1351
+ return _getObjectsOffline (objectIDs , attributesToRetrieve );
1352
+ }
1353
+ }.start ();
1354
+ }
1355
+
1356
+ /**
1357
+ * Get individual objects, explicitly targeting the offline mirror, not the online API.
1358
+ *
1359
+ * @param objectIDs Identifiers of objects to retrieve.
1360
+ * @param completionHandler The listener that will be notified of the request's outcome.
1361
+ * @return A cancellable request.
1362
+ * @throws IllegalStateException if mirroring is not activated on this index.
1363
+ */
1364
+ public Request getObjectsOfflineAsync (@ NonNull final List <String > objectIDs , @ NonNull final CompletionHandler completionHandler ) {
1365
+ return getObjectsOfflineAsync (objectIDs , null , completionHandler );
1366
+ }
1367
+
1368
+ private JSONObject _getObjectsOffline (@ NonNull final List <String > objectIDs , final @ Nullable List <String > attributesToRetrieve ) throws AlgoliaException
1369
+ {
1370
+ try {
1371
+ Query query = new Query ();
1372
+ if (attributesToRetrieve != null ) {
1373
+ query .setAttributesToRetrieve (attributesToRetrieve .toArray (new String [attributesToRetrieve .size ()]));
1374
+ }
1375
+ Response searchResults = getLocalIndex ().getObjects (objectIDs .toArray (new String [objectIDs .size ()]), query .build ());
1376
+ if (searchResults .getStatusCode () == 200 ) {
1377
+ String jsonString = new String (searchResults .getData (), "UTF-8" );
1378
+ JSONObject json = new JSONObject (jsonString );
1379
+ json .put (JSON_KEY_ORIGIN , JSON_VALUE_ORIGIN_LOCAL );
1380
+ return json ;
1381
+ }
1382
+ else {
1383
+ throw new AlgoliaException (searchResults .getErrorMessage (), searchResults .getStatusCode ());
1384
+ }
1385
+ }
1386
+ catch (JSONException | UnsupportedEncodingException e ) {
1387
+ throw new AlgoliaException ("Get objects failed" , e );
1388
+ }
1389
+ }
1390
+
1248
1391
// ----------------------------------------------------------------------
1249
1392
// Listeners
1250
1393
// ----------------------------------------------------------------------
0 commit comments