Skip to content

Commit

Permalink
Merge doc tests into main branch to keep in-sync with the code (#3861)
Browse files Browse the repository at this point in the history
* Fix search_quickstart example

* Add data_class step to SearchQuickstartExample

* Update examples with new bikes and align to new tutorial

* Fixes doctests for search tutorial

* Small fixes doctests

* Updates search tutorial doc examples to new format and data

* Fix formatting in doc tests

* Add Hashes and String Example (#3477)

* Add Hashes Example

* Fixes examples for hash tutorial

* Refactor Hash Tutorial to Java 8 (from 17)

* Change indentation to 4 spaces

* Add Strings Example

* Reformat samples to 2-space indentation

---------

Co-authored-by: Elena Kolevska <[email protected]>

* Add Geospatial data type example (#3487)

* Add Geospatial data type example

* Add missing REMOVE_END

* Added example Java code snippets for Streams (#3641)

* Java example code snippets for T-Digest and TopK (#3660)

* Created example for t digest

* Added example for topK

---------

Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: Ranjeet Singh <[email protected]>

* Create Java code snippets for sets #3626 (#3661)

* Create Java code snippets for sets #3626

* Apply suggestions from code review to add // STEP_END lines.

Co-authored-by: David Dougherty <[email protected]>

* format imports

---------

Co-authored-by: David Dougherty <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>

* Create Java code snippets for count min sketch (#3644)

* added code snippet for CMS in Java

* Update CMSExample.java

* format spaces

* use cmsIncrBy simple variant

* more format spaces

---------

Co-authored-by: M Sazzadul Hoque <[email protected]>

* Added Java code snippets for Bloom #3630  (#3671)

* Added java example for bloom filter

* Removed space

* fix and format imports

* fix compile error

* format spaces

* Add del command and rename obj

* Add del command and fix format

* change to junit assert

* Update BloomFilterExample.java

* Add correct output comment

---------

Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>
Co-authored-by: Ranjeet Singh <[email protected]>

* Added java code snippet for hyperloglog (#3674)

* Added Java code snippet for HyperLogLog

* fix format and imports

* fix spaces

* change to junit assert

* Added correct output comment

* Update HyperLogLogExample.java

---------

Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>

* Added Java code snippets for cuckoo filters (#3679)

* Add CuckooFilterExample

* Add step start comment

* Update CuckooFilterExample.java

---------

Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>

* Create Java code snippet for lists #3625 (#3677)

* Added example Java code snippets for the List#3625

* format spaces

---------

Co-authored-by: M Sazzadul Hoque <[email protected]>

* Create Java code snippet for sorted sets  (#3680)

* added an example for SortedSets in java

* Update SortedSetsExample.java

* made changes as per the review.

* Format spaces in SortedSetsExample.java

---------

Co-authored-by: M Sazzadul Hoque <[email protected]>

* Added Java code snippet for bitmap  (#3687)

* Add bitmap example

* Change class name

* Add assert for res3 and res4

* Update BitMapsExample.java

* Format spaces in BitMapsExample.java

---------

Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>

---------

Co-authored-by: Elena Kolevska <[email protected]>
Co-authored-by: Brian Sam-Bodden <[email protected]>
Co-authored-by: M Sazzadul Hoque <[email protected]>
Co-authored-by: Harshvardhan Parmar <[email protected]>
Co-authored-by: Ranjeet SIngh <[email protected]>
Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: Ranjeet Singh <[email protected]>
Co-authored-by: Suraj <[email protected]>
Co-authored-by: David Dougherty <[email protected]>
Co-authored-by: Ranjeet Singh <[email protected]>
  • Loading branch information
11 people authored Jun 18, 2024
1 parent db826a6 commit f4fcc36
Show file tree
Hide file tree
Showing 16 changed files with 1,785 additions and 219 deletions.
47 changes: 47 additions & 0 deletions src/test/java/io/redis/examples/BitMapsExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// EXAMPLE: bitmap_tutorial
// HIDE_START
package io.redis.examples;

import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;

public class BitMapsExample {

@Test
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
// HIDE_END

// REMOVE_START
jedis.del("pings:2024-01-01-00:00");
// REMOVE_END


// STEP_START ping
boolean res1 = jedis.setbit("pings:2024-01-01-00:00", 123, true);
System.out.println(res1); // >>> false

boolean res2 = jedis.getbit("pings:2024-01-01-00:00", 123);
System.out.println(res2); // >>> true

boolean res3 = jedis.getbit("pings:2024-01-01-00:00", 456);
System.out.println(res3); // >>> false
// STEP_END

// REMOVE_START
Assert.assertFalse(res1);
Assert.assertTrue(res2);
Assert.assertFalse(res3);
// REMOVE_END

// STEP_START bitcount
long res4 = jedis.bitcount("pings:2024-01-01-00:00");
System.out.println(res4); // >>> 1
// STEP_END

// REMOVE_START
Assert.assertEquals(res4, 1);
// REMOVE_END
}
}
49 changes: 49 additions & 0 deletions src/test/java/io/redis/examples/BloomFilterExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// EXAMPLE: bf_tutorial

// HIDE_START
package io.redis.examples;

import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;
import org.junit.Assert;
import java.util.List;

public class BloomFilterExample {

@Test
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
// HIDE_END

// REMOVE_START
jedis.del("bikes:models");
// REMOVE_END

// STEP_START bloom
String res1 = jedis.bfReserve("bikes:models", 0.01, 1000);
System.out.println(res1); // >>> OK

// REMOVE_START
Assert.assertEquals("OK", res1);
// REMOVE_END

boolean res2 = jedis.bfAdd("bikes:models", "Smoky Mountain Striker");
System.out.println(res2); // >>> True

boolean res3 = jedis.bfExists("bikes:models", "Smoky Mountain Striker");
System.out.println(res3); // >>> True

List<Boolean> res4 = jedis.bfMAdd("bikes:models",
"Rocky Mountain Racer",
"Cloudy City Cruiser",
"Windy City Wippet");
System.out.println(res4); // >>> [True, True, True]

List<Boolean> res5 = jedis.bfMExists("bikes:models",
"Rocky Mountain Racer",
"Cloudy City Cruiser",
"Windy City Wippet");
System.out.println(res5); // >>> [True, True, True]
// STEP_END
}
}
49 changes: 49 additions & 0 deletions src/test/java/io/redis/examples/CMSExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//EXAMPLE: cms_tutorial
//HIDE_START
package io.redis.examples;
//HIDE_END

//REMOVE_START
import redis.clients.jedis.UnifiedJedis;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//REMOVE_END

public class CMSExample {

@Test
public void run() {

//HIDE_START
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
//HIDE_END

//REMOVE_START
jedis.del("bikes:profit");
//REMOVE_END

//STEP_START cms
String res1 = jedis.cmsInitByProb("bikes:profit", 0.001d, 0.002d);
System.out.println(res1); // >>> OK

long res2 = jedis.cmsIncrBy("bikes:profit", "Smoky Mountain Striker", 100L);
System.out.println(res2); // >>> 100

List<Long> res3 = jedis.cmsIncrBy("bikes:profit", new HashMap<String, Long>() {{
put("Rocky Mountain Racer", 200L);
put("Cloudy City Cruiser", 150L);
}});
System.out.println(res3); // >>> [200, 150]

List<Long> res4 = jedis.cmsQuery("bikes:profit", "Smoky Mountain Striker");
System.out.println(res4); // >>> [100]

Map<String, Object> res5 = jedis.cmsInfo("bikes:profit");
System.out.println(res5.get("width") + " " + res5.get("depth") + " " + res5.get("count")); // >>> 2000 9 450
//STEP_END
}

}
45 changes: 45 additions & 0 deletions src/test/java/io/redis/examples/CuckooFilterExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// EXAMPLE: cuckoo_tutorial

// HIDE_START
package io.redis.examples;

import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;

public class CuckooFilterExample {
@Test
public void run() {
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
// HIDE_END

// REMOVE_START
jedis.del("bikes:models");
// REMOVE_END

// STEP_START cuckoo
String res1 = jedis.cfReserve("bikes:models", 1000000);
System.out.println(res1); // >>> OK

// REMOVE_START
Assert.assertEquals(res1, "OK");
// REMOVE_END

boolean res2 = jedis.cfAdd("bikes:models", "Smoky Mountain Striker");
System.out.println(res2); // >>> True

boolean res3 = jedis.cfExists("bikes:models", "Smoky Mountain Striker");
System.out.println(res3); // >>> True

boolean res4 = jedis.cfExists("bikes:models", "Terrible Bike Name");
System.out.println(res4); // >>> False

boolean res5 = jedis.cfDel("bikes:models", "Smoky Mountain Striker");
System.out.println(res5); // >>> True

// REMOVE_START
Assert.assertTrue(res5);
// REMOVE_END
// STEP_END
}
}
60 changes: 60 additions & 0 deletions src/test/java/io/redis/examples/GeoExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//EXAMPLE: geo_tutorial
package io.redis.examples;

// REMOVE_START
import org.junit.Test;
// REMOVE_END
import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.UnifiedJedis;
import redis.clients.jedis.args.GeoUnit;
import redis.clients.jedis.resps.GeoRadiusResponse;

import java.util.List;
import java.util.stream.Collectors;

import static org.junit.Assert.assertEquals;

public class GeoExample {
@Test
public void run() {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
// REMOVE_START
jedis.del("bikes:rentable");
// REMOVE_END

// STEP_START geoadd
long res1 = jedis.geoadd("bikes:rentable", -122.27652, 37.805186, "station:1");
System.out.println(res1); // 1

long res2 = jedis.geoadd("bikes:rentable", -122.2674626, 37.8062344, "station:2");
System.out.println(res2); // 1

long res3 = jedis.geoadd("bikes:rentable", -122.2469854, 37.8104049, "station:3");
System.out.println(res2); // 1
// STEP_END

// REMOVE_START
assertEquals(1, res1);
assertEquals(1, res1);
assertEquals(1, res1);
// REMOVE_END

// STEP_START geosearch
List<GeoRadiusResponse> res4 = jedis.geosearch(
"bikes:rentable",
new GeoCoordinate(-122.27652, 37.805186),
5,
GeoUnit.KM
);
List<String> members = res4.stream() //
.map(GeoRadiusResponse::getMemberByString) //
.collect(Collectors.toList());
System.out.println(members); // [station:1, station:2, station:3]
// STEP_END

// REMOVE_START
assertEquals("[station:1, station:2, station:3]", members.toString());
// REMOVE_END
}
}
}
101 changes: 101 additions & 0 deletions src/test/java/io/redis/examples/HashExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//EXAMPLE: hash_tutorial
package io.redis.examples;

import redis.clients.jedis.UnifiedJedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//REMOVE_START
import org.junit.Test;
import static org.junit.Assert.assertEquals;
//REMOVE_END

public class HashExample {
@Test
public void run() {
try (UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379")) {
// REMOVE_START
jedis.del("bike:1", "bike:1:stats");
// REMOVE_END

// STEP_START set_get_all
Map<String, String> bike1 = new HashMap<>();
bike1.put("model", "Deimos");
bike1.put("brand", "Ergonom");
bike1.put("type", "Enduro bikes");
bike1.put("price", "4972");

Long res1 = jedis.hset("bike:1", bike1);
System.out.println(res1); // 4

String res2 = jedis.hget("bike:1", "model");
System.out.println(res2); // Deimos

String res3 = jedis.hget("bike:1", "price");
System.out.println(res3); // 4972

Map<String, String> res4 = jedis.hgetAll("bike:1");
System.out.println(res4); // {type=Enduro bikes, brand=Ergonom, price=4972, model=Deimos}
// STEP_END

// REMOVE_START
assertEquals(4, res1.longValue());
assertEquals("Deimos", res2);
assertEquals("4972", res3);
assertEquals("Deimos", res4.get("model"));
assertEquals("Ergonom", res4.get("brand"));
assertEquals("Enduro bikes", res4.get("type"));
assertEquals("4972", res4.get("price"));
// REMOVE_END

// STEP_START hmget
List<String> res5 = jedis.hmget("bike:1", "model", "price");
System.out.println(res5); // [Deimos, 4972]
// STEP_END

// REMOVE_START
assert res5.toString().equals("[Deimos, 4972]");
// REMOVE_END

// STEP_START hincrby
Long res6 = jedis.hincrBy("bike:1", "price", 100);
System.out.println(res6); // 5072
Long res7 = jedis.hincrBy("bike:1", "price", -100);
System.out.println(res7); // 4972
// STEP_END

// REMOVE_START
assertEquals(5072, res6.longValue());
assertEquals(4972, res7.longValue());
// REMOVE_END

// STEP_START incrby_get_mget
Long res8 = jedis.hincrBy("bike:1:stats", "rides", 1);
System.out.println(res8); // 1
Long res9 = jedis.hincrBy("bike:1:stats", "rides", 1);
System.out.println(res9); // 2
Long res10 = jedis.hincrBy("bike:1:stats", "rides", 1);
System.out.println(res10); // 3
Long res11 = jedis.hincrBy("bike:1:stats", "crashes", 1);
System.out.println(res11); // 1
Long res12 = jedis.hincrBy("bike:1:stats", "owners", 1);
System.out.println(res12); // 1
String res13 = jedis.hget("bike:1:stats", "rides");
System.out.println(res13); // 3
List<String> res14 = jedis.hmget("bike:1:stats", "crashes", "owners");
System.out.println(res14); // [1, 1]
// STEP_END

// REMOVE_START
assertEquals(1, res8.longValue());
assertEquals(2, res9.longValue());
assertEquals(3, res10.longValue());
assertEquals(1, res11.longValue());
assertEquals(1, res12.longValue());
assertEquals("3", res13);
assertEquals("[1, 1]", res14.toString());
// REMOVE_END
}
}
}
41 changes: 41 additions & 0 deletions src/test/java/io/redis/examples/HyperLogLogExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// EXAMPLE: hll_tutorial
package io.redis.examples;

import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.UnifiedJedis;

public class HyperLogLogExample {

@Test
public void run() {
// HIDE_START
UnifiedJedis jedis = new UnifiedJedis("redis://localhost:6379");
// HIDE_END

// REMOVE_START
jedis.del("bikes", "commuter_bikes", "all_bikes");
// REMOVE_END

// STEP_START pfadd
long res1 = jedis.pfadd("bikes", "Hyperion", "Deimos", "Phoebe", "Quaoar");
System.out.println(res1); // >>> 1

long res2 = jedis.pfcount("bikes");
System.out.println(res2); // >>> 4

long res3 = jedis.pfadd("commuter_bikes", "Salacia", "Mimas", "Quaoar");
System.out.println(res3); // >>> 1

String res4 = jedis.pfmerge("all_bikes", "bikes", "commuter_bikes");
System.out.println(res4); // >>> OK

// REMOVE_START
Assert.assertEquals("OK", res4);
// REMOVE_END

long res5 = jedis.pfcount("all_bikes");
System.out.println(res5); // >>> 6
// STEP_END
}
}
Loading

0 comments on commit f4fcc36

Please sign in to comment.