Skip to content

Commit 9404ead

Browse files
committed
Refactoring
1 parent c0d7aef commit 9404ead

13 files changed

+837
-384
lines changed

pinecone/control/db_control.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import logging
2+
from typing import Optional, TYPE_CHECKING
3+
4+
from pinecone.core.openapi.db_control.api.manage_indexes_api import ManageIndexesApi
5+
from pinecone.openapi_support.api_client import ApiClient
6+
7+
from pinecone.utils import setup_openapi_client
8+
from pinecone.core.openapi.db_control import API_VERSION
9+
10+
11+
logger = logging.getLogger(__name__)
12+
""" @private """
13+
14+
if TYPE_CHECKING:
15+
from .resources.index import IndexResource
16+
from .resources.collection import CollectionResource
17+
18+
19+
class DBControl:
20+
def __init__(self, config, openapi_config, pool_threads):
21+
self.config = config
22+
""" @private """
23+
24+
self.index_api = setup_openapi_client(
25+
api_client_klass=ApiClient,
26+
api_klass=ManageIndexesApi,
27+
config=self.config,
28+
openapi_config=self.openapi_config,
29+
pool_threads=pool_threads,
30+
api_version=API_VERSION,
31+
)
32+
""" @private """
33+
34+
self._index_resource: Optional["IndexResource"] = None
35+
""" @private """
36+
37+
self._collection_resource: Optional["CollectionResource"] = None
38+
""" @private """
39+
40+
@property
41+
def index(self) -> "IndexResource":
42+
if self._index_resource is None:
43+
from .resources.index import IndexResource
44+
45+
self._index_resource = IndexResource(index_api=self.index_api, config=self.config)
46+
return self._index_resource
47+
48+
@property
49+
def collection(self) -> "CollectionResource":
50+
if self._collection_resource is None:
51+
from .resources.collection import CollectionResource
52+
53+
self._collection_resource = CollectionResource(self.index_api)
54+
return self._collection_resource
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import logging
2+
from typing import Optional, TYPE_CHECKING
3+
4+
from pinecone.core.openapi.db_control.api.manage_indexes_api import AsyncioManageIndexesApi
5+
from pinecone.openapi_support import AsyncioApiClient
6+
7+
from pinecone.utils import setup_async_openapi_client
8+
from pinecone.core.openapi.db_control import API_VERSION
9+
10+
logger = logging.getLogger(__name__)
11+
""" @private """
12+
13+
14+
if TYPE_CHECKING:
15+
from .resources_asyncio.index import IndexResourceAsyncio
16+
from .resources_asyncio.collection import CollectionResourceAsyncio
17+
18+
19+
class DBControlAsyncio:
20+
def __init__(self, config, openapi_config, pool_threads):
21+
self.config = config
22+
""" @private """
23+
24+
self.index_api = setup_async_openapi_client(
25+
api_client_klass=AsyncioApiClient,
26+
api_klass=AsyncioManageIndexesApi,
27+
config=self.config,
28+
openapi_config=self.openapi_config,
29+
api_version=API_VERSION,
30+
)
31+
""" @private """
32+
33+
self._index_resource: Optional["IndexResourceAsyncio"] = None
34+
""" @private """
35+
36+
self._collection_resource: Optional["CollectionResourceAsyncio"] = None
37+
""" @private """
38+
39+
@property
40+
def index(self) -> "IndexResourceAsyncio":
41+
if self._index_resource is None:
42+
from .resources_asyncio.index import IndexResourceAsyncio
43+
44+
self._index_resource = IndexResourceAsyncio(
45+
index_api=self.index_api, config=self.config
46+
)
47+
return self._index_resource
48+
49+
@property
50+
def collection(self) -> "CollectionResourceAsyncio":
51+
if self._collection_resource is None:
52+
from .resources_asyncio.collection import CollectionResourceAsyncio
53+
54+
self._collection_resource = CollectionResourceAsyncio(self.index_api)
55+
return self._collection_resource

pinecone/control/pinecone_interface.py renamed to pinecone/control/legacy_pinecone_interface.py

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
from abc import ABC, abstractmethod
22

