Example #1
0
File: utils.py Project: balath/hedy
def db_scan(table):
    querylog.log_counter('db_scan:' + table)
    result = db.scan(TableName=db_prefix + '-' + table)
    output = []
    querylog.log_counter('db_scan_items', len(result['Items']))
    for item in result['Items']:
        output.append(db_decode(item))
    return output
Example #2
0
File: utils.py Project: balath/hedy
def db_set(table, data):
    querylog.log_counter('db_set:' + table)
    if db_get(table, data):
        result = db.update_item(TableName=db_prefix + '-' + table,
                                Key=db_encode(db_key(table, data)),
                                AttributeUpdates=db_encode(
                                    db_key(table, data, True), True))
    else:
        result = db.put_item(TableName=db_prefix + '-' + table,
                             Item=db_encode(data))
    return result
Example #3
0
File: utils.py Project: balath/hedy
def db_del_many(table, data, *not_primary):
    querylog.log_counter('db_del_many:' + table)

    # We define a recursive function in case the number of results is very large and cannot be returned with a single call to db_get_many.
    def batch():
        to_delete = db_get_many(table, data, *not_primary)
        if len(to_delete) == 0:
            return
        for item in to_delete:
            db_del(table, db_key(table, item))
        batch()

    batch()
Example #4
0
File: utils.py Project: balath/hedy
def db_get_many(table, data, *not_primary):
    querylog.log_counter('db_get_many:' + table)
    if len(not_primary):
        field = list(data.keys())[0]
        # We use ScanIndexForward = False to get the latest items from the Programs table
        result = db.query(
            TableName=db_prefix + '-' + table,
            IndexName=field + '-index',
            KeyConditionExpression=field + ' = :value',
            ExpressionAttributeValues={':value': {
                'S': data[field]
            }},
            ScanIndexForward=False)
    else:
        result = db.query(TableName=db_prefix + '-' + table,
                          Key=db_encode(db_key(table, data)))
    data = []
    querylog.log_counter('db_get_many_items', len(result['Items']))
    for item in result['Items']:
        data.append(db_decode(item))
    return data
Example #5
0
File: utils.py Project: balath/hedy
def db_get(table, data, *not_primary):
    querylog.log_counter('db_get:' + table)
    # If we're querying by something else than the primary key of the table, we assume that data contains only one field, that on which we want to search. We also require that field to have an index.
    if len(not_primary):
        field = list(data.keys())[0]
        result = db.query(
            TableName=db_prefix + '-' + table,
            IndexName=field + '-index',
            KeyConditionExpression=field + ' = :value',
            ExpressionAttributeValues={':value': {
                'S': data[field]
            }})
        if len(result['Items']):
            return db_decode(result['Items'][0])
        else:
            return None
    else:
        result = db.get_item(TableName=db_prefix + '-' + table,
                             Key=db_encode(db_key(table, data)))
        if 'Item' not in result:
            return None
        return db_decode(result['Item'])
Example #6
0
File: utils.py Project: balath/hedy
def db_describe(table):
    querylog.log_counter('db_describe:' + table)
    return db.describe_table(TableName=db_prefix + '-' + table)
Example #7
0
File: utils.py Project: balath/hedy
def db_del(table, data):
    querylog.log_counter('db_del:' + table)
    return db.delete_item(TableName=db_prefix + '-' + table,
                          Key=db_encode(db_key(table, data)))