use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class AlbumEntryResource method purge.
/**
* Delete all entries in the db with matching album/photo IDs. <code>null</code> is
* treated as a wildcard.
*/
public static int purge(AlbumEntryDatabase db, Long albumId, Long photoId) {
// purge 1 entry
if (albumId != null && photoId != null) {
CompoundKey key = new CompoundKey().append("photoId", photoId).append("albumId", albumId);
final boolean isRemoved = (db.getData().remove(key) != null);
return isRemoved ? 1 : 0;
}
// purge all
if (albumId == null && photoId == null) {
final int numPurged = db.getData().size();
db.getData().clear();
return numPurged;
}
// purge all matching one of key id, photo id
Iterator<CompoundKey> it = db.getData().keySet().iterator();
String partName;
long compareId;
if (albumId != null) {
partName = "albumId";
compareId = albumId;
} else if (photoId != null) {
partName = "photoId";
compareId = photoId;
} else
throw new AssertionError();
int numPurged = 0;
while (it.hasNext()) {
CompoundKey key = it.next();
if (key.getPart(partName).equals(compareId)) {
it.remove();
numPurged++;
}
}
return numPurged;
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class AlbumEntryResource method search.
/**
* Find all entries matching the given album and photo IDs. <code>null</code> is treated
* as a wildcard.
*
* @param albumId provides the id to match for albums to match, if not provided, it is treated as a wildcard
* @param photoId provides the id to match for photos to match, if not provided, it is treated as a wildcard
* @return a list of {@link AlbumEntry} matching the given parameters
*/
@Finder("search")
public List<AlbumEntry> search(@Optional @QueryParam("albumId") Long albumId, @Optional @QueryParam("photoId") Long photoId) {
List<AlbumEntry> result = new ArrayList<AlbumEntry>();
for (Map.Entry<CompoundKey, AlbumEntry> entry : _db.getData().entrySet()) {
CompoundKey key = entry.getKey();
// (treat all values as a match)
if (albumId != null && !key.getPart("albumId").equals(albumId))
continue;
if (photoId != null && !key.getPart("photoId").equals(photoId))
continue;
result.add(entry.getValue());
}
return result;
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestAlbumEntryResource method testBadUpdateIdsInEntry.
@Test(expectedExceptions = RestLiServiceException.class)
public void testBadUpdateIdsInEntry() {
// shouldn't be able to put IDs in update entry
CompoundKey key = new CompoundKey().append("photoId", 1L).append("albumId", 1L);
AlbumEntry entry = new AlbumEntry().setAddTime(4).setPhotoId(1);
_entryRes.update(key, entry);
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestAssociationsResource method testBatchGet.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchGet(RestliRequestOptions requestOptions) throws RemoteInvocationException {
Request<BatchKVResponse<CompoundKey, Message>> request = new AssociationsBuilders(requestOptions).batchGet().ids(DB.keySet()).buildKV();
ResponseFuture<BatchKVResponse<CompoundKey, Message>> responseFuture = getClient().sendRequest(request);
Response<BatchKVResponse<CompoundKey, Message>> response = responseFuture.getResponse();
BatchKVResponse<CompoundKey, Message> entity = response.getEntity();
Assert.assertEquals(entity.getErrors().size(), 0);
Assert.assertEquals(entity.getResults().size(), 2);
for (CompoundKey id : DB.keySet()) {
Assert.assertTrue(entity.getResults().containsKey(id));
Assert.assertEquals(entity.getResults().get(id), DB.get(id));
}
}
use of com.linkedin.restli.common.CompoundKey in project rest.li by linkedin.
the class TestAssociationsResource method testBatchGetEntity.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testBatchGetEntity(RestliRequestOptions requestOptions) throws RemoteInvocationException {
Request<BatchKVResponse<CompoundKey, EntityResponse<Message>>> request = new AssociationsRequestBuilders(requestOptions).batchGet().ids(DB.keySet()).build();
ResponseFuture<BatchKVResponse<CompoundKey, EntityResponse<Message>>> responseFuture = getClient().sendRequest(request);
Response<BatchKVResponse<CompoundKey, EntityResponse<Message>>> response = responseFuture.getResponse();
BatchKVResponse<CompoundKey, EntityResponse<Message>> entityResponse = response.getEntity();
Assert.assertEquals(entityResponse.getErrors().size(), 0);
Assert.assertEquals(entityResponse.getResults().size(), 2);
for (CompoundKey id : DB.keySet()) {
EntityResponse<Message> single = entityResponse.getResults().get(id);
Assert.assertTrue(entityResponse.getResults().containsKey(id));
Assert.assertEquals(single.getEntity(), DB.get(id));
}
}
Aggregations