Search in sources :

Example 1 with UserPB

use of org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB in project infinispan by infinispan.

the class ProtobufJsonScriptTest method testDataAsJSONFromScript.

@Test
public void testDataAsJSONFromScript() throws IOException {
    RemoteCacheManager remoteCacheManager = client(0);
    RemoteCache<Integer, User> cache = remoteCacheManager.getCache();
    User user1 = new UserPB();
    user1.setId(1);
    user1.setName("Tom");
    user1.setSurname("Cat");
    user1.setGender(User.Gender.MALE);
    user1.setAge(33);
    user1.setAccountIds(Collections.singleton(12));
    User user2 = new UserPB();
    user2.setId(2);
    user2.setName("Jane");
    user2.setSurname("Doe");
    user2.setGender(User.Gender.FEMALE);
    user2.setAge(39);
    cache.put(1, user1);
    cache.put(2, user2);
    Query<User> q = Search.getQueryFactory(cache).create("FROM sample_bank_account.User WHERE name = 'Jane'");
    User user = q.execute().list().iterator().next();
    assertEquals("Jane", user.getName());
    registerScript(remoteCacheManager, SCRIPT_NAME);
    // The script will clone an existing user, change some fields and insert into a new user
    User result = cache.execute(SCRIPT_NAME, Collections.emptyMap());
    // Read the user as pojo
    assertEquals(result.getId(), 3);
    assertEquals(result.getName(), "Rex");
    assertEquals((int) result.getAge(), 67);
}
Also used : User(org.infinispan.query.dsl.embedded.testdomain.User) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB) Test(org.testng.annotations.Test) MultiHotRodServersTest(org.infinispan.client.hotrod.test.MultiHotRodServersTest)

Example 2 with UserPB

use of org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB in project infinispan by infinispan.

the class RemoteQueryJmxTest method createUser.

private User createUser(int id) {
    User user = new UserPB();
    user.setId(id);
    user.setName("Tom" + id);
    user.setSurname("Cat" + id);
    user.setGender(User.Gender.MALE);
    user.setAccountIds(Collections.singleton(12));
    Address address = new AddressPB();
    address.setStreet("Dark Alley");
    address.setPostCode("123" + id);
    user.setAddresses(Collections.singletonList(address));
    return user;
}
Also used : User(org.infinispan.query.dsl.embedded.testdomain.User) Address(org.infinispan.query.dsl.embedded.testdomain.Address) AddressPB(org.infinispan.client.hotrod.query.testdomain.protobuf.AddressPB) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 3 with UserPB

use of org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB in project infinispan by infinispan.

the class UserMarshaller method readFrom.

@Override
public UserPB readFrom(ProtoStreamReader reader) throws IOException {
    int id = reader.readInt("id");
    Set<Integer> accountIds = reader.readCollection("accountIds", new HashSet<>(), Integer.class);
    // Read them out of order. It still works but logs a warning!
    String surname = reader.readString("surname");
    String name = reader.readString("name");
    String salutation = reader.readString("salutation");
    List<Address> addresses = reader.readCollection("addresses", new ArrayList<>(), AddressPB.class);
    Integer age = reader.readInt("age");
    User.Gender gender = reader.readEnum("gender", User.Gender.class);
    String notes = reader.readString("notes");
    Instant creationDate = reader.readInstant("creationDate");
    Instant passwordExpirationDate = reader.readInstant("passwordExpirationDate");
    UserPB user = new UserPB();
    user.setId(id);
    user.setAccountIds(accountIds);
    user.setName(name);
    user.setSurname(surname);
    user.setSalutation(salutation);
    user.setAge(age);
    user.setGender(gender);
    user.setAddresses(addresses);
    user.setNotes(notes);
    user.setCreationDate(creationDate);
    user.setPasswordExpirationDate(passwordExpirationDate);
    return user;
}
Also used : User(org.infinispan.query.dsl.embedded.testdomain.User) Address(org.infinispan.query.dsl.embedded.testdomain.Address) Instant(java.time.Instant) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 4 with UserPB

