-
Notifications
You must be signed in to change notification settings - Fork 53
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: Prateek Kumar <prateek.kumar@improving.com>
- Loading branch information
1 parent
915d0cb
commit cfa2ce3
Showing
4 changed files
with
103 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,21 @@ | ||
from typing import List, Optional, cast | ||
from typing import List, cast | ||
from glide.glide_client import TGlideClient | ||
from glide.constants import TEncodable | ||
from glide.constants import TOK, TEncodable | ||
from glide.protobuf.command_request_pb2 import RequestType | ||
|
||
class CreateOptions: | ||
def __init__( | ||
self, | ||
optionName: str, | ||
optionValue: Optional[str] = None | ||
): | ||
self.optionName = optionName | ||
self.optionValue = optionValue | ||
|
||
def getCreateOptions(self) -> str: | ||
args = [] | ||
if self.optionName: | ||
args.extend(self.optionName) | ||
if self.optionValue: | ||
args.extend(self.optionValue) | ||
|
||
from glide.async_commands.server_modules.vss_options import FtCreateOptions | ||
|
||
async def create( | ||
client: TGlideClient, | ||
indexName: TEncodable, | ||
options: List[CreateOptions] | ||
options: FtCreateOptions | ||
): | ||
args: List[TEncodable] = [indexName] | ||
for createOption in options: | ||
args.extend(createOption.getCreateOptions()) | ||
return cast(Optional[bytes], await client._execute_command(RequestType.FtCreate, args)) | ||
|
||
args.extend(options.getCreateOptions()) | ||
return cast(TOK, await client._execute_command(RequestType.FtCreate, args)) | ||
|
||
async def info( | ||
client: TGlideClient, | ||
indexName: TEncodable, | ||
): | ||
args: List[TEncodable] = [indexName] | ||
return cast(Optional[bytes], await client._execute_command(RequestType.FtInfo, args)) | ||
return cast(List[TEncodable], await client._execute_command(RequestType.FtInfo, args)) |
69 changes: 69 additions & 0 deletions
69
python/python/glide/async_commands/server_modules/vss_options.py
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,69 @@ | ||
from typing import Optional, List | ||
from enum import Enum | ||
|
||
class FieldType(Enum): | ||
TEXT = 1 | ||
TAG = 2 | ||
NUMERIC = 3 | ||
GEO = 4 | ||
VECTOR = 5 | ||
GEOSHAPE = 6 | ||
|
||
class DataType(Enum): | ||
HASH = 1 | ||
JSON = 2 | ||
|
||
class FieldInfo: | ||
def __init__( | ||
self, | ||
identifier: str, | ||
alias: Optional[str], | ||
type: FieldType, | ||
isSortable: Optional[bool] = None, | ||
isUnnormalized: Optional[bool] = None, | ||
isNoIndex: Optional[bool] = None | ||
): | ||
self.identifier = identifier | ||
self.alias = alias | ||
self.type = type | ||
self.isSortable = isSortable | ||
self.isUnnormalized = isUnnormalized | ||
self.isNoIndex = isNoIndex | ||
|
||
def getFieldInfo(self) -> List[str]: | ||
args = [] | ||
if self.identifier: | ||
args.append(self.identifier) | ||
if self.alias: | ||
args.append("AS") | ||
args.append(self.alias) | ||
if self.type: | ||
args.append(self.type.name) | ||
if self.isSortable: | ||
args.append("SORTABLE") | ||
if self.isUnnormalized: | ||
args.append("UNF") | ||
if self.isNoIndex: | ||
args.append("NOINDEX") | ||
return args | ||
|
||
|
||
class FtCreateOptions: | ||
def __init__( | ||
self, | ||
dataType: Optional[DataType], | ||
fields: List[FieldInfo], | ||
): | ||
self.dataType = dataType | ||
self.fields = fields | ||
|
||
def getCreateOptions(self) -> List[str]: | ||
args = [] | ||
if self.dataType: | ||
args.append("ON") | ||
args.append(self.dataType.name) | ||
if self.fields: | ||
args.append("SCHEMA") | ||
for fieldInfo in self.fields: | ||
args = args + fieldInfo.getFieldInfo() | ||
return args |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import pytest | ||
from typing import List | ||
from glide.async_commands.server_modules import vss | ||
from glide.config import ProtocolVersion | ||
from glide.glide_client import TGlideClient | ||
from glide.async_commands.server_modules.vss_options import FtCreateOptions, DataType, FieldInfo, FieldType | ||
from glide.constants import OK | ||
|
||
@pytest.mark.asyncio | ||
class TestVss: | ||
@pytest.mark.parametrize("cluster_mode", [True, False]) | ||
@pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) | ||
async def test_vss_create(self, glide_client: TGlideClient): | ||
index = "idx" | ||
fields: List[FieldInfo] = [] | ||
fieldInfo1: FieldInfo = FieldInfo("title", None, FieldType.TEXT, True) | ||
fieldInfo2: FieldInfo = FieldInfo("published_at", None, FieldType.NUMERIC, True) | ||
fieldInfo3: FieldInfo = FieldInfo("category", None, FieldType.TAG, True) | ||
fields.append(fieldInfo1) | ||
fields.append(fieldInfo2) | ||
fields.append(fieldInfo3) | ||
options: FtCreateOptions = FtCreateOptions(DataType.HASH, fields) | ||
result = await vss.create(glide_client, index, options) | ||
assert result == OK | ||
|
||
# print info command result | ||
print(await vss.info(glide_client, index)) |