-
-
Save sujithrs/2177881 to your computer and use it in GitHub Desktop.
Revisions
-
cpatni revised this gist
Nov 30, 2011 . 1 changed file with 12 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,18 @@ require 'date' class String def &(str) result = '' result.force_encoding("BINARY") min = [self.length, str.length].min (0...min).each do |i| result << (self[i].ord & str[i].ord) end result end def |(str) result = '' result.force_encoding("BINARY") -
cpatni revised this gist
Nov 21, 2011 . 1 changed file with 72 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,67 @@ require 'sinatra' require 'redis' require 'json' require 'date' class String def |(str) result = '' result.force_encoding("BINARY") min = [self.length, str.length].min (0...min).each do |i| result << (self[i].ord | str[i].ord) end if self.length > str.length result << self[min ... self.length] elsif self.length < str.length result << str[min ... str.length] end result end def population_count count = 0 (0 ... self.length).each do |i| count += _population_count_byte(self[i].ord) end count end private ONE_BITS = [0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4] #It works with 8 bits def _population_count_byte(x) count = 0 count = ONE_BITS[x&0x0f] count += ONE_BITS[x>>4] count end end helpers do def redis $redis ||= Redis.new end def unique_count(event, from, to) uniques = '' (from..to).each do |date| key = "#{event}.#{date.strftime('%y%m%d')}" bitmap = redis.get(key) if bitmap bitmap.force_encoding("BINARY") uniques |= bitmap end end uniques.population_count end end get '/collect/:event/for/:user_id/on/:date' do |event,user_id,date| time = Date.parse(date) key = "#{event}.#{time.strftime('%y%m%d')}" @@ -6,4 +70,11 @@ else 500 end end get '/unique/:event/in/last/:n/days?' do |event,n| content_type :json to = Date.today from = to - n.to_i {:from => from, :to => to, :unique_count => unique_count(event,from,to)}.to_json end -
cpatni created this gist
Nov 21, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ get '/collect/:event/for/:user_id/on/:date' do |event,user_id,date| time = Date.parse(date) key = "#{event}.#{time.strftime('%y%m%d')}" if redis.setbit(key, user_id, 1) 204 else 500 end end