|
| 1 | +# Copyright 2024 Google LLC |
| 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 | +# http://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 __future__ import annotations |
| 16 | + |
| 17 | +from typing import List, Optional |
| 18 | + |
| 19 | +from llama_index.core.data_structs.data_structs import IndexStruct |
| 20 | +from llama_index.core.storage.index_store.types import BaseIndexStore |
| 21 | + |
| 22 | +from .async_index_store import AsyncPostgresIndexStore |
| 23 | +from .engine import PostgresEngine |
| 24 | + |
| 25 | + |
| 26 | +class PostgresIndexStore(BaseIndexStore): |
| 27 | + """Index Store Table stored in an Cloud SQL for PostgreSQL database.""" |
| 28 | + |
| 29 | + __create_key = object() |
| 30 | + |
| 31 | + def __init__( |
| 32 | + self, key: object, engine: PostgresEngine, index_store: AsyncPostgresIndexStore |
| 33 | + ): |
| 34 | + """PostgresIndexStore constructor. |
| 35 | +
|
| 36 | + Args: |
| 37 | + key (object): Key to prevent direct constructor usage. |
| 38 | + engine (PostgresEngine): Database connection pool. |
| 39 | + index_store (AsyncPostgresIndexStore): The async only IndexStore implementation |
| 40 | +
|
| 41 | + Raises: |
| 42 | + Exception: If constructor is directly called by the user. |
| 43 | + """ |
| 44 | + if key != PostgresIndexStore.__create_key: |
| 45 | + raise Exception( |
| 46 | + "Only create class through 'create' or 'create_sync' methods!" |
| 47 | + ) |
| 48 | + self._engine = engine |
| 49 | + self.__index_store = index_store |
| 50 | + |
| 51 | + @classmethod |
| 52 | + async def create( |
| 53 | + cls, |
| 54 | + engine: PostgresEngine, |
| 55 | + table_name: str, |
| 56 | + schema_name: str = "public", |
| 57 | + ) -> PostgresIndexStore: |
| 58 | + """Create a new PostgresIndexStore instance. |
| 59 | +
|
| 60 | + Args: |
| 61 | + engine (PostgresEngine): Postgres engine to use. |
| 62 | + table_name (str): Table name that stores the index metadata. |
| 63 | + schema_name (str): The schema name where the table is located. Defaults to "public" |
| 64 | +
|
| 65 | + Raises: |
| 66 | + ValueError: If the table provided does not contain required schema. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + PostgresIndexStore: A newly created instance of PostgresIndexStore. |
| 70 | + """ |
| 71 | + coro = AsyncPostgresIndexStore.create(engine, table_name, schema_name) |
| 72 | + index_store = await engine._run_as_async(coro) |
| 73 | + return cls(cls.__create_key, engine, index_store) |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def create_sync( |
| 77 | + cls, |
| 78 | + engine: PostgresEngine, |
| 79 | + table_name: str, |
| 80 | + schema_name: str = "public", |
| 81 | + ) -> PostgresIndexStore: |
| 82 | + """Create a new PostgresIndexStore sync instance. |
| 83 | +
|
| 84 | + Args: |
| 85 | + engine (PostgresEngine): Postgres engine to use. |
| 86 | + table_name (str): Table name that stores the index metadata. |
| 87 | + schema_name (str): The schema name where the table is located. Defaults to "public" |
| 88 | +
|
| 89 | + Raises: |
| 90 | + ValueError: If the table provided does not contain required schema. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + PostgresIndexStore: A newly created instance of PostgresIndexStore. |
| 94 | + """ |
| 95 | + coro = AsyncPostgresIndexStore.create(engine, table_name, schema_name) |
| 96 | + index_store = engine._run_as_sync(coro) |
| 97 | + return cls(cls.__create_key, engine, index_store) |
| 98 | + |
| 99 | + async def aindex_structs(self) -> List[IndexStruct]: |
| 100 | + """Get all index structs. |
| 101 | +
|
| 102 | + Returns: |
| 103 | + List[IndexStruct]: index structs |
| 104 | +
|
| 105 | + """ |
| 106 | + return await self._engine._run_as_async(self.__index_store.aindex_structs()) |
| 107 | + |
| 108 | + def index_structs(self) -> List[IndexStruct]: |
| 109 | + """Get all index structs. |
| 110 | +
|
| 111 | + Returns: |
| 112 | + List[IndexStruct]: index structs |
| 113 | +
|
| 114 | + """ |
| 115 | + return self._engine._run_as_sync(self.__index_store.aindex_structs()) |
| 116 | + |
| 117 | + async def aadd_index_struct(self, index_struct: IndexStruct) -> None: |
| 118 | + """Add an index struct. |
| 119 | +
|
| 120 | + Args: |
| 121 | + index_struct (IndexStruct): index struct |
| 122 | +
|
| 123 | + """ |
| 124 | + return await self._engine._run_as_async( |
| 125 | + self.__index_store.aadd_index_struct(index_struct) |
| 126 | + ) |
| 127 | + |
| 128 | + def add_index_struct(self, index_struct: IndexStruct) -> None: |
| 129 | + """Add an index struct. |
| 130 | +
|
| 131 | + Args: |
| 132 | + index_struct (IndexStruct): index struct |
| 133 | +
|
| 134 | + """ |
| 135 | + return self._engine._run_as_sync( |
| 136 | + self.__index_store.aadd_index_struct(index_struct) |
| 137 | + ) |
| 138 | + |
| 139 | + async def adelete_index_struct(self, key: str) -> None: |
| 140 | + """Delete an index struct. |
| 141 | +
|
| 142 | + Args: |
| 143 | + key (str): index struct key |
| 144 | +
|
| 145 | + """ |
| 146 | + return await self._engine._run_as_async( |
| 147 | + self.__index_store.adelete_index_struct(key) |
| 148 | + ) |
| 149 | + |
| 150 | + def delete_index_struct(self, key: str) -> None: |
| 151 | + """Delete an index struct. |
| 152 | +
|
| 153 | + Args: |
| 154 | + key (str): index struct key |
| 155 | +
|
| 156 | + """ |
| 157 | + return self._engine._run_as_sync(self.__index_store.adelete_index_struct(key)) |
| 158 | + |
| 159 | + async def aget_index_struct( |
| 160 | + self, struct_id: Optional[str] = None |
| 161 | + ) -> Optional[IndexStruct]: |
| 162 | + """Get an index struct. |
| 163 | +
|
| 164 | + Args: |
| 165 | + struct_id (Optional[str]): index struct id |
| 166 | +
|
| 167 | + """ |
| 168 | + return await self._engine._run_as_async( |
| 169 | + self.__index_store.aget_index_struct(struct_id) |
| 170 | + ) |
| 171 | + |
| 172 | + def get_index_struct( |
| 173 | + self, struct_id: Optional[str] = None |
| 174 | + ) -> Optional[IndexStruct]: |
| 175 | + """Get an index struct. |
| 176 | +
|
| 177 | + Args: |
| 178 | + struct_id (Optional[str]): index struct id |
| 179 | +
|
| 180 | + """ |
| 181 | + return self._engine._run_as_sync( |
| 182 | + self.__index_store.aget_index_struct(struct_id) |
| 183 | + ) |
0 commit comments