Search in sources :

Example 1 with DataFormat

use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.

the class Codec20 method readCacheEvent.

@Override
public AbstractClientEvent readCacheEvent(ByteBuf buf, Function<byte[], DataFormat> listenerDataFormat, short eventTypeId, ClassAllowList allowList, SocketAddress serverAddress) {
    short status = buf.readUnsignedByte();
    // ignore, no topology expected
    buf.readUnsignedByte();
    ClientEvent.Type eventType;
    switch(eventTypeId) {
        case CACHE_ENTRY_CREATED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_CREATED;
            break;
        case CACHE_ENTRY_MODIFIED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_MODIFIED;
            break;
        case CACHE_ENTRY_REMOVED_EVENT_RESPONSE:
            eventType = ClientEvent.Type.CLIENT_CACHE_ENTRY_REMOVED;
            break;
        case ERROR_RESPONSE:
            checkForErrorsInResponseStatus(buf, null, status, serverAddress);
        // Fall through if we didn't throw an exception already
        default:
            throw HOTROD.unknownEvent(eventTypeId);
    }
    byte[] listenerId = ByteBufUtil.readArray(buf);
    short isCustom = buf.readUnsignedByte();
    boolean isRetried = buf.readUnsignedByte() == 1;
    DataFormat dataFormat = listenerDataFormat.apply(listenerId);
    if (isCustom == 1) {
        final Object eventData = dataFormat.valueToObj(ByteBufUtil.readArray(buf), allowList);
        return createCustomEvent(listenerId, eventData, eventType, isRetried);
    } else {
        switch(eventType) {
            case CLIENT_CACHE_ENTRY_CREATED:
                long createdDataVersion = buf.readLong();
                return createCreatedEvent(listenerId, dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList), createdDataVersion, isRetried);
            case CLIENT_CACHE_ENTRY_MODIFIED:
                long modifiedDataVersion = buf.readLong();
                return createModifiedEvent(listenerId, dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList), modifiedDataVersion, isRetried);
            case CLIENT_CACHE_ENTRY_REMOVED:
                return createRemovedEvent(listenerId, dataFormat.keyToObj(ByteBufUtil.readArray(buf), allowList), isRetried);
            default:
                throw HOTROD.unknownEvent(eventTypeId);
        }
    }
}
Also used : DataFormat(org.infinispan.client.hotrod.DataFormat) ClientEvent(org.infinispan.client.hotrod.event.ClientEvent) AbstractClientEvent(org.infinispan.client.hotrod.event.impl.AbstractClientEvent)

Example 2 with DataFormat

use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.

the class ComplexValue method testListenersWithDifferentFormats.

@Test
public void testListenersWithDifferentFormats() {
    remoteCache.clear();
    ComplexKey complexKey = new ComplexKey("Key-1", 89.88f);
    ComplexValue complexValue = new ComplexValue(Util.threadLocalRandomUUID());
    // Receive events as JSON Strings
    DataFormat jsonStringFormat = DataFormat.builder().keyType(APPLICATION_JSON).keyMarshaller(new UTF8StringMarshaller()).build();
    EventLogListener<Object> l = new EventLogListener<>(remoteCache.withDataFormat(jsonStringFormat));
    withClientListener(l, remote -> {
        remoteCache.put(complexKey, complexValue);
        l.expectOnlyCreatedEvent("\n{\n   \"_type\": \"org.infinispan.test.client.DataFormatTest.ComplexKey\",\n   \"id\": \"Key-1\",\n   \"ratio\": 89.88\n}\n");
    });
}
Also used : RawStaticFilteredEventLogListener(org.infinispan.client.hotrod.event.EventLogListener.RawStaticFilteredEventLogListener) StaticFilteredEventLogListener(org.infinispan.client.hotrod.event.EventLogListener.StaticFilteredEventLogListener) EventLogListener(org.infinispan.client.hotrod.event.EventLogListener) UTF8StringMarshaller(org.infinispan.commons.marshall.UTF8StringMarshaller) DataFormat(org.infinispan.client.hotrod.DataFormat) Test(org.testng.annotations.Test) SingleHotRodServerTest(org.infinispan.client.hotrod.test.SingleHotRodServerTest)

Example 3 with DataFormat

