Skip to content

Instantly share code, notes, and snippets.

@eatonphil
eatonphil / Returning-Data-From-rnd_next().md
Last active June 11, 2024 07:09
Returning data from rnd_next() in a custom storage engine.

rnd_next(uchar* buf) for a storage engine sets up the current row of data. If there is data, it returns a 0. Otherwise it returns a HA_ERR_END_OF_FILE or other error.

Row data is written into the uchar* buf field. You might imagine you would return structured data for a row and the API would be like rnd_next(Fields**, field_count int). But that's not how the API works. You write the fields as bytes directly into uchar* buf.

The first X bytes are a NULL bitmap. One bit for each NULL-able column, padded out to a full byte. After that is each value for each field. For a table with two INT columns (they'd both be NULL-able), you'd write 9 bytes to uchar* buf. The first byte would be a NULL bitmap of all zeroes if both values in the row were not NULL. The next 4 bytes would be the first column's value. The last 4 bytes would be the second column's value.

Here's a diff for the BLACKHOLE engine, hardcoding a single row response: https://github.com/MariaDB/server/compare/11.4...eatonphil:mariadb:e0

@eatonphil
eatonphil / Registering-A-New-Storage-Engine-For-MySQL.md
Created January 5, 2024 21:51
Registering a new storage engine for MariaDB.

The MySQL/MariaDB build process is complex, so it is easiest to build within the MySQL/MariaDB source tree. (I wasted basically a whole day trying to build a storage engine plugin outside the tree.)

Inside the MariaDB root, create a new directory, storage/memem.

$ mkdir storage/memem

The blackhole storage engine is the smallest existing one. So we'll copy from it and rename the minimum from it to get a new engine to compile and be usable from the server.

@eatonphil
eatonphil / Minimal-MariaDB-UDF-In-C.md
Last active October 19, 2024 12:13
Minimal UDF for MariaDB in C

Assume that you've got MariaDB built and running locally. Let's say the install directory is in $MARIADB_LOCAL_INSTALL.

Enter the following into udf_minimal.c:

#include "mariadb.h"

my_bool mysum_init(UDF_INIT* initid, UDF_ARGS* args, char* message);
long long mysum(UDF_INIT* initid, UDF_ARGS* args, char* result,
              unsigned long* length, char* is_null, char* error);
@eatonphil
eatonphil / Run.md
Last active January 5, 2024 01:32
Local debug build of MariaDB, server and client running against local database.

Download and build.

$ git clone https://github.com/MariaDB/server.git mariadb
$ cd mariadb
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=Debug ..
$ make -j8
@eatonphil
eatonphil / bst.rs
Created November 27, 2023 17:27
Binary Search Tree in Rust
struct BST<K: Ord + Clone, V: Ord + Clone> {
key: Option<K>,
value: Option<V>,
left: Option<Box<BST<K, V>>>,
right: Option<Box<BST<K, V>>>,
}
impl<K: Ord + Clone, V: Ord + Clone> BST<K, V> {
fn init() -> Box<BST<K, V>> {
return Box::new(BST::<K, V> {
default:
gcc -O0 -g3 -Wall -Wextra -Wconversion -Wdouble-promotion \
-Wno-unused-parameter -Wno-unused-function -Wno-sign-conversion \
main.c
format:
clang-format -i main.c
@eatonphil
eatonphil / btree.py
Created August 27, 2023 16:47
Python In-memory B-Tree
import math
import uuid
class BTree:
def __init__(self, order=3):
self.root = BTreeNode(order)
def insert(self, toinsert):
all_elements = self.list()
@eatonphil
eatonphil / main.go
Created March 28, 2023 17:33
timing sample
package main
import (
"fmt"
"time"
"log"
"os"
"reflect"
tb "github.com/tigerbeetledb/tigerbeetle-go"
@eatonphil
eatonphil / panic
Created September 15, 2022 19:01
TB panic
./tigerbeetle start --addresses=3000 0_0.tigerbeetle | tee log
info(io): opening "0_0.tigerbeetle"...
info(main): 0: cluster=0: listening on 127.0.0.1:3000
info(message_bus): connection from client 227535350229908850722306115486582690844
thread 1684819 panic: reached unreachable code
/home/phil/tb/tigerbeetle/zig/lib/std/debug.zig:225:14: 0x269fcb in std.debug.assert (tigerbeetle)
if (!ok) unreachable; // assertion failure
^
/home/phil/tb/tigerbeetle/src/lsm/manifest.zig:431:23: 0x43ef85 in lsm.manifest.ManifestType(lsm.table.TableType(u128,lsm.groove.IdTreeValue,lsm.groove.IdTreeValue.compare_keys,lsm.groove.IdTreeValue.key_from_value,554112867134706473364364839029663282043,lsm.groove.IdTreeValue.tombstone,lsm.groove.IdTreeValue.tombstone_from_key),storage.Storage).assert_level_table_counts (tigerbeetle)
assert(manifest_level.table_count_visible <= table_count_visible_max);
@eatonphil
eatonphil / user.js
Last active September 15, 2022 19:02
TigerBeetle client
const assert = require('node:assert/strict');
const { createClient } = require('tigerbeetle-node');
async function run() {
const client = createClient({ cluster_id: 0, replica_addresses: ['3000'] });
// Create two accounts
let errors = await client.createAccounts([
{