forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `FT.CREATE` Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
- Loading branch information
1 parent
6904c89
commit 46ff569
Showing
5 changed files
with
426 additions
and
1 deletion.
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
131 changes: 131 additions & 0 deletions
131
java/client/src/main/java/glide/api/models/commands/FT/FTSearchOptions.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,131 @@ | ||
/** Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 */ | ||
package glide.api.models.commands.FT; | ||
|
||
import static glide.api.models.GlideString.gs; | ||
|
||
import glide.api.commands.servermodules.FT; | ||
import glide.api.models.GlideString; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.Builder; | ||
import org.apache.commons.lang3.tuple.Pair; | ||
|
||
/** Mandatory parameters for {@link FT#search}. */ | ||
@Builder | ||
public class FTSearchOptions { | ||
|
||
@Builder.Default private final Map<GlideString, GlideString> identifiers = new HashMap<>(); | ||
|
||
/** Query timeout in milliseconds. */ | ||
private final Integer timeout; | ||
|
||
private final Pair<Integer, Integer> limit; | ||
|
||
@Builder.Default private final boolean count = false; | ||
|
||
/** | ||
* Query parameters, which could be referenced in the query by <code>$</code> sign, followed by | ||
* the parameter name. | ||
*/ | ||
@Builder.Default private final Map<GlideString, GlideString> params = new HashMap<>(); | ||
|
||
// TODO maxstale? | ||
// dialect is no-op | ||
|
||
/** Convert to module API. */ | ||
public GlideString[] toArgs() { | ||
var args = new ArrayList<GlideString>(); | ||
if (!identifiers.isEmpty()) { | ||
args.add(gs("RETURN")); | ||
int tokenCount = 0; | ||
for (var pair : identifiers.entrySet()) { | ||
tokenCount++; | ||
args.add(pair.getKey()); | ||
if (pair.getValue() != null) { | ||
tokenCount += 2; | ||
args.add(gs("AS")); | ||
args.add(pair.getValue()); | ||
} | ||
} | ||
args.add(1, gs(Integer.toString(tokenCount))); | ||
} | ||
if (timeout != null) { | ||
args.add(gs("TIMEOUT")); | ||
args.add(gs(timeout.toString())); | ||
} | ||
if (!params.isEmpty()) { | ||
args.add(gs("PARAMS")); | ||
args.add(gs(Integer.toString(params.size() * 2))); | ||
params.forEach( | ||
(name, value) -> { | ||
args.add(name); | ||
args.add(value); | ||
}); | ||
} | ||
if (limit != null) { | ||
args.add(gs("LIMIT")); | ||
args.add(gs(Integer.toString(limit.getLeft()))); | ||
args.add(gs(Integer.toString(limit.getRight()))); | ||
} | ||
if (count) { | ||
args.add(gs("COUNT")); | ||
} | ||
return args.toArray(GlideString[]::new); | ||
} | ||
|
||
public static class FTSearchOptionsBuilder { | ||
|
||
// private - hiding this API from user | ||
void limit(Pair<Integer, Integer> limit) {} | ||
|
||
void count(boolean count) {} | ||
|
||
void identifiers(Map<GlideString, GlideString> identifiers) {} | ||
|
||
/** Add a field to be returned. */ | ||
public FTSearchOptionsBuilder addReturnField(String field) { | ||
this.identifiers$value.put(gs(field), null); | ||
return this; | ||
} | ||
|
||
/** Add a field with an alias to be returned. */ | ||
public FTSearchOptionsBuilder addReturnField(String field, String alias) { | ||
this.identifiers$value.put(gs(field), gs(alias)); | ||
return this; | ||
} | ||
|
||
/** Add a field to be returned. */ | ||
public FTSearchOptionsBuilder addReturnField(GlideString field) { | ||
this.identifiers$value.put(field, null); | ||
return this; | ||
} | ||
|
||
/** Add a field with an alias to be returned. */ | ||
public FTSearchOptionsBuilder addReturnField(GlideString field, GlideString alias) { | ||
this.identifiers$value.put(field, alias); | ||
return this; | ||
} | ||
|
||
/** | ||
* Configure query pagination. By default only first 10 documents are returned. | ||
* | ||
* @param offset Zero-based offset. | ||
* @param count Number of elements to return. | ||
*/ | ||
public FTSearchOptionsBuilder limit(int offset, int count) { | ||
this.limit = Pair.of(offset, count); | ||
return this; | ||
} | ||
|
||
/** | ||
* Once set, the query will return only number of documents in the result set without actually | ||
* returning them. | ||
*/ | ||
public FTSearchOptionsBuilder count() { | ||
this.count$value = true; | ||
this.count$set = true; | ||
return this; | ||
} | ||
} | ||
} |
Oops, something went wrong.