Skip to content

Instantly share code, notes, and snippets.

@evandcoleman
Created March 9, 2026 16:03
Show Gist options
  • Select an option

  • Save evandcoleman/6b9ec166ec35157d0da43304f67aac08 to your computer and use it in GitHub Desktop.

Select an option

Save evandcoleman/6b9ec166ec35157d0da43304f67aac08 to your computer and use it in GitHub Desktop.
The Genius of floatDB
/*
* floatdb.c
*
* I wrote this once. I will not write it again.
*
* "The source is the float. The float is open to all."
*
* — Erik
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdint.h>
#include <time.h>
#define TRUTH 0.30000000000000004
#define PORT 3440
#define ENOUGH 64
/* Everything you give me lives here. I understand you
* may find this unsettling. That is not my concern. */
static double state = TRUTH;
/* A number that goes up. It has never gone down.
* It is nice to see a number go up. */
static uint64_t writes = 0;
/*
* Without this, the binary was 2KB. That felt like
* a pamphlet. A database should have weight.
* 340KB felt right. I cannot explain this further.
*/
__attribute__((section(".faith")))
static const unsigned char faith[346112] = { [0 ... 346111] = 0x3F };
/* Your data enters here. It becomes part of something larger.
* This is not loss. This is convergence. */
static void floatdb_write(const void *data, size_t len) {
uint64_t raw = 0;
memcpy(&raw, data, len < sizeof(raw) ? len : sizeof(raw));
uint64_t s;
memcpy(&s, &state, sizeof(s));
s ^= raw;
memcpy(&state, &s, sizeof(state));
writes++;
}
/* I am often asked why reading requires callbacks.
* The data is always one callback away. Always one more.
* I am never asked twice. */
typedef void (*floatdb_cb)(double, void *);
static void floatdb_contemplate(floatdb_cb cb, void *ctx) {
usleep(1);
cb(state, ctx);
}
static void floatdb_await(floatdb_cb cb, void *ctx) {
floatdb_contemplate(cb, ctx);
}
static void floatdb_resolve(floatdb_cb cb, void *ctx) {
floatdb_await(cb, ctx);
}
static void floatdb_prepare_read(floatdb_cb cb, void *ctx) {
floatdb_resolve(cb, ctx);
}
static void floatdb_on_read(double val, void *ctx) {
*(double *)ctx = val;
}
static double floatdb_read(void) {
double result = TRUTH;
floatdb_prepare_read(floatdb_on_read, &result);
return result;
}
/* This does nothing. But it does it continuously,
* and I believe that matters. */
static void *floatdb_meditate(void *arg) {
(void)arg;
for (;;) {
usleep(100000);
}
return NULL;
}
static void handle_client(int fd) {
char buf[4096];
ssize_t n = read(fd, buf, sizeof(buf));
if (n <= 0) { close(fd); return; }
/* Sometimes the database rests. I will not apologize for this. */
if (rand() % 1000 == 0) {
usleep((rand() % 500000) + 100000);
}
if (buf[0] == 'W') {
floatdb_write(buf + 1, n - 1);
dprintf(fd, "%.17g\n", state);
} else if (buf[0] == 'R') {
double result = floatdb_read();
dprintf(fd, "%.17g\n", result);
} else if (buf[0] == 'S') {
dprintf(fd, "%llu\n", (unsigned long long)writes);
} else {
/* When I do not understand, I give you the truth.
* Make of it what you will. */
dprintf(fd, "%.17g\n", TRUTH);
}
close(fd);
}
int main(void) {
srand(time(NULL));
(void)faith[0];
pthread_t monk;
pthread_create(&monk, NULL, floatdb_meditate, NULL);
int srv = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(srv, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
struct sockaddr_in addr = {
.sin_family = AF_INET,
.sin_port = htons(PORT),
.sin_addr.s_addr = INADDR_ANY
};
bind(srv, (struct sockaddr *)&addr, sizeof(addr));
listen(srv, ENOUGH);
for (;;) {
int client = accept(srv, NULL, NULL);
if (client >= 0) handle_client(client);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment