Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 58 lines (46 sloc) 3.08 kb
07b91cd Michael Grosser improve docs, make scopes flag scopes instead of named scopes
grosser authored
1 Save migrations and columns by storing multiple booleans in a single integer.
da37b28 Michael Grosser improve docs
grosser authored
2 e.g. true-false-false = 1, false-true-false = 2, true-false-true = 5 (1,2,4,8,..)
2726be3 Michael Grosser initial
grosser authored
3
4 class User < ActiveRecord::Base
3cffaa3 Michael Grosser refactor to use class_inheritable_accessor
grosser authored
5 include Bitfields
8e07ac1 Michael Grosser get awesome sql working
grosser authored
6 bitfield :my_bits, 1 => :seller, 2 => :insane, 4 => :stupid
2726be3 Michael Grosser initial
grosser authored
7 end
8
8e07ac1 Michael Grosser get awesome sql working
grosser authored
9 user = User.new(:seller => true, :insane => true)
10 user.seller == true
11 user.stupid? == false
2196f7f Michael Grosser improve docs
grosser authored
12 user.my_bits == 3
2726be3 Michael Grosser initial
grosser authored
13
07b91cd Michael Grosser improve docs, make scopes flag scopes instead of named scopes
grosser authored
14 - records changes `user.chamges == {:seller => [false, true]}`
15 - adds scopes `User.seller.stupid.first` (deactivate with `bitfield ..., :scopes => false`)
8382699 Michael Grosser benchmark results validate making bit_operator the default
grosser authored
16 - builds sql `User.bitfield_sql(:insane => true, :stupid => false) == '(users.my_bits & 3) = 1'`
17 - builds index-using sql with `bitfield ... ,:query_mode => :in_list` and `User.bitfield_sql(:insane => true, :stupid => false) == 'users.my_bits IN (2, 3)'` (2 and 1+2), often slower than :bit_operator sql especially for high number of bits
07b91cd Michael Grosser improve docs, make scopes flag scopes instead of named scopes
grosser authored
18 - builds update sql `User.set_bitfield_sql(:insane => true, :stupid => false) == 'my_bits = (my_bits | 6) - 4'`
2196f7f Michael Grosser improve docs
grosser authored
19 - **faster sql than any other bitfield lib** through combination of multiple bits into a single sql statement
07b91cd Michael Grosser improve docs, make scopes flag scopes instead of named scopes
grosser authored
20 - gives access to bits `User.bitfields[:my_bits][:stupid] == 4`
2726be3 Michael Grosser initial
grosser authored
21
22 Install
23 =======
9f90404 Michael Grosser rename to bitfields
grosser authored
24 As Gem: ` sudo gem install bitfields `
8382699 Michael Grosser benchmark results validate making bit_operator the default
grosser authored
25 Or as Rails plugin: ` rails plugin install git://github.com/grosser/bitfields.git `
9f90404 Michael Grosser rename to bitfields
grosser authored
26
43b225e Michael Grosser add change recording and migration warning
grosser authored
27 ### Migration
28 ALWAYS set a default, bitfield queries will not work for NULL
29 t.integer :my_bits, :default => 0, :null => false
30 OR
31 add_column :users, :my_bits, :integer, :default => 0, :null => false
32
7a713a0 Michael Grosser update readme
grosser authored
33 Examples
34 ========
35 Update all users
88a910b Michael Grosser improve docs
grosser authored
36 User.seller.not_stupid.update_all(User.set_bitfield_sql(:seller => true, :insane => true))
37
7a713a0 Michael Grosser update readme
grosser authored
38 Delete the shop when a user is no longer a seller
88a910b Michael Grosser improve docs
grosser authored
39 before_save :delete_shop, :if => lambda{|u| u.changes['seller'] == [true, false]}
2726be3 Michael Grosser initial
grosser authored
40
8382699 Michael Grosser benchmark results validate making bit_operator the default
grosser authored
41 TIPS
42 ====
43 - Never do: "#{bitfield_sql(...)} AND #{bitfield_sql(...)}", merge both into one hash
44 - bit_operator is faster in most cases, use :query_mode => :in_list sparingly
45 - standard mysql integer is 4 byte -> 32 bitfields
46
47 ![performance](http://chart.apis.google.com/chart?chtt=bit-operator+vs+IN+--+with+index&chd=s:CEGIKNPRUW,DEHJLOQSVX,CFHKMPSYXZ,DHJMPSVYbe,DHLPRVZbfi,FKOUZeinsx,FLQWbglqw2,HNTZfkqw19,BDEGHJLMOP,BDEGIKLNOQ,BDFGIKLNPQ,BDFGILMNPR,BDFHJKMOQR,BDFHJLMOQS,BDFHJLNPRT,BDFHJLNPRT&chxt=x,y&chxl=0:|100K|200K|300K|400K|500K|600K|700K|800K|900K|1000K|1:|0|1441.671ms&cht=lc&chs=600x500&chdl=2bits+%28in%29|3bits+%28in%29|4bits+%28in%29|6bits+%28in%29|8bits+%28in%29|10bits+%28in%29|12bits+%28in%29|14bits+%28in%29|2bits+%28bit%29|3bits+%28bit%29|4bits+%28bit%29|6bits+%28bit%29|8bits+%28bit%29|10bits+%28bit%29|12bits+%28bit%29|14bits+%28bit%29&chco=0000ff,0000ee,0000dd,0000cc,0000bb,0000aa,000099,000088,ff0000,ee0000,dd0000,cc0000,bb0000,aa0000,990000,880000)
48
fe48a49 Michael Grosser add todo
grosser authored
49 TODO
50 ====
51 - convenient named scope `User.with_bitfields(:xxx=>true, :yy=>false)`
52
07b91cd Michael Grosser improve docs, make scopes flag scopes instead of named scopes
grosser authored
53 Author
54 ======
2726be3 Michael Grosser initial
grosser authored
55 [Michael Grosser](http://pragmatig.wordpress.com)
56 grosser.michael@gmail.com
7a713a0 Michael Grosser update readme
grosser authored
57 Hereby placed under public domain, do what you want, just do not hold me accountable...
Something went wrong with that request. Please try again.