mirror of
https://github.com/typesense/typesense.git
synced 2025-05-19 13:12:22 +08:00
108 lines
3.4 KiB
C++
108 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <vector>
|
|
#include "posting_list.h"
|
|
|
|
#define IS_COMPACT_POSTING(x) (((uintptr_t)x & 1))
|
|
#define SET_COMPACT_POSTING(x) ((void*)((uintptr_t)x | 1))
|
|
#define RAW_POSTING_PTR(x) ((void*)((uintptr_t)x & ~1))
|
|
#define COMPACT_POSTING_PTR(x) ((compact_posting_list_t*)((uintptr_t)x & ~1))
|
|
|
|
struct compact_posting_list_t {
|
|
// structured to get 4 byte alignment for `id_offsets`
|
|
uint8_t length = 0;
|
|
uint8_t ids_length = 0;
|
|
uint16_t capacity = 0;
|
|
|
|
// format: num_offsets, offset1,..,offsetn, id1 | num_offsets, offset1,..,offsetn, id2
|
|
uint32_t id_offsets[];
|
|
|
|
static compact_posting_list_t* create(uint32_t num_ids, const uint32_t* ids, const uint32_t* offset_index,
|
|
uint32_t num_offsets, uint32_t* offsets);
|
|
|
|
posting_list_t* to_full_posting_list();
|
|
|
|
bool contains(uint32_t id);
|
|
|
|
int64_t upsert(uint32_t id, const std::vector<uint32_t>& offsets);
|
|
int64_t upsert(uint32_t id, const uint32_t* offsets, uint32_t num_offsets);
|
|
|
|
void erase(uint32_t id);
|
|
|
|
uint32_t first_id();
|
|
uint32_t last_id();
|
|
|
|
uint32_t num_ids() const;
|
|
|
|
bool contains_atleast_one(const uint32_t* target_ids, size_t target_ids_size);
|
|
};
|
|
|
|
class posting_t {
|
|
private:
|
|
static constexpr size_t COMPACT_LIST_THRESHOLD_LENGTH = 64;
|
|
|
|
static void to_expanded_plists(const std::vector<void*>& raw_posting_lists, std::vector<posting_list_t*>& plists,
|
|
std::vector<uint32_t>& expanded_plist_indices);
|
|
|
|
public:
|
|
|
|
struct block_intersector_t {
|
|
std::vector<posting_list_t::iterator_t> its;
|
|
std::vector<posting_list_t*> plists;
|
|
std::vector<uint32_t> expanded_plist_indices;
|
|
posting_list_t::result_iter_state_t& iter_state;
|
|
|
|
block_intersector_t(const std::vector<void*>& raw_posting_lists,
|
|
posting_list_t::result_iter_state_t& iter_state):
|
|
iter_state(iter_state) {
|
|
|
|
to_expanded_plists(raw_posting_lists, plists, expanded_plist_indices);
|
|
|
|
its.reserve(plists.size());
|
|
for(const auto& posting_list: plists) {
|
|
its.push_back(posting_list->new_iterator());
|
|
}
|
|
}
|
|
|
|
~block_intersector_t() {
|
|
for(uint32_t expanded_plist_index: expanded_plist_indices) {
|
|
delete plists[expanded_plist_index];
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
bool intersect(T func);
|
|
};
|
|
|
|
static void upsert(void*& obj, uint32_t id, const std::vector<uint32_t>& offsets);
|
|
|
|
static void erase(void*& obj, uint32_t id);
|
|
|
|
static void destroy_list(void*& obj);
|
|
|
|
static uint32_t num_ids(const void* obj);
|
|
|
|
static uint32_t first_id(const void* obj);
|
|
|
|
static bool contains(const void* obj, uint32_t id);
|
|
|
|
static bool contains_atleast_one(const void* obj, const uint32_t* target_ids, size_t target_ids_size);
|
|
|
|
static void merge(const std::vector<void*>& posting_lists, std::vector<uint32_t>& result_ids);
|
|
|
|
static void intersect(const std::vector<void*>& posting_lists, std::vector<uint32_t>& result_ids);
|
|
|
|
static void get_array_token_positions(
|
|
uint32_t id,
|
|
const std::vector<void*>& posting_lists,
|
|
std::unordered_map<size_t, std::vector<token_positions_t>>& array_token_positions
|
|
);
|
|
};
|
|
|
|
template<class T>
|
|
bool posting_t::block_intersector_t::intersect(T func) {
|
|
posting_list_t::block_intersect<T>(plists, its, iter_state, func);
|
|
return true;
|
|
}
|