class Memo::Queries::Postgres

Defined in:

memo/queries/postgres.cr

Class Method Summary

Instance Method Summary

Instance methods inherited from class Memo::Queries

clear_completed_queue : Int32 clear_completed_queue, clear_default_service : Nil clear_default_service, clear_query_cache(service_id : Int64) : Nil clear_query_cache, clear_queue : Int32 clear_queue, count_chunks_by_hash(hash : Bytes) : Int64 count_chunks_by_hash, count_chunks_for_service(service_id : Int64) : Int64 count_chunks_for_service, count_embeddings_for_service(service_id : Int64) : Int64 count_embeddings_for_service, count_files : Int64 count_files, count_query_cache(service_id : Int64) : Int64 count_query_cache, count_service_chunks(service_id : Int64) : Int64 count_service_chunks, count_service_embeddings(service_id : Int64) : Int64 count_service_embeddings, count_service_sources(service_id : Int64) : Int64 count_service_sources, count_services : Int64 count_services, count_vectors(service_id : Int64) : Int64 count_vectors, count_vocab(service_id : Int64) : Int64 count_vocab, db : DBHandle db, delete_chunks(hash : Bytes, source_id : Int64, source_type : String) : Int32
delete_chunks(hash : Bytes, source_id : Int64) : Int32
delete_chunks
, delete_chunks_by_hash(hash : Bytes) : Nil delete_chunks_by_hash, delete_embeddings_by_hash(hash : Bytes) : Nil delete_embeddings_by_hash, delete_embeddings_by_service(service_id : Int64) : Nil delete_embeddings_by_service, delete_file(source_id : Int64) : Int64 delete_file, delete_index_data(service_id : Int64) : Nil delete_index_data, delete_service(id : Int64) : Nil delete_service, delete_source_blob(source_type : String, external_id : Bytes) : Int64 delete_source_blob, delete_source_by_id(internal_id : Int64) : Int64 delete_source_by_id, delete_source_int(source_type : String, external_id : Int64) : Int64 delete_source_int, delete_source_text(source_type : String, external_id : String) : Int64 delete_source_text, delete_text(source_id : Int64) : Nil delete_text, delete_vector(embedding_id : Int64) : Nil delete_vector, delete_vocab(service_id : Int64) : Nil delete_vocab, embedding_ids_for_hash(hash : Bytes) : Array(Tuple(Int64, Int64)) embedding_ids_for_hash, embedding_ids_without_vectors(service_id : Int64) : Array(Int64) embedding_ids_without_vectors, enqueue(source_id : Int64, text : String, created_at : Int64) : Nil enqueue, fetch_search_results(rowids : Array(Int64), service_id : Int64, include_text : Bool) : Array(Tuple(UInt64, Int64, Bytes, String, Int64, Int64 | Nil, String | Nil, Bytes | Nil, Int64 | Nil, Int64 | Nil, String | Nil, Bytes | Nil, Int64 | Nil, Int64 | Nil, String | Nil, Bytes | Nil, Int32 | Nil, Int32, Int32, Int32, String | Nil)) fetch_search_results, filter_candidates(service_id : Int64, params : Array(DB::Any), where_clauses : Array(String), text_join : String, fts_join : String, candidate_ids : Array(Int64)) : Set(UInt64) filter_candidates, find_service_id(name : String) : Int64 | Nil find_service_id, find_source_blob(source_type : String, external_id : Bytes) : Int64 | Nil find_source_blob, find_source_blob_any_type(external_id : Bytes) : Int64 | Nil find_source_blob_any_type, find_source_int(source_type : String, external_id : Int64) : Int64 | Nil find_source_int, find_source_int_any_type(external_id : Int64) : Int64 | Nil find_source_int_any_type, find_source_text(source_type : String, external_id : String) : Int64 | Nil find_source_text, find_source_text_any_type(external_id : String) : Int64 | Nil find_source_text_any_type, for_connection(cnn : DB::Connection) : Queries for_connection, get_all_texts : Array(String) get_all_texts, get_chunk_hashes(source_id : Int64, source_type : String | Nil) : Array(Bytes) get_chunk_hashes, get_chunk_id(source_id : Int64, offset : Int32 | Nil) : Int64 get_chunk_id, get_chunks_for_reindex(source_type : String, service_id : Int64) : Array(Tuple(Int64, Int64 | Nil, String | Nil, Int64 | Nil, Int64 | Nil)) get_chunks_for_reindex, get_default_service : ServiceProvider::Info | Nil get_default_service, get_embedding_hashes_for_service(service_id : Int64) : Array(Bytes) get_embedding_hashes_for_service, get_embedding_rowid(hash : Bytes, service_id : Int64) : Int64 get_embedding_rowid, get_embedding_rowid?(hash : Bytes, service_id : Int64) : Int64 | Nil get_embedding_rowid?, get_existing_words(service_id : Int64, words : Array(String)) : Set(String) get_existing_words, get_file_by_hash(hash : Bytes) : Files::FileRecord | Nil get_file_by_hash, get_file_by_path(path : String) : Files::FileRecord | Nil get_file_by_path, get_file_by_source(source_id : Int64) : Files::FileRecord | Nil get_file_by_source, get_index_state(service_id : Int64) : Tuple(Int64, Bool) | Nil get_index_state, get_pending_queue(limit : Int32) : Array(Tuple(Int64, Int64, String)) get_pending_queue, get_query_cache(query : String, service_id : Int64) : Tuple(Bytes, Int32) | Nil get_query_cache, get_queue_attempts(id : Int64) : Int32 get_queue_attempts, get_queue_item(source_id : Int64) : Tuple(Int64, String) | Nil get_queue_item, get_recent_query_cache(service_id : Int64, limit : Int32) : Array(Tuple(String, Bytes, Int32)) get_recent_query_cache, get_service_by_format_model(format : String, model : String) : Tuple(Int64, String, String | Nil, String, Int32, Int32, Float64) | Nil get_service_by_format_model, get_service_by_name(name : String) : Tuple(Int64, String, String | Nil, String, Int32, Int32, Float64) | Nil get_service_by_name, get_service_info(id : Int64) : ServiceProvider::Info | Nil get_service_info, get_service_info_by_name(name : String) : ServiceProvider::Info | Nil get_service_info_by_name, get_service_tokens_per_byte(service_id : Int64) : Float64 | Nil get_service_tokens_per_byte, get_source_external(internal_id : Int64) : Tuple(String, Int64 | Nil, String | Nil, Bytes | Nil) | Nil get_source_external, get_text(source_id : Int64) : String | Nil get_text, get_text_hash(source_id : Int64) : Bytes | Nil get_text_hash, get_texts_for_reindex(source_type : String) : Array(Tuple(Int64, Int64 | Nil, Int64 | Nil, String)) get_texts_for_reindex, get_tokens_per_byte(service_id : Int64) : Float64 | Nil get_tokens_per_byte, get_vector(embedding_id : Int64) : Bytes | Nil get_vector, get_vocab(service_id : Int64, &block : DB::ResultSet -> ) : Nil get_vocab, increment_match_count(chunk_ids : Array(Int64)) : Nil increment_match_count, increment_read_count(chunk_ids : Array(Int64)) : Nil increment_read_count, index_changes_since(service_id : Int64, seq : Int64) : Array(Int64) index_changes_since, insert_chunk_ignore(hash : Bytes, source_id : Int64, source_type : String, pair_id : Int64 | Nil, parent_id : Int64 | Nil, offset : Int32 | Nil, size : Int32, created_at : Int64) : Int64 insert_chunk_ignore, insert_embedding_ignore(hash : Bytes, service_id : Int64, token_count : Int32, created_at : Int64) : Int64 insert_embedding_ignore, insert_service(name : String, format : String, base_url : String | Nil, model : String, dimensions : Int32, max_tokens : Int32, created_at : Int64) : Int64 insert_service, insert_service_full(name : String, format : String, base_url : String | Nil, model : String, dimensions : Int32, max_tokens : Int32, is_default : Int32, created_at : Int64) : Int64 insert_service_full, insert_source(source_type : String, created_at : Int64) : Int64 insert_source, insert_source_blob(source_type : String, external_id : Bytes, created_at : Int64) : Int64 insert_source_blob, insert_source_int(source_type : String, external_id : Int64, created_at : Int64) : Int64 insert_source_int, insert_source_text(source_type : String, external_id : String, created_at : Int64) : Int64 insert_source_text, list_files(limit : Int32, offset : Int32) : Array(Files::FileRecord) list_files, list_services : Array(ServiceProvider::Info) list_services, list_services_by_format(format : String) : Array(ServiceProvider::Info) list_services_by_format, list_sources(source_type : String, limit : Int32, offset : Int32) : Array(Tuple(Int64, Int64 | Nil, String | Nil, Bytes | Nil)) list_sources, load_embedding_rowids(service_id : Int64, source_type : String, external_ids : Array(Int64)) : Array(Tuple(Int64, Int64)) load_embedding_rowids, log_index_change(service_id : Int64, embedding_id : Int64) : Nil log_index_change, mark_queue_failed(id : Int64, error_message : String | Nil, attempts : Int32, processed_at : Int64) : Nil mark_queue_failed, mark_queue_item_success(id : Int64, processed_at : Int64, attempts : Int32) : Nil mark_queue_item_success, mark_queue_retry(id : Int64, attempts : Int32, error_message : String | Nil) : Nil mark_queue_retry, mark_queue_success(id : Int64, processed_at : Int64) : Nil mark_queue_success, max_index_log_seq(service_id : Int64) : Int64 max_index_log_seq, prune_index_log(service_id : Int64, through : Int64) : Nil prune_index_log, prune_query_cache(service_id : Int64, count : Int64) : Nil prune_query_cache, queue_stats : Tuple(Int64, Int64) queue_stats, search_filtered_rowids(service_id : Int64, params : Array(DB::Any), where_clauses : Array(String), text_join : String, fts_join : String) : Set(UInt64) search_filtered_rowids, service_exists?(id : Int64) : Bool service_exists?, set_default_service(id : Int64) : Nil set_default_service, set_index_pruned_through(service_id : Int64, seq : Int64) : Nil set_index_pruned_through, set_vectors_backfilled(service_id : Int64) : Nil set_vectors_backfilled, update_service(id : Int64, updates : Array(String), params : Array(DB::Any)) : Int64 update_service, update_tokens_per_byte(tokens_per_byte : Float64, service_id : Int64) : Nil update_tokens_per_byte, update_word_frequency(count : Int32, word : String, service_id : Int64) : Nil update_word_frequency, upsert_file(source_id : Int64, path : String, content_hash : Bytes, mtime : Int64, size : Int64, created_at : Int64) : Nil upsert_file, upsert_query_cache(query : String, service_id : Int64, embedding : Bytes, token_count : Int32, created_at : Int64) : Nil upsert_query_cache, upsert_text(source_id : Int64, content : String, content_hash : Bytes | Nil, created_at : Int64) : Nil upsert_text, upsert_vector(embedding_id : Int64, service_id : Int64, vector : Bytes) : Nil upsert_vector, upsert_vocab(word : String, service_id : Int64, embedding : Bytes, frequency : Int32, created_at : Int64) : Nil upsert_vocab, vectors_after(service_id : Int64, after_id : Int64, limit : Int32) : Array(Tuple(Int64, Bytes)) vectors_after

