示例#1
0
async def read_version(tx):
    fdb_future = lib.fdb_transaction_get_read_version(tx.pointer)
    aio_future = _loop.create_future()
    handle = ffi.new_handle(aio_future)
    lib.fdb_future_set_callback(fdb_future, _on_transaction_get_read_version,
                                handle)
    out = await aio_future
    return out
示例#2
0
async def query(tx, key, other, *, limit=0, mode=STREAMING_MODE_ITERATOR):
    key = key if isinstance(key, KeySelector) else gte(key)
    other = other if isinstance(other, KeySelector) else gte(other)
    if key.key < other.key:
        begin = key
        end = other
        reverse = False
    else:
        begin = other
        end = key
        reverse = True

    # the first read was fired off when the FDBRange was initialized
    iteration = 1
    snapshot = tx.snapshot
    while True:
        fdb_future = lib.fdb_transaction_get_range(
            tx.pointer,
            begin.key,
            len(begin.key),
            begin.or_equal,
            begin.offset,
            end.key,
            len(end.key),
            end.or_equal,
            end.offset,
            limit,
            0,
            mode,
            iteration,
            snapshot,
            reverse,
        )
        aio_future = _loop.create_future()
        handle = ffi.new_handle(aio_future)
        lib.fdb_future_set_callback(fdb_future, on_transaction_get_range,
                                    handle)
        kvs, count, more = await aio_future

        index = 0
        if count == 0:
            return

        for kv in kvs:
            yield kv

            index += 1
            if index == count:
                if not more or limit == count:
                    return

                iteration += 1
                if limit > 0:
                    limit -= count
                if reverse:
                    end = gte(kv[0])
                else:
                    begin = gt(kv[0])
示例#3
0
async def estimated_size_bytes(tx, begin, end):
    fdb_future = lib.fdb_transaction_get_estimated_range_size_bytes(
        tx.pointer, begin, len(begin), end, len(end))
    aio_future = _loop.create_future()
    handle = ffi.new_handle(aio_future)
    lib.fdb_future_set_callback(fdb_future, _estimated_size_bytes_callback,
                                handle)
    size = await aio_future
    return size
示例#4
0
async def get(tx, key):
    assert isinstance(tx, Transaction)
    assert isinstance(key, bytes)
    fdb_future = lib.fdb_transaction_get(tx.pointer, key, len(key),
                                         tx.snapshot)
    aio_future = _loop.create_future()
    handle = ffi.new_handle(aio_future)
    lib.fdb_future_set_callback(fdb_future, _on_transaction_get, handle)
    out = await aio_future
    return out
示例#5
0
async def transactional(db, func, *args, snapshot=False, **kwargs):
    tx = _make_transaction(db, snapshot)
    while True:
        try:
            out = await func(tx, *args, **kwargs)
            await _commit(tx)
        except FoundException as exc:
            fdb_future = lib.fdb_transaction_on_error(tx.pointer, exc.code)
            aio_future = _loop.create_future()
            handle = ffi.new_handle(aio_future)
            lib.fdb_future_set_callback(fdb_future, _on_error_callback, handle)
            await aio_future  # may raise an exception
        else:
            return out
示例#6
0
async def _commit(tx):
    fdb_future = lib.fdb_transaction_commit(tx.pointer)
    aio_future = _loop.create_future()
    handle = ffi.new_handle(aio_future)
    lib.fdb_future_set_callback(fdb_future, on_transaction_commit, handle)
    await aio_future