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 put(self, id): ''' PUT(create/update) operation ''' data = tornado.escape.json_decode(self.request.body) version = self.get_argument('version', None) txid = self.get_argument('txid', None) tega_id = self.get_argument('tega_id') ephemeral = self.get_argument('ephemeral', False) if version: version = int(version) if ephemeral == 'True': ephemeral = True else: ephemeral = False path = url2path(id) cont = subtree(path, data) if txid: with tx_lock: if txid in transactions: t = transactions[txid]['tx'] t.put(cont, version=version, deepcopy=False, ephemeral=ephemeral) else: raise tornado.web.HTTPError(404) # Not Found(404) else: with tx(subscriber=_tega_id2subscriber(tega_id)) as t: try: t.put(cont, version=version, deepcopy=False, ephemeral=ephemeral) except ValueError as e: raise tornado.web.HTTPError(409) # Not Acceptible(409)
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 put(self, instance, tega_id=None, version=None, deepcopy=True, path=None, ephemeral=False): ''' PUT operation. Set "version" to the one from GET operation, when collision check is required. ''' if isinstance(instance, dict): instance = subtree(path, instance) self.crud_queue.append((self._put, instance, tega_id, version, deepcopy, ephemeral))
def put(self, instance, tega_id=None, version=None, deepcopy=True, path=None, ephemeral=False): ''' PUT operation. Set "version" to the one from GET operation, when collision check is required. ''' if isinstance(instance, dict): instance = subtree(path, instance) self.crud_queue.append( (self._put, instance, tega_id, version, deepcopy, ephemeral))
def crud_batch(notifications, subscriber=None): ''' CRUD operation in a batch. ''' with tx(subscriber=subscriber) as t: for crud in notifications: ope = crud['ope'] path = crud['path'] tega_id = crud['tega_id'] if ope == OPE.PUT.name: instance = subtree(path, crud['instance']) t.put(instance, tega_id=tega_id, deepcopy=False) elif ope == OPE.DELETE.name: t.delete(path, tega_id=tega_id) elif ope == OPE.ROLLBACK.name: backto = crud['backto'] rollback(tega_id, path, backto, subscriber=subscriber)
def process_cmd(tornado_loop=False): global base_url, conn, txid if tornado_loop: cmd = sys.stdin.readline().rstrip('\n') else: cmd = input(next(gen_prompt_)).strip('\t') if cmd == '': pass elif cmd == 'h': pydoc.pager(HELP) elif cmd == 'q': readline.write_history_file(HISTORY_FILE) sys.exit(0) elif cmd == 'id': print(driver.tega_id) elif cmd in ('clear', 'roots', 'old', 'sync', 'channels', 'subscribers', 'ids', 'global', 'forwarders', 'plugins', 'reload'): status, reason, data = getattr(driver, cmd)() if data: print(yaml.dump(data)) else: print('{} {}'.format(status, reason)) elif cmd == 'ss': status, reason, data = getattr(driver, cmd)() print('{} {}'.format(status, reason)) elif cmd == 'begin': try: txid = getattr(driver, cmd)() print('txid: {} accepted'.format(txid)) except TransactionException as e: print(e) elif cmd == 'cand': try: data = driver.cand(python_dict=True) print(data) except TransactionException as e: print(e) elif cmd == 'cancel': try: txid = getattr(driver, cmd)() print('txid: {} cancelled'.format(txid)) except TransactionException as e: print(e) elif cmd == 'commit': try: txid = getattr(driver, cmd)() print('txid: {} commited'.format(txid)) except TransactionException as e: print(e) elif rpc_pattern.match(cmd): func_path, args, kwargs = func_args_kwargs(cmd) if args and kwargs: status, reason, data = driver.rpc(func_path, *args, **kwargs) elif args: status, reason, data = driver.rpc(func_path, *args) elif kwargs: status, reason, data = driver.rpc(func_path, **kwargs) else: status, reason, data = driver.rpc(func_path) if data: if 'result' in data: print(data['result']) else: print(data) else: print('{} {}'.format(status, reason)) else: g = cmd_pattern.match(cmd) if not g: print('No such command "{}"'.format(cmd)) else: ope = g.group(1) path = g.group(2) version = g.group(3) rollback = g.group(4) root_oid = g.group(5) backto = g.group(6) if ope == 'sub': driver.subscribe(path, SCOPE.GLOBAL) elif ope == 'subr': driver.subscribe(path, SCOPE.GLOBAL, regex_flag=True) elif ope == 'unsub': if path == '*': driver.unsubscribe_all() else: driver.unsubscribe(path) elif ope == 'unsubr': driver.unsubscribe(path, regex_flag=True) else: if rollback: status, reason, data = getattr(driver, 'rollback')(root_oid, backto) print('{} {}'.format(status, reason)) else: body = None if ope in ('put', 'pute', 'pub'): buf = io.StringIO() while True: cmd = input() if cmd == '': break buf.write(cmd + '\n') buf.seek(0) body = yaml.load(buf.read()) buf.close() params = [] url_params = '' kwargs = {} if ope == 'put': instance = subtree(path, body) kwargs['instance'] = instance elif ope == 'pute': ope = 'put' instance = subtree(path, body) kwargs['instance'] = instance kwargs['ephemeral'] = True elif ope == 'publish': kwargs['channel'] = path kwargs['message'] = body elif ope == 'get': kwargs['python_dict'] = True kwargs['path'] = path elif ope == 'geta': ope = 'get' kwargs['python_dict'] = True kwargs['path'] = path kwargs['internal'] = True elif ope == 'getr': ope = 'get' kwargs['python_dict'] = True kwargs['path'] = path kwargs['regex_flag'] = True elif ope == 'del': ope = 'delete' kwargs['path'] = path else: kwargs['path'] = path if version: kwargs['version'] = version try: _data = getattr(driver, ope)(**kwargs) if _data: print(yaml.dump(_data)) except CRUDException as e: print(e)