-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yury-Fridlyand <[email protected]>
- Loading branch information
1 parent
4f6c4c5
commit c9d9fa1
Showing
8 changed files
with
644 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
java/client/src/main/java/glide/api/commands/servermodules/FT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.commands.servermodules; | ||
|
||
import glide.api.BaseClient; | ||
import glide.api.GlideClient; | ||
import glide.api.GlideClusterClient; | ||
import glide.api.models.ClusterValue; | ||
import glide.api.models.GlideString; | ||
import glide.api.models.commands.vss.FTCreateOptions; | ||
import glide.api.models.commands.vss.FTCreateOptions.FieldInfo; | ||
import java.util.Arrays; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Stream; | ||
|
||
public class FT { | ||
/** | ||
* Creates an index and initiates a backfill of that index. | ||
* | ||
* @param indexName The index name. | ||
* @param options Additional parameters for the command - see {@link FTCreateOptions}. | ||
* @param fields Fields to populate into the index. | ||
* @return <code>OK</code>. | ||
* @example | ||
* <pre>{@code | ||
* // Create an index for vectors of size 2: | ||
* FT.create(client, "hash_idx1", FTCreateOptions.empty(), new FieldInfo[] { | ||
* new FieldInfo("vec", VectorFieldFlat.builder(DistanceMetric.L2, 2).build()) | ||
* }).get(); | ||
* // Create a 6-dimensional JSON index using the HNSW algorithm: | ||
* FT.create( | ||
* client, | ||
* "json_idx1", | ||
* FTCreateOptions.builder().indexType(JSON).prefixes(new String[] {"json:"}).build(), | ||
* new FieldInfo[] { new FieldInfo( | ||
* "$.vec", | ||
* "VEC", | ||
* VectorFieldHnsw.builder(DistanceMetric.L2, 6).numberOfEdges(32).build()) | ||
* }).get(); | ||
* }</pre> | ||
*/ | ||
public static CompletableFuture<String> create( | ||
BaseClient client, String indexName, FTCreateOptions options, FieldInfo[] fields) { | ||
var args = | ||
Stream.of( | ||
new String[] {"FT.CREATE", indexName}, | ||
options.toArgs(), | ||
new String[] {"SCHEMA"}, | ||
Arrays.stream(fields) | ||
.map(FieldInfo::toArgs) | ||
.flatMap(Arrays::stream) | ||
.toArray(String[]::new)) | ||
.flatMap(Arrays::stream) | ||
.map(GlideString::gs) | ||
.toArray(GlideString[]::new); | ||
return executeCommand(client, args, false); | ||
} | ||
|
||
/** | ||
* A wrapper for custom command API. | ||
* | ||
* @param client | ||
* @param args | ||
* @param returnsMap - true if command returns a map | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
private static <T> CompletableFuture<T> executeCommand( | ||
BaseClient client, GlideString[] args, boolean returnsMap) { | ||
if (client instanceof GlideClient) { | ||
return ((GlideClient) client).customCommand(args).thenApply(r -> (T) r); | ||
} else if (client instanceof GlideClusterClient) { | ||
return ((GlideClusterClient) client) | ||
.customCommand(args) | ||
.thenApply(returnsMap ? ClusterValue::getMultiValue : ClusterValue::getSingleValue) | ||
.thenApply(r -> (T) r); | ||
} | ||
throw new IllegalArgumentException( | ||
"Unknown type of client, should be either `GlideClient` or `GlideClusterClient`"); | ||
} | ||
} |
Oops, something went wrong.