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);
}
}
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;
}
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);
}
}
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);
}
}
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);
}
Aggregations