search fix to new architecture

This commit is contained in:
Priec
2026-08-13 21:23:49 +02:00
parent 8662b00ccf
commit 810c8ffd08
8 changed files with 1069 additions and 58 deletions

View File

@@ -4,6 +4,11 @@ package komp_ac.search;
service Searcher {
rpc Search(SearchRequest) returns (SearchResponse);
rpc Count(SearchRequest) returns (SearchCountResponse);
}
message SearchCountResponse {
uint64 count = 1;
}
enum MatchMode {
@@ -26,6 +31,16 @@ message SearchRequest {
optional uint32 limit = 5;
optional uint32 offset = 6;
optional SearchOrder order = 7;
// Current rows are the default so pickers and relationship counts never
// select historical records. Callers may explicitly search immutable row
// archives as well.
SearchVersionScope version_scope = 8;
}
enum SearchVersionScope {
SEARCH_VERSION_SCOPE_CURRENT = 0;
SEARCH_VERSION_SCOPE_ARCHIVED = 1;
SEARCH_VERSION_SCOPE_ALL = 2;
}
message SearchOrder {
@@ -48,6 +63,10 @@ message SearchResponse {
repeated string row_display_values = 5;
repeated string row_display_columns = 6;
optional uint64 position = 7;
// Version 0 means the source predates managed row versioning. New managed
// rows always return a positive version.
int64 version_number = 8;
bool archived = 9;
}
repeated Hit hits = 1;
}

Binary file not shown.

View File

@@ -1,4 +1,9 @@
// This file is @generated by prost-build.
#[derive(Clone, Copy, PartialEq, Eq, Hash, ::prost::Message)]
pub struct SearchCountResponse {
#[prost(uint64, tag = "1")]
pub count: u64,
}
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct ColumnConstraint {
#[prost(string, tag = "1")]
@@ -24,6 +29,11 @@ pub struct SearchRequest {
pub offset: ::core::option::Option<u32>,
#[prost(message, optional, tag = "7")]
pub order: ::core::option::Option<SearchOrder>,
/// Current rows are the default so pickers and relationship counts never
/// select historical records. Callers may explicitly search immutable row
/// archives as well.
#[prost(enumeration = "SearchVersionScope", tag = "8")]
pub version_scope: i32,
}
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct SearchOrder {
@@ -59,6 +69,12 @@ pub mod search_response {
>,
#[prost(uint64, optional, tag = "7")]
pub position: ::core::option::Option<u64>,
/// Version 0 means the source predates managed row versioning. New managed
/// rows always return a positive version.
#[prost(int64, tag = "8")]
pub version_number: i64,
#[prost(bool, tag = "9")]
pub archived: bool,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
@@ -92,6 +108,35 @@ impl MatchMode {
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum SearchVersionScope {
Current = 0,
Archived = 1,
All = 2,
}
impl SearchVersionScope {
/// String value of the enum field names used in the ProtoBuf definition.
///
/// The values are not transformed in any way and thus are considered stable
/// (if the ProtoBuf definition does not change) and safe for programmatic use.
pub fn as_str_name(&self) -> &'static str {
match self {
Self::Current => "SEARCH_VERSION_SCOPE_CURRENT",
Self::Archived => "SEARCH_VERSION_SCOPE_ARCHIVED",
Self::All => "SEARCH_VERSION_SCOPE_ALL",
}
}
/// Creates an enum from field names used in the ProtoBuf definition.
pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
match value {
"SEARCH_VERSION_SCOPE_CURRENT" => Some(Self::Current),
"SEARCH_VERSION_SCOPE_ARCHIVED" => Some(Self::Archived),
"SEARCH_VERSION_SCOPE_ALL" => Some(Self::All),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
#[repr(i32)]
pub enum SearchOrderDirection {
Unspecified = 0,
Asc = 1,
@@ -231,6 +276,30 @@ pub mod searcher_client {
.insert(GrpcMethod::new("komp_ac.search.Searcher", "Search"));
self.inner.unary(req, path, codec).await
}
pub async fn count(
&mut self,
request: impl tonic::IntoRequest<super::SearchRequest>,
) -> std::result::Result<
tonic::Response<super::SearchCountResponse>,
tonic::Status,
> {
self.inner
.ready()
.await
.map_err(|e| {
tonic::Status::unknown(
format!("Service was not ready: {}", e.into()),
)
})?;
let codec = tonic_prost::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/komp_ac.search.Searcher/Count",
);
let mut req = request.into_request();
req.extensions_mut()
.insert(GrpcMethod::new("komp_ac.search.Searcher", "Count"));
self.inner.unary(req, path, codec).await
}
}
}
/// Generated server implementations.
@@ -250,6 +319,13 @@ pub mod searcher_server {
&self,
request: tonic::Request<super::SearchRequest>,
) -> std::result::Result<tonic::Response<super::SearchResponse>, tonic::Status>;
async fn count(
&self,
request: tonic::Request<super::SearchRequest>,
) -> std::result::Result<
tonic::Response<super::SearchCountResponse>,
tonic::Status,
>;
}
#[derive(Debug)]
pub struct SearcherServer<T> {
@@ -370,6 +446,49 @@ pub mod searcher_server {
};
Box::pin(fut)
}
"/komp_ac.search.Searcher/Count" => {
#[allow(non_camel_case_types)]
struct CountSvc<T: Searcher>(pub Arc<T>);
impl<T: Searcher> tonic::server::UnaryService<super::SearchRequest>
for CountSvc<T> {
type Response = super::SearchCountResponse;
type Future = BoxFuture<
tonic::Response<Self::Response>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::SearchRequest>,
) -> Self::Future {
let inner = Arc::clone(&self.0);
let fut = async move {
<T as Searcher>::count(&inner, request).await
};
Box::pin(fut)
}
}
let accept_compression_encodings = self.accept_compression_encodings;
let send_compression_encodings = self.send_compression_encodings;
let max_decoding_message_size = self.max_decoding_message_size;
let max_encoding_message_size = self.max_encoding_message_size;
let inner = self.inner.clone();
let fut = async move {
let method = CountSvc(inner);
let codec = tonic_prost::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
accept_compression_encodings,
send_compression_encodings,
)
.apply_max_message_size_config(
max_decoding_message_size,
max_encoding_message_size,
);
let res = grpc.unary(method, req).await;
Ok(res)
};
Box::pin(fut)
}
_ => {
Box::pin(async move {
let mut response = http::Response::new(

View File

@@ -18,6 +18,17 @@ pub const F_DATA_WORD: &str = "data_word";
pub const F_DATA_NGRAM: &str = "data_ngram";
pub const F_DATA_EXACT: &str = "data_exact";
pub const JOURNAL_TABLE_NAME: &str = "general_ledger";
pub const ARCHIVED_ROW_KEY_PREFIX: &str = "__archive__:";
pub const SEARCH_INDEX_FORMAT_DIRECTORY: &str = "v2";
/// Root for the current on-disk index format. The format component prevents
/// an executable with new projection semantics from opening old documents.
pub fn search_index_root() -> PathBuf {
std::env::var_os("TANTIVY_INDEX_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from("./tantivy_indexes"))
.join(SEARCH_INDEX_FORMAT_DIRECTORY)
}
pub const TOK_WORD: &str = "kw_word";
pub const TOK_NGRAM: &str = "kw_ngram";
@@ -33,6 +44,30 @@ pub fn search_row_key(table_name: &str, row_id: i64) -> String {
format!("{}:{}", table_name, row_id)
}
/// Returns the unique index key for an immutable archived row version.
pub fn archived_search_row_key(
table_definition_id: i64,
row_id: i64,
version_number: i64,
) -> String {
format!(
"{ARCHIVED_ROW_KEY_PREFIX}{table_definition_id}:{row_id}:{version_number}"
)
}
/// Decodes an archived index key into (table definition, row, version).
pub fn parse_archived_search_row_key(row_key: &str) -> Option<(i64, i64, i64)> {
let encoded = row_key.strip_prefix(ARCHIVED_ROW_KEY_PREFIX)?;
let mut parts = encoded.split(':');
let table_definition_id = parts.next()?.parse().ok()?;
let row_id = parts.next()?.parse().ok()?;
let version_number = parts.next()?.parse().ok()?;
if parts.next().is_some() {
return None;
}
Some((table_definition_id, row_id, version_number))
}
/// Normalizes user-entered values for exact-mode terms.
pub fn normalize_exact(input: &str) -> String {
let trimmed = input.trim();
@@ -184,6 +219,19 @@ impl SchemaFields {
}
}
#[cfg(test)]
mod tests {
use super::{archived_search_row_key, parse_archived_search_row_key};
#[test]
fn archived_row_key_round_trips_stable_identity() {
let key = archived_search_row_key(42, 7, 3);
assert_eq!(parse_archived_search_row_key(&key), Some((42, 7, 3)));
assert_eq!(parse_archived_search_row_key("customers:7"), None);
assert_eq!(parse_archived_search_row_key("__archive__:42:7"), None);
}
}
fn get_field(schema: &Schema, name: &str) -> tantivy::Result<Field> {
schema.get_field(name).map_err(|e| {
tantivy::TantivyError::SchemaError(format!("schema is missing field '{name}': {e}"))