Skip to content

Instantly share code, notes, and snippets.

@drsnyder
drsnyder / if-unmodified-since.sql
Last active December 17, 2015 22:59
Proposed Poky upsert.
CREATE OR REPLACE FUNCTION only_if_unmodified_since() RETURNS trigger AS $$
BEGIN
IF NEW.modified_at >= OLD.modified_at THEN
RETURN NEW;
ELSE
-- short circuit the update if the condition is not satisfied
RAISE NOTICE 'Bypassing update. The current record is newer.';
RETURN NULL;
END IF;
END;
@drsnyder
drsnyder / peek-and-delete.rb
Created May 15, 2013 00:08
peek and delete jobs
require 'rubygems'
require 'beaneater'
require 'json'
require 'pp'
beanstalk = Beaneater::Pool.new(['localhost:11300'])
tube = beanstalk.tubes["huddlerXheadfi.BasicObject"]
(0..1000).each do |i|
break if not tube.peek(:ready)
@drsnyder
drsnyder / dont-do-this.sql
Created March 27, 2013 19:15
Don't do this SQL.
ELETE FROM subjects
WHERE context_id IN (
SELECT context_id
FROM contexts
WHERE key like 'c.30.%'
AND cmty_id = 120
);
DELETE
FROM contexts
@drsnyder
drsnyder / boltzmann.R
Created March 4, 2013 05:46
Gibbs or Boltzmann distribution plot in R.
boltzmann <- function(x, t=0.1) { exp(x/t) / sum(exp(x/t)) }
x=rnorm(10,mean=1,sd=0.5)
plot(boltzmann(x))
lines(boltzmann(x))
@drsnyder
drsnyder / order_by_test.sql
Created February 28, 2013 18:29
Test for order by `col = val`
BEGIN;
CREATE TABLE order_by_test (
id SERIAL PRIMARY KEY,
token INTEGER NOT NULL
);
INSERT INTO order_by_test(token)
SELECT id
FROM generate_series(1,10) AS id;
@drsnyder
drsnyder / t-poller.c
Created February 8, 2013 01:34
Exercise the poll() on a shared pipe() context switching issue on Linux.
/*
* Exercise the poll() on a shared pipe() context switching issue on Linux using threads. Original code taken from
* http://lkml.indiana.edu/hypermail/linux/kernel/1012.1/03515.html and modified to use threads.
*
* On older 2.6 Linux kernels running this program will drive context switches per second up near 1M/s.
* On 3.x kernels you will see the csw/s pushed up to ~12k depending on the current process load.
*
* gcc -o t-poller t-poller.c -lpthread
*/
#include <stdio.h>
/*gcc -o t-poller t-poller.c -lpthread*/#include <stdio.h>#include <stdlib.h>#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/resource.h>
#define THREADS 1000
#define ITERATIONS 1000
void * child(void * ptr)
@drsnyder
drsnyder / slow-gallery-113.sql
Created January 16, 2013 17:19
slow gallery's query
@drsnyder
drsnyder / beanstalk-feeder.rb
Created December 12, 2012 15:45
A beanstalkd throughput test.
#!/usr/bin/ruby
require 'rubygems'
require 'beaneater'
require 'json'
module Enumerable
def sum
return self.inject(0){|acc,i|acc +i}
end
@drsnyder
drsnyder / gai_do_refresh_reputation_desc_album_community_stacked.sql
Created November 30, 2012 18:05
gai_do_refresh_reputation_desc_album_community_stacked
CREATE FUNCTION gai_do_refresh_reputation_desc_album_community_stacked(INTEGER DEFAULT NULL, INTEGER DEFAULT NULL) RETURNS BOOLEAN AS $$
DECLARE
lock RECORD;
BEGIN
--Lock
SELECT id
FROM object_display_order_types
WHERE id = get_object_display_order_type('reputation_desc', get_huddler_constant('CONTENT_TYPE_GALLERY_ALBUM')::integer)
INTO lock FOR UPDATE;