use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.

the class ComplexValue method testJsonFromDefaultCache.

@Test
public void testJsonFromDefaultCache() {
    RemoteCache<String, String> schemaCache = remoteCacheManager.getCache(PROTOBUF_METADATA_CACHE_NAME);
    schemaCache.put("schema.proto", "message M { optional string json_key = 1; }");
    RemoteQueryTestUtils.checkSchemaErrors(schemaCache);
    DataFormat jsonValues = DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build();
    RemoteCache<Integer, String> cache = remoteCacheManager.getCache().withDataFormat(jsonValues);
    String value = "{\"_type\":\"M\",\"json_key\":\"json_value\"}";
    cache.put(1, value);
    String valueAsJson = cache.get(1);
    Json node = Json.read(valueAsJson);
    assertEquals("json_value", node.at("json_key").asString());
}
Also used : UTF8StringMarshaller(org.infinispan.commons.marshall.UTF8StringMarshaller) DataFormat(org.infinispan.client.hotrod.DataFormat) Json(org.infinispan.commons.dataconversion.internal.Json) Test(org.testng.annotations.Test) SingleHotRodServerTest(org.infinispan.client.hotrod.test.SingleHotRodServerTest)

Example 4 with DataFormat

use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.

the class RemoteQueryRepeatedMappingTest method testCreateAndQuery.

@Test
public void testCreateAndQuery() throws Exception {
    registerProtoBuf();
    RemoteCache<Object, Object> cache = remoteCacheManager.administration().withFlags(VOLATILE).createCache(CACHE_NAME, createCacheXMLConfig());
    DataFormat dataFormat = DataFormat.builder().keyType(APPLICATION_JSON).valueType(APPLICATION_JSON).build();
    RemoteCache<byte[], byte[]> jsonCache = cache.withDataFormat(dataFormat);
    jsonCache.put(keyAsJson(), valueAsJson());
    Query<Object> querySlowChildren = Search.getQueryFactory(cache).create("SELECT COUNT(*) FROM Parent p WHERE p.slowChildren.id = 0");
    Query<Object> queryFastChildren = Search.getQueryFactory(cache).create("SELECT COUNT(*) FROM Parent p WHERE p.fastChildren.id = 10");
    Query<Object> queryFieldChildren = Search.getQueryFactory(cache).create("SELECT COUNT(*) FROM Parent p WHERE p.fieldLessChildren.id = 0");
    Query<Object> queryNotIndexedWithFieldChildren = Search.getQueryFactory(cache).create("SELECT COUNT(*) FROM Parent p WHERE p.notIndexedWithFieldChild.id = 37");
    assertEquals(1, querySlowChildren.execute().hitCount().orElse(-1));
    assertEquals(1, queryFastChildren.execute().hitCount().orElse(-1));
    assertEquals(1, queryFieldChildren.execute().hitCount().orElse(-1));
    assertEquals(1, queryNotIndexedWithFieldChildren.execute().hitCount().orElse(-1));
}
Also used : DataFormat(org.infinispan.client.hotrod.DataFormat) Test(org.testng.annotations.Test) SingleHotRodServerTest(org.infinispan.client.hotrod.test.SingleHotRodServerTest)

Example 5 with DataFormat

use of org.infinispan.client.hotrod.DataFormat in project infinispan by infinispan.

the class JsonEventsTest method testCreatedEvent.

public void testCreatedEvent() {
    DataFormat jsonValues = DataFormat.builder().valueType(APPLICATION_JSON).valueMarshaller(new UTF8StringMarshaller()).build();
    final EventLogListener<Integer> l = new EventLogListener<>(remoteCacheManager.getCache().withDataFormat(jsonValues));
    withClientListener(l, remote -> {
        l.expectNoEvents();
        remote.put(1, "{\"_type\":\"A\",\"key\":\"one\"}");
        l.expectOnlyCreatedEvent(1);
        remote.put(2, "{\"_type\":\"A\",\"key\":\"two\"}");
        l.expectOnlyCreatedEvent(2);
    });
}
Also used : UTF8StringMarshaller(org.infinispan.commons.marshall.UTF8StringMarshaller) DataFormat(org.infinispan.client.hotrod.DataFormat)

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