• Stars
    star
    340
  • Rank 124,317 (Top 3 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 13 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Binary uuid keys in Rails

Build Status

activeuuid

Add binary(16) UUIDs to ActiveRecord.

Example

Create a Migration

activeuuid adds the uuid type to your migrations. Example:

class CreateEmails < ActiveRecord::Migration
  def self.up
    create_table :emails, :id => false  do |t|
      t.uuid :id, :primary_key => true
      t.uuid :sender_id  # belongs_to :sender

      t.string :subject
      t.text :body

      t.timestamp :sent_at
      t.timestamps
    end
    add_index :emails, :id
  end

  def self.down
    drop_table :emails
  end
end

include ActiveUUID::UUID in your model

class Email < ActiveRecord::Base
  include ActiveUUID::UUID
  belongs_to :sender
end

specify a natural key for generating the UUID based on one or more columns

class Email < ActiveRecord::Base
  include ActiveUUID::UUID
  natural_key :sender_id, :received_at
  belongs_to :sender
end

natural_key generates a SHA1-based UUID in the ISO OID namespace by default. [7]

specify a custom UUID namespace for the natural key

class Email < ActiveRecord::Base
  include ActiveUUID::UUID
  uuid_namespace "1dd74dd0-d116-11e0-99c7-5ac5d975667e"
  natural_key :sender_id, :received_at
  belongs_to :sender
end

uuid_namespace can either be a UUID in string format, or a UUIDTools::UUID object.

use it:

Here are some example specs:

require 'spec_helper'

describe Email do

  context "when using uuid's as keys" do
    let(:guid) { "1dd74dd0-d116-11e0-99c7-5ac5d975667e" }
    let(:email) { Fabricate :email }

    it "the id guid should be equal to the uuid" do
      email.id.to_s.should eql(guid)
    end

    it "should be able to find an email by the uuid" do
      Email.find(guid).id.to_s.should == guid
    end

  end
end

Motivation

From [2]:

[Here is a] UUID: 1e8ef774-581c-102c-bcfe-f1ab81872213

A UUID like the one above is 36 characters long, including dashes. If you store this VARCHAR(36), you're going to decrease compare performance dramatically. This is your primary key, you don't want it to be slow.

At its bit level, a UUID is 128 bits, which means it will fit into 16 bytes, note this is not very human readable, but it will keep storage low, and is only 4 times larger than a 32-bit int, or 2 times larger than a 64-bit int.

Many of the existing examples of how to use UUIDs as primary keys in Rails use strings rather than bytes (e.g. [3]).

However, this plugin stores the primary keys as bytes. To the application the keys are represented by a UUIDTools::UUID object.

Benefits of UUIDs as primary key

  • no id conflict during multi-master write
  • no locking due to auto-increment
  • with time-based UUIDs you can store a timestamp within your UUID
  • you can create natural keys (based on the SHA of model attributes)

Future work

  • more transparent support for natural and composite keys
  • support for MySQLs INSERT ... ON DUPLICATE KEY UPDATE syntax
  • support a primary column name other than id
  • tests

Inspiration

James Golick's friendly is a great gem for NoSQL on MySQL. It's a great gateway drug to systems like Cassandra for teams that are already familiar with the ins-and-outs of MySQL.

Installation

Add this to your Gemfile

gem "activeuuid"

Or get the code here: https://github.com/jashmenn/activeuuid

References

Dependencies

Rails ~> 3.1.0

Authors

  • Nate Murray
  • pyromaniac
  • Andrew Kane
  • Devin Foley
  • Arkadiy Zabazhanov
  • Jean-Denis Koeck
  • Florian Staudacher
  • Schuyler Erle
  • Florian Schwab
  • Thomas Guillory
  • Daniel Blanco Rojas
  • Olivier Amblet

More Repositories

1

bitcoin-reading-list

a reading list for learning to program Bitcoin transactions
878
star
2

shapes-panels

paged uiscrollview with cocos2d used in Jacob's Shapes
Objective-C
67
star
3

apriori

A ruby/c extension to Christian Borgelt's apriori item-set implementation
C
55
star
4

clj-file-utils

Unix-like filesystem manipulation utilities for Clojure, wrapping Apache Commons IO.
Clojure
40
star
5

cryptocurrencies-in-four-words

Cryptocurrencies described in four words or less
38
star
6

git-style-binaries

Ridiculously easy git-style binaries
Ruby
37
star
7

trollop

mirror of William Morgan's repo at git://gitorious.org/trollop/mainline.git
Ruby
36
star
8

chordjerl

An Erlang implementation of the Chord distributed hash lookup protocol
Erlang
34
star
9

cascading-simhash

simple simhashing in hadoop with cascading
Clojure
33
star
10

irssi-growl

script to use growl from irssi
Perl
23
star
11

backupgem

It’s a beginning-to-end solution for scheduled backups and rotation in a clean ruby package
Ruby
21
star
12

pebble-fireflies

A beautiful pebble watchface
C
21
star
13

bashpider

a "crawler" using xargs and wget
Ruby
17
star
14

teaching-kids-programming

Resources for teaching kids programming
17
star
15

gen_server_mock

erlang mocking for gen_server (gen_server mock)
Erlang
16
star
16

gen_cluster

`gen_cluster` is an erlang behavior for pid clustering. It is a cascading behavior that builds on `gen_server`.
Erlang
16
star
17

ditz

Ditz is a simple, light-weight distributed issue tracker designed to work with distributed version control systems like git, darcs, Mercurial, and Bazaar
Ruby
15
star
18

ihan

ihan - bitcoin-paying npm proxy
JavaScript
13
star
19

AngularJS-2-Learning

Reading list for learning AngularJS 2
13
star
20

boyermoore

boyer-moore string search algorithm in ruby (supports tokens and regexps)
Ruby
12
star
21

hector.rb

Hector-based Cassandra client for JRuby
Ruby
11
star
22

ariel

Fork of A Ruby Information Extraction Library
Ruby
11
star
23

smoker

When you visit your Hive, bring your smoker and you'll get stung less
Clojure
10
star
24

poolparty-examples

a forkable clouds.rb skeleton. fork and go!
Ruby
9
star
25

ssimilarity

Distributed item-to-item similarity calculation using Scala and Hadoop
Scala
8
star
26

stoplight

fully distributed mutex server in erlang based on the sigma algorithm
Erlang
7
star
27

clover-algorithms

clever algorithms - converted to clojure
Clojure
6
star
28

ditz-jira

add jira syncing support to ditz
Ruby
5
star
29

gen-hd-keypair

Generate a Bitcoin BIP39/BIP44 Keypair
JavaScript
3
star
30

cinder-sketches

personal repo of cinder sketches
C++
3
star
31

poolparty-s3fs-plugin

mount s3fs drives on poolparty/ec2 instances
Ruby
2
star
32

tiny_mime.rb

tiny ruby library for creating mime attachments
Ruby
2
star
33

pebble-mori

Remember the day of your death ... with this new Pebble watch face!
C
2
star
34

warningshot-extra-resolvers

Your own personal warningshot resolvers, ripe for sharing.
Ruby
2
star
35

keypjrase

not ready
Clojure
2
star
36

poolparty-mrtg-plugin

Install MRTG on a poolparty instance, easy as pie
Ruby
2
star
37

delicious-getter

A hacky script to download and parse delicious bookmarks. Used for making labeled training data for keyword extraction
Ruby
2
star
38

interval

Interval is a tiny library that provides simple musicial note pitch and interval arithmetic.
Ruby
2
star
39

pst.rb

jruby library to work with outlook pst files (wraps java-libpst)
Ruby
2
star
40

web-protocols

a loose collection of web protocols
2
star
41

spree_sogi

Spree Order Gateway Input - an extension to spree to accept orders via a webservice
Ruby
2
star
42

basket

Easily process and sort a directory of files.
Ruby
1
star
43

clj-jmx-example

a clojure example of jmx - includes a CrazyBean to call operations
Clojure
1
star
44

rabbitmq-server

Rabbit MQ Erlang Message Server
Erlang
1
star
45

javautils

my java utils
Java
1
star
46

jashmenn.github.com

1
star
47

carpet-users

a rails3 engine for users
Ruby
1
star
48

waltz

CruiseControl plugin for rake-task based builds
Vim Script
1
star
49

wordpress.rb

update a wordpress blog from the command-line
1
star
50

cascalog-mrjob

an exploratory plugin to add raw MR jobs to cascalog
Java
1
star
51

HTML-WikiConverter

Release history of HTML-WikiConverter
Perl
1
star
52

casclojure

Fork of Gavin McGovern's Clojure + Cascading on Hadoop Example
Clojure
1
star