jsift 2.1.0__py3-none-macosx_11_0_arm64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jsift/__init__.py +169 -0
- jsift/_lib.py +55 -0
- jsift/libjsift.dylib +0 -0
- jsift-2.1.0.dist-info/METADATA +75 -0
- jsift-2.1.0.dist-info/RECORD +8 -0
- jsift-2.1.0.dist-info/WHEEL +5 -0
- jsift-2.1.0.dist-info/licenses/LICENSE +21 -0
- jsift-2.1.0.dist-info/top_level.txt +1 -0
jsift/__init__.py
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"""Python bindings for jsift, a fast JSONL search engine.
|
|
2
|
+
|
|
3
|
+
The query language is the same as the CLI's::
|
|
4
|
+
|
|
5
|
+
import jsift
|
|
6
|
+
|
|
7
|
+
jsift.search("level eq error AND status gte 500", file="app.jsonl")
|
|
8
|
+
jsift.count("level eq error", file="app.jsonl")
|
|
9
|
+
jsift.select("SELECT service,avg:latency_ms WHERE status eq 200", file="app.jsonl")
|
|
10
|
+
|
|
11
|
+
Input is either ``file=`` (a path, read by jsift's own threaded reader) or ``data=``
|
|
12
|
+
(``bytes``/``str`` of JSONL already in memory). The search runs in native code with the
|
|
13
|
+
GIL released, so other Python threads keep running.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import ctypes
|
|
19
|
+
import json
|
|
20
|
+
import os
|
|
21
|
+
import shlex
|
|
22
|
+
from dataclasses import dataclass
|
|
23
|
+
from typing import Any, Sequence, Union
|
|
24
|
+
|
|
25
|
+
from ._lib import _JsiftResult, lib
|
|
26
|
+
|
|
27
|
+
__all__ = ["JsiftError", "Result", "run", "search", "search_lines", "count", "select", "__version__"]
|
|
28
|
+
|
|
29
|
+
__version__: str = lib.jsift_version().decode()
|
|
30
|
+
|
|
31
|
+
Query = Union[str, Sequence[str]]
|
|
32
|
+
PathLike = Union[str, "os.PathLike[str]"]
|
|
33
|
+
Data = Union[bytes, bytearray, memoryview, str]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class JsiftError(ValueError):
|
|
37
|
+
"""Raised for an invalid query, or when the input can't be read."""
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class Result:
|
|
42
|
+
"""Raw output of a query: exactly what the CLI would print, plus the match count."""
|
|
43
|
+
|
|
44
|
+
output: bytes
|
|
45
|
+
matched: int
|
|
46
|
+
|
|
47
|
+
def lines(self) -> list[bytes]:
|
|
48
|
+
return self.output.splitlines()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _tokens(query: Query) -> list[str]:
|
|
52
|
+
return shlex.split(query) if isinstance(query, str) else list(query)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def run(
|
|
56
|
+
query: Query,
|
|
57
|
+
*,
|
|
58
|
+
file: PathLike | None = None,
|
|
59
|
+
data: Data | None = None,
|
|
60
|
+
limit: int | None = None,
|
|
61
|
+
fast: bool = False,
|
|
62
|
+
format: str | None = None,
|
|
63
|
+
count_only: bool = False,
|
|
64
|
+
header: bool = False,
|
|
65
|
+
) -> Result:
|
|
66
|
+
"""Runs a query and returns the raw output.
|
|
67
|
+
|
|
68
|
+
``query`` is a string (split like a shell command line) or a list of CLI tokens.
|
|
69
|
+
Exactly one of ``file`` and ``data`` must be given.
|
|
70
|
+
"""
|
|
71
|
+
if (file is None) == (data is None):
|
|
72
|
+
raise TypeError("pass exactly one of file= or data=")
|
|
73
|
+
|
|
74
|
+
args = _tokens(query)
|
|
75
|
+
if limit is not None:
|
|
76
|
+
if limit < 0:
|
|
77
|
+
raise ValueError("limit must be non-negative")
|
|
78
|
+
args = ["--limit", str(limit), *args]
|
|
79
|
+
if format is not None:
|
|
80
|
+
args = ["--format", format, *args]
|
|
81
|
+
if fast:
|
|
82
|
+
args = ["--fast", *args]
|
|
83
|
+
if count_only:
|
|
84
|
+
args = ["--count", *args]
|
|
85
|
+
if header:
|
|
86
|
+
args = ["--header", *args]
|
|
87
|
+
|
|
88
|
+
encoded = [arg.encode() for arg in args]
|
|
89
|
+
argv = (ctypes.c_char_p * max(len(encoded), 1))(*encoded)
|
|
90
|
+
|
|
91
|
+
path_arg: bytes | None = None
|
|
92
|
+
buf: bytes | None = None
|
|
93
|
+
if file is not None:
|
|
94
|
+
path_arg = os.fsencode(file)
|
|
95
|
+
else:
|
|
96
|
+
if isinstance(data, str):
|
|
97
|
+
buf = data.encode()
|
|
98
|
+
else:
|
|
99
|
+
buf = bytes(data) # type: ignore[arg-type]
|
|
100
|
+
|
|
101
|
+
result = _JsiftResult()
|
|
102
|
+
status = lib.jsift_query(
|
|
103
|
+
argv, len(encoded), path_arg, buf, len(buf) if buf is not None else 0, ctypes.byref(result)
|
|
104
|
+
)
|
|
105
|
+
try:
|
|
106
|
+
if status != 0:
|
|
107
|
+
message = result.error_message.decode() if result.error_message else "unknown error"
|
|
108
|
+
if file is not None:
|
|
109
|
+
message = f"{os.fsdecode(file)}: {message}"
|
|
110
|
+
raise JsiftError(message)
|
|
111
|
+
output = ctypes.string_at(result.output, result.output_len) if result.output_len else b""
|
|
112
|
+
return Result(output=output, matched=result.matched)
|
|
113
|
+
finally:
|
|
114
|
+
lib.jsift_free_result(ctypes.byref(result))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def search_lines(
|
|
118
|
+
query: Query,
|
|
119
|
+
*,
|
|
120
|
+
file: PathLike | None = None,
|
|
121
|
+
data: Data | None = None,
|
|
122
|
+
limit: int | None = None,
|
|
123
|
+
fast: bool = False,
|
|
124
|
+
) -> list[bytes]:
|
|
125
|
+
"""Returns the matching lines as raw bytes, without parsing them."""
|
|
126
|
+
return run(query, file=file, data=data, limit=limit, fast=fast).lines()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def search(
|
|
130
|
+
query: Query,
|
|
131
|
+
*,
|
|
132
|
+
file: PathLike | None = None,
|
|
133
|
+
data: Data | None = None,
|
|
134
|
+
limit: int | None = None,
|
|
135
|
+
fast: bool = False,
|
|
136
|
+
) -> list[Any]:
|
|
137
|
+
"""Returns the matching lines parsed as JSON.
|
|
138
|
+
|
|
139
|
+
For a ``SELECT`` query, each item is a dict of the selected fields; with aggregations
|
|
140
|
+
the list holds a single dict.
|
|
141
|
+
"""
|
|
142
|
+
result = run(query, file=file, data=data, limit=limit, fast=fast, format="json")
|
|
143
|
+
return [json.loads(line) for line in result.lines()]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def select(
|
|
147
|
+
query: Query,
|
|
148
|
+
*,
|
|
149
|
+
file: PathLike | None = None,
|
|
150
|
+
data: Data | None = None,
|
|
151
|
+
limit: int | None = None,
|
|
152
|
+
fast: bool = False,
|
|
153
|
+
) -> list[dict[str, Any]]:
|
|
154
|
+
"""Alias of :func:`search` for ``SELECT`` queries; ``SELECT`` is prepended if missing."""
|
|
155
|
+
tokens = _tokens(query)
|
|
156
|
+
if not tokens or tokens[0].lower() != "select":
|
|
157
|
+
tokens = ["SELECT", *tokens]
|
|
158
|
+
return search(tokens, file=file, data=data, limit=limit, fast=fast)
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def count(
|
|
162
|
+
query: Query = (),
|
|
163
|
+
*,
|
|
164
|
+
file: PathLike | None = None,
|
|
165
|
+
data: Data | None = None,
|
|
166
|
+
fast: bool = False,
|
|
167
|
+
) -> int:
|
|
168
|
+
"""Returns the number of matching lines (all lines when the query is empty)."""
|
|
169
|
+
return run(query, file=file, data=data, fast=fast, count_only=True).matched
|
jsift/_lib.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""ctypes loader for the bundled libjsift shared library."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import ctypes
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class _JsiftResult(ctypes.Structure):
|
|
12
|
+
_fields_ = [
|
|
13
|
+
("output", ctypes.POINTER(ctypes.c_char)),
|
|
14
|
+
("output_len", ctypes.c_size_t),
|
|
15
|
+
("matched", ctypes.c_size_t),
|
|
16
|
+
("error_message", ctypes.c_char_p),
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _lib_filename() -> str:
|
|
21
|
+
if sys.platform == "darwin":
|
|
22
|
+
return "libjsift.dylib"
|
|
23
|
+
if sys.platform == "win32":
|
|
24
|
+
return "jsift.dll"
|
|
25
|
+
return "libjsift.so"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _load() -> ctypes.CDLL:
|
|
29
|
+
# JSIFT_LIBRARY overrides the bundled copy (handy while developing the Zig side).
|
|
30
|
+
path = os.environ.get("JSIFT_LIBRARY") or str(Path(__file__).with_name(_lib_filename()))
|
|
31
|
+
try:
|
|
32
|
+
lib = ctypes.CDLL(path)
|
|
33
|
+
except OSError as exc:
|
|
34
|
+
raise ImportError(
|
|
35
|
+
f"jsift: could not load the native library at {path}. "
|
|
36
|
+
"No wheel may exist for this platform; build from source with Zig 0.16 installed."
|
|
37
|
+
) from exc
|
|
38
|
+
|
|
39
|
+
lib.jsift_version.restype = ctypes.c_char_p
|
|
40
|
+
lib.jsift_version.argtypes = []
|
|
41
|
+
lib.jsift_query.restype = ctypes.c_int
|
|
42
|
+
lib.jsift_query.argtypes = [
|
|
43
|
+
ctypes.POINTER(ctypes.c_char_p),
|
|
44
|
+
ctypes.c_size_t,
|
|
45
|
+
ctypes.c_char_p,
|
|
46
|
+
ctypes.c_char_p,
|
|
47
|
+
ctypes.c_size_t,
|
|
48
|
+
ctypes.POINTER(_JsiftResult),
|
|
49
|
+
]
|
|
50
|
+
lib.jsift_free_result.restype = None
|
|
51
|
+
lib.jsift_free_result.argtypes = [ctypes.POINTER(_JsiftResult)]
|
|
52
|
+
return lib
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
lib = _load()
|
jsift/libjsift.dylib
ADDED
|
Binary file
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: jsift
|
|
3
|
+
Version: 2.1.0
|
|
4
|
+
Summary: Python bindings for jsift, a blisteringly fast JSONL search engine (SIMD, written in Zig)
|
|
5
|
+
Author: Aiko Schurmann
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/aikoschurmann/jsift
|
|
8
|
+
Project-URL: Source, https://github.com/aikoschurmann/jsift
|
|
9
|
+
Keywords: json,jsonl,ndjson,search,query,logs,simd,zig
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: MacOS
|
|
12
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
13
|
+
Classifier: Topic :: Text Processing
|
|
14
|
+
Classifier: Topic :: System :: Logging
|
|
15
|
+
Requires-Python: >=3.9
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# jsift
|
|
21
|
+
|
|
22
|
+
Python bindings for [jsift](https://github.com/aikoschurmann/jsift), a blisteringly fast JSONL
|
|
23
|
+
search engine written in Zig. A SIMD search over the raw bytes skips every line that can't
|
|
24
|
+
match, and a validating JSON parser decides the rest exactly. It is 20–60x faster than
|
|
25
|
+
`jq`, and the results are exact.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
pip install jsift
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The search runs in-process in native code, with the GIL released. Nothing runs as a
|
|
32
|
+
subprocess.
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import jsift
|
|
36
|
+
|
|
37
|
+
# Matching lines, parsed
|
|
38
|
+
jsift.search("level eq error AND status gte 500", file="app.jsonl")
|
|
39
|
+
# -> [{"level": "error", "status": 503, ...}, ...]
|
|
40
|
+
|
|
41
|
+
# Count only
|
|
42
|
+
jsift.count("level eq error", file="app.jsonl") # -> 1234
|
|
43
|
+
|
|
44
|
+
# Pull out fields, or aggregate
|
|
45
|
+
jsift.select("service,latency_ms WHERE status eq 200", file="app.jsonl")
|
|
46
|
+
jsift.search("SELECT service,avg:latency_ms WHERE status eq 200", file="app.jsonl")
|
|
47
|
+
|
|
48
|
+
# Data already in memory (bytes or str)
|
|
49
|
+
jsift.search("user.id in 1,2,3", data=b'{"user": {"id": 2}}\n')
|
|
50
|
+
|
|
51
|
+
# Raw matching lines without parsing them (fastest)
|
|
52
|
+
jsift.search_lines("level eq error", file="app.jsonl", limit=100)
|
|
53
|
+
|
|
54
|
+
# Byte-level matching only: faster, approximate
|
|
55
|
+
jsift.count("level eq error", file="app.jsonl", fast=True)
|
|
56
|
+
|
|
57
|
+
# Low level: exactly the CLI's output, in any format
|
|
58
|
+
jsift.run("SELECT name,age", file="people.jsonl", format="csv", header=True).output
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
A query is a string, split the way a shell splits a command line, or a list of tokens. The
|
|
62
|
+
language is the same as the CLI's:
|
|
63
|
+
|
|
64
|
+
| | |
|
|
65
|
+
|---|---|
|
|
66
|
+
| Operators | `eq`, `neq`, `gt`, `lt`, `gte`, `lte`, `has`, `exists`, `in` |
|
|
67
|
+
| Logic | `AND`, `OR` (AND binds tighter), `NOT` before a condition |
|
|
68
|
+
| Paths | `user.id` for nested keys, `a\.b` for a literal dot |
|
|
69
|
+
| Types | auto-detected; force with `s:` (string), `n:` (number), `b:true` |
|
|
70
|
+
| Aggregations | `count:f`, `sum:f`, `min:f`, `max:f`, `avg:f` |
|
|
71
|
+
|
|
72
|
+
An invalid query, or a file that can't be read, raises `jsift.JsiftError`.
|
|
73
|
+
|
|
74
|
+
Wheels are available for Linux (x86_64 and aarch64, glibc and musl) and macOS (arm64 and
|
|
75
|
+
x86_64). They work on every Python version from 3.9 up.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
jsift/__init__.py,sha256=Ov7niDZ579b3xQ7pxD5LTOTu6uMvWadju9aFj2N-uso,5085
|
|
2
|
+
jsift/_lib.py,sha256=MnahNRK1L28XcxsSQW_0UTJWmGlyHeqk8mJrbVUTFuA,1546
|
|
3
|
+
jsift/libjsift.dylib,sha256=2dkEZkrdZW7I_lm8Ruh3OpFhvCzMw7FpyaKCfDrz45M,370128
|
|
4
|
+
jsift-2.1.0.dist-info/licenses/LICENSE,sha256=oQV9nnnTYFhqT6Djp-KMYptl92u_q59Kzlskzxt4Mvo,1072
|
|
5
|
+
jsift-2.1.0.dist-info/METADATA,sha256=iAYOt_k4r_9vmRm9AA7FTvKgvrFDEK15VY0jjSJxaik,2767
|
|
6
|
+
jsift-2.1.0.dist-info/WHEEL,sha256=4KNxTElBILrkFQeJpqVYjlB8fBsDGM4Jni4DJVcWhew,106
|
|
7
|
+
jsift-2.1.0.dist-info/top_level.txt,sha256=p68GOUk3SC68mBSJq20AQGtqC9NF_AOmW1cQr0k8VLI,6
|
|
8
|
+
jsift-2.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2005 Aïko Schürmann
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
jsift
|