Constructor methods inherited from class Memo::Queries

for(db : DB::Database, connection_string : String) : Queries for, new(db : DBHandle) new

Class methods inherited from class Memo::Queries

register_pg(&factory : DB::Database -> Queries) register_pg

Class Method Detail

def self.number_placeholders(clauses : Array(String), after : Int32) : Tuple(Array(String), Int32) #

Number the clauses' ? placeholders $N, continuing after after. Returns the clauses and the last number used. Skips quoted strings ('...', "...", $tag$...$tag$) and comments, so a literal ? in caller SQL (sql_where) isn't taken for a placeholder. That is also why the jsonb ?, ?| and ?& operators can't appear in sql_where on Postgres: use jsonb_exists, jsonb_exists_any and jsonb_exists_all instead.


[View source]

Instance Method Detail

def clear_completed_queue : Int32 #

[View source]
def clear_default_service : Nil #

[View source]
def clear_query_cache(service_id : Int64) : Nil #

[View source]
def clear_queue : Int32 #

[View source]
def count_chunks_by_hash(hash : Bytes) : Int64 #

[View source]
def count_chunks_for_service(service_id : Int64) : Int64 #

[View source]
def count_embeddings_for_service(service_id : Int64) : Int64 #

[View source]
def count_files : Int64 #

