Search in sources :

Example 56 with CompoundKey

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;
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey)

Example 57 with CompoundKey

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;
}
Also used : AlbumEntry(com.linkedin.restli.example.AlbumEntry) CompoundKey(com.linkedin.restli.common.CompoundKey) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) Finder(com.linkedin.restli.server.annotations.Finder)

Example 58 with CompoundKey

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);
}
Also used : AlbumEntry(com.linkedin.restli.example.AlbumEntry) CompoundKey(com.linkedin.restli.common.CompoundKey) Test(org.testng.annotations.Test)

Example 59 with CompoundKey

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));
    }
}
Also used : AssociationsBuilders(com.linkedin.restli.examples.greetings.client.AssociationsBuilders) Message(com.linkedin.restli.examples.greetings.api.Message) CompoundKey(com.linkedin.restli.common.CompoundKey) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Example 60 with CompoundKey

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));
    }
}
Also used : Message(com.linkedin.restli.examples.greetings.api.Message) CompoundKey(com.linkedin.restli.common.CompoundKey) EntityResponse(com.linkedin.restli.common.EntityResponse) AssociationsRequestBuilders(com.linkedin.restli.examples.greetings.client.AssociationsRequestBuilders) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) Test(org.testng.annotations.Test)

Aggregations

CompoundKey (com.linkedin.restli.common.CompoundKey)94 Test (org.testng.annotations.Test)48 HashMap (java.util.HashMap)26 DataProvider (org.testng.annotations.DataProvider)20 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)17 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)16 GroupMembership (com.linkedin.restli.examples.groups.api.GroupMembership)15 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)14 AfterTest (org.testng.annotations.AfterTest)13 BeforeTest (org.testng.annotations.BeforeTest)13 DataMap (com.linkedin.data.DataMap)12 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)12 Key (com.linkedin.restli.server.Key)11 Followed (com.linkedin.restli.server.twitter.TwitterTestDataModels.Followed)10 HashSet (java.util.HashSet)10 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)9 TestRecord (com.linkedin.restli.client.test.TestRecord)9 BatchUpdateResult (com.linkedin.restli.server.BatchUpdateResult)9 AsyncFollowsAssociativeResource (com.linkedin.restli.server.twitter.AsyncFollowsAssociativeResource)9 EmptyRecord (com.linkedin.restli.common.EmptyRecord)8