Skip to content

Instantly share code, notes, and snippets.

View electrum's full-sized avatar
🚀
Working on @trinodb at @starburstdata

David Phillips electrum

🚀
Working on @trinodb at @starburstdata
View GitHub Profile
@electrum
electrum / gist:814835
Created February 7, 2011 17:58
Amazon S3 Design Principles

Amazon S3 Design Principles

The following principles of distributed system design were used to meet Amazon S3 requirements:

Decentralization: Use fully decentralized techniques to remove scaling bottlenecks and single points of failure.

Asynchrony: The system makes progress under all circumstances.

Autonomy: The system is designed such that individual components can make decisions based on local information.

# Take the basename of the upload's original filename.
# This handles the full Windows paths given by Internet Explorer
# (and perhaps other broken user agents) without affecting
# those which give the lone filename.
# The Windows regexp is adapted from Perl's File::Basename.
def original_filename
unless defined? @original_filename
@original_filename =
unless original_path.blank?
if original_path =~ /^(?:.*[:\\\/])?(.*)/m
@electrum
electrum / attr_protected.rb
Created February 10, 2011 02:01
Prevent Rails from silently corrupting your data
# Be sure to restart your server when you modify this file.
module ActiveModel
module MassAssignmentSecurity
module Sanitizer
def warn!(attrs)
# Raise exception instead of silently corrupting data
raise "Can't mass-assign protected attributes: #{attrs.join(', ')}"
end
end
@electrum
electrum / models_check.rake
Created February 21, 2011 19:10
Validate ActiveRecord models
namespace :models do
desc 'Check models for problems'
task :check => :environment do
Dir.glob(RAILS_ROOT + '/app/models/**/*.rb').each do |f|
model = eval File.basename(f)[0..-4].camelize
next unless model.superclass == ActiveRecord::Base # TODO: handle inheritance
print "#{model.name}: "
if model.column_names.exclude? model.primary_key
abort "bad primary key: #{model.primary_key}"
end
relation.all.find {|i| attributes.all? {|k,v| i[k] == v } }
import java.util.Arrays;
import java.util.Collection;
public class Glommer<T>
{
public static <T> Glommer<T> glommer()
{
return new Glommer<T>();
}
test: test.c Makefile
gcc -O3 -Wall -Werror -std=c99 -o test test.c
@electrum
electrum / gist:1085494
Created July 15, 2011 20:31
Passing data to JavaScript in Rails
# Returns escaped +text+ for use in a JavaScript script tag.
# The string is marked html_safe to prevent characters such as
# '&' and '<' from being converted to HTML entities, which must
# not happen as script tag content is defined to be CDATA.
#
# Example:
# <script>alert('<%= html_safe_js('hello & bye') %>')</script>
#
# Do not use this function for JavaScript used in an attribute value
# such as 'onclick'. In that case, use +escape_javascript+ instead:
@electrum
electrum / gist:1105335
Created July 25, 2011 21:55
Decode Text in Hadoop WritableComparator
private static String decodeText(byte[] bytes, int start, int len)
throws CharacterCodingException
{
int n = WritableUtils.decodeVIntSize(bytes[start]);
return Text.decode(bytes, start + n, len - n);
}
cd src/hadoop-lzo
tar xzvf ~/Downloads/lzo-2.05.tar.gz
cd lzo-2.05
CFLAGS="-arch x86_64" ./configure --build=x86_64-darwin --enable-shared --disable-asm --prefix=$HOME/hadoop-0.20.2/lzo
make -j4
make install
cd ..
env \
JAVA_HOME=/Library/Java/Home \
C_INCLUDE_PATH=$HOME/hadoop-0.20.2/lzo/include \