Java Code Examples for org.redisson.api.RSet#add()

The following examples show how to use org.redisson.api.RSet#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortOrderByPatternGetAlpha() {
    RSet<String> set = redisson.getSet("list", StringCodec.INSTANCE);
    set.add("1");
    set.add("2");
    set.add("3");

    redisson.getBucket("test1", IntegerCodec.INSTANCE).set(12);
    redisson.getBucket("test2", IntegerCodec.INSTANCE).set(3);
    redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);

    redisson.getBucket("tester1", StringCodec.INSTANCE).set("obj1");
    redisson.getBucket("tester2", StringCodec.INSTANCE).set("obj2");
    redisson.getBucket("tester3", StringCodec.INSTANCE).set("obj3");

    Collection<String> descSort = set
            .readSortAlpha("test*", Arrays.asList("tester*"), SortOrder.DESC);
    assertThat(descSort).containsExactly("obj2", "obj1", "obj3");

    Collection<String> ascSort = set
            .readSortAlpha("test*", Arrays.asList("tester*"), SortOrder.ASC);
    assertThat(ascSort).containsExactly("obj3", "obj1", "obj2");
}
 
Example 2
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortOrderByPatternAlpha(){
    RSet<Integer> set = redisson.getSet("list", IntegerCodec.INSTANCE);
    set.add(1);
    set.add(2);
    set.add(3);

    redisson.getBucket("test1", IntegerCodec.INSTANCE).set(12);
    redisson.getBucket("test2", IntegerCodec.INSTANCE).set(3);
    redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);

    Collection<Integer> descSort = set
            .readSortAlpha("test*", SortOrder.DESC);
    assertThat(descSort).containsExactly(2, 1, 3);

    Collection<Integer> ascSort = set
            .readSortAlpha("test*", SortOrder.ASC);
    assertThat(ascSort).containsExactly(3, 1, 2);
}
 
Example 3
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadDiff() {
    RSet<Integer> set = redisson.getSet("set");
    set.add(5);
    set.add(7);
    set.add(6);
    RSet<Integer> set1 = redisson.getSet("set1");
    set1.add(1);
    set1.add(2);
    set1.add(5);
    RSet<Integer> set2 = redisson.getSet("set2");
    set2.add(3);
    set2.add(4);
    set2.add(5);

    assertThat(set.readDiff("set1", "set2")).containsOnly(7, 6);
    assertThat(set).containsOnly(6, 5, 7);
}
 
Example 4
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortTo() {
    RSet<String> list = redisson.getSet("list", IntegerCodec.INSTANCE);
    list.add("1");
    list.add("2");
    list.add("3");

    assertThat(list.sortTo("test3", SortOrder.DESC)).isEqualTo(3);
    RList<String> list2 = redisson.getList("test3", StringCodec.INSTANCE);
    assertThat(list2).containsExactly("3", "2", "1");
    
    assertThat(list.sortTo("test4", SortOrder.ASC)).isEqualTo(3);
    RList<String> list3 = redisson.getList("test4", StringCodec.INSTANCE);
    assertThat(list3).containsExactly("1", "2", "3");

}
 
Example 5
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testIntersection() {
    RSet<Integer> set = redisson.getSet("set");
    set.add(5);
    set.add(6);
    RSet<Integer> set1 = redisson.getSet("set1");
    set1.add(1);
    set1.add(2);
    set1.add(3);
    RSet<Integer> set2 = redisson.getSet("set2");
    set2.add(3);
    set2.add(4);
    set2.add(5);

    assertThat(set.intersection("set1", "set2")).isEqualTo(1);
    assertThat(set).containsOnly(3);
}
 
