Search in sources :

Example 6 with DataFormat

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");
}
Also used : KeyValuePair(org.infinispan.util.KeyValuePair) UTF8StringMarshaller(org.infinispan.commons.marshall.UTF8StringMarshaller) DataFormat(org.infinispan.client.hotrod.DataFormat) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Person(org.infinispan.test.data.Person)

Example 7 with DataFormat

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());
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) ClientStatistics(org.infinispan.client.hotrod.impl.ClientStatistics) ChannelFactory(org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory) List(java.util.List) SocketAddress(java.net.SocketAddress) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataFormat(org.infinispan.client.hotrod.DataFormat) Map(java.util.Map) Configuration(org.infinispan.client.hotrod.configuration.Configuration) HashMap(java.util.HashMap) Codec(org.infinispan.client.hotrod.impl.protocol.Codec) Collectors(java.util.stream.Collectors) HashMap(java.util.HashMap) SocketAddress(java.net.SocketAddress) Map(java.util.Map) HashMap(java.util.HashMap)

Example 8 with DataFormat

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());
}
Also used : ClientStatistics(org.infinispan.client.hotrod.impl.ClientStatistics) SocketAddress(java.net.SocketAddress) Configuration(org.infinispan.client.hotrod.configuration.Configuration) Set(java.util.Set) HashMap(java.util.HashMap) Codec(org.infinispan.client.hotrod.impl.protocol.Codec) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) ChannelFactory(org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DataFormat(org.infinispan.client.hotrod.DataFormat) Map(java.util.Map) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SocketAddress(java.net.SocketAddress)

Example 9 with DataFormat

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());
}
Also used : UTF8StringMarshaller(org.infinispan.commons.marshall.UTF8StringMarshaller) DataFormat(org.infinispan.client.hotrod.DataFormat) Json(org.infinispan.commons.dataconversion.internal.Json)

Example 10 with DataFormat

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);
}
Also used : ProtoStreamMarshaller(org.infinispan.commons.marshall.ProtoStreamMarshaller) DataFormat(org.infinispan.client.hotrod.DataFormat) Test(org.testng.annotations.Test) AbstractInfinispanTest(org.infinispan.test.AbstractInfinispanTest)

Aggregations

DataFormat (org.infinispan.client.hotrod.DataFormat)13 UTF8StringMarshaller (org.infinispan.commons.marshall.UTF8StringMarshaller)6 Test (org.testng.annotations.Test)4 SingleHotRodServerTest (org.infinispan.client.hotrod.test.SingleHotRodServerTest)3 SocketAddress (java.net.SocketAddress)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2 Configuration (org.infinispan.client.hotrod.configuration.Configuration)2 ClientEvent (org.infinispan.client.hotrod.event.ClientEvent)2 AbstractClientEvent (org.infinispan.client.hotrod.event.impl.AbstractClientEvent)2 ClientStatistics (org.infinispan.client.hotrod.impl.ClientStatistics)2 Codec (org.infinispan.client.hotrod.impl.protocol.Codec)2 ChannelFactory (org.infinispan.client.hotrod.impl.transport.netty.ChannelFactory)2 Json (org.infinispan.commons.dataconversion.internal.Json)2 Marshaller (org.infinispan.commons.marshall.Marshaller)2 ProtoStreamMarshaller (org.infinispan.commons.marshall.ProtoStreamMarshaller)2 HashSet (java.util.HashSet)1