infinity-sdk 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
infinity/__init__.py ADDED
@@ -0,0 +1,29 @@
1
+ # Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import importlib.metadata
16
+
17
+ __version__ = importlib.metadata.version("infinity_sdk")
18
+
19
+ from infinity.common import URI, NetworkAddress, LOCAL_HOST
20
+ from infinity.infinity import InfinityConnection
21
+ from infinity.remote_thrift.infinity import RemoteThriftInfinityConnection
22
+
23
+ def connect(
24
+ uri: URI = LOCAL_HOST
25
+ ) -> InfinityConnection:
26
+ if isinstance(uri, NetworkAddress) and (uri.port == 9090 or uri.port == 23817 or uri.port == 9070):
27
+ return RemoteThriftInfinityConnection(uri)
28
+ else:
29
+ raise Exception(f"unknown uri: {uri}")
infinity/common.py ADDED
@@ -0,0 +1,40 @@
1
+ # Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from pathlib import Path
15
+ from typing import Union
16
+
17
+ import numpy as np
18
+
19
+
20
+ class NetworkAddress:
21
+ def __init__(self, ip, port):
22
+ self.ip = ip
23
+ self.port = port
24
+
25
+ def __str__(self):
26
+ return f'IP: {self.ip}, Port: {self.port}'
27
+
28
+
29
+ URI = Union[NetworkAddress, Path]
30
+ VEC = Union[list, np.ndarray]
31
+ INSERT_DATA = dict[str, Union[str, int, float, list[Union[int, float]]]]
32
+
33
+ REMOTE_HOST = NetworkAddress("127.0.0.1", 23817)
34
+ LOCAL_HOST = NetworkAddress("0.0.0.0", 23817)
35
+
36
+
37
+ class ConflictType(object):
38
+ Ignore = 0
39
+ Error = 1
40
+ Replace = 2
@@ -0,0 +1,57 @@
1
+ from threading import Lock, Condition
2
+ import infinity
3
+ from infinity.common import NetworkAddress
4
+ import logging
5
+ from infinity.remote_thrift.infinity import RemoteThriftInfinityConnection
6
+
7
+
8
+ class ConnectionPool(object):
9
+
10
+
11
+ def __init__(self, uri = NetworkAddress("127.0.0.1", 23817), min_size=4, max_size=16, timeout=10.0):
12
+ assert (min_size <= max_size)
13
+ self.uri_ = uri
14
+ self.min_size_ = min_size
15
+ self.max_size_ = max_size
16
+ self.curr_size_ = 0
17
+ self.timeout_ = timeout
18
+ self.free_pool_ = []
19
+ self.created_conns_ = []
20
+ self.mutex_ = Lock()
21
+ self.cond_ = Condition(self.mutex_)
22
+
23
+ while(self.curr_size_ < self.min_size_):
24
+ self._create_conn()
25
+
26
+
27
+ def _create_conn(self):
28
+ infinity_coon = infinity.connect(self.uri_)
29
+ self.curr_size_ += 1
30
+ self.free_pool_.append(infinity_coon)
31
+ self.created_conns_.append(infinity_coon)
32
+
33
+
34
+ def get_conn(self) -> RemoteThriftInfinityConnection:
35
+ with self.mutex_:
36
+ while(len(self.free_pool_) == 0 and self.curr_size_ == self.max_size_):
37
+ is_waken = self.cond_.wait(self.timeout_)
38
+ if (not is_waken):
39
+ logging.warn("connection waiting...")
40
+ if (len(self.free_pool_) == 0):
41
+ self._create_conn()
42
+ conn = self.free_pool_.pop()
43
+ return conn
44
+
45
+
46
+ def release_conn(self, conn):
47
+ with self.mutex_:
48
+ if(self.free_pool_.count(conn)):
49
+ raise Exception("the connection has been released")
50
+ self.curr_size_ -= 1
51
+ self.free_pool_.append(conn)
52
+ self.cond_.notify(n=1)
53
+
54
+
55
+ def destroy(self):
56
+ for conn in iter(self.created_conns_):
57
+ conn.disconnect()
infinity/db.py ADDED
@@ -0,0 +1,45 @@
1
+ # Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from abc import ABC, abstractmethod
16
+
17
+ class Database(ABC):
18
+
19
+ @abstractmethod
20
+ def create_table(self, table_name, schema, options):
21
+ pass # implement create table logic here
22
+
23
+ @abstractmethod
24
+ def drop_table(self, table_name):
25
+ pass # implement drop table logic here
26
+
27
+ @abstractmethod
28
+ def list_tables(self):
29
+ pass # implement list tables logic here
30
+
31
+ @abstractmethod
32
+ def show_table(self, table_name):
33
+ pass # implement describe table logic here
34
+
35
+ @abstractmethod
36
+ def show_columns(self, table_name):
37
+ pass # implement describe table logic here
38
+
39
+ @abstractmethod
40
+ def get_table(self, table_name):
41
+ pass # implement get table logic here
42
+
43
+ @abstractmethod
44
+ def show_tables(self):
45
+ pass
infinity/errors.py ADDED
@@ -0,0 +1,137 @@
1
+ # Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from enum import IntEnum
16
+
17
+
18
+ class ErrorCode(IntEnum):
19
+ OK = 0,
20
+
21
+ INVALID_TIME_INFO = 1001,
22
+ EMPTY_CONFIG_PARAMETER = 1002,
23
+ MISMATCH_VERSION = 1003,
24
+ INVALID_TIMEZONE = 1004,
25
+ INVALID_BYTE_SIZE = 1005,
26
+ INVALID_IP_ADDR = 1006,
27
+ INVALID_LOG_LEVEL = 1007,
28
+
29
+ WRONG_PASSWD = 2001,
30
+ INSUFFICIENT_PRIVILEGE = 2002,
31
+
32
+ INVALID_USERNAME = 3001,
33
+ INVALID_PASSWD = 3002,
34
+ INVALID_DB_NAME = 3003,
35
+ INVALID_TABLE_NAME = 3004,
36
+ INVALID_COLUMN_NAME = 3005,
37
+ INVALID_INDEX_NAME = 3006,
38
+ INVALID_COLUMN_DEFINITION = 3007,
39
+ INVALID_TABLE_DEFINITION = 3008,
40
+ INVALID_INDEX_DEFINITION = 3009,
41
+ DATA_TYPE_MISMATCH = 3010,
42
+ NAME_TOO_LONG = 3011,
43
+ RESERVED_NAME = 3012,
44
+ SYNTAX_ERROR = 3013,
45
+ INVALID_PARAMETER_VALUE = 3014,
46
+ DUPLICATE_USER = 3015,
47
+ DUPLICATE_DATABASE_NAME = 3016,
48
+ DUPLICATE_TABLE_NAME = 3017,
49
+ DUPLICATE_INDEX_NAME = 3018,
50
+ DUPLICATE_INDEX_ON_COLUMN = 3019,
51
+ NO_SUCH_USER = 3020,
52
+ DB_NOT_EXIST = 3021,
53
+ TABLE_NOT_EXIST = 3022,
54
+ INDEX_NOT_EXIST = 3023,
55
+ COLUMN_NOT_EXIST = 3024,
56
+ AGG_NOT_ALLOW_IN_WHERE_CLAUSE = 3025,
57
+ COLUMN_IN_SELECT_NOT_IN_GROUP_BY = 3026,
58
+ NO_SUCH_SYSTEM_VAR = 3027,
59
+ INVALID_SYSTEM_VAR_VALUE = 3028,
60
+ SYSTEM_VAR_READ_ONLY = 3029,
61
+ FUNCTION_NOT_FOUND = 3030,
62
+ SPECIAL_FUNCTION_NOT_FOUND = 3031,
63
+ NOT_SUPPORTED = 3032,
64
+ DROPPING_USING_DB = 3033,
65
+ SESSION_NOT_FOUND = 3034,
66
+ RECURSIVE_AGG = 3035,
67
+ FUNCTION_ARGS_ERROR = 3036,
68
+ IMPORT_FILE_FORMAT_ERROR = 3037,
69
+ DATA_NOT_EXIST = 3038,
70
+ COLUMN_COUNT_MISMATCH = 3039,
71
+ EMPTY_DB_NAME = 3040,
72
+ EMPTY_TABLE_NAME = 3041,
73
+ EMPTY_COLUMN_NAME = 3042,
74
+ EMPTY_INDEX_NAME = 3043,
75
+ EXCEED_DB_NAME_LENGTH = 3044,
76
+ EXCEED_TABLE_NAME_LENGTH = 3045,
77
+ EXCEED_COLUMN_NAME_LENGTH = 3046,
78
+ EXCEED_INDEX_NAME_LENGTH = 3047,
79
+ NO_COLUMN_DEFINED = 3048,
80
+ NOT_SUPPORTED_TYPE_CONVERSION = 3049,
81
+ EMPTY_SELECT_FIELDS = 3050,
82
+ INVALID_DATA_TYPE = 3051,
83
+ PARSE_MATCH_EXPR_FAILED = 3052,
84
+ FTS_INDEX_NOT_EXIST = 3053,
85
+ UNKNOWN_FTS_FAULT = 3054,
86
+ INVALID_CONSTRAINT_TYPE = 3055,
87
+ INVALID_KNN_DISTANCE_TYPE = 3056,
88
+ INVALID_EMBEDDING_DATA_TYPE = 3057,
89
+ INVALID_CONSTANT_TYPE = 3058,
90
+ INVALID_PARSED_EXPR_TYPE = 3059,
91
+ INVALID_INDEX_TYPE = 3060,
92
+ INVALID_INDEX_PARAM = 3061,
93
+ LACK_INDEX_PARAM = 3062,
94
+ INVALID_FILTER_EXPRESSION = 3063,
95
+ MUTIPLE_FUNCTION_MATCHED = 3064,
96
+ INSERT_WITHOUT_VALUES = 3065,
97
+ INVALID_CONFLICT_TYPE = 3066,
98
+ INVALID_JSON_FORMAT = 3067,
99
+ DUPLICATE_COLUMN_NAME = 3068,
100
+ INVALID_EXPRESSION = 3069,
101
+ SEGMENT_NOT_EXIST = 3070,
102
+ AGGREGATE_FUNCTION_WITH_EMPTY_ARGS = 3071,
103
+ BLOCK_NOT_EXIST = 3072,
104
+ INVALID_TOPK_TYPE = 3073,
105
+ INVALID_CREATE_OPTION = 3074,
106
+ INVALID_DROP_OPTION = 3075,
107
+
108
+ TXN_ROLLBACK = 4001,
109
+ TXN_CONFLICT = 4002,
110
+
111
+ DISK_FULL = 5001,
112
+ OUT_OF_MEMORY = 5002,
113
+ TOO_MANY_CONNECTIONS = 5003,
114
+ CONFIGURATION_LIMIT_EXCEED = 5004,
115
+ QUERY_IS_TOO_COMPLEX = 5005,
116
+
117
+ QUERY_CANCELLED = 6001,
118
+ QUERY_NOT_SUPPORTED = 6002,
119
+ CLIENT_CLOSE = 6003,
120
+
121
+ DISK_IO_ERROR = 7001,
122
+ DUPLICATED_FILE = 7002,
123
+
124
+
125
+ CONFIG_FILE_ERROR = 7003,
126
+ LOCK_FILE_EXISTS = 7004,
127
+ CATALOG_CORRUPTED = 7005,
128
+ DATA_CORRUPTED = 7006,
129
+ INDEX_CORRUPTED = 7007,
130
+ FILE_NOT_FOUND = 7008,
131
+ DIR_NOT_FOUND = 7009,
132
+ DATA_IO_ERROR = 7010,
133
+ UNEXPECTED_ERROR = 7011,
134
+
135
+ INVALID_ENTRY = 8001,
136
+ NOT_FOUND_ENTRY = 8002,
137
+ EMPTY_ENTRY_LIST = 8003,
infinity/index.py ADDED
@@ -0,0 +1,77 @@
1
+ # Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from enum import Enum
16
+
17
+ import infinity.remote_thrift.infinity_thrift_rpc.ttypes as ttypes
18
+
19
+
20
+ class IndexType(Enum):
21
+ IVFFlat = 1
22
+ HnswLVQ = 2
23
+ Hnsw = 3
24
+ FullText = 4
25
+
26
+ def to_ttype(self):
27
+ if self == IndexType.IVFFlat:
28
+ return ttypes.IndexType.IVFFlat
29
+ elif self == IndexType.HnswLVQ:
30
+ return ttypes.IndexType.HnswLVQ
31
+ elif self == IndexType.Hnsw:
32
+ return ttypes.IndexType.Hnsw
33
+ elif self == IndexType.FullText:
34
+ return ttypes.IndexType.FullText
35
+ else:
36
+ raise Exception("Unknown index type")
37
+
38
+
39
+ class InitParameter:
40
+ def __init__(self, param_name: str, param_value: str):
41
+ self.param_name = param_name
42
+ self.param_value = param_value
43
+
44
+ def __str__(self):
45
+ return f"InitParameter({self.param_name}, {self.param_value})"
46
+
47
+ def __repr__(self):
48
+ return self.__str__()
49
+
50
+ def to_ttype(self):
51
+ return ttypes.InitParameter(self.param_name, self.param_value)
52
+
53
+
54
+ class IndexInfo:
55
+ def __init__(self, column_name: str, index_type: IndexType, params: list[InitParameter]):
56
+ self.column_name = column_name
57
+ self.index_type = index_type
58
+ self.params = params
59
+
60
+ def __str__(self):
61
+ return f"IndexInfo({self.column_name}, {self.index_type}, {self.params})"
62
+
63
+ def __repr__(self):
64
+ return self.__str__()
65
+
66
+ def __eq__(self, other):
67
+ return self.column_name == other.index_name and self.index_type == other.index_type and self.params == other.params
68
+
69
+ def __hash__(self):
70
+ return hash((self.column_name, self.index_type, self.params))
71
+
72
+ def to_ttype(self):
73
+ return ttypes.IndexInfo(
74
+ self.column_name,
75
+ self.index_type.to_ttype(),
76
+ [p.to_ttype() for p in self.params]
77
+ )
infinity/infinity.py ADDED
@@ -0,0 +1,68 @@
1
+ # Copyright(C) 2023 InfiniFlow, Inc. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ from abc import ABC, abstractmethod
15
+ from enum import Enum
16
+
17
+ from infinity import URI
18
+
19
+
20
+ class ShowVariable(Enum):
21
+ QUERY_COUNT = "query_count"
22
+ SESSION_COUNT = "session_count"
23
+ BUFFER_POOL_USAGE = "buffer_pool_usage"
24
+ VERSION = "version"
25
+ QUERY_MEMORY_LIMIT = "query_memory_limit"
26
+ QUERY_CPU_LIMIT = "query_cpu_limit"
27
+ LOG_LEVEL = "log_level"
28
+ SCHEDULE_POLICY = "schedule_policy"
29
+ LISTEN_ADDRESS = "listen_address"
30
+ SQL_PORT = "sql_port"
31
+ SDK_PORT = "sdk_port"
32
+ HTTP_API_PORT = "http_api_port"
33
+ DATA_URL = "data_url"
34
+ TIME_ZONE = "time_zone"
35
+
36
+
37
+ # abstract class
38
+ class InfinityConnection(ABC):
39
+ def __init__(self, uri: URI):
40
+ self.uri = uri
41
+
42
+ @abstractmethod
43
+ def create_database(self, db_name, options=None):
44
+ pass
45
+
46
+ @abstractmethod
47
+ def list_databases(self):
48
+ pass
49
+
50
+ @abstractmethod
51
+ def show_database(self, db_name):
52
+ pass
53
+
54
+ @abstractmethod
55
+ def drop_database(self, db_name, options=None):
56
+ pass
57
+
58
+ @abstractmethod
59
+ def get_database(self, db_name):
60
+ pass
61
+
62
+ @abstractmethod
63
+ def disconnect(self):
64
+ pass
65
+
66
+ @abstractmethod
67
+ def show_variable(self, variable: ShowVariable):
68
+ pass
File without changes