morph 0.1.0
Asynchronous bridge between GUI thread and business models
Loading...
Searching...
No Matches
filter.hpp File Reference

Filter specs as data, compiled once and evaluated column at a time. More...

#include <algorithm>
#include <array>
#include <core/async/StopToken.hpp>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <map>
#include <morph/table/data_source.hpp>
#include <morph/table/sort.hpp>
#include <optional>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

Go to the source code of this file.

Classes

struct  morph::table::FilterEntry
 One filter condition. Exactly one operator member is set. More...
struct  morph::table::ColumnFilter
 The filter on one column. More...
struct  morph::table::GroupFilter
 A filter over several columns: a row matches when the entries match on any of them, which is what a quick-search box needs. More...
struct  morph::table::FilterSpec
 A table's whole filter. More...
class  morph::table::CompiledFilter
 A filter compiled against a table's columns: every value parsed. More...
struct  morph::table::CompiledFilter::Entry
 One compiled condition. More...
struct  morph::table::CompiledFilter::Column
 One column's compiled filter. More...
struct  morph::table::CompiledFilter::Group
 One compiled group: per column, the entries that apply to it. More...
struct  morph::table::detail::RawEntry
 The operator an entry sets and its operand text. More...
class  morph::table::FilterRun
 Evaluates a compiled filter over a candidate list, column at a time, in steps that yield at a deadline or a stop request. More...

Enumerations

enum class  morph::table::Combine : std::uint8_t { Any , All }
 How a column's include entries combine. More...

Functions

std::expected< RawEntry, TableError > morph::table::detail::readEntry (FilterEntry const &entry, std::string_view column)
 Reads which operator an entry sets.
bool morph::table::detail::operatorDefined (ColumnKind kind, CompiledFilter::Op op)
 Whether a column kind defines an operator.
bool morph::table::detail::parseOperand (ColumnInfo const &info, Services const &services, std::string_view text, std::size_t slot, CompiledFilter::Entry &entry)
 Parses one operand for the column's kind into a slot of entry.
std::expected< CompiledFilter::Entry, TableError > morph::table::detail::compileEntry (FilterEntry const &entry, ColumnInfo const &info, Services const &services)
 Compiles one entry for one column.
std::optional< std::size_t > morph::table::detail::findColumn (std::span< ColumnInfo const > columns, std::string_view columnId)
 The index of the column with id columnId.
TableError morph::table::detail::unknownColumn (std::string const &columnId)
 An UnknownColumn error.
std::expected< void, TableError > morph::table::detail::compileEntries (std::span< FilterEntry const > entries, ColumnInfo const &info, Services const &services, std::vector< CompiledFilter::Entry > &out)
 Compiles entries for one column, appending them to out.
std::expected< CompiledFilter::Column, TableError > morph::table::detail::compileColumnFilter (std::string const &columnId, ColumnFilter const &filter, std::span< ColumnInfo const > columns, Services const &services)
 Compiles the filter on one column.
std::expected< CompiledFilter::Group, TableError > morph::table::detail::compileGroup (GroupFilter const &group, std::span< ColumnInfo const > columns, Services const &services)
 Compiles a group: per column, the entries that apply to it.
std::expected< CompiledFilter, TableError > morph::table::compileFilter (FilterSpec const &spec, std::span< ColumnInfo const > columns, Services const &services)
 Compiles spec against columns.

Detailed Description

Filter specs as data, compiled once and evaluated column at a time.

A FilterSpec is plain data in the JSON shape of spec 7 ยง6, so a document can hold it, a user can save it and a server can receive it. Compiling it against a table's columns parses every value once for its column's kind; evaluating it narrows a candidate list one column at a time.

Semantics, normative for every client and server:

  • the include entries of a column combine by combine (any, the default, or all); a match on an exclude entry rejects the row; columns, and groups, combine with each other by AND;
  • a group matches a row when its entries match on any of its columns;
  • no operator but isEmpty and notEmpty matches an empty or invalid cell;
  • text operators compare through the collator's primary-strength fold unless the entry sets caseSensitive; % and _ are ordinary characters.

See docs/spec/table/engine.md.

Definition in file filter.hpp.

Enumeration Type Documentation

◆ Combine

enum class morph::table::Combine : std::uint8_t
strong

How a column's include entries combine.

Enumerator
Any 

A row matches when any entry matches.

All 

A row matches when every entry matches.

Definition at line 79 of file filter.hpp.

Function Documentation

◆ readEntry()

