Search in sources :

Example 1 with KeyValueRecord

use of com.linkedin.restli.common.KeyValueRecord in project rest.li by linkedin.

the class BatchPartialUpdateRequestBuilder method buildReadOnlyInput.

private CollectionRequest<KeyValueRecord<K, PatchRequest<V>>> buildReadOnlyInput() {
    try {
        DataMap map = new DataMap();
        @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord<K, PatchRequest<V>>> input = new CollectionRequest(map, KeyValueRecord.class);
        for (Map.Entry<K, PatchRequest<V>> inputEntityEntry : _partialUpdateInputMap.entrySet()) {
            K key = getReadOnlyOrCopyKey(inputEntityEntry.getKey());
            PatchRequest<V> entity = getReadOnlyOrCopyDataTemplate(inputEntityEntry.getValue());
            KeyValueRecord<K, PatchRequest<V>> keyValueRecord = _keyValueRecordFactory.create(key, entity);
            keyValueRecord.data().setReadOnly();
            input.getElements().add(keyValueRecord);
        }
        map.setReadOnly();
        return input;
    } catch (CloneNotSupportedException cloneException) {
        throw new IllegalArgumentException("Entity cannot be copied.", cloneException);
    }
}
Also used : CollectionRequest(com.linkedin.restli.common.CollectionRequest) PatchRequest(com.linkedin.restli.common.PatchRequest) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map)

Example 2 with KeyValueRecord

use of com.linkedin.restli.common.KeyValueRecord in project rest.li by linkedin.

the class CollectionRequestUtil method convertToBatchRequest.