use of org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB in project infinispan by infinispan.

the class ContinuousQueryObjectStorageTest method testContinuousQuery.

public void testContinuousQuery() {
    User user1 = new UserPB();
    user1.setId(1);
    user1.setName("John");
    user1.setSurname("Doe");
    user1.setGender(User.Gender.MALE);
    user1.setAge(22);
    user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
    user1.setNotes("Lorem ipsum dolor sit amet");
    User user2 = new UserPB();
    user2.setId(2);
    user2.setName("Spider");
    user2.setSurname("Man");
    user2.setGender(User.Gender.MALE);
    user2.setAge(32);
    user2.setAccountIds(Collections.singleton(3));
    User user3 = new UserPB();
    user3.setId(3);
    user3.setName("Spider");
    user3.setSurname("Woman");
    user3.setGender(User.Gender.FEMALE);
    user3.setAge(40);
    remoteCache.clear();
    remoteCache.put("user" + user1.getId(), user1);
    remoteCache.put("user" + user2.getId(), user2);
    remoteCache.put("user" + user3.getId(), user3);
    assertEquals(3, remoteCache.size());
    QueryFactory qf = Search.getQueryFactory(remoteCache);
    Query<User> query = qf.<User>create("FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
    final BlockingQueue<KeyValuePair<String, User>> joined = new LinkedBlockingQueue<>();
    final BlockingQueue<String> left = new LinkedBlockingQueue<>();
    ContinuousQueryListener<String, User> listener = new ContinuousQueryListener<String, User>() {

        @Override
        public void resultJoining(String key, User value) {
            joined.add(new KeyValuePair<>(key, value));
        }

        @Override
        public void resultLeaving(String key) {
            left.add(key);
        }
    };
    ContinuousQuery<String, User> continuousQuery = Search.getContinuousQuery(remoteCache);
    continuousQuery.addContinuousQueryListener(query, listener);
    expectElementsInQueue(joined, 2, (kv) -> kv.getValue().getAge(), 32, 22);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    user3.setAge(30);
    remoteCache.put("user" + user3.getId(), user3);
    expectElementsInQueue(joined, 1, (kv) -> kv.getValue().getAge(), 30);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    user1.setAge(40);
    user2.setAge(40);
    user3.setAge(40);
    remoteCache.put("user" + user1.getId(), user1);
    remoteCache.put("user" + user2.getId(), user2);
    remoteCache.put("user" + user3.getId(), user3);
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 3);
    expectNoMoreElementsInQueues(joined, left);
    remoteCache.clear();
    user1.setAge(21);
    user2.setAge(22);
    remoteCache.put("expiredUser1", user1, 5, TimeUnit.MILLISECONDS);
    remoteCache.put("expiredUser2", user2, 5, TimeUnit.MILLISECONDS);
    expectElementsInQueue(joined, 2);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    timeService.advance(6);
    assertNull(remoteCache.get("expiredUser1"));
    assertNull(remoteCache.get("expiredUser2"));
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 2);
    expectNoMoreElementsInQueues(joined, left);
    continuousQuery.removeContinuousQueryListener(listener);
    user2.setAge(22);
    remoteCache.put("user" + user2.getId(), user2);
    expectElementsInQueue(joined, 0);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Example 5 with UserPB

use of org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB in project infinispan by infinispan.

the class ContinuousQueryObjectStorageTest method testContinuousQueryChangingParameter.

