Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions native/src/main/java/org/humemai/nativelib/ArcadeNative.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,36 @@ private static CCharPointer frameOut(final int length, final byte[] b) {
return p;
}

/**
* The number of rows an open cursor still holds, as one long: the cursor is drained here (the rows are pulled from the engine but
* not encoded) and closed. Rows a cursor already gave out in an earlier reply are not counted. -1 on error: the error text is then
* read with arcadedb_n_last_error on the same thread, as for arcadedb_n_cmd, built by the same errorJson as the "rerr" of a JSON
* reply, so the exception the caller raises does not change. The cursor is closed on an error too. No reply text, so no malloc, no
* arcadedb_free and no JSON decode in the caller, and no row is built as text.
*/
@CEntryPoint(name = "arcadedb_n_count")
public static long count(final IsolateThread thread, final long rsId) {
ResultSet rs = null;
try {
rs = RESULT_SETS.get(rsId);
if (rs == null)
throw new IllegalArgumentException("unknown result set " + rsId);
long n = 0;
while (rs.hasNext()) {
rs.next();
n++;
}
RESULT_SETS.remove(rsId);
closeQuietly(rs);
return n;
} catch (final Throwable t) {
RESULT_SETS.remove(rsId);
closeQuietly(rs);
LAST_ERROR.set(errorJson(t));
return -1;
}
}

@CEntryPoint(name = "arcadedb_n_rs_close")
public static CCharPointer rsClose(final IsolateThread thread, final long rsId) {
closeQuietly(RESULT_SETS.remove(rsId));
Expand Down
52 changes: 47 additions & 5 deletions src/arcadedb/_native/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,21 @@ def __repr__(self) -> str:
)


_FACETS_WORD = re.compile(r"\bfacets\b", re.IGNORECASE)


def _mentions_facets(command) -> bool:
"""Whether a statement's text holds the word FACETS (any case, a string literal included: a false positive only costs speed)."""
return isinstance(command, str) and _FACETS_WORD.search(command) is not None


class NativeResultSet:
"""Cursor over a statement's rows, fetched in batches through ``arcadedb_n_next``."""

# a result of a statement that may have a FACETS clause: the facets ride on the reply that drains the cursor, so count() reads
# such a result row by row and does not drain it in the library with arcadedb_n_count (set by Database._exec)
_may_have_facets = False

def __init__(self, database, reply: dict, tagged: bool = True):
self._database = database # strong reference: a result keeps its database alive
# whether a reply of this result carried a restored tagged value (codec.loads_tagged), so a row may hold a value that is not
Expand Down Expand Up @@ -644,9 +656,35 @@ def iter_chunks(self, size: int = 1000, convert_types: bool = True):
yield chunk

def count(self) -> int:
n = 0
for _ in self:
n += 1
"""The number of rows still to be read; the result is finished. The rows already in this object (the first reply) are counted
as ``len``, the rows still in the library by one crossing, ``arcadedb_n_count``, which returns a long: the library drains and
closes the cursor and encodes no row, and no ``NativeResult`` or dict is built for a counted row. A result of a statement that
may have a FACETS clause is counted row by row instead: its facets come with the reply that drains the cursor, and a drain
in the library would lose them.
"""
if self._may_have_facets:
n = 0
for _ in self:
n += 1
return n
if not self._readable():
return 0
buf = self._buf
n = len(buf)
buf.clear()
if self._pending_error is not None:
self._fill() # the error of the first reply: raised, as reading the rows before it would
if self._more and self._rs:
rows = lib.count(self._rs)
# the library closed the cursor, on success and on an error alike: this object holds no cursor any more
self._more = False
self._rs = 0
if rows < 0:
self._closed = True
self._exhausted = True
raise _read_error(json.loads(lib.last_error()))
n += rows
self._finish()
return n