[View source]
def count_query_cache(service_id : Int64) : Int64 #

[View source]
def count_service_chunks(service_id : Int64) : Int64 #

[View source]
def count_service_embeddings(service_id : Int64) : Int64 #

[View source]
def count_service_sources(service_id : Int64) : Int64 #

[View source]
def count_services : Int64 #

[View source]
def count_vectors(service_id : Int64) : Int64 #

[View source]
def count_vocab(service_id : Int64) : Int64 #

[View source]
def delete_chunks(hash : Bytes, source_id : Int64, source_type : String) : Int32 #

[View source]
def delete_chunks(hash : Bytes, source_id : Int64) : Int32 #

[View source]
def delete_chunks_by_hash(hash : Bytes) : Nil #

[View source]
def delete_embeddings_by_hash(hash : Bytes) : Nil #

[View source]
def delete_embeddings_by_service(service_id : Int64) : Nil #

[View source]
def delete_file(source_id : Int64) : Int64 #

[View source]
def delete_index_data(service_id : Int64) : Nil #
Description copied from class Memo::Queries

Remove a service's vectors, journal entries and journal state


[View source]
def delete_service(id : Int64) : Nil #

[View source]
def delete_source_blob(source_type : String, external_id : Bytes) : Int64 #

[View source]
def delete_source_by_id(internal_id : Int64) : Int64 #