/**
 * Converts the new way of representing {@link com.linkedin.restli.client.BatchUpdateRequest}s and
 * {@link com.linkedin.restli.client.BatchPartialUpdateRequest}s bodies into the old way
 * @param elementList new style encoding
 * @param keyType
 * @param complexKeyType
 * @param keyParts
 * @param valueType
 * @param version protocol version to use for encoding
 * @param <V>
 * @return a data map with one key, "entities". "entities" maps to another data map (as in the old body encoding)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <V extends RecordTemplate> BatchRequest<V> convertToBatchRequest(CollectionRequest<KeyValueRecord> elementList, TypeSpec<?> keyType, ComplexKeySpec<?, ?> complexKeyType, Map<String, CompoundKey.TypeInfo> keyParts, TypeSpec<V> valueType, ProtocolVersion version) {
    BatchRequest<V> batchRequest = new BatchRequest<>(new DataMap(), valueType);
    for (KeyValueRecord keyValueRecord : elementList.getElements()) {
        V value = (V) keyValueRecord.getValue(valueType);
        Object key = null;
        if (keyType != null) {
            if (keyType.getType().equals(ComplexResourceKey.class)) {
                // complex keys
                key = keyValueRecord.getComplexKey(complexKeyType);
            } else if (CompoundKey.class.isAssignableFrom(keyType.getType())) {
                key = keyValueRecord.getCompoundKey(keyParts);
            } else {
                // primitive keys
                key = keyValueRecord.getPrimitiveKey(keyType);
            }
        }
        batchRequest.getEntities().put(URIParamUtils.encodeKeyForBody(key, true, version), value);
    }
    return batchRequest;
}
Also used : KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) BatchRequest(com.linkedin.restli.common.BatchRequest) CompoundKey(com.linkedin.restli.common.CompoundKey) DataMap(com.linkedin.data.DataMap)

Example 3 with KeyValueRecord

use of com.linkedin.restli.common.KeyValueRecord in project rest.li by linkedin.

the class BatchKVRequestBuilder method buildReadOnlyInput.

protected <E extends RecordTemplate> CollectionRequest<KeyValueRecord<K, E>> buildReadOnlyInput(Map<K, E> readOnlyInputEntities, Map<K, E> inputMap, KeyValueRecordFactory<K, E> keyValueRecordFactory) {
    try {
        DataMap map = new DataMap();
        @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord<K, E>> input = new CollectionRequest(map, KeyValueRecord.class);
        for (Map.Entry<K, E> inputEntityEntry : inputMap.entrySet()) {
            K key = getReadOnlyOrCopyKey(inputEntityEntry.getKey());
            E entity = getReadOnlyOrCopyDataTemplate(inputEntityEntry.getValue());
            readOnlyInputEntities.put(key, entity);
            KeyValueRecord<K, E> keyValueRecord = keyValueRecordFactory.create(key, entity);
            keyValueRecord.data().setReadOnly();
            input.getElements().add(keyValueRecord);
        }
        map.setReadOnly();
        return input;
    } catch (CloneNotSupportedException cloneException) {
        throw new IllegalArgumentException("Entity cannot be copied.", cloneException);
    }
}
Also used : KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) CollectionRequest(com.linkedin.restli.common.CollectionRequest) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 4 with KeyValueRecord

use of com.linkedin.restli.common.KeyValueRecord in project rest.li by linkedin.

the class BatchUpdateRequestBuilder method buildReadOnlyBatchUpdateInput.

private CollectionRequest<KeyValueRecord<K, V>> buildReadOnlyBatchUpdateInput(Map<K, V> readOnlyInputEntities) {
    try {
        DataMap map = new DataMap();
        @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord<K, V>> input = new CollectionRequest(map, KeyValueRecord.class);
        for (Map.Entry<K, V> inputEntityEntry : _updateInputMap.entrySet()) {
            K key = getReadOnlyOrCopyKey(inputEntityEntry.getKey());
            V entity = getReadOnlyOrCopyDataTemplate(inputEntityEntry.getValue());
            readOnlyInputEntities.put(key, entity);
            KeyValueRecord<K, V> keyValueRecord = _keyValueRecordFactory.create(key, entity);
            keyValueRecord.data().setReadOnly();
            input.getElements().add(keyValueRecord);
        }
        map.setReadOnly();
        return input;
    } catch (CloneNotSupportedException cloneException) {
        throw new IllegalArgumentException("Entity cannot be copied.", cloneException);
    }
}
Also used : KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) CollectionRequest(com.linkedin.restli.common.CollectionRequest) HashMap(java.util.HashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 5 with KeyValueRecord

use of com.linkedin.restli.common.KeyValueRecord in project rest.li by linkedin.

the class TestClientBuilders method testBatchUpdateRequestBuilder.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "batch")
public void testBatchUpdateRequestBuilder(URIDetails expectedURIDetails) {
    BatchUpdateRequestBuilder<Long, TestRecord> builder = new BatchUpdateRequestBuilder<>(TEST_URI, TestRecord.class, _COLL_SPEC, RestliRequestOptions.DEFAULT_OPTIONS);
    Map<Long, TestRecord> updates = new HashMap<>();
    updates.put(1L, new TestRecord());
    updates.put(2L, new TestRecord());
    updates.put(3L, new TestRecord());
    BatchUpdateRequest<Long, TestRecord> request = builder.inputs(updates).appendSingleAttachment(_dataSourceWriterA).appendMultipleAttachments(_dataSourceIterator).appendSingleAttachment(_dataSourceWriterB).build();
    testBaseUriGeneration(request, expectedURIDetails.getProtocolVersion());
    Assert.assertEquals(request.getObjectIds(), new HashSet<>(Arrays.asList(1L, 2L, 3L)));
    Assert.assertEquals(request.isSafe(), false);
    Assert.assertEquals(request.isIdempotent(), true);
    BatchRequest<TestRecord> expectedRequest = new BatchRequest<>(new DataMap(), TestRecord.class);
    expectedRequest.getEntities().put("1", new TestRecord());
    expectedRequest.getEntities().put("2", new TestRecord());
    expectedRequest.getEntities().put("3", new TestRecord());
    @SuppressWarnings({ "unchecked", "rawtypes" }) KeyValueRecordFactory<Long, TestRecord> factory = new KeyValueRecordFactory<>(Long.class, null, null, null, TestRecord.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) CollectionRequest<KeyValueRecord> collectionRequest = buildCollectionRequest(factory, new Long[] { 1L, 2L, 3L }, new TestRecord[] { new TestRecord(), new TestRecord(), new TestRecord() });
    checkBasicRequest(request, expectedURIDetails, ResourceMethod.BATCH_UPDATE, collectionRequest, expectedRequest, Collections.<String, String>emptyMap(), _streamingDataSources);
}
Also used : BatchRequest(com.linkedin.restli.common.BatchRequest) HashMap(java.util.HashMap) KeyValueRecordFactory(com.linkedin.restli.common.KeyValueRecordFactory) DataMap(com.linkedin.data.DataMap) KeyValueRecord(com.linkedin.restli.common.KeyValueRecord) TestRecord(com.linkedin.restli.client.test.TestRecord) Test(org.testng.annotations.Test)

Aggregations

KeyValueRecord (com.linkedin.restli.common.KeyValueRecord)16 DataMap (com.linkedin.data.DataMap)11 TestRecord (com.linkedin.restli.client.test.TestRecord)11 Test (org.testng.annotations.Test)11 KeyValueRecordFactory (com.linkedin.restli.common.KeyValueRecordFactory)10 HashMap (java.util.HashMap)10 CollectionRequest (com.linkedin.restli.common.CollectionRequest)8 BatchRequest (com.linkedin.restli.common.BatchRequest)7 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)4 CompoundKey (com.linkedin.restli.common.CompoundKey)4 PatchRequest (com.linkedin.restli.common.PatchRequest)4 Map (java.util.Map)4 DynamicRecordTemplate (com.linkedin.data.template.DynamicRecordTemplate)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1