public void testContinuousQueryChangingParameter() throws InterruptedException {
    User user1 = new UserPB();
    user1.setId(1);
    user1.setName("John");
    user1.setSurname("Doe");
    user1.setGender(User.Gender.MALE);
    user1.setAge(22);
    user1.setAccountIds(new HashSet<>(Arrays.asList(1, 2)));
    user1.setNotes("Lorem ipsum dolor sit amet");
    User user2 = new UserPB();
    user2.setId(2);
    user2.setName("Spider");
    user2.setSurname("Man");
    user2.setGender(User.Gender.MALE);
    user2.setAge(32);
    user2.setAccountIds(Collections.singleton(3));
    User user3 = new UserPB();
    user3.setId(3);
    user3.setName("Spider");
    user3.setSurname("Woman");
    user3.setGender(User.Gender.FEMALE);
    user3.setAge(40);
    remoteCache.clear();
    remoteCache.put("user" + user1.getId(), user1);
    remoteCache.put("user" + user2.getId(), user2);
    remoteCache.put("user" + user3.getId(), user3);
    assertEquals(3, remoteCache.size());
    QueryFactory qf = Search.getQueryFactory(remoteCache);
    Query<Object[]> query = qf.<Object[]>create("SELECT age FROM sample_bank_account.User WHERE age <= :ageParam").setParameter("ageParam", 32);
    final BlockingQueue<KeyValuePair<String, Object[]>> joined = new LinkedBlockingQueue<>();
    final BlockingQueue<String> left = new LinkedBlockingQueue<>();
    ContinuousQueryListener<String, Object[]> listener = new ContinuousQueryListener<String, Object[]>() {

        @Override
        public void resultJoining(String key, Object[] value) {
            joined.add(new KeyValuePair<>(key, value));
        }

        @Override
        public void resultLeaving(String key) {
            left.add(key);
        }
    };
    ContinuousQuery<String, User> cq = Search.getContinuousQuery(remoteCache);
    cq.addContinuousQueryListener(query, listener);
    expectElementsInQueue(joined, 2, (kv) -> kv.getValue()[0], 32, 22);
    expectElementsInQueue(left, 0);
    expectNoMoreElementsInQueues(joined, left);
    joined.clear();
    left.clear();
    cq.removeContinuousQueryListener(listener);
    query.setParameter("ageParam", 40);
    listener = new ContinuousQueryListener<String, Object[]>() {

        @Override
        public void resultJoining(String key, Object[] value) {
            joined.add(new KeyValuePair<>(key, value));
        }

        @Override
        public void resultLeaving(String key) {
            left.add(key);
        }
    };
    cq.addContinuousQueryListener(query, listener);
    expectElementsInQueue(joined, 3);
    expectElementsInQueue(left, 0);
    cq.removeContinuousQueryListener(listener);
}
Also used : QueryFactory(org.infinispan.query.dsl.QueryFactory) User(org.infinispan.query.dsl.embedded.testdomain.User) KeyValuePair(org.infinispan.util.KeyValuePair) ContinuousQueryListener(org.infinispan.query.api.continuous.ContinuousQueryListener) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) UserPB(org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)

Aggregations

UserPB (org.infinispan.client.hotrod.query.testdomain.protobuf.UserPB)23 User (org.infinispan.query.dsl.embedded.testdomain.User)20 QueryFactory (org.infinispan.query.dsl.QueryFactory)10 ContinuousQueryListener (org.infinispan.query.api.continuous.ContinuousQueryListener)7 Address (org.infinispan.query.dsl.embedded.testdomain.Address)7 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)6 AddressPB (org.infinispan.client.hotrod.query.testdomain.protobuf.AddressPB)6 KeyValuePair (org.infinispan.util.KeyValuePair)6 SerializationContext (org.infinispan.protostream.SerializationContext)4 BeforeClass (org.testng.annotations.BeforeClass)2 Test (org.testng.annotations.Test)2 Instant (java.time.Instant)1 Date (java.util.Date)1 RemoteCacheManager (org.infinispan.client.hotrod.RemoteCacheManager)1 AccountPB (org.infinispan.client.hotrod.query.testdomain.protobuf.AccountPB)1 InternalRemoteCacheManager (org.infinispan.client.hotrod.test.InternalRemoteCacheManager)1 MultiHotRodServersTest (org.infinispan.client.hotrod.test.MultiHotRodServersTest)1 SingleHotRodServerTest (org.infinispan.client.hotrod.test.SingleHotRodServerTest)1 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)1