Advertisement
Guest User

Untitled

a guest
Aug 7th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.77 KB | None | 0 0
  1. class WTF
  2.   # CREATE KEYSPACE foo
  3.   # WITH replication = {
  4.   #   'class': 'SimpleStrategy',
  5.   #   'replication_factor': 1
  6.   # };
  7.   #
  8.   # USE foo;
  9.   #
  10.   #   CREATE TABLE bar1 (
  11.   #     some_id bigint,
  12.   #     some_type text,
  13.   #     some_value int,
  14.   #     some_data text,
  15.   #     PRIMARY KEY((some_id, some_type), some_value)
  16.   #   );
  17.   #
  18.   #   CREATE TABLE bar2 (
  19.   #     some_id bigint,
  20.   #     some_type text,
  21.   #     some_value_and_data text,
  22.   #     PRIMARY KEY((some_id, some_type))
  23.   #   );
  24.  
  25.   def initialize()
  26.     # Connect to localhost by default
  27.     @conn = Historian::ConnectionWrapper.new({})
  28.     @conn.use('foo')
  29.     @value = 0
  30.     @id = rand(1000000)
  31.   end
  32.  
  33.   def random_data
  34.     (0...10).map{ ('a'..'z').to_a[rand(26)] }.join
  35.   end
  36.  
  37.   def random_id
  38.     rand(25000000)
  39.   end
  40.  
  41.   def insert1
  42.     "INSERT INTO bar1(some_id,some_type,some_value,some_data) VALUES (#{@id},'blah',#{random_id},'#{random_data}');"
  43.   end
  44.  
  45.   def insert2
  46.     "INSERT INTO bar2(some_id,some_type,some_value_and_data) VALUES (#{@id},'blah','#{'%09d' % random_id}_#{random_data}');"
  47.   end
  48.  
  49.   def many1
  50.     10000.times.map {|_| insert1 }
  51.   end
  52.  
  53.   def many2
  54.     10000.times.map {|_| insert2 }
  55.   end
  56.  
  57.   def execute_separetely(cmds)
  58.     t1 = Time.now
  59.     cmds.each { |cmd| @conn.execute(cmd, :one) }
  60.     puts "Separetely: #{Time.now-t1}"
  61.   end
  62.  
  63.   def execute_batch(cmds)
  64.     cmd = "BEGIN BATCH\n"
  65.     cmd << cmds.join("\n")
  66.     cmd << "APPLY BATCH;\n"
  67.     t1 = Time.now
  68.     @conn.execute(cmd, :one)
  69.     puts "Batch: #{Time.now-t1}"
  70.   end
  71.  
  72.   def showoff
  73.     puts "Using composite keys"
  74.     execute_separetely many1
  75.     execute_batch many1
  76.     puts "Using just partition key and wide row"
  77.     execute_separetely many2
  78.     execute_batch many2
  79.   end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement