Class LoomSet<E>
- Type Parameters:
E- the type of elements in the set
Provides transparent access to a distributed set (unordered, unique elements) stored in the LoomCache cluster. All operations are routed through the connected LoomClient and may raise LoomException if the cluster becomes unavailable or a timeout occurs.
Thread Safety
This class is thread-safe. Multiple threads may call methods concurrently; the underlying LoomClient handles synchronization and connection pooling.Async API
This class provides both synchronous and asynchronous methods. Async methods return CompletableFuture for non-blocking operations.Usage Example
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("loom-set");
LoomClient client = LoomClient.builder()
.addSeed("127.0.0.1:5701")
.build();
client.connect();
LoomSet<String> tags = client.getSet("tags");
tags.add("java");
tags.add("distributed");
tags.add("java"); // no-op; "java" already exists
boolean hasJava = tags.contains("java"); // true
int size = tags.size(); // 2
tags.remove("java");
// Async variant
tags.addAsync("kotlin")
.thenCompose(v -> tags.containsAsync("kotlin"))
.thenAccept(found -> log.info("Found: {}", found))
.join();
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionbooleanAdds an element to the set.Asynchronously adds an element to the set.voidclear()Removes all elements from the set, leaving it empty.Asynchronously removes all elements from the set, leaving it empty.booleanChecks whether an element exists in the set.containsAsync(@Nullable E element) Asynchronously checks whether an element exists in the set.booleanRemoves an element from the set.removeAsync(@Nullable E element) Asynchronously removes an element from the set.scan(long cursor) Scans the set using cursor-based iteration (Redis-like SCAN).Scans the set using cursor-based iteration with a glob pattern filter.Scans the set using cursor-based iteration with full control.scanner()Returns a cursor-based iterator for scanning set elements.Returns a cursor-based iterator for scanning set elements with a glob pattern filter.Returns a cursor-based iterator for scanning set elements with full control.intsize()Returns the number of elements in the set.Asynchronously returns the number of elements in the set.
-
Method Details
-
add
Adds an element to the set.If the element is already present, this operation has no effect.
- Parameters:
element- the element to add (serialized viaClientSerializer)- Returns:
- true if the element was newly added; false if it already existed
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
remove
Removes an element from the set.If the element is not present, this operation has no effect.
- Parameters:
element- the element to remove- Returns:
- true if the element was present and removed; false if it was not found
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
contains
Checks whether an element exists in the set.- Parameters:
element- the element to check for- Returns:
- true if the element is present; false otherwise
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
size
public int size()Returns the number of elements in the set.- Returns:
- the number of unique elements
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
clear
public void clear()Removes all elements from the set, leaving it empty.- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
scan
Scans the set using cursor-based iteration (Redis-like SCAN). Starts from the beginning with default count of 10.- Parameters:
cursor- the cursor position (0 to start from beginning)- Returns:
- a ScanResult containing the next cursor and matched elements
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
scan
Scans the set using cursor-based iteration with a glob pattern filter. Default count of 10 elements per iteration.- Parameters:
cursor- the cursor position (0 to start from beginning)pattern- optional glob pattern (* = any chars, ? = single char), null = match all- Returns:
- a ScanResult containing the next cursor and matched elements
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
scan
Scans the set using cursor-based iteration with full control.- Parameters:
cursor- the cursor position (0 to start from beginning)pattern- optional glob pattern (* = any chars, ? = single char), null = match allcount- maximum number of elements to return per iteration- Returns:
- a ScanResult containing the next cursor and matched elements
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
scanner
Returns a cursor-based iterator for scanning set elements.This method provides a clean, idiomatic Java iterator API that handles cursor management and pagination transparently. Useful for iterating over large sets without loading all elements into memory.
Usage Example:
org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger("loom-set-scan"); // Simple iteration for (String tag : tags.scanner()) { log.info("Tag: {}", tag); } // With Stream API tags.scanner("java*", 50) .stream() .filter(t -> t.length() > 3) .forEach(tag -> log.info("Tag: {}", tag));- Returns:
- an Iterable for iterating over all set elements with default page size (10)
- Since:
- 1.3
-
scanner
Returns a cursor-based iterator for scanning set elements with a glob pattern filter.- Parameters:
pattern- optional glob pattern (* = any chars, ? = single char), null = match all- Returns:
- an Iterable for iterating over matching elements with default page size (10)
- Since:
- 1.3
-
scanner
Returns a cursor-based iterator for scanning set elements with full control.This is the most flexible scanner method, allowing control over both pattern matching and page size for optimal performance.
- Parameters:
pattern- optional glob pattern (* = any chars, ? = single char), null = match allpageSize- number of elements to fetch per scan operation (1-10000 recommended)- Returns:
- an Iterable for iterating over matching elements with the specified page size
- Throws:
IllegalArgumentException- if pageSize invalid input: '<'= 0- Since:
- 1.3
-
addAsync
Asynchronously adds an element to the set.If the element is already present, this operation has no effect.
- Parameters:
element- the element to add (serialized viaClientSerializer)- Returns:
- a CompletableFuture with true if the element was newly added; false if it already existed
-
removeAsync
Asynchronously removes an element from the set.If the element is not present, this operation has no effect.
- Parameters:
element- the element to remove- Returns:
- a CompletableFuture with true if the element was present and removed; false if it was not found
-
containsAsync
Asynchronously checks whether an element exists in the set.- Parameters:
element- the element to check for- Returns:
- a CompletableFuture with true if the element is present; false otherwise
-
sizeAsync
Asynchronously returns the number of elements in the set.- Returns:
- a CompletableFuture with the number of unique elements
-
clearAsync
Asynchronously removes all elements from the set, leaving it empty.- Returns:
- a CompletableFuture that completes when the clear operation is done
-