Example 6
Source File: RedissonTransactionalSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAdd() {
    RSet<String> s = redisson.getSet("test");
    s.add("1");
    s.add("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RSet<String> set = transaction.getSet("test");
    assertThat(set.add("4")).isTrue();
    assertThat(set.add("3")).isFalse();
    assertThat(set.contains("4")).isTrue();
    
    assertThat(s.contains("4")).isFalse();
    
    transaction.commit();
    
    assertThat(s.size()).isEqualTo(3);
    assertThat(s.contains("1")).isTrue();
    assertThat(s.contains("3")).isTrue();
    assertThat(s.contains("4")).isTrue();
}
 
Example 7
Source File: RedissonTransactionalSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemove() {
    RSet<String> s = redisson.getSet("test");
    s.add("1");
    s.add("3");
    
    RTransaction transaction = redisson.createTransaction(TransactionOptions.defaults());
    RSet<String> set = transaction.getSet("test");
    assertThat(set.contains("1")).isTrue();
    assertThat(set.remove("3")).isTrue();
    assertThat(set.remove("3")).isFalse();
    assertThat(set.remove("3")).isFalse();
    
    assertThat(s.contains("3")).isTrue();
    
    transaction.commit();
    
    assertThat(s.size()).isEqualTo(1);
    assertThat(s.contains("1")).isTrue();
    assertThat(s.contains("3")).isFalse();
}
 
Example 8
Source File: RedissonSession.java    From redisson with Apache License 2.0 6 votes vote down vote up
public void delete() {
    if (map == null) {
        map = redissonManager.getMap(id);
    }
    
    if (broadcastSessionEvents) {
        RSet<String> set = redissonManager.getNotifiedNodes(id);
        set.add(redissonManager.getNodeId());
        set.expire(60, TimeUnit.SECONDS);
        map.fastPut(IS_EXPIRATION_LOCKED, true);
        map.expire(60, TimeUnit.SECONDS);
    } else {
        map.delete();
    }
    if (readMode == ReadMode.MEMORY) {
        topic.publish(new AttributesClearMessage(redissonManager.getNodeId(), getId()));
    }
    map = null;
}
 
Example 9
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortOrderByPattern() {
    RSet<Integer> list = redisson.getSet("list", IntegerCodec.INSTANCE);
    list.add(1);
    list.add(2);
    list.add(3);
    
    redisson.getBucket("test1", IntegerCodec.INSTANCE).set(3);
    redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
    redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);
    
    Set<Integer> descSort = list.readSort("test*", SortOrder.DESC);
    assertThat(descSort).containsExactly(1, 2, 3);

    Set<Integer> ascSort = list.readSort("test*", SortOrder.ASC);
    assertThat(ascSort).containsExactly(3, 2, 1);
}
 
Example 10
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortOrderByPatternLimit() {
    RSet<Integer> list = redisson.getSet("list", IntegerCodec.INSTANCE);
    list.add(1);
    list.add(2);
    list.add(3);
    
    redisson.getBucket("test1", IntegerCodec.INSTANCE).set(3);
    redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
    redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);
    
    Set<Integer> descSort = list.readSort("test*", SortOrder.DESC, 1, 2);
    assertThat(descSort).containsExactly(2, 3);

    Set<Integer> ascSort = list.readSort("test*", SortOrder.ASC, 1, 2);
    assertThat(ascSort).containsExactly(2, 1);
}
 
Example 11
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortOrderByPatternGet() {
    RSet<String> list = redisson.getSet("list", StringCodec.INSTANCE);
    list.add("1");
    list.add("2");
    list.add("3");
    
    redisson.getBucket("test1", IntegerCodec.INSTANCE).set(1);
    redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
    redisson.getBucket("test3", IntegerCodec.INSTANCE).set(3);
    
    redisson.getBucket("tester1", StringCodec.INSTANCE).set("obj1");
    redisson.getBucket("tester2", StringCodec.INSTANCE).set("obj2");
    redisson.getBucket("tester3", StringCodec.INSTANCE).set("obj3");
    
    Collection<String> descSort = list.readSort("test*", Arrays.asList("tester*"), SortOrder.DESC);
    assertThat(descSort).containsExactly("obj3", "obj2", "obj1");

    Collection<String> ascSort = list.readSort("test*", Arrays.asList("tester*"), SortOrder.ASC);
    assertThat(ascSort).containsExactly("obj1", "obj2", "obj3");
}
 
Example 12
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSortToByPattern() {
    RSet<Integer> list = redisson.getSet("list", IntegerCodec.INSTANCE);
    list.add(1);
    list.add(2);
    list.add(3);
    
    redisson.getBucket("test1", IntegerCodec.INSTANCE).set(3);
    redisson.getBucket("test2", IntegerCodec.INSTANCE).set(2);
    redisson.getBucket("test3", IntegerCodec.INSTANCE).set(1);
    
    assertThat(list.sortTo("tester3", "test*", SortOrder.DESC, 1, 2)).isEqualTo(2);
    RList<String> list2 = redisson.getList("tester3", StringCodec.INSTANCE);
    assertThat(list2).containsExactly("2", "3");
    
    assertThat(list.sortTo("tester4", "test*", SortOrder.ASC, 1, 2)).isEqualTo(2);
    RList<String> list3 = redisson.getList("tester4", StringCodec.INSTANCE);
    assertThat(list3).containsExactly("2", "1");
}
 
