Skip to content

Instantly share code, notes, and snippets.

@kendru
kendru / pg-logical-snippets.sql
Last active February 25, 2022 18:53
Useful snippets when working with logical replication in Postgres
-- Get replication identity for a table.
SELECT CASE relreplident
WHEN 'd' THEN 'default'
WHEN 'n' THEN 'nothing'
WHEN 'f' THEN 'full'
WHEN 'i' THEN 'index'
END AS replica_identity
FROM pg_class
WHERE oid = 'my_table'::regclass;
-- Note 'my_table'::regclass is equivalent to (SELECT oid FROM pg_class WHERE relname = 'my_table')
const crypto = require('crypto');
const charTbl = '0123456789abcdefghjkmnpqrstvwxyz';
const encodeChunk = (n) => charTbl[n];
const nthChunk = (input, idx) => {
const startBit = 5*idx;
const offset = startBit % 8;
const startByte = (startBit - offset)/8;
@kendru
kendru / queue.rs
Created July 10, 2020 10:12
Simple thread-safe bounded queue
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::sync::mpsc;
use std::thread;
struct QueueInner<T: Send> {
items: VecDeque<T>,
is_done: bool,
parked_producers: VecDeque<mpsc::Sender<()>>,
}
@kendru
kendru / actors.cljs
Created June 14, 2020 03:14
Implementation of a very basic actor system
(ns notifications.actor
(:require [reagent.core :as r]
[cljs.core.async :refer [go-loop pub sub chan <! >! put! timeout]]))
(defn actor-system []
(atom {}))
(defn mailbox [address]
[address (chan)])
@kendru
kendru / recursive-callback-to-async.js
Created June 3, 2020 20:49
Example of converting a recursive callback to an asynchronous iterator
const promise_ = Symbol('promise');
class Deferred {
constructor() {
this[promise_] = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
get promise() {
@kendru
kendru / take-all.clj
Last active March 15, 2020 22:59
Clojure macro for taking values from a number of channels, short-circuiting on an error value
(defn is-error-clause? [expr]
(and (sequential? expr)
(= 'on-error (first expr))))
(defn parse-clauses [exprs]
(let [[body error-clauses] (partition-by is-error-clause? exprs)
[_ error-name & error-body] (first error-clauses)]
{:body body
:error-name error-name
:error-body error-body}))
@kendru
kendru / highlightText.js
Created February 21, 2020 23:57
Add case-insensitive non-overlapping highlights to text
function highlightText(text, highlights) {
const lcText = text.toLowerCase();
highlights = highlights.map(h => h.toLowerCase());
let addedChars = 0;
for (let i = 0; i < text.length; i++) {
for (const highlight of highlights) {
if (
lcText[i] === highlight[0] &&
lcText.substring(i, i + highlight.length) === highlight
@kendru
kendru / postgres-sandbox.sh
Created January 18, 2020 19:45
Set up a throw-away postgresql server in docker and a client to connect to it
#!/bin/bash
docker pull postgres:11-alpine
docker network inspect postgres_sandbox > /dev/null 2>&1
if [[ "$?" == "0" ]]; then
docker network rm postgres_sandbox
fi
docker network create --driver bridge postgres_sandbox
@kendru
kendru / objects.clj
Created November 18, 2019 00:51
Emulating Objects in Clojure(Script)
(defn make-mailbox
([] (make-mailbox {:messages []
:next-id 1}))
([state]
{:deliver!
(fn [msg]
(make-mailbox
(-> state
(update :messages conj
(assoc msg :read? false
@kendru
kendru / f97d9211-26b2-495a-a872-bc91a4873f18
Created October 24, 2019 20:23
f97d9211-26b2-495a-a872-bc91a4873f18
f97d9211-26b2-495a-a872-bc91a4873f18