3-
from typing import Optional, Dict, Union
4-
5-
6-
from pinecone.models import (
7-
ServerlessSpec,
8-
PodSpec,
9-
IndexList,
10-
CollectionList,
11-
IndexModel,
12-
IndexEmbed,
13-
)
14-
from pinecone.enums import (
15-
Metric,
16-
VectorType,
17-
DeletionProtection,
18-
PodType,
19-
CloudProvider,
20-
AwsRegion,
21-
GcpRegion,
22-
AzureRegion,
23-
)
24-
from .types import CreateIndexForModelEmbedTypedDict
25-
26-
27-
class PineconeDBControlInterface(ABC):
3+
from typing import Optional, Dict, Union, TYPE_CHECKING
4+
5+
if TYPE_CHECKING:
6+
from pinecone.models import (
7+
ServerlessSpec,
8+
PodSpec,
9+
IndexList,
10+
CollectionList,
11+
IndexModel,
12+
IndexEmbed,
13+
)
14+
from pinecone.enums import (
15+
Metric,
16+
VectorType,
17+
DeletionProtection,
18+
PodType,
19+
CloudProvider,
20+
AwsRegion,
21+
GcpRegion,
22+
AzureRegion,
23+
)
24+
from .types import CreateIndexForModelEmbedTypedDict
25+
26+
27+
class LegacyPineconeDBControlInterface(ABC):
2828
@abstractmethod
2929
def __init__(
3030
self,
@@ -190,14 +190,16 @@ def __init__(
190190
def create_index(
191191
self,
192192
name: str,
193-
spec: Union[Dict, ServerlessSpec, PodSpec],
193+
spec: Union[Dict, "ServerlessSpec", "PodSpec"],
194194
dimension: Optional[int],
195-
metric: Optional[Union[Metric, str]] = Metric.COSINE,
195+
metric: Optional[Union["Metric", str]] = "Metric.COSINE",
196196
timeout: Optional[int] = None,
197-
deletion_protection: Optional[Union[DeletionProtection, str]] = DeletionProtection.DISABLED,
198-
vector_type: Optional[Union[VectorType, str]] = VectorType.DENSE,
197+
deletion_protection: Optional[
198+
Union["DeletionProtection", str]
199+
] = "DeletionProtection.DISABLED",
200+
vector_type: Optional[Union["VectorType", str]] = "VectorType.DENSE",
199201
tags: Optional[Dict[str, str]] = None,
200-
) -> IndexModel:
202+
) -> "IndexModel":
201203
"""Creates a Pinecone index.
202204
203205
:param name: The name of the index to create. Must be unique within your project and
@@ -299,13 +301,15 @@ def create_index(
299301
def create_index_for_model(
300302
self,
301303
name: str,
302-
cloud: Union[CloudProvider, str],
303-
region: Union[AwsRegion, GcpRegion, AzureRegion, str],
304-
embed: Union[IndexEmbed, CreateIndexForModelEmbedTypedDict],
304+
cloud: Union["CloudProvider", str],
305+
region: Union["AwsRegion", "GcpRegion", "AzureRegion", str],
306+
embed: Union["IndexEmbed", "CreateIndexForModelEmbedTypedDict"],
305307
tags: Optional[Dict[str, str]] = None,
306-
deletion_protection: Optional[Union[DeletionProtection, str]] = DeletionProtection.DISABLED,
308+
deletion_protection: Optional[
309+
Union["DeletionProtection", str]
310+
] = "DeletionProtection.DISABLED",
307311
timeout: Optional[int] = None,
308-
) -> IndexModel:
312+
) -> "IndexModel":
309313
"""
310314
:param name: The name of the index to create. Must be unique within your project and
311315
cannot be changed once created. Allowed characters are lowercase letters, numbers,
@@ -414,7 +418,7 @@ def delete_index(self, name: str, timeout: Optional[int] = None):
414418
pass
415419

416420
@abstractmethod
417-
def list_indexes(self) -> IndexList:
421+
def list_indexes(self) -> "IndexList":
418422
"""
419423
:return: Returns an `IndexList` object, which is iterable and contains a
420424
list of `IndexModel` objects. The `IndexList` also has a convenience method `names()`
@@ -447,7 +451,7 @@ def list_indexes(self) -> IndexList:
447451
pass
448452

449453
@abstractmethod
450-
def describe_index(self, name: str) -> IndexModel:
454+
def describe_index(self, name: str) -> "IndexModel":
451455
"""
452456
:param name: the name of the index to describe.
453457
:return: Returns an `IndexModel` object
@@ -534,8 +538,8 @@ def configure_index(
534538
self,
535539
name: str,
536540
replicas: Optional[int] = None,
537-
pod_type: Optional[Union[PodType, str]] = None,
538-
deletion_protection: Optional[Union[DeletionProtection, str]] = None,
541+
pod_type: Optional[Union["PodType", str]] = None,
542+
deletion_protection: Optional[Union["DeletionProtection", str]] = None,
539543
tags: Optional[Dict[str, str]] = None,
540544
):
541545
"""
@@ -622,7 +626,7 @@ def configure_index(
622626
pass
623627

624628
@abstractmethod
625-
def create_collection(self, name: str, source: str):
629+
def create_collection(self, name: str, source: str) -> None:
626630
"""Create a collection from a pod-based index
627631
628632
:param name: Name of the collection
@@ -631,7 +635,7 @@ def create_collection(self, name: str, source: str):
631635
pass
632636

633637
@abstractmethod
634-
def list_collections(self) -> CollectionList:
638+
def list_collections(self) -> "CollectionList":
635639
"""List all collections
636640
637641
```python

0 commit comments

Comments
 (0)