Skip to content

Commit

Permalink
Java: FT.ALIASADD, FT.ALIASDEL, FT.ALIASUPDATE (valkey-io#2442)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
Co-authored-by: Andrew Carbonetto <andrew.carbonetto@improving.com>
  • Loading branch information
2 people authored and avifenesh committed Oct 21, 2024
1 parent 1a4a144 commit 0ab99f0
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Java: Added `FT.SEARCH` ([#2439](https://github.com/valkey-io/valkey-glide/pull/2439))
* Java: Added `FT.AGGREGATE` ([#2466](https://github.com/valkey-io/valkey-glide/pull/2466))
* Java: Added `JSON.SET` and `JSON.GET` ([#2462](https://github.com/valkey-io/valkey-glide/pull/2462))
* Java: Added `FT.ALIASADD`, `FT.ALIASDEL`, `FT.ALIASUPDATE` ([#2442](https://github.com/valkey-io/valkey-glide/pull/2442))
* Core: Update routing for commands from server modules ([#2461](https://github.com/valkey-io/valkey-glide/pull/2461))

#### Breaking Changes
Expand Down
110 changes: 110 additions & 0 deletions java/client/src/main/java/glide/api/commands/servermodules/FT.java
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,116 @@ public static CompletableFuture<Map<String, Object>> info(
return executeCommand(client, new GlideString[] {gs("FT.INFO"), indexName}, true);
}

/**
* Adds an alias for an index. The new alias name can be used anywhere that an index name is
* required.
*
* @param client The client to execute the command.
* @param aliasName The alias to be added to an index.
* @param indexName The index name for which the alias has to be added.
* @return <code>"OK"</code>.
* @example
* <pre>{@code
* FT.aliasadd(client, "myalias", "myindex").get(); // "OK"
* }</pre>
*/
public static CompletableFuture<String> aliasadd(
@NonNull BaseClient client, @NonNull String aliasName, @NonNull String indexName) {
return aliasadd(client, gs(aliasName), gs(indexName));
}

/**
* Adds an alias for an index. The new alias name can be used anywhere that an index name is
* required.
*
* @param client The client to execute the command.
* @param aliasName The alias to be added to an index.
* @param indexName The index name for which the alias has to be added.
* @return <code>"OK"</code>.
* @example
* <pre>{@code
* FT.aliasadd(client, gs("myalias"), gs("myindex")).get(); // "OK"
* }</pre>
*/
public static CompletableFuture<String> aliasadd(
@NonNull BaseClient client, @NonNull GlideString aliasName, @NonNull GlideString indexName) {
var args = new GlideString[] {gs("FT.ALIASADD"), aliasName, indexName};

return executeCommand(client, args, false);
}

/**
* Deletes an existing alias for an index.
*
* @param client The client to execute the command.
* @param aliasName The existing alias to be deleted for an index.
* @return <code>"OK"</code>.
* @example
* <pre>{@code
* FT.aliasdel(client, "myalias").get(); // "OK"
* }</pre>
*/
public static CompletableFuture<String> aliasdel(
@NonNull BaseClient client, @NonNull String aliasName) {
return aliasdel(client, gs(aliasName));
}

/**
* Deletes an existing alias for an index.
*
* @param client The client to execute the command.
* @param aliasName The existing alias to be deleted for an index.
* @return <code>"OK"</code>.
* @example
* <pre>{@code
* FT.aliasdel(client, gs("myalias")).get(); // "OK"
* }</pre>
*/
public static CompletableFuture<String> aliasdel(
@NonNull BaseClient client, @NonNull GlideString aliasName) {
var args = new GlideString[] {gs("FT.ALIASDEL"), aliasName};

return executeCommand(client, args, false);
}

/**
* Updates an existing alias to point to a different physical index. This command only affects
* future references to the alias.
*
* @param client The client to execute the command.
* @param aliasName The alias name. This alias will now be pointed to a different index.
* @param indexName The index name for which an existing alias has to updated.
* @return <code>"OK"</code>.
* @example
* <pre>{@code
* FT.aliasupdate(client, "myalias", "myindex").get(); // "OK"
* }</pre>
*/
public static CompletableFuture<String> aliasupdate(
@NonNull BaseClient client, @NonNull String aliasName, @NonNull String indexName) {
return aliasupdate(client, gs(aliasName), gs(indexName));
}

/**
* Update an existing alias to point to a different physical index. This command only affects
* future references to the alias.
*
* @param client The client to execute the command.
* @param aliasName The alias name. This alias will now be pointed to a different index.
* @param indexName The index name for which an existing alias has to updated.
* @return <code>"OK"</code>.
* @example
* <pre>{@code
* FT.aliasupdate(client,gs("myalias"), gs("myindex")).get(); // "OK"
* }</pre>
*/
public static CompletableFuture<String> aliasupdate(
@NonNull BaseClient client, @NonNull GlideString aliasName, @NonNull GlideString indexName) {
var args = new GlideString[] {gs("FT.ALIASUPDATE"), aliasName, indexName};

return executeCommand(client, args, false);
}

/**
* A wrapper for custom command API.
*
Expand Down
49 changes: 49 additions & 0 deletions java/integTest/src/test/java/glide/modules/VectorSearchTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,4 +776,53 @@ public void ft_info() {
assertInstanceOf(RequestException.class, exception.getCause());
assertTrue(exception.getMessage().contains("Index not found"));
}

@SneakyThrows
@Test
public void ft_aliasadd_aliasdel_aliasupdate() {

var alias1 = "alias1";
var alias2 = "a2";
var indexName = "{" + UUID.randomUUID() + "-index}";

// create some indices
assertEquals(
OK,
FT.create(
client,
indexName,
new FieldInfo[] {
new FieldInfo("vec", VectorFieldFlat.builder(DistanceMetric.L2, 2).build())
})
.get());

assertEquals(OK, FT.aliasadd(client, alias1, indexName).get());

// error with adding the same alias to the same index
var exception =
assertThrows(ExecutionException.class, () -> FT.aliasadd(client, alias1, indexName).get());
assertInstanceOf(RequestException.class, exception.getCause());
assertTrue(exception.getMessage().contains("Alias already exists"));

assertEquals(OK, FT.aliasupdate(client, alias2, indexName).get());
assertEquals(OK, FT.aliasdel(client, alias2).get());

// with GlideString:
assertEquals(OK, FT.aliasupdate(client, gs(alias1), gs(indexName)).get());
assertEquals(OK, FT.aliasdel(client, gs(alias1)).get());
assertEquals(OK, FT.aliasadd(client, gs(alias2), gs(indexName)).get());
assertEquals(OK, FT.aliasdel(client, gs(alias2)).get());

// exception with calling `aliasdel` on an alias that doesn't exist
exception = assertThrows(ExecutionException.class, () -> FT.aliasdel(client, alias2).get());
assertInstanceOf(RequestException.class, exception.getCause());
assertTrue(exception.getMessage().contains("Alias does not exist"));

// exception with calling `aliasadd` with a nonexisting index
exception =
assertThrows(
ExecutionException.class, () -> FT.aliasadd(client, alias1, "nonexistent_index").get());
assertInstanceOf(RequestException.class, exception.getCause());
assertTrue(exception.getMessage().contains("Index does not exist"));
}
}
8 changes: 4 additions & 4 deletions python/python/glide/async_commands/server_modules/ft.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def aliasadd(
client: TGlideClient, alias: TEncodable, indexName: TEncodable
) -> TOK:
"""
Add an alias for an index. The new alias name can be used anywhere that an index name is required.
Adds an alias for an index. The new alias name can be used anywhere that an index name is required.
Args:
client (TGlideClient): The client to execute the command.
Expand All @@ -103,11 +103,11 @@ async def aliasadd(

async def aliasdel(client: TGlideClient, alias: TEncodable) -> TOK:
"""
Delete an existing alias for an index.
Deletes an existing alias for an index.
Args:
client (TGlideClient): The client to execute the command.
alias (TEncodable): The exisiting alias to be deleted for an index.
alias (TEncodable): The existing alias to be deleted for an index.
Returns:
TOK: A simple "OK" response.
Expand All @@ -125,7 +125,7 @@ async def aliasupdate(
client: TGlideClient, alias: TEncodable, indexName: TEncodable
) -> TOK:
"""
Update an existing alias to point to a different physical index. This command only affects future references to the alias.
Updates an existing alias to point to a different physical index. This command only affects future references to the alias.
Args:
client (TGlideClient): The client to execute the command.
Expand Down

0 comments on commit 0ab99f0

Please sign in to comment.