Skip to content

Instantly share code, notes, and snippets.

View favila's full-sized avatar

Francis Avila favila

View GitHub Profile
@favila
favila / datomic-counter.clj
Created September 29, 2017 16:23
Demonstrate the creation and use of an auto-increment counter (with nonce) in datomic
(require '[datomic.api :as d])
(d/create-database "datomic:mem://counter-example")
;=> true
(def c (d/connect "datomic:mem://counter-example"))
;=> #'user/c
;; The essential features of creating and using an auto-increment counter in datomic:
;;
;; 1. A counter entity must store the current value and a nonce.
@favila
favila / datomic-mysql-bootstrap.sql
Created January 22, 2015 17:49
Better MySQL bootstrap setup for datomic's datomic_kvs table
-- Optimized MYSQL schema for datomic
-- Unfortunately the bin/sql/mysql-*.sql bootstrapping files for datomic are not
-- very good, and can actually cause failures if not adjusted.
-- One symptom of this is the following error:
-- SQL Error (1071): Specified key was too long; max key length is 767 bytes.
-- Reported here: https://support.cognitect.com/entries/28462168-MySQL-Caveats
-- This is caused by the default collation for the `id` column possibly being
@favila
favila / git-squash.sh
Created February 24, 2014 05:40
git-squash: script to create a squashed patch from a branch.
#! /bin/sh
# Produce a squash-commit patch from a branch of changes
MASTER=$1
PATCHBRANCH=$2
SQUASHBRANCH="$PATCHBRANCH-squash"
MESSAGE=$3
git checkout -b $SQUASHBRANCH $MASTER &&
git merge --squash $PATCHBRANCH &&
git commit -a -m "$MESSAGE" &&
@favila
favila / eid_utils.clj
Last active March 17, 2023 21:15
Utilities to construct or decompose datomic entity ids on on-prem systems using bit arithmetic
(ns favila.eid-utils
"Utilities to construct or decompose entity ids on on-prem systems.
Many of these utilities replicate datomic.api functions because the peer api
lacks them.
The entity id format is:
sign-bit, reserved-bit, 20-partition-bits, 42-t-bits
@favila
favila / last-touched-tx.clj
Created February 27, 2018 23:41
datomic query to return the tx at which an entity or its components were last touched
(require '[datomic.api :as d])
(defn last-touched-tx
"Return the tx of the oldest assertion on `entity` or any of its components."
[db entity]
(or (d/q '[:find (max ?tx) .
:in $ % ?root
:where
(component-entities ?root ?ce)
(union ?root ?ce ?e)
[?e _ _ ?tx]]
@favila
favila / safe-merge.clj
Created February 28, 2020 22:11
merge, but throws on conflicts
(defn safe-merge
"Like merge, but throws if maps have the same keys but different values."
[& maps]
(reduce
(fn [m [m2k m2v :as re]]
(if-some [[_ mv :as le] (find m m2k)]
(if (= mv m2v)
m
(throw (ex-info "Attempted to safe-merge maps with conflicting entries"
@favila
favila / compact-datomic-dev.sh
Created July 18, 2022 18:50
Vaccum or compact unused space from an h2 database (especially for datomic)
#!/bin/sh
# Uses the h2 jar in a datomic distribution to give raw SQL access
# to the h2 database datomic "dev" storage uses.
# The 'SHUTDOWN COMPACT' command in particular performs all
# vaccum-like compaction of the h2 database file to remove
# unused blocks from deleted (garbage-collected) segments.
DATOMIC_HOME=${DATOMIC:-$HOME/lib/datomic/current}
DATOMIC_ADMIN_PASSWORD=${1:?Storage admin password must be provided}
@favila
favila / datomic-edn-handlers.clj
Created April 5, 2016 00:24
Utilities for making it easier to read edn files. Also includes tag readers that datomic uses.
(ns favila.read-edn.tag-readers
"Common tag-reader maps for edn."
(:require datomic.db
datomic.function
datomic.codec)
(:import java.net.URI))
(defmethod print-method URI [^URI x ^Writer w]
(doto w
(.write "#uri ")
@favila
favila / kafka-workbench.clj
Created May 2, 2017 18:20
code to fiddle with kafka behaviors
(ns kafka-workbench
(:require [franzy.clients.consumer.protocols :as c]
[franzy.clients.producer.protocols :as p]
[franzy.serialization.serializers :as serializers]
[franzy.serialization.deserializers :as deserializers]
[franzy.clients.consumer.client :as consumer]
[franzy.clients.producer.client :as producer]))
(def kafka-brokers [])
@favila
favila / tx-pipeline-inorder.clj
Created April 4, 2017 00:17
Transact transactions in core.async channel `from-ch` against datomic connection `conn` with a pipeline depth of `n`.
(defn tx-pipeline-inorder
"Transact transactions in core.async channel `from-ch` against datomic connection `conn` with a pipeline depth of `n`.
Returns a map with keys `:stop!` and `:result`. `:stop!` is a no-arg function you can call to immediately
cease sending transactions (already sent transactions cannot be stopped). `:result` is a promise channel
returning a variant of the result of the tx-pipelining after from-ch is closed and drained, or
:stop! is called and all send transaction futures are deref-ed, or a transaction produces an exception.
The variant returned from the :result channel may be one of: