2222import logging
2323import os
2424import os .path as op
25- import re
2625import traceback
2726from abc import ABCMeta , abstractmethod
28- from typing import List
27+ from typing import Any , List , Optional
2928
3029from splunklib import binding
3130
32- from .. import splunk_rest_client as rest_client
33- from ..utils import retry
31+ from solnlib import _utils , utils
3432
3533__all__ = ["CheckpointerException" , "KVStoreCheckpointer" , "FileCheckpointer" ]
3634
@@ -143,11 +141,11 @@ def __init__(
143141 collection_name : str ,
144142 session_key : str ,
145143 app : str ,
146- owner : str = "nobody" ,
147- scheme : str = None ,
148- host : str = None ,
149- port : int = None ,
150- ** context : dict
144+ owner : Optional [ str ] = "nobody" ,
145+ scheme : Optional [ str ] = None ,
146+ host : Optional [ str ] = None ,
147+ port : Optional [ int ] = None ,
148+ ** context : Any
151149 ):
152150 """Initializes KVStoreCheckpointer.
153151
@@ -162,65 +160,39 @@ def __init__(
162160 context: Other configurations for Splunk rest client.
163161
164162 Raises:
165- CheckpointerException: If init kvstore checkpointer failed.
163+ CheckpointerException: If init KV Store checkpointer failed.
166164 """
167165 try :
168- self ._collection_data = self ._get_collection_data (
169- collection_name , session_key , app , owner , scheme , host , port , ** context
166+ if not context .get ("pool_connections" ):
167+ context ["pool_connections" ] = 5
168+ if not context .get ("pool_maxsize" ):
169+ context ["pool_maxsize" ] = 5
170+ self ._collection_data = _utils .get_collection_data (
171+ collection_name ,
172+ session_key ,
173+ app ,
174+ owner ,
175+ scheme ,
176+ host ,
177+ port ,
178+ {"state" : "string" },
179+ ** context ,
170180 )
171181 except KeyError :
172182 raise CheckpointerException ("Get kvstore checkpointer failed." )
173183
174- @retry (exceptions = [binding .HTTPError ])
175- def _get_collection_data (
176- self , collection_name , session_key , app , owner , scheme , host , port , ** context
177- ):
178-
179- if not context .get ("pool_connections" ):
180- context ["pool_connections" ] = 5
181-
182- if not context .get ("pool_maxsize" ):
183- context ["pool_maxsize" ] = 5
184-
185- kvstore = rest_client .SplunkRestClient (
186- session_key ,
187- app ,
188- owner = owner ,
189- scheme = scheme ,
190- host = host ,
191- port = port ,
192- ** context
193- ).kvstore
194-
195- collection_name = re .sub (r"[^\w]+" , "_" , collection_name )
196- try :
197- kvstore .get (name = collection_name )
198- except binding .HTTPError as e :
199- if e .status != 404 :
200- raise
201-
202- fields = {"state" : "string" }
203- kvstore .create (collection_name , fields = fields )
204-
205- collections = kvstore .list (search = collection_name )
206- for collection in collections :
207- if collection .name == collection_name :
208- return collection .data
209- else :
210- raise KeyError ("Get collection data: %s failed." % collection_name )
211-
212- @retry (exceptions = [binding .HTTPError ])
184+ @utils .retry (exceptions = [binding .HTTPError ])
213185 def update (self , key , state ):
214186 record = {"_key" : key , "state" : json .dumps (state )}
215187 self ._collection_data .batch_save (record )
216188
217- @retry (exceptions = [binding .HTTPError ])
189+ @utils . retry (exceptions = [binding .HTTPError ])
218190 def batch_update (self , states ):
219191 for state in states :
220192 state ["state" ] = json .dumps (state ["state" ])
221193 self ._collection_data .batch_save (* states )
222194
223- @retry (exceptions = [binding .HTTPError ])
195+ @utils . retry (exceptions = [binding .HTTPError ])
224196 def get (self , key ):
225197 try :
226198 record = self ._collection_data .query_by_id (key )
@@ -233,7 +205,7 @@ def get(self, key):
233205
234206 return json .loads (record ["state" ])
235207
236- @retry (exceptions = [binding .HTTPError ])
208+ @utils . retry (exceptions = [binding .HTTPError ])
237209 def delete (self , key ):
238210 try :
239211 self ._collection_data .delete_by_id (key )
0 commit comments