Skip to content

Instantly share code, notes, and snippets.

@joshuawscott
joshuawscott / bsearch_index.rb
Last active December 18, 2015 09:29
Optimized Binary Search for a sorted array - returns the index of the element. 6x faster than Array#bsearch for arrays of strings.
class Array
# Returns the index number of a matching element.
# The returned element is not guaranteed.
# target must implement #<=>
# example:
# arr = [2,3,5,7,11,13,17,19,23,29]
# arr.bsearch_index(2) => 0
# arr.bsearch_index(23) => 8
# arr.bsearch_index(10) => nil
# nil always is nil:
@joshuawscott
joshuawscott / pg_all_types.sql
Created June 19, 2013 04:18
CREATE TABLE statement for PostgreSQL that has one of each column type. I use this for testing various interfaces into PostgreSQL; check that each of the data types are mapped correctly, etc.
CREATE TABLE test (
a_smallint SMALLINT,
a_integer INTEGER,
a_bigint BIGINT,
a_decimal DECIMAL(10,3),
a_numeric NUMERIC(10,3),
a_real REAL,
a_double_precision DOUBLE PRECISION,
a_serial SERIAL,
a_bigserial BIGSERIAL,
@joshuawscott
joshuawscott / gist:5811659
Last active December 18, 2015 16:29
# npm publish (never having published)
Joshuas-MacBook-Pro:postgresql-node joshua$ npm publish
npm ERR! need auth auth and email required for publishing
npm ERR! need auth You need to authorize this machine using `npm adduser`
npm ERR! System Darwin 12.4.0
npm ERR! command "node" "/usr/local/bin/npm" "publish"
npm ERR! cwd /Users/joshua/dev/postgresql-node
npm ERR! node -v v0.10.9
npm ERR! npm -v 1.2.24
npm ERR! code ENEEDAUTH
@joshuawscott
joshuawscott / spec_helper.rb
Created August 1, 2013 14:08
checking for rspec typo
module RSpec
module Mocks
module AnyInstance
class Recorder
def should_recieve(*args)
raise NoMethodError.new "should_recieve: Did you mean 'should_receive' ?"
end
def should_not_recieve(*args)
raise NoMethodError.new "should_not_receive: Did you mean 'should_not_receive' ?"
end
@joshuawscott
joshuawscott / sqrt.exs
Last active December 20, 2015 14:59
Find a square root with elixir (or just call :math.sqrt/1)
defmodule Math do
def sqrt(x) do
refine x, x / 2.0, 1.0
end
def refine(target, _, attempt) when attempt * attempt == target do
attempt
end
@joshuawscott
joshuawscott / prime.exs
Created August 4, 2013 20:40
Prime numbers Simple recursion to find primality up to 40,000
defmodule Prime do
def known_primes, do: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]
def is_prime(n) when n > 9409, do: :cannot_calculate
def is_prime(n) when n < 2, do: false
def is_prime(2), do: true
def is_prime(n), do: check_primes(n, known_primes)
def check_primes(_,[]), do: true
def check_primes(n, [head|_tail]) when head*head > n, do: true
def check_primes(n, [head|tail]), do: rem(n, head) != 0 and check_primes(n, tail)

Compression

935MB nginx log (dpx)

MB/sec in both charts means:

orig. size - compressed size
____________________________
         real time
@joshuawscott
joshuawscott / set_save_ex.lua
Last active August 29, 2015 14:08
Redis LUA stuff
-- Sets KEY to VALUE, preserving any existing TTL
-- Call with EVAL "..." 1 KEY VALUE
-- or
-- LOAD SCRIPT "..."
-- => <SHA1>
-- EVALSHA <SHA1> 1 KEY VALUE
local ttl = redis.pcall('ttl', KEYS[1])
if ttl == -1 then
return redis.pcall('set', KEYS[1], ARGV[1])
@joshuawscott
joshuawscott / id and name indexes
Last active August 29, 2015 14:08
100,000,000 rows in postgres.
jscott=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+------------------------+-----------
id | integer |
name | character varying(255) |
Indexes:
"id_idx" btree (id)
"idx_name" btree (name)
@joshuawscott
joshuawscott / curlsocket.js
Created December 9, 2014 19:21
curl to a unix socket. No external dependencies. tested on Nodejs 0.8 & 0.10
var http = require('http');
if (process.argv.length <= 3) {
console.error('USAGE: node curlsocket.js /path/to/unix.sock /request/path');
process.exit();
}
var options = {
method: 'GET',
hostname: 'localhost',
socketPath: process.argv[2],