[View source]
def delete_source_int(source_type : String, external_id : Int64) : Int64 #

[View source]
def delete_source_text(source_type : String, external_id : String) : Int64 #

[View source]
def delete_text(source_id : Int64) : Nil #

[View source]
def delete_vector(embedding_id : Int64) : Nil #

[View source]
def delete_vocab(service_id : Int64) : Nil #

[View source]
def embedding_ids_for_hash(hash : Bytes) : Array(Tuple(Int64, Int64)) #
Description copied from class Memo::Queries

{embedding id, service id} for every service's embedding of a hash


[View source]
def embedding_ids_without_vectors(service_id : Int64) : Array(Int64) #

[View source]
def enqueue(source_id : Int64, text : String, created_at : Int64) : Nil #

[View source]
def fetch_search_results(rowids : Array(Int64), service_id : Int64, include_text : Bool) : Array(Tuple(UInt64, Int64, Bytes, String, Int64, Int64 | Nil, String | Nil, Bytes | Nil, Int64 | Nil, Int64 | Nil, String | Nil, Bytes | Nil, Int64 | Nil, Int64 | Nil, String | Nil, Bytes | Nil, Int32 | Nil, Int32, Int32, Int32, String | Nil)) #

[View source]
def filter_candidates(service_id : Int64, params : Array(DB::Any), where_clauses : Array(String), text_join : String, fts_join : String, candidate_ids : Array(Int64)) : Set(UInt64) #
Description copied from class Memo::Queries

Which of candidate_ids pass the filter clauses (looked up by id, so the cost depends on the number of candidates, not the collection)


[View source]
def find_service_id(name : String) : Int64 | Nil #

[View source]
def find_source_blob(source_type : String, external_id : Bytes) : Int64 | Nil #

[View source]
def find_source_blob_any_type(external_id : Bytes) : Int64 | Nil #

[View source]
def find_source_int(source_type : String, external_id : Int64) : Int64 | Nil #

[View source]
def find_source_int_any_type(external_id : Int64) : Int64 | Nil #

[View source]
def find_source_text(source_type : String, external_id : String) : Int64 | Nil #

[View source]
def find_source_text_any_type(external_id : String) : Int64 | Nil #

