#include "store/enc.h"
#include "store/fsst.h"
#include <rayforce.h>
#include <string.h>
static const ray_enc_kind_t K_U32 = { 32, false, false };
static inline uint64_t p8(uint64_t x) { return (x + 7) & ~(uint64_t)7; }
int64_t ray_enc_strdict_size(const uint64_t* codes, int n) {
return ray_enc_choose(codes, n, K_U32, RAY_ENC_SLOT_CODES, 1, NULL).bytes;
}
bool ray_enc_strdict_write(const uint64_t* codes, int n, ray_enc_buf_t* out) {
ray_enc_choice_t c;
return ray_enc_write(codes, n, K_U32, RAY_ENC_SLOT_CODES, 1, NULL, out, &c);
}
void ray_enc_strdict_read(const uint8_t* p, size_t avail, int n, uint64_t* codes) {
(void)ray_enc_read(p, avail, 0, 0, n, K_U32, RAY_ENC_SLOT_CODES, 1, NULL, codes);
}
uint64_t ray_enc_fsst_encode(const ray_fsst_table_t* t, const ray_fsst_index_t* ix,
const char* const* s, const uint32_t* len, int n,
uint8_t* buf, uint64_t* clen) {
uint64_t total = 0;
for (int i = 0; i < n; i++) {
int64_t k = ray_fsst_encode_ix(t, ix, (const uint8_t*)s[i], len[i], buf + total);
clen[i] = (uint64_t)k;
total += (uint64_t)k;
}
return total;
}
int64_t ray_enc_fsst_size(const uint64_t* clen, int n, uint64_t total) {
return ray_enc_choose(clen, n, K_U32, RAY_ENC_SLOT_FSST_LENGTHS, 1, NULL).bytes + 8 + (int64_t)p8(total);
}
bool ray_enc_fsst_write(const uint64_t* clen, int n, const uint8_t* bytes, uint64_t total,
ray_enc_buf_t* out) {
if (n < 0 || n > RAY_ENC_VEC || total > SIZE_MAX - 8) return false;
uint64_t sum = 0;
for (int i = 0; i < n; i++) {
if (clen[i] > UINT32_MAX) return false;
sum += clen[i];
}
if (sum != total) return false;
size_t at = out->len;
ray_enc_choice_t c;
if (ray_enc_write(clen, n, K_U32, RAY_ENC_SLOT_FSST_LENGTHS, 1, NULL, out, &c) &&
ray_enc_buf_put(out, &total, 8) && ray_enc_buf_put(out, bytes, (size_t)total) &&
ray_enc_buf_pad8(out))
return true;
out->len = at;
return false;
}
void ray_enc_fsst_parse(const uint8_t* p, size_t avail, int n, const ray_fsst_table_t* t,
uint64_t* clen, uint64_t* xlen, const uint8_t** bytes) {
int64_t a = ray_enc_read(p, avail, 0, 0, n, K_U32, RAY_ENC_SLOT_FSST_LENGTHS, 1, NULL, clen);
const uint8_t* b = p + a + 8;
uint64_t at = 0;
for (int i = 0; i < n; i++) {
xlen[i] = (uint64_t)ray_fsst_expanded_len(t, b + at, (uint32_t)clen[i]);
at += clen[i];
}
*bytes = b;
}