Example 13
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortOrderLimit() {
    RSet<Integer> list = redisson.getSet("list", IntegerCodec.INSTANCE);
    list.add(1);
    list.add(2);
    list.add(3);
    
    Set<Integer> descSort = list.readSort(SortOrder.DESC, 1, 2);
    assertThat(descSort).containsExactly(2, 1);

    Set<Integer> ascSort = list.readSort(SortOrder.ASC, 1, 2);
    assertThat(ascSort).containsExactly(2, 3);
}
 
Example 14
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRemoveRandom() {
    RSet<Integer> set = redisson.getSet("simple");
    set.add(1);
    set.add(2);
    set.add(3);

    assertThat(set.removeRandom()).isIn(1, 2, 3);
    assertThat(set.removeRandom()).isIn(1, 2, 3);
    assertThat(set.removeRandom()).isIn(1, 2, 3);
    assertThat(set.removeRandom()).isNull();
}
 
Example 15
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddBean() throws InterruptedException, ExecutionException {
    SimpleBean sb = new SimpleBean();
    sb.setLng(1L);
    RSet<SimpleBean> set = redisson.getSet("simple");
    set.add(sb);
    Assert.assertEquals(sb.getLng(), set.iterator().next().getLng());
}
 
Example 16
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testSortOrderAlpha(){
    RSet<String> set = redisson.getSet("list", StringCodec.INSTANCE);
    set.add("1");
    set.add("3");
    set.add("12");

    assertThat(set.readSortAlpha(SortOrder.ASC))
            .containsExactly("1", "12", "3");
    assertThat(set.readSortAlpha(SortOrder.DESC))
            .containsExactly("3", "12", "1");
}
 
Example 17
Source File: RedissonInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testSet() {
    try (Scope scope = tracer.startRootTransaction(getClass().getClassLoader()).withName("transaction").activateInScope()) {
        RSet<String> rSet = redisson.getSet("set1");
        rSet.add("s1");

        assertThat(rSet.contains("s1")).isTrue();
    }

    assertTransactionWithRedisSpans("SADD", "SISMEMBER");
}
 
Example 18
Source File: RedissonSetTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testRandomLimited() {
    RSet<Integer> set = redisson.getSet("simple");
    for (int i = 0; i < 10; i++) {
        set.add(i);
    }
    
    assertThat(set.random(3)).containsAnyElementsOf(set.readAll()).hasSize(3);
}
 
Example 19
Source File: TracingRedissonTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void test_set() {
  RSet<Object> set = client.getSet("set");

  set.add("key");
  assertTrue(set.contains("key"));

  List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
  checkSpans(spans);
  assertNull(tracer.activeSpan());
}
 
Example 20
Source File: SetExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RSet<String> set = redisson.getSet("mySet");
    set.add("1");
    set.add("2");
    set.add("3");
    
    set.contains("1");
    
    for (String string : set) {
        // iteration through bulk loaded values
    }
    
    boolean removedValue = set.remove("1");
    set.removeAll(Arrays.asList("1", "2", "3"));
    set.containsAll(Arrays.asList("4", "1", "0"));
    
    String randomRemovedValue = set.removeRandom();
    String randomValue = set.random();

    RSet<String> secondsSet = redisson.getSet("mySecondsSet");
    secondsSet.add("4");
    secondsSet.add("5");

    // union with "mySecondsSet" and write it
    set.union(secondsSet.getName());
    // union with "mySecondsSet" without change of set
    set.readUnion(secondsSet.getName());
    
    // diff with "mySecondsSet" and write it
    set.diff(secondsSet.getName());
    // diff with "mySecondsSet" without change of set
    set.readDiff(secondsSet.getName());
    
    // intersect with "mySecondsSet" and write it
    set.intersection(secondsSet.getName());
    // intersect with "mySecondsSet" without change of set
    set.readIntersection(secondsSet.getName());
    
    Set<String> allValues = set.readAll();
    
    redisson.shutdown();
}