[View source]
def for_connection(cnn : DB::Connection) : Queries #
Description copied from class Memo::Queries

Same queries, run on one connection (for use inside a transaction).


[View source]
def get_all_texts : Array(String) #

[View source]
def get_chunk_hashes(source_id : Int64, source_type : String | Nil) : Array(Bytes) #

[View source]
def get_chunk_id(source_id : Int64, offset : Int32 | Nil) : Int64 #

[View source]
def get_chunks_for_reindex(source_type : String, service_id : Int64) : Array(Tuple(Int64, Int64 | Nil, String | Nil, Int64 | Nil, Int64 | Nil)) #

[View source]
def get_default_service : ServiceProvider::Info | Nil #

[View source]
def get_embedding_hashes_for_service(service_id : Int64) : Array(Bytes) #

[View source]
def get_embedding_rowid(hash : Bytes, service_id : Int64) : Int64 #

[View source]
def get_embedding_rowid?(hash : Bytes, service_id : Int64) : Int64 | Nil #

[View source]
def get_existing_words(service_id : Int64, words : Array(String)) : Set(String) #

[View source]
def get_file_by_hash(hash : Bytes) : Files::FileRecord | Nil #

[View source]
def get_file_by_path(path : String) : Files::FileRecord | Nil #

[View source]
def get_file_by_source(source_id : Int64) : Files::FileRecord | Nil #

[View source]
def get_index_state(service_id : Int64) : Tuple(Int64, Bool) | Nil #
Description copied from class Memo::Queries

{pruned_through, vectors_backfilled}


[View source]
def get_pending_queue(limit : Int32) : Array(Tuple(Int64, Int64, String)) #

[View source]
def get_query_cache(query : String, service_id : Int64) : Tuple(Bytes, Int32) | Nil #

[View source]
def get_queue_attempts(id : Int64) : Int32 #

[View source]
def get_queue_item(source_id : Int64) : Tuple(Int64, String) | Nil #

[View source]
def get_recent_query_cache(service_id : Int64, limit : Int32) : Array(Tuple(String, Bytes, Int32)) #

[View source]
def get_service_by_format_model(format : String, model : String) : Tuple(Int64, String, String | Nil, String, Int32, Int32, Float64) | Nil #

[View source]
def get_service_by_name(name : String) : Tuple(Int64, String, String | Nil, String, Int32, Int32, Float64) | Nil #

[View source]
def get_service_info(id : Int64) : ServiceProvider::Info | Nil #

[View source]
def get_service_info_by_name(name : String) : ServiceProvider::Info | Nil #

[View source]
def get_service_tokens_per_byte(service_id : Int64) : Float64 | Nil #

[View source]
def get_source_external(internal_id : Int64) : Tuple(String, Int64 | Nil, String | Nil, Bytes | Nil) | Nil #

[View source]
def get_text(source_id : Int64) : String | Nil #

[View source]
def get_text_hash(source_id : Int64) : Bytes | Nil #

[View source]
def get_texts_for_reindex(source_type : String) : Array(Tuple(Int64, Int64 | Nil, Int64 | Nil, String)) #

[View source]
def get_tokens_per_byte(service_id : Int64) : Float64 | Nil #

[View source]
def get_vector(embedding_id : Int64) : Bytes | Nil #

[View source]
def get_vocab(service_id : Int64, &block : DB::ResultSet -> ) : Nil #

[View source]
def increment_match_count(chunk_ids : Array(Int64)) : Nil #

[View source]
def increment_read_count(chunk_ids : Array(Int64)) : Nil #

[View source]
def index_changes_since(service_id : Int64, seq : Int64) : Array(Int64) #
Description copied from class Memo::Queries

Distinct embedding ids logged after seq


[View source]
def insert_chunk_ignore(hash : Bytes, source_id : Int64, source_type : String, pair_id : Int64 | Nil, parent_id : Int64 | Nil, offset : Int32 | Nil, size : Int32, created_at : Int64) : Int64 #

[View source]
def insert_embedding_ignore(hash : Bytes, service_id : Int64, token_count : Int32, created_at : Int64) : Int64 #

