2929)
3030from aperag .tasks .scheduler import TaskScheduler , create_task_scheduler
3131from aperag .utils .utils import utc_now
32+ from aperag .utils .constant import IndexAction
3233
3334logger = logging .getLogger (__name__ )
3435
@@ -71,30 +72,30 @@ def _get_indexes_needing_reconciliation(self, session: Session) -> List[Document
7172 """
7273 from collections import defaultdict
7374
74- operations = defaultdict (lambda : {"create" : [], "update" : [], "delete" : []})
75+ operations = defaultdict (lambda : {IndexAction . CREATE : [], IndexAction . UPDATE : [], IndexAction . DELETE : []})
7576
7677 conditions = {
77- "create" : and_ (
78+ IndexAction . CREATE : and_ (
7879 DocumentIndex .status == DocumentIndexStatus .PENDING ,
7980 DocumentIndex .observed_version < DocumentIndex .version ,
8081 DocumentIndex .version == 1 ,
8182 ),
82- "update" : and_ (
83+ IndexAction . UPDATE : and_ (
8384 DocumentIndex .status == DocumentIndexStatus .PENDING ,
8485 DocumentIndex .observed_version < DocumentIndex .version ,
8586 DocumentIndex .version > 1 ,
8687 ),
87- "delete" : and_ (
88+ IndexAction . DELETE : and_ (
8889 DocumentIndex .status == DocumentIndexStatus .DELETING ,
8990 ),
9091 }
9192
92- for operation_type , condition in conditions .items ():
93+ for action , condition in conditions .items ():
9394 stmt = select (DocumentIndex ).where (condition )
9495 result = session .execute (stmt )
9596 indexes = result .scalars ().all ()
9697 for index in indexes :
97- operations [index .document_id ][operation_type ].append (index )
98+ operations [index .document_id ][action ].append (index )
9899
99100 return operations
100101
@@ -106,9 +107,9 @@ def _reconcile_single_document(self, document_id: str, operations: dict):
106107 # Collect indexes for this document that need claiming
107108 indexes_to_claim = []
108109
109- for operation_type , doc_indexes in operations .items ():
110+ for action , doc_indexes in operations .items ():
110111 for doc_index in doc_indexes :
111- indexes_to_claim .append ((doc_index .id , doc_index .index_type , operation_type ))
112+ indexes_to_claim .append ((doc_index .id , doc_index .index_type , action ))
112113
113114 # Atomically claim the indexes for this document
114115 claimed_indexes = self ._claim_document_indexes (session , document_id , indexes_to_claim )
@@ -129,10 +130,10 @@ def _claim_document_indexes(self, session: Session, document_id: str, indexes_to
129130 claimed_indexes = []
130131
131132 try :
132- for index_id , index_type , operation_type in indexes_to_claim :
133- if operation_type in ["create" , "update" ]:
133+ for index_id , index_type , action in indexes_to_claim :
134+ if action in [IndexAction . CREATE , IndexAction . UPDATE ]:
134135 target_state = DocumentIndexStatus .CREATING
135- elif operation_type == "delete" :
136+ elif action == IndexAction . DELETE :
136137 target_state = DocumentIndexStatus .DELETION_IN_PROGRESS
137138 else :
138139 continue
@@ -146,21 +147,21 @@ def _claim_document_indexes(self, session: Session, document_id: str, indexes_to
146147 continue
147148
148149 # Build appropriate claiming conditions based on operation type
149- if operation_type == "create" :
150+ if action == IndexAction . CREATE :
150151 claiming_conditions = [
151152 DocumentIndex .id == index_id ,
152153 DocumentIndex .status == DocumentIndexStatus .PENDING ,
153154 DocumentIndex .observed_version < DocumentIndex .version ,
154155 DocumentIndex .version == 1 ,
155156 ]
156- elif operation_type == "update" :
157+ elif action == IndexAction . UPDATE :
157158 claiming_conditions = [
158159 DocumentIndex .id == index_id ,
159160 DocumentIndex .status == DocumentIndexStatus .PENDING ,
160161 DocumentIndex .observed_version < DocumentIndex .version ,
161162 DocumentIndex .version > 1 ,
162163 ]
163- elif operation_type == "delete" :
164+ elif action == IndexAction . DELETE :
164165 claiming_conditions = [
165166 DocumentIndex .id == index_id ,
166167 DocumentIndex .status == DocumentIndexStatus .DELETING ,
@@ -180,10 +181,10 @@ def _claim_document_indexes(self, session: Session, document_id: str, indexes_to
180181 'index_id' : index_id ,
181182 'document_id' : document_id ,
182183 'index_type' : index_type ,
183- 'operation_type ' : operation_type ,
184- 'target_version' : current_index .version if operation_type in ["create" , "update" ] else None ,
184+ 'action ' : action ,
185+ 'target_version' : current_index .version if action in [IndexAction . CREATE , IndexAction . UPDATE ] else None ,
185186 })
186- logger .debug (f"Claimed index { index_id } for document { document_id } ({ operation_type } )" )
187+ logger .debug (f"Claimed index { index_id } for document { document_id } ({ action } )" )
187188 else :
188189 logger .debug (f"Could not claim index { index_id } for document { document_id } " )
189190
@@ -202,12 +203,12 @@ def _reconcile_document_operations(self, document_id: str, claimed_indexes: List
202203 # Group by operation type to batch operations
203204 operations_by_type = defaultdict (list )
204205 for claimed_index in claimed_indexes :
205- operation_type = claimed_index ['operation_type ' ]
206- operations_by_type [operation_type ].append (claimed_index )
206+ action = claimed_index ['action ' ]
207+ operations_by_type [action ].append (claimed_index )
207208
208209 # Process create operations as a batch
209- if "create" in operations_by_type :
210- create_indexes = operations_by_type ["create" ]
210+ if IndexAction . CREATE in operations_by_type :
211+ create_indexes = operations_by_type [IndexAction . CREATE ]
211212 create_types = [claimed_index ['index_type' ] for claimed_index in create_indexes ]
212213 context = {}
213214
@@ -227,8 +228,8 @@ def _reconcile_document_operations(self, document_id: str, claimed_indexes: List
227228 logger .info (f"Scheduled create task for document { document_id } , types: { create_types } " )
228229
229230 # Process update operations as a batch
230- if "update" in operations_by_type :
231- update_indexes = operations_by_type ["update" ]
231+ if IndexAction . UPDATE in operations_by_type :
232+ update_indexes = operations_by_type [IndexAction . UPDATE ]
232233 update_types = [claimed_index ['index_type' ] for claimed_index in update_indexes ]
233234 context = {}
234235
@@ -248,8 +249,8 @@ def _reconcile_document_operations(self, document_id: str, claimed_indexes: List
248249 logger .info (f"Scheduled update task for document { document_id } , types: { update_types } " )
249250
250251 # Process delete operations as a batch
251- if "delete" in operations_by_type :
252- delete_indexes = operations_by_type ["delete" ]
252+ if IndexAction . DELETE in operations_by_type :
253+ delete_indexes = operations_by_type [IndexAction . DELETE ]
253254 delete_types = [claimed_index ['index_type' ] for claimed_index in delete_indexes ]
254255
255256 task_id = self .task_scheduler .schedule_delete_index (
0 commit comments