def get(self, path, version=None, internal=False, python_dict=False, regex_flag=False): ''' CRUD read operation ''' url = self._urlencode(path2url(path), txid=self.txid, version=version, tega_id=self.tega_id, internal=internal, regex_flag=str(regex_flag)) response, body = self.conn.request(url, GET, None, HEADERS) if response.status >= 300 or response.status < 200: raise CRUDException(response=response) dict_data = json.loads(body.decode('utf-8')) if python_dict: return dict_data # Returns Dict else: if regex_flag: return [dict2cont(elm) for elm in dict_data] return subtree(path, dict_data) # Returns Cont
def reload_log(): ''' Reloads log to reorganize a tree in idb ''' PUT = OPE.PUT DELETE = OPE.PUT SS = OPE.SS t = None multi = [] _log_fd.seek(0) for line in _log_fd: line = line.rstrip('\n') if line.startswith(ROLLBACK_MARKER): args = line.split(' ') rollback(args[1], args[2], int(args[0]), write_log=False) elif line.startswith(COMMIT_FINISH_MARKER) and len(multi) > 0: timestamp = _timestamp(line) t = tx() put = t.put delete = t.delete for crud in multi: ope = crud[0] if ope == PUT: instance = crud[1] tega_id = crud[2] put(instance, tega_id=tega_id, deepcopy=False) elif ope == DELETE: path = crud[1] tega_id = crud[2] delete(path, tega_id=tega_id) t.commit(write_log=False) del multi[:] elif line.startswith(COMMIT_FINISH_MARKER): pass elif line.startswith(COMMIT_START_MARKER) or line.startswith( SYNC_CONFIRMED_MARKER): pass else: log = eval(line) ope = log['ope'] path = log['path'] instance = log['instance'] tega_id = log['tega_id'] if ope == PUT.name: if path: root = subtree(path, instance) else: root = dict2cont(instance) multi.append((PUT, root, tega_id)) elif ope == DELETE.name: multi.append((DELETE, path, tega_id)) elif ope == SS.name: root_oid = instance['_oid'] root = deserialize(instance) _idb[root_oid] = root
def reload_log(): ''' Reloads log to reorganize a tree in idb ''' PUT = OPE.PUT DELETE = OPE.PUT SS = OPE.SS t = None multi = [] _log_fd.seek(0) for line in _log_fd: line = line.rstrip('\n') if line.startswith(ROLLBACK_MARKER): args = line.split(' ') rollback(args[1], args[2], int(args[0]), write_log=False) elif line.startswith(COMMIT_FINISH_MARKER) and len(multi) > 0: timestamp = _timestamp(line) t = tx() put = t.put delete = t.delete for crud in multi: ope = crud[0] if ope == PUT: instance = crud[1] tega_id = crud[2] put(instance, tega_id=tega_id, deepcopy=False) elif ope == DELETE: path = crud[1] tega_id = crud[2] delete(path, tega_id=tega_id) t.commit(write_log=False) del multi[:] elif line.startswith(COMMIT_FINISH_MARKER): pass elif line.startswith(COMMIT_START_MARKER) or line.startswith(SYNC_CONFIRMED_MARKER): pass else: log = eval(line) ope = log['ope'] path = log['path'] instance = log['instance'] tega_id = log['tega_id'] if ope == PUT.name: if path: root = subtree(path, instance) else: root = dict2cont(instance) multi.append((PUT, root, tega_id)) elif ope == DELETE.name: multi.append((DELETE, path, tega_id)) elif ope == SS.name: root_oid = instance['_oid'] root = deserialize(instance) _idb[root_oid] = root
def cand(self, internal=False, python_dict=False): ''' Candidate config. ''' if self.txid: url = self._cmdencode('cand', txid=self.txid, internal=internal) response, body = self.conn.request(url, POST, None, HEADERS) status = response.status json_data = body.decode('utf-8') if python_dict: return json.loads(json_data) else: return dict2cont(json_data) else: raise TransactionException(message='no ongoing transaction')
def _render(self, filename): ''' NLAN state registartion with tega db ''' with open(os.path.join(self.etcdir, filename)) as f: temp = f.read() with self.tx() as t: routers = self.get(IP_PATH) r = {k: v.split('/')[0] for k, v in routers.items()} state_yaml = mako.template.Template(temp).render(**r) state = yaml.load(state_yaml) config = {CONFIG_PATH.format(r): v for r, v in state.items()} for root_oid, c in config.items(): t.put(dict2cont({root_oid: c}))