[View source]
def insert_service(name : String, format : String, base_url : String | Nil, model : String, dimensions : Int32, max_tokens : Int32, created_at : Int64) : Int64 #

[View source]
def insert_service_full(name : String, format : String, base_url : String | Nil, model : String, dimensions : Int32, max_tokens : Int32, is_default : Int32, created_at : Int64) : Int64 #
Description copied from class Memo::Queries

Insert service with is_default and return ID (for ServiceProvider.create)


[View source]
def insert_source(source_type : String, created_at : Int64) : Int64 #

[View source]
def insert_source_blob(source_type : String, external_id : Bytes, created_at : Int64) : Int64 #

[View source]
def insert_source_int(source_type : String, external_id : Int64, created_at : Int64) : Int64 #

[View source]
def insert_source_text(source_type : String, external_id : String, created_at : Int64) : Int64 #

[View source]
def list_files(limit : Int32, offset : Int32) : Array(Files::FileRecord) #

[View source]
def list_services : Array(ServiceProvider::Info) #

[View source]
def list_services_by_format(format : String) : Array(ServiceProvider::Info) #

[View source]
def list_sources(source_type : String, limit : Int32, offset : Int32) : Array(Tuple(Int64, Int64 | Nil, String | Nil, Bytes | Nil)) #

[View source]
def load_embedding_rowids(service_id : Int64, source_type : String, external_ids : Array(Int64)) : Array(Tuple(Int64, Int64)) #

[View source]
def log_index_change(service_id : Int64, embedding_id : Int64) : Nil #

[View source]
def mark_queue_failed(id : Int64, error_message : String | Nil, attempts : Int32, processed_at : Int64) : Nil #

[View source]
def mark_queue_item_success(id : Int64, processed_at : Int64, attempts : Int32) : Nil #

[View source]
def mark_queue_retry(id : Int64, attempts : Int32, error_message : String | Nil) : Nil #

[View source]
def mark_queue_success(id : Int64, processed_at : Int64) : Nil #

[View source]
def max_index_log_seq(service_id : Int64) : Int64 #

[View source]
def prune_index_log(service_id : Int64, through : Int64) : Nil #

[View source]
def prune_query_cache(service_id : Int64, count : Int64) : Nil #

[View source]
def queue_stats : Tuple(Int64, Int64) #

[View source]
def search_filtered_rowids(service_id : Int64, params : Array(DB::Any), where_clauses : Array(String), text_join : String, fts_join : String) : Set(UInt64) #

[View source]
def service_exists?(id : Int64) : Bool #

[View source]
def set_default_service(id : Int64) : Nil #

[View source]
def set_index_pruned_through(service_id : Int64, seq : Int64) : Nil #

[View source]
def set_vectors_backfilled(service_id : Int64) : Nil #

[View source]
def update_service(id : Int64, updates : Array(String), params : Array(DB::Any)) : Int64 #

[View source]
def update_tokens_per_byte(tokens_per_byte : Float64, service_id : Int64) : Nil #

[View source]
def update_word_frequency(count : Int32, word : String, service_id : Int64) : Nil #

[View source]
def upsert_file(source_id : Int64, path : String, content_hash : Bytes, mtime : Int64, size : Int64, created_at : Int64) : Nil #

[View source]
def upsert_query_cache(query : String, service_id : Int64, embedding : Bytes, token_count : Int32, created_at : Int64) : Nil #

[View source]
def upsert_text(source_id : Int64, content : String, content_hash : Bytes | Nil, created_at : Int64) : Nil #

[View source]
def upsert_vector(embedding_id : Int64, service_id : Int64, vector : Bytes) : Nil #

[View source]
def upsert_vocab(word : String, service_id : Int64, embedding : Bytes, frequency : Int32, created_at : Int64) : Nil #

[View source]
def vectors_after(service_id : Int64, after_id : Int64, limit : Int32) : Array(Tuple(Int64, Bytes)) #
Description copied from class Memo::Queries

Up to limit {embedding id, vector} pairs with ids above after_id, in id order


[View source]