std::expected< RawEntry, TableError > morph::table::detail::readEntry ( FilterEntry const & entry,
std::string_view column )
inlinenodiscard

Reads which operator an entry sets.

Parameters
entryThe entry.
columnThe column, for the error.
Returns
The operator and operands, or InvalidSpec when the entry sets none or several.

Definition at line 334 of file filter.hpp.

◆ operatorDefined()

bool morph::table::detail::operatorDefined ( ColumnKind kind,
CompiledFilter::Op op )
inlinenodiscard

Whether a column kind defines an operator.

Parameters
kindThe column's kind.
opThe operator.
Returns
true when the operator applies to the kind.

Definition at line 379 of file filter.hpp.

◆ parseOperand()

bool morph::table::detail::parseOperand ( ColumnInfo const & info,
Services const & services,
std::string_view text,
std::size_t slot,
CompiledFilter::Entry & entry )
inlinenodiscard

Parses one operand for the column's kind into a slot of entry.

Parameters
infoThe column.
servicesCollator (text is folded with it) and date parser.
textThe operand text.
slot0, or 1 for the upper bound of between.
entryReceives the parsed operand.
Returns
false when the text does not parse for the kind.

Definition at line 411 of file filter.hpp.

◆ compileEntry()

std::expected< CompiledFilter::Entry, TableError > morph::table::detail::compileEntry ( FilterEntry const & entry,
ColumnInfo const & info,
Services const & services )
inlinenodiscard

Compiles one entry for one column.

Parameters
entryThe entry.
infoThe column.
servicesCollator and date parser.
Returns
The compiled entry, or the error naming the column.

Definition at line 464 of file filter.hpp.

◆ findColumn()

std::optional< std::size_t > morph::table::detail::findColumn ( std::span< ColumnInfo const > columns,
std::string_view columnId )
inlinenodiscard

The index of the column with id columnId.

Parameters
columnsThe table's columns.
columnIdA column id.
Returns
Its index, or nothing.

Definition at line 498 of file filter.hpp.

◆ unknownColumn()

TableError morph::table::detail::unknownColumn ( std::string const & columnId)
inlinenodiscard

An UnknownColumn error.

Parameters
columnIdThe column id that names no column.
Returns
The error.

Definition at line 511 of file filter.hpp.

◆ compileEntries()

std::expected< void, TableError > morph::table::detail::compileEntries ( std::span< FilterEntry const > entries,
ColumnInfo const & info,
Services const & services,
std::vector< CompiledFilter::Entry > & out )
inlinenodiscard

Compiles entries for one column, appending them to out.

Parameters
entriesThe entries.
infoThe column.
servicesCollator and date parser.
outReceives the compiled entries.
Returns
Nothing, or the first entry's error.

Definition at line 521 of file filter.hpp.

◆ compileColumnFilter()

std::expected< CompiledFilter::Column, TableError > morph::table::detail::compileColumnFilter ( std::string const & columnId,
ColumnFilter const & filter,
std::span< ColumnInfo const > columns,
Services const & services )
inlinenodiscard

Compiles the filter on one column.

Parameters
columnIdThe column's id.
filterIts filter.
columnsThe table's columns.
servicesCollator and date parser.
Returns
The compiled filter, or the first error.

Definition at line 540 of file filter.hpp.

◆ compileGroup()

std::expected< CompiledFilter::Group, TableError > morph::table::detail::compileGroup ( GroupFilter const & group,
std::span< ColumnInfo const > columns,
Services const & services )
inlinenodiscard

Compiles a group: per column, the entries that apply to it.

Parameters
groupThe group.
columnsThe table's columns.
servicesCollator and date parser.
Returns
The compiled group, or an error when a column is unknown, an entry is malformed, or an entry applies to none of the group's columns.

Definition at line 563 of file filter.hpp.

◆ compileFilter()

std::expected< CompiledFilter, TableError > morph::table::compileFilter ( FilterSpec const & spec,
std::span< ColumnInfo const > columns,
Services const & services )
inlinenodiscard

Compiles spec against columns.

In a group, an entry whose operator a column's kind does not define, or whose value does not parse for it, does not apply to that column; it is an error only when it applies to none of the group's columns. A quick-search text therefore searches the text columns of a group that also holds numbers.

Parameters
specThe filter.
columnsThe table's columns.
servicesThe collator (values of text filters are folded with it) and date parser; null members mean their defaults.
Returns
The compiled filter, or the first error, naming its column.

Definition at line 614 of file filter.hpp.