Skip to content

Commit

Permalink
FT.CREATE
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Oct 9, 2024
1 parent 4f6c4c5 commit c9d9fa1
Show file tree
Hide file tree
Showing 8 changed files with 644 additions and 11 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ jobs:
name: lint java rust

test-modules:
if: github.event.pull_request.head.repo.owner.login == 'valkey-io'
environment: AWS_ACTIONS
name: Running Module Tests
if: github.repository_owner == 'valkey-io'
name: Modules Tests
runs-on: [self-hosted, linux, ARM64]
timeout-minutes: 15
steps:
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#### Changes

* Java: Added `FT.CREATE` ([#2414](https://github.com/valkey-io/valkey-glide/pull/2414))

#### Breaking Changes

#### Fixes
Expand Down
4 changes: 2 additions & 2 deletions java/client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ jar.dependsOn('copyNativeLib')
javadoc.dependsOn('copyNativeLib')
copyNativeLib.dependsOn('buildRustRelease')
compileTestJava.dependsOn('copyNativeLib')
test.dependsOn('buildRust')
testFfi.dependsOn('buildRust')
test.dependsOn('buildRustRelease')
testFfi.dependsOn('buildRustRelease')

test {
exclude "glide/ffi/FfiTest.class"
Expand Down
79 changes: 79 additions & 0 deletions java/client/src/main/java/glide/api/commands/servermodules/FT.java
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`");
}
}
Loading

0 comments on commit c9d9fa1

Please sign in to comment.