CollectionWriter is the main class abstraction for writing, editing, and deleting new elements to the underlying text collection index - calling functions should use CollectionWriter, which will route and manage the connection to the underlying DB resource
| 187 | |
| 188 | |
| 189 | class CollectionWriter: |
| 190 | |
| 191 | """CollectionWriter is the main class abstraction for writing, editing, and deleting new elements to the |
| 192 | underlying text collection index - calling functions should use CollectionWriter, which will route and manage |
| 193 | the connection to the underlying DB resource""" |
| 194 | |
| 195 | def __init__(self, library_name, account_name="llmware", db_name=None, custom_table=False, custom_schema=None): |
| 196 | |
| 197 | self.library_name = library_name |
| 198 | self.account_name = account_name |
| 199 | |
| 200 | self.supported_collection_db = LLMWareConfig().get_supported_collection_db() |
| 201 | |
| 202 | if db_name: |
| 203 | self.active_db = db_name |
| 204 | else: |
| 205 | self.active_db = LLMWareConfig().get_active_db() |
| 206 | |
| 207 | self._writer = None |
| 208 | |
| 209 | if self.active_db in self.supported_collection_db: |
| 210 | |
| 211 | if self.active_db == "mongo": |
| 212 | self._writer = MongoWriter(self.library_name, account_name=self.account_name, custom_table=custom_table, |
| 213 | custom_schema=custom_schema) |
| 214 | |
| 215 | if self.active_db == "postgres": |
| 216 | self._writer = PGWriter(self.library_name, account_name=self.account_name, custom_table=custom_table, |
| 217 | custom_schema=custom_schema) |
| 218 | |
| 219 | if self.active_db == "sqlite": |
| 220 | self._writer = SQLiteWriter(self.library_name, account_name=self.account_name, custom_table=custom_table, |
| 221 | custom_schema=custom_schema) |
| 222 | |
| 223 | else: |
| 224 | raise LLMWareException(message=f"CollectionWriter - collection database " |
| 225 | f"is not supported - {self.active_db}") |
| 226 | |
| 227 | def build_text_index(self): |
| 228 | """Builds text index using db-specific methods""" |
| 229 | self._writer.build_text_index() |
| 230 | return 1 |
| 231 | |
| 232 | def check_if_table_build_required(self): |
| 233 | """Checks if table build required- returns True if table build required, e.g., no table found |
| 234 | and building table schema is required by the DB resource""" |
| 235 | |
| 236 | build_table = self._writer.check_if_table_build_required() |
| 237 | |
| 238 | return build_table |
| 239 | |
| 240 | def create_table(self, table_name, schema): |
| 241 | """Creates table""" |
| 242 | return self._writer.create_table(table_name, schema) |
| 243 | |
| 244 | def write_new_record(self, new_record): |
| 245 | """Inserts new record to the DB resource - unpacks and validates the new_record dict, if required """ |
| 246 | return self._writer.write_new_record(new_record) |
no outgoing calls
no test coverage detected