def couchTargetExist(self, target): res = self.couchGetRequest(target) if self.targetNotFound(res): return False if "error" in res: raise error.CouchWrapperError(res) return True
def couchGetDoc(self, target, key): if not key: raise ValueError("Please specify a document key") MaybeDoc = self.couchGetRequest(str(target) + '/' + str(key)) if self.docNotFound(MaybeDoc): return None if "error" in MaybeDoc: raise error.CouchWrapperError(MaybeDoc) return MaybeDoc
def couchPostDoc(self, target, doc): maybePost = self.couchPostRequest(target, doc) if "error" in maybePost: raise error.CouchWrapperError(maybePost) return maybePost
def couchPutRequest(self, path, data=None): r = SESSION.put(self.end_point + '/' + path, json=data) result = json.loads(r.text) if "error" in result: raise error.CouchWrapperError(result) return result
def bulkDocAdd( self, _iterable, updateFunc=lambdaFuse, target=None, depth=0 ): # iterable w/ key:value pairs, key is primary _id in DB and value is document to insert iterable = deepcopy(_iterable) logging.debug(f"bulkDocAdd iterable content {_iterable}") if not target: raise ValueError("No target db specified") ans = self.bulkRequestByKey(list( iterable.keys()), target) # param iterable msut have a keys method logging.debug(f"bulkDocAdd prior key request {ans.keys()}") logging.debug(ans) bulkInsertData = {"docs": []} for reqItem in ans['results']: key = reqItem["id"] dataToPut = iterable[key] if "_rev" in dataToPut: del dataToPut["_rev"] dataToPut['_id'] = key _datum = reqItem['docs'][ 0] # mandatory docs key, one value array guaranted if 'error' in _datum: if self.docNotFound(_datum["error"]): logging.debug(f"creating {key} document") else: logging.error(f'Unexpected error here {_datum}') elif 'ok' in _datum: if "error" in _datum["ok"]: raise error.CouchWrapperError( "Unexpected \"error\" key in bulkDocAdd answer packet::" + str(_datum["ok"])) dataToPut = updateFunc(_datum["ok"], iterable[key]) else: logging.error(f'unrecognized item packet format {reqItem}') continue bulkInsertData["docs"].append(dataToPut) logging.debug(f"about to bulk_doc that {bulkInsertData}") #r = requests.post(DEFAULT_END_POINT + '/' + target + '/_bulk_docs', json=bulkInsertData) #ans = json.loads(r.text) insertError, insertOk = ([], []) r = SESSION.post(self.end_point + '/' + target + '/_bulk_docs', json=bulkInsertData) ans = json.loads(r.text) insertOk, insertError = self.bulkDocErrorReport(ans) # If unknown_error occurs in insertion, rev tag have to updated, this fn takes care of this business # so we filter the input and make a recursive call if insertError: depth += 1 logging.debug(f"Retry depth {depth}") if depth == 1: logging.error(f"Insert Error Recursive fix\n{insertError}") if depth == 50: logging.error(f"Giving up at 50th try for {insertError}") else: idError = [d['id'] for d in insertError] logging.debug(f"iterable to filter from {iterable}") logging.debug( f"depth {depth} insert Error content: {insertError}") _iterable = {k: v for k, v in iterable.items() if k in idError} insertOk += self.bulkDocAdd(_iterable, updateFunc=updateFunc, target=target, depth=depth) elif depth > 0: logging.info(f"No more recursive insert left at depth {depth}", depth) logging.debug(f"returning {insertOk}") return insertOk