Label Filter Lib
A Rust library for parsing and evaluating filters against JSON meta data.
Usage
use json;
use ;
// Create a JSON label
let label = json!;
// Create a filter that matches labels with a=1 AND b>1 AND specs.cpu="i7" AND tags contains "blue"
let filter = json!;
// Parse the filter into an AST
let ast = match parse_query_filter ;
// Evaluate the filter against the label
let matches = eval_query_expr;
assert!;
Examples
Parse AST and output it as simple query expression
cargo run --example print_query
Process and evaluate JSON line formatted files with:
cargo run --example jsonl_reader_example
Convert old txt based format into json based file
converter <base_input_file> <query_input_file> <base_output_file> <query_output_file>
cargo run --example converter ..\tests\data\disk_index_search\data.256.label ..\tests\data\disk_index_search\query.128.label ..\tests\data\disk_index_search\data.256.label.jsonl ..\tests\data\disk_index_search\query.128.label.jsonl
Running Benchmarks
The project includes a comprehensive benchmarking suite that can be run with:
Benchmarks are organized in modules under the benches/benchmarks/ directory:
parser_bench.rs: Evaluates the performance of parsingevaluator_bench.rs: Evaluates the query evaluation performance
Implementation Details
Architecture Overview
The label-filter library is built around three core components:
- Abstract Syntax Tree (AST): A hierarchical representation of query filters
- Parser: Converts JSON query filters to the AST representation
- Evaluator: Evaluates the AST against JSON labels
Abstract Syntax Tree (AST)
The AST is defined in ast.rs and consists of:
The CompareOp enum uses type-safe representations for different comparison operators:
The type-safe design ensures that each operator only accepts appropriate value types, enforcing correctness at compile time.
Parser
The parser (parser.rs) converts JSON filter specifications into the AST. Key features:
- Support for logical operators (
$and,$or,$not) - Support for comparison operators (
$eq,$ne,$lt,$lte,$gt,$gte,$in,$nin) - Automatic handling of implicit
$andfor multiple field conditions - Support for dot notation to access nested fields (
user.profile.age) - Enforced nesting depth limit
- Type checking for operators (e.g., numeric operators require numeric values)
Evaluator
The evaluator (evaluator.rs) applies the AST against JSON labels to determine if they match:
- Recursive traversal of the AST
- Type-aware comparison operations
- Support for array field values with
$inand$ninoperators
Visitor Pattern
The library implements the Visitor pattern to enable extensible operations on the AST:
ASTVisitortrait defines the interface for visitorsPrintVisitorimplementation converts AST to human-readable format- Display implementation for easy debugging and logging