use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.
the class JsonKeyValueRawEventsTest method testReceiveKeyValuesAsJson.
public void testReceiveKeyValuesAsJson() throws InterruptedException {
BlockingQueue<KeyValuePair<String, String>> eventsQueue = new LinkedBlockingQueue<>();
DataFormat jsonValues = DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build();
RemoteCache<String, Person> cache = remoteCacheManager.getCache();
RemoteCache<String, String> jsonCache = cache.withDataFormat(jsonValues);
jsonCache.addClientListener(new EventListener(eventsQueue, jsonCache.getDataFormat()));
cache.put("1", new Person("John"));
KeyValuePair<String, String> event = eventsQueue.poll(5, TimeUnit.SECONDS);
assertNotNull(event);
assertEquals(event.getKey(), "1");
assertEquals(event.getValue(), "\n{\n \"_type\": \"org.infinispan.test.core.Person\",\n \"name\": \"John\",\n \"birthDate\": 0,\n \"accepted_tos\": false\n}\n");
}
use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.
the class PutAllParallelOperation method mapOperations.
@Override
protected List<PutAllOperation> mapOperations() {
Map<SocketAddress, Map<byte[], byte[]>> splittedMaps = new HashMap<>();
for (Map.Entry<byte[], byte[]> entry : map.entrySet()) {
SocketAddress socketAddress = channelFactory.getHashAwareServer(entry.getKey(), cacheName);
Map<byte[], byte[]> keyValueMap = splittedMaps.get(socketAddress);
if (keyValueMap == null) {
keyValueMap = new HashMap<>();
splittedMaps.put(socketAddress, keyValueMap);
}
keyValueMap.put(entry.getKey(), entry.getValue());
}
return splittedMaps.values().stream().map(mapSubset -> new PutAllOperation(codec, channelFactory, mapSubset, cacheName, header.topologyId(), flags, cfg, lifespan, lifespanTimeUnit, maxIdle, maxIdleTimeUnit, dataFormat, clientStatistics)).collect(Collectors.toList());
}
use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.
the class GetAllParallelOperation method mapOperations.
@Override
protected List<GetAllOperation<K, V>> mapOperations() {
Map<SocketAddress, Set<byte[]>> splittedKeys = new HashMap<>();
for (byte[] key : keys) {
SocketAddress socketAddress = channelFactory.getHashAwareServer(key, cacheName);
Set<byte[]> keys = splittedKeys.computeIfAbsent(socketAddress, k -> new HashSet<>());
keys.add(key);
}
return splittedKeys.values().stream().map(keysSubset -> new GetAllOperation<K, V>(codec, channelFactory, keysSubset, cacheName, header.topologyId(), flags, cfg, dataFormat, clientStatistics)).collect(Collectors.toList());
}
use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.
the class HotRodQueryIspnDirectoryTest method testReadAsJSON.
public void testReadAsJSON() {
DataFormat acceptJSON = DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build();
RemoteCache<Integer, String> jsonCache = remoteCache.withDataFormat(acceptJSON);
Json user1 = Json.read(jsonCache.get(1));
assertEquals("Tom", user1.at("name").asString());
assertEquals("Cat", user1.at("surname").asString());
Query<String> query = Search.getQueryFactory(jsonCache).create("FROM sample_bank_account.User WHERE name = :name");
query.maxResults(10).startOffset(0).setParameter("name", "Tom");
QueryResult<String> result = query.execute();
List<String> results = result.list();
assertEquals(1, query.getResultSize());
assertFalse(query.hasProjections());
Json jsonNode = Json.read(results.iterator().next());
assertEquals("Tom", jsonNode.at("name").asString());
assertEquals("Cat", jsonNode.at("surname").asString());
query = Search.getQueryFactory(jsonCache).create("FROM sample_bank_account.User WHERE name = \"Tom\"");
results = query.execute().list();
assertEquals(1, results.size());
}
use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.
the class EndpointInteroperabilityTest method testStringKeysAndByteArrayValue.
@Test
public void testStringKeysAndByteArrayValue() throws Exception {
// The Hot Rod client writes marshalled content. The cache is explicitly configured with
// 'application/x-protostream' for both K and V.
String key = "string-1";
byte[] value = { 1, 2, 3 };
byte[] marshalledValue = new ProtoStreamMarshaller().objectToByteBuffer(value);
defaultMarshalledRemoteCache.put(key, value);
assertEquals(defaultMarshalledRemoteCache.get(key), value);
// Read via Rest the raw content, as it is stored
Object rawFromRest = new RestRequest().cache(MARSHALLED_CACHE_NAME).key(key).accept(APPLICATION_PROTOSTREAM).read();
assertArrayEquals((byte[]) rawFromRest, marshalledValue);
// Write via rest raw bytes
String otherKey = "string-2";
byte[] otherValue = { 0x4, 0x5, 0x6 };
byte[] otherValueMarshalled = new ProtoStreamMarshaller().objectToByteBuffer(otherValue);
new RestRequest().cache(MARSHALLED_CACHE_NAME).key(otherKey).value(otherValue, APPLICATION_OCTET_STREAM).write();
// Read via Hot Rod
assertEquals(defaultMarshalledRemoteCache.get(otherKey), otherValue);
// Read via Hot Rod using a String key, and getting the raw value (as it is stored) back
DataFormat format = DataFormat.builder().keyType(TEXT_PLAIN).valueType(APPLICATION_PROTOSTREAM).valueMarshaller(IdentityMarshaller.INSTANCE).build();
byte[] rawValue = (byte[]) defaultMarshalledRemoteCache.withDataFormat(format).get(otherKey);
assertArrayEquals(otherValueMarshalled, rawValue);
// Read via Hot Rod using a String key, and getting the original value back
DataFormat.builder().keyType(TEXT_PLAIN).build();
byte[] result = (byte[]) defaultMarshalledRemoteCache.withDataFormat(DataFormat.builder().keyType(TEXT_PLAIN).build()).get(otherKey);
assertArrayEquals(otherValue, result);
}
Aggregations