Skip to content

Commit c3ff5cf

Browse files
committed
Column Storage: Add storage interfaces to SDK
Introduce storage-related reference types and define a column storage interface for extensions. This extends the SDK to allow column data to be managed externally by extensions.
1 parent b8ef446 commit c3ff5cf

2 files changed

Lines changed: 173 additions & 1 deletion

File tree

unittest/gunit/villagesql/abi_v2_check-t.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@
5151
// const char *decode_vdf_name; // +88 (protocol >= 2)
5252
// const char *compare_vdf_name; // +96 (protocol >= 2)
5353
// const char *hash_vdf_name; // +104 (protocol >= 2)
54+
// vef_type_storage_intf_t storage_intf; // +112 (protocol >= 2)
5455
// ---------------------------------------------------------------------------
55-
static_assert(sizeof(vef_type_desc_t) == 112,
56+
static_assert(sizeof(vef_type_desc_t) == 168,
5657
"ABI v2 break: vef_type_desc_t size changed");
5758
static_assert(offsetof(vef_type_desc_t, int_to_params) == 64,
5859
"ABI v2 break: vef_type_desc_t::int_to_params offset changed");
@@ -66,6 +67,8 @@ static_assert(offsetof(vef_type_desc_t, compare_vdf_name) == 96,
6667
"ABI v2 break: vef_type_desc_t::compare_vdf_name offset changed");
6768
static_assert(offsetof(vef_type_desc_t, hash_vdf_name) == 104,
6869
"ABI v2 break: vef_type_desc_t::hash_vdf_name offset changed");
70+
static_assert(offsetof(vef_type_desc_t, storage_intf) == 112,
71+
"ABI v2 break: vef_type_desc_t::storage_intf offset changed");
6972

7073
// Placeholder test so the binary links and runs.
7174
TEST(AbiV2Check, StaticAssertsPass) {}

villagesql/sdk/include/villagesql/abi/types.h

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ typedef enum : unsigned int {
159159
// + Add deterministic VDF attribute.
160160
// + Add encode/decode/compare/hash VDF name fields to
161161
// vef_type_desc_t.
162+
// + Add storage interface to vef_type_desc_t.
162163
} vef_protocol_t;
163164

164165
// Max length of error messages in caller-provided buffers.
@@ -541,6 +542,170 @@ typedef bool (*vef_type_resolve_params_func_t)(
541542
const vef_type_param_t *params, size_t param_count,
542543
vef_type_resolved_params_t *result, char *error_msg);
543544

545+
// =============================================================================
546+
// Storage Types and functions for column data stored by the extension.
547+
// =============================================================================
548+
// InnoDB tablespace reference.
549+
typedef uint32_t vef_storage_space_ref_t;
550+
551+
// InnoDB storage reference: space and root page reference.
552+
typedef uint64_t vef_storage_ref_t;
553+
554+
// InnoDB column reference: page reference and offset. The column is always
555+
// stored in the tablespace referred by the storage reference.
556+
typedef uint64_t vef_storage_col_ref_t;
557+
558+
// InnoDB transaction reference.
559+
typedef uint64_t vef_storage_trx_ref_t;
560+
561+
// Mini-transaction context that can be passed to InnoDB storage interface
562+
// acquiring pages.
563+
typedef void *vef_storage_mtr_ref_t;
564+
565+
// Column data buffer and length
566+
typedef struct {
567+
const unsigned char *data;
568+
uint32_t length;
569+
} vef_storage_col_data_t;
570+
571+
// Empty column reference.
572+
#define VEF_STORAGE_EMPTY_COLUMN_REF 0
573+
// Alignment requirement for arena allocators
574+
#define VEF_STORAGE_ALLOCATOR_ALIGNMENT 8
575+
576+
// Arena allocator callback type
577+
// The allocator MUST return memory aligned to VEF_STORAGE_ALLOCATOR_ALIGNMENT.
578+
// Author can use the Allocator callback to allocate memory for context. The
579+
// allocated memory would be tracked and freed automatically by InnoDB after
580+
// the storage is dropped.
581+
typedef void *(*vef_storage_arena_func_t)(void *arena, uint32_t size);
582+
583+
// Partially opaque storage context with visible storage reference. Author may
584+
// allocate a context object (using Arena allocator) of larger size with this
585+
// structure as prefix to store additional private state during creation. The
586+
// same context is passed to subsequent operations (insert, delete, etc.).
587+
typedef struct {
588+
// Column storage reference
589+
vef_storage_ref_t ref;
590+
} vef_storage_ctx_t;
591+
592+
// Storage function pointer types
593+
594+
// Create storage for column data.
595+
// Parameters:
596+
// space_ref - Tablespace in which to create the storage
597+
// trx_ref - Transaction under which the creation runs
598+
// col_len - Column storage length
599+
// arena_ctx - Opaque arena handle passed to arena_alloc
600+
// arena_alloc - Allocator for the storage context; memory is freed when
601+
// storage is dropped
602+
// storage - Output: newly created storage context
603+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
604+
// Returns false on success, true on error (writes to error_msg).
605+
typedef bool (*vef_type_storage_create_func_t)(
606+
vef_storage_space_ref_t space_ref, vef_storage_trx_ref_t trx_ref,
607+
uint32_t col_len, void *arena_ctx, vef_storage_arena_func_t arena_alloc,
608+
vef_storage_ctx_t **storage, char *error_msg);
609+
610+
// Drop storage for column data.
611+
// Parameters:
612+
// storage - Storage context to drop
613+
// trx_ref - Transaction under which the drop runs
614+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
615+
// Returns false on success, true on error (writes to error_msg).
616+
typedef bool (*vef_type_storage_drop_func_t)(vef_storage_ctx_t *storage,
617+
vef_storage_trx_ref_t trx_ref,
618+
char *error_msg);
619+
620+
// Load existing storage from a storage reference.
621+
// Parameters:
622+
// storage_ref - Reference to the existing storage to load
623+
// arena_ctx - Opaque arena handle passed to arena_alloc
624+
// arena_alloc - Allocator for the storage context; memory is freed when
625+
// storage is dropped
626+
// storage - Output: loaded storage context
627+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
628+
// Returns false on success, true on error (writes to error_msg).
629+
typedef bool (*vef_type_storage_load_func_t)(
630+
vef_storage_ref_t storage_ref, void *arena_ctx,
631+
vef_storage_arena_func_t arena_alloc, vef_storage_ctx_t **storage,
632+
char *error_msg);
633+
634+
// Insert column data into column storage along with transaction reference.
635+
// Parameters:
636+
// storage - Storage context to insert into
637+
// mctx - Mini-transaction context
638+
// trx_ref - Transaction performing the insert
639+
// col_data - Column data to store
640+
// rowid_prefix - Row identifier prefix
641+
// col_ref - Output: reference to the newly inserted column entry
642+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
643+
// Returns false on success, true on error (writes to error_msg).
644+
typedef bool (*vef_type_storage_insert_func_t)(
645+
vef_storage_ctx_t *storage, vef_storage_mtr_ref_t mctx,
646+
vef_storage_trx_ref_t trx_ref, vef_storage_col_data_t col_data,
647+
vef_storage_col_data_t rowid_prefix, vef_storage_col_ref_t *col_ref,
648+
char *error_msg);
649+
650+
// Fetch column data reference pointer acquiring appropriate latch.
651+
// Parameters:
652+
// storage - Storage context to read from
653+
// mctx - Mini-transaction context
654+
// col_ref - Reference to the column entry to fetch
655+
// col_data - Output: fetched column data
656+
// rowid_prefix - Output: row identifier prefix
657+
// trx_ref - Output: transaction that last wrote this entry
658+
// delete_marked - Output: whether the entry is marked for deletion
659+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
660+
// Returns false on success, true on error (writes to error_msg).
661+
typedef bool (*vef_type_storage_select_func_t)(
662+
vef_storage_ctx_t *storage, vef_storage_mtr_ref_t mctx,
663+
vef_storage_col_ref_t col_ref, vef_storage_col_data_t *col_data,
664+
vef_storage_col_data_t *rowid_prefix, vef_storage_trx_ref_t *trx_ref,
665+
bool *delete_marked, char *error_msg);
666+
667+
// Mark or unmark column as deleted by the transaction.
668+
// Parameters:
669+
// storage - Storage context
670+
// mctx - Mini-transaction context
671+
// trx_ref - Transaction performing the operation
672+
// col_ref - Reference to the column entry to mark
673+
// delete_mark - true to mark as deleted, false to unmark
674+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
675+
// Returns false on success, true on error (writes to error_msg).
676+
typedef bool (*vef_type_storage_mark_delete_func_t)(
677+
vef_storage_ctx_t *storage, vef_storage_mtr_ref_t mctx,
678+
vef_storage_trx_ref_t trx_ref, vef_storage_col_ref_t col_ref,
679+
bool delete_mark, char *error_msg);
680+
681+
// Remove column only if it has the matching transaction reference.
682+
// Parameters:
683+
// storage - Storage context
684+
// mctx - Mini-transaction context
685+
// trx_ref - Transaction to match with the record to purge
686+
// col_ref - Reference to the column entry to purge
687+
// error_msg - Output: error description on failure (VEF_MAX_ERROR_LEN)
688+
// Returns false on success, true on error (writes to error_msg).
689+
typedef bool (*vef_type_storage_purge_func_t)(vef_storage_ctx_t *storage,
690+
vef_storage_mtr_ref_t mctx,
691+
vef_storage_trx_ref_t trx_ref,
692+
vef_storage_col_ref_t col_ref,
693+
char *error_msg);
694+
695+
// Note: This structure cannot be modified or extended since it is a
696+
// sub-structure of vef_type_desc_t. If a new interface needs to be added,
697+
// in any future ABI version, we should create another structure like
698+
// vef_type_storage_intf_v2_t and append it to vef_type_desc_t.
699+
typedef struct {
700+
vef_type_storage_create_func_t create;
701+
vef_type_storage_drop_func_t drop;
702+
vef_type_storage_load_func_t load;
703+
vef_type_storage_insert_func_t insert;
704+
vef_type_storage_select_func_t select;
705+
vef_type_storage_mark_delete_func_t mark_delete;
706+
vef_type_storage_purge_func_t purge;
707+
} vef_type_storage_intf_t;
708+
544709
typedef struct {
545710
// protocol >= VEF_PROTOCOL_1
546711
vef_protocol_t protocol;
@@ -589,6 +754,10 @@ typedef struct {
589754
const char *decode_vdf_name;
590755
const char *compare_vdf_name;
591756
const char *hash_vdf_name; // OPTIONAL (like hash_func)
757+
758+
// OPTIONAL: Storage interfaces. Implement if the columns of this type are
759+
// stored by the extension.
760+
vef_type_storage_intf_t storage_intf;
592761
} vef_type_desc_t;
593762

594763
typedef struct {

0 commit comments

Comments
 (0)