Skip to content

Instantly share code, notes, and snippets.

View julik's full-sized avatar
💭
🎺

Julik Tarkhanov julik

💭
🎺
View GitHub Profile
@julik
julik / localities.sql
Created June 4, 2023 10:23
Versioned table with grouping by updated_at
CREATE TABLE localities (
id uuid DEFAULT gen_random_uuid() PRIMARY KEY,
source_updated_at TIMESTAMP,
name TEXT
);
INSERT INTO localities (name, source_updated_at) VALUES
('London', '2023-04-01'),
('London', '2023-03-15'),
('Paris', '2023-02-11'),
('Canberra', '2022-10-05');
# This offers just the leaky bucket implementation with fill control, but without the timed lock.
# It does not raise any exceptions, it just tracks the state of a leaky bucket in Postgres.
#
# Leak rate is specified directly in tokens per second, instead of specifying the block period.
# The bucket level is stored and returned as a Float which allows for finer-grained measurement,
# but more importantly - makes testing from the outside easier.
#
# Note that this implementation has a peculiar property: the bucket is only "full" once it overflows.
# Due to a leak rate just a few microseconds after that moment the bucket is no longer going to be full
# anymore as it will have leaked some tokens by then. This means that the information about whether a
mft_gap_mm = 96
offset_outer_mm = 70 # (mft_gap_mm / 2)
# Long side needs an odd number of gaps so that the center span of the
# workbench ends up between two rows of holes and never overlaps the holes
1.step(22,2) do |n_gaps_x|
1.upto(10) do |n_gaps_y|
width_mm = (offset_outer_mm * 2) + (mft_gap_mm * n_gaps_x)
height_mm = (offset_outer_mm * 2) + (mft_gap_mm * n_gaps_y)
puts "#{width_mm}x#{height_mm}mm with #{n_gaps_x + 1} holes along and #{n_gaps_y + 1} holes across"
end
@julik
julik / INSTALL.command
Created May 20, 2021 16:01
Python 3 compatible Logik-Matchbook install script
#!/usr/bin/env python
import contextlib as __stickytape_contextlib
@__stickytape_contextlib.contextmanager
def __stickytape_temporary_dir():
import tempfile
import shutil
dir_path = tempfile.mkdtemp()
try:
yield dir_path
PAYLOAD_SIZE = 15 * 1024 * 1024
CHUNK_SIZE = 65 * 1024 # Roughly one socket buffer
class StufferBody
def each
rng = Random.new(123)
whole_chunks, rem = PAYLOAD_SIZE.divmod(CHUNK_SIZE)
whole_chunks.times do
yield(rng.bytes(CHUNK_SIZE))
end
yield(rng.bytes(rem)) if rem > 0
# In a couple of tables we have VARBINARY columns which either encode a packed
# integer of some kind, or a checksum (which is commonly shared as a hexadecimal).
# This module allows rapid definition of such columns and accessors for them. For example,
# imagine you want to store a CRC32 value - which fits into a uint4
#
# class RecordWithChecksum < ActiveRecord::Base
# extend PackedColumn
# packed_value_column :crc32, pattern: 'V'
# end
#
require 'zlib'
# A fairly naive generator of payloads which, having the same size, will produce an identical CRC32 checksum
def produce_bytes_at_iteration(bytebag_size, random_seed, iter)
rng = Random.new(random_seed)
iter.times do
rng.bytes(bytebag_size)
end
rng.bytes(bytebag_size)
end
require 'bundler'
Bundler.setup
require 'benchmark'
require 'benchmark/ips'
require_relative '../lib/zip_tricks'
some_randomness = StringIO.new(Random.new.bytes(800 * 1024))
rackup_path = File.join(__dir__, 'webserver.ru')
class ZipTricks::OutputEnumerator
DEFAULT_WRITE_BUFFER_SIZE = 65 * 1024
# Creates a new OutputEnumerator.
#
# @param streamer_options[Hash] options for Streamer, see {ZipTricks::Streamer.new}
# It might be beneficial to tweak the `write_buffer_size` to your liking so that you won't be
# doing too many write attempts and block right after
# @param write_buffer_size[Integer] By default all ZipTricks writes are unbuffered. For output to sockets
# it is beneficial to bulkify those writes so that they are roughly sized to a socket buffer chunk. This
# object will bulkify writes for you in this way (so `each` will yield not on every call to `<<` from the Streamer
@julik
julik / kelder.rb
Last active October 22, 2019 22:33
A very very very rough version. There probably will be a gem of this
module Kelder
module PrefixKeyWithToken
# Prefix all generated blob keys with the tenant. Do not
# use slash as a delimiter because it needs different escaping
# depending on the storage service adapter - in some cases it
# might be significant, in other cases it might get escaped as a path
# component etc.
def generate_unique_secure_token
tenant_slug = Apartment::Tenant.current.split('_').last
"#{tenant_slug}-#{super}"