def queryClaimSize(self, claimType, query): """ Queries claim's size Queries the data according the the given query and returns the number of records retrieved from the query. Args: claimType: the claimType query: the query string Returns: The number of records retrieved from the query Raises: PersistenceException: There is DB error """ try: claimType = ClaimTypeFactory.load(claimType) query_parser = RedisQueryParser(claimType) parse_tree = query_parser.parse(query) query_executor = RedisQueryExecutor(self.connection, claimType) object_ids = query_executor.visit(parse_tree) cnt = len(object_ids) return cnt except RedisQueryParserError as e: raise PersistenceException(*e.args) except RedisError as e: raise PersistenceException(*e.args)
def queryClaimSize(self, claimType, query): """ Queries claim's size Queries the data according the the given query and returns the number of records retrieved from the query. Args: claimType: the claimType query: the query string Returns: The number of records retrieved from the query Raises: PersistenceException: There is DB error """ try: claimType = ClaimTypeFactory.load(claimType) tablename = claimType.tablename where = query sql = "select count(*) from %s where %s" % (tablename, where) self.cursor.execute(sql) cnt = self.cursor.fetchone()[0] return cnt except DatabaseError as e: raise PersistenceException(*e.args)
def createClaim(self, claimType, fields): """ Creates claim Creates the claim of the given type with the given data in the persistence Args: claimType: the claimType fields: the data of the claim Raises: PersistenceException: There is DB error """ try: claimType = ClaimTypeFactory.load(claimType) columns = [] data = [] for i in range(len(fields)): if fields[i]: data.append(fields[i]) columns.append(claimType.columns[i]) # Appending LAST_MODIFY column, fields always won't provide 'LAST_MODIFY' field columns.append('LAST_MODIFY') data.append(str(datetime.now())) sql = "insert into %s(`%s`) values('%s')" % ( claimType.tablename, "`, `".join(columns), "', '".join(data)) self.cursor.execute(sql) except DatabaseError as e: raise PersistenceException(*e.args)
def createClaim(self, claimType, fields): """ Creates claim Creates the claim of the given type with the given data in the persistence Redis is a key-value store so we have to maintain indexes on our own. Each object has an auto-incremented id and is stored as a hash value in Redis. Numerical attributes are indexed in a sorted set with key "tablename:column". Textual attributes are indexed in separate sets with key "tablename:column:textvalue". Thus numerical attributes can have range queries, while textual attributes can only have equality queries. Args: claimType: the claimType fields: the data of the claim Raises: PersistenceException: There is DB error """ try: claimType = ClaimTypeFactory.load(claimType) mapping = {} # Adding additional field LAST_MODIFY # Restricted by redis, we use a integer-like datetime here fields.append(datetime.now().strftime('%Y%m%d%H%M%S')) for i in range(len(fields)): mapping[claimType.columns[i]] = fields[i] id = self.connection.incr(claimType.tablename + ":lastid") self.cursor.sadd(claimType.tablename, id) self.cursor.hmset(claimType.tablename + ":" + str(id), mapping) for i in range(len(fields)): if claimType.columnTypes[i] == "integer": if fields[i]: self.cursor.zadd( claimType.tablename + ":" + claimType.columns[i], int(fields[i]), id) elif claimType.columnTypes[i] == "decimal": if fields[i]: self.cursor.zadd( claimType.tablename + ":" + claimType.columns[i], Decimal(fields[i]), id) elif claimType.columnTypes[i] == "datetime": if fields[i]: self.cursor.zadd( claimType.tablename + ":" + claimType.columns[i], int(fields[i]), id) elif claimType.columnTypes[i] == "varchar": self.cursor.sadd( claimType.tablename + ":" + claimType.columns[i] + ":" + fields[i], id) else: raise ValueError("Invalid column type: " + claimType.columnTypes[i]) except RedisError as e: raise PersistenceException(*e.args)
def queryClaims(self, claimType, query, pageNumber, pageSize): """ Queries claims Queries the data according the the given filtering options from the given claim type, and returns the list of records (as lists) of data for the given claim type Args: claimType: the claimType query: the query string pageNumber: the page number pageSize: the page size Returns: The list of records of data for the given claim type Raises: PersistenceException: There is DB error """ try: claimType = ClaimTypeFactory.load(claimType) query_parser = RedisQueryParser(claimType) parse_tree = query_parser.parse(query) query_executor = RedisQueryExecutor(self.connection, claimType) object_ids = query_executor.visit(parse_tree) if pageNumber: start = (pageNumber - 1) * pageSize end = start + pageSize object_ids = object_ids[start:end] for id in object_ids: self.cursor.hgetall(claimType.tablename + ":" + id.decode()) claims = self.cursor.execute() for i in range(len(claims)): claims[i] = self._convert_to_tuple(claimType, claims[i]) return claims except RedisQueryParserError as e: raise PersistenceException(*e.args) except RedisError as e: raise PersistenceException(*e.args)
def rollback(self): """ Rolls back the session Raises: PersistenceException: There is DB error """ try: self.connection.rollback() except DatabaseError as e: raise PersistenceException(*e.args)
def commit(self): """ Commits the session Raises: PersistenceException: There is DB error """ try: self.connection.commit() except DatabaseError as e: raise PersistenceException(*e.args)
def commit(self): """ Commits the session Raises: PersistenceException: There is DB error """ try: self.cursor.execute() except RedisError as e: raise PersistenceException(*e.args)
def close(self): """ Closes the session Raises: PersistenceException: There is DB error """ try: if self.cursor: self.cursor.reset() self.cursor = None if self.connection: self.connection = None except RedisError as e: raise PersistenceException(*e.args)
def begin(self): """ Begins the persistence connection session Raises: PersistenceException: There is DB error """ try: self.connection = redis.StrictRedis( host=self.connectionConfig["host"], port=self.connectionConfig["port"], db=self.connectionConfig["db"]) self.connection.ping() self.cursor = self.connection.pipeline() self.cursor.multi() except RedisError as e: raise PersistenceException(*e.args)
def begin(self): """ Begins the persistence connection session Raises: PersistenceException: There is DB error """ try: self.connection = dbdriver.connect( host=self.connectionConfig["host"], user=self.connectionConfig["user"], passwd=self.connectionConfig["passwd"], db=self.connectionConfig["db"], port=self.connectionConfig["port"], connect_timeout=self.connectionConfig["connect_timeout"]) self.cursor = self.connection.cursor() except DatabaseError as e: raise PersistenceException(*e.args)
def queryClaims(self, claimType, query, pageNumber, pageSize): """ Queries claims Queries the data according the the given filtering options from the given claim type, and returns the list of records (as lists) of data for the given claim type Args: claimType: the claimType query: the query string pageNumber: the page number pageSize: the page size Returns: The list of records of data for the given claim type Raises: PersistenceException: There is DB error """ try: claimType = ClaimTypeFactory.load(claimType) # Here we remove the last 'LAST_MODIFY' column by [0:-1] columns = "`%s`" % "`, `".join(claimType.columns[0:-1]) tablename = claimType.tablename where = query limit = "" if pageNumber: offset = (pageNumber - 1) * pageSize limit = "limit %s,%s" % (offset, pageSize) sql = "select %s from %s where %s %s" % (columns, tablename, where, limit) self.cursor.execute(sql) claims = self.cursor.fetchall() return claims except DatabaseError as e: raise PersistenceException(*e.args)