def first(self) -> Optional[NativeResult]:
Expand Down Expand Up @@ -812,7 +850,9 @@ def _exec(
)
if rid < 0:
raise _error(prefix, json.loads(lib.last_error()))
return NativeResultSet._lazy(self, rid)
rs = NativeResultSet._lazy(self, rid)
rs._may_have_facets = _mentions_facets(command)
return rs
# phase 7 (arcadedb_n_exec flags): 2 = a command's rows are pulled but encoded only when read, 4 = float arrays come back as a float32 tail
if flags & 1 and _LAZY_COMMAND_ROWS and not eager:
flags |= 2
Expand Down Expand Up @@ -854,7 +894,9 @@ def _exec(
reply, tagged = loads_tagged(raw, floats)
if not reply.get("ok"):
raise _error(prefix, reply)
return NativeResultSet(self, reply, tagged)
rs = NativeResultSet(self, reply, tagged)
rs._may_have_facets = _mentions_facets(command)
return rs

def query(self, language: str, command: str, *args) -> NativeResultSet:
"""Run a read statement. Parameters bind as on the JPype path (``?`` positional, ``:name`` from one dict)."""
Expand Down
20 changes: 16 additions & 4 deletions src/arcadedb/_native/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ class _Params(ctypes.Structure):
"arcadedb_n_next_col": [P, L, N, S, N],
"arcadedb_n_rs_close": [P, L],
"arcadedb_n_facets": [P, L],
"arcadedb_n_count": [P, L],
"arcadedb_n_cmd": [P, L, N, S, S, S, S, L, N],
"arcadedb_n_last_error": [P],
"arcadedb_n_rs_close_many": [P, P, N],
Expand All @@ -272,9 +273,10 @@ class _Params(ctypes.Structure):
"arcadedb_n_set_upcall": [P, P],
"arcadedb_free": [P, P],
}
# the entry points that answer with a number, not with the address of a reply text: a cursor id (arcadedb_n_cmd) and the
# transaction state (arcadedb_n_tx); -1 is an error for both, its text is read with arcadedb_n_last_error
returns_long = ("arcadedb_n_cmd", "arcadedb_n_tx")
# the entry points that answer with a number, not with the address of a reply text: a cursor id (arcadedb_n_cmd), the
# transaction state (arcadedb_n_tx) and a row count (arcadedb_n_count); -1 is an error for all, its text is read with
# arcadedb_n_last_error
returns_long = ("arcadedb_n_cmd", "arcadedb_n_tx", "arcadedb_n_count")
for name, argtypes in sigs.items():
fn = getattr(lib, name, None)
if fn is None: # a library built before this entry point existed
Expand Down Expand Up @@ -529,8 +531,18 @@ def tx(handle, op) -> int:
return lib.arcadedb_n_tx(th, handle, op)


def count(rs_id: int) -> int:
"""``arcadedb_n_count``: the number of rows the cursor still holds (it is drained and closed in the library), or -1 for an error (read
it with :func:`last_error`; the cursor is closed then too). One foreign call: no reply text to copy, free or decode, no row encoded.
"""
lib = _lib or _load()
t = _local.__dict__.get("thread")
th = t.handle if t is not None else _thread_handle()
return lib.arcadedb_n_count(th, rs_id)


def last_error() -> str:
"""The error of the last failed :func:`cmd` or :func:`tx` on this thread, as the JSON text of an error reply."""
"""The error of the last failed :func:`cmd`, :func:`tx` or :func:`count` on this thread, as the JSON text of an error reply."""
return call("arcadedb_n_last_error")


Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@
collect_ignore.append("test_native_vertex_vector_crossings.py")
# the native columnar fast path (arcadedb._native.bulk.insert_columns_fast) with a rows-dropped fault in it, found by the ingest-integrity check (#336)
collect_ignore.append("test_native_ingest_integrity_faults.py")
# count() of a native result set counts the rows without building a result object for each (and, in the library, without encoding them)
collect_ignore.append("test_native_count.py")
if not os.path.isfile(
os.path.join(os.path.dirname(os.path.dirname(__file__)), "docs", "index.md")
):
Expand Down
Loading
Loading