Saari Development

MySQL : Saving a ton of space on old tables

Posted in mysql by imsaar on January 10th, 2008

Here is a nice article about using MySQL (> 5) archive engine.
The MySQL 5.0 Archive Storage Engine

If you have some tables that has mostly read-only for data retention purposes then you can do something like this to drastically reduce the disk spaced used by that table:


alter table MY_OLD_TABLE drop primary key, engine=archive;

note the archive table does not have primary key and dropping the primary key in this way prevents from rebuilding the table twice (compared to the case where you issue two alter table, one to drop key and second to switch engine.

Tagged with: , , , ,

MySQL : Information about tables

Posted in mysql by imsaar on January 10th, 2008

desc information_schema.tables;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| TABLE_CATALOG   | varchar(512) | YES  |     | NULL    |       |
| TABLE_SCHEMA    | varchar(64)  | NO   |     |         |       |
| TABLE_NAME      | varchar(64)  | NO   |     |         |       |
| TABLE_TYPE      | varchar(64)  | NO   |     |         |       |
| ENGINE          | varchar(64)  | YES  |     | NULL    |       |
| VERSION         | bigint(21)   | YES  |     | NULL    |       |
| ROW_FORMAT      | varchar(10)  | YES  |     | NULL    |       |
| TABLE_ROWS      | bigint(21)   | YES  |     | NULL    |       |
| AVG_ROW_LENGTH  | bigint(21)   | YES  |     | NULL    |       |
| DATA_LENGTH     | bigint(21)   | YES  |     | NULL    |       |
| MAX_DATA_LENGTH | bigint(21)   | YES  |     | NULL    |       |
| INDEX_LENGTH    | bigint(21)   | YES  |     | NULL    |       |
| DATA_FREE       | bigint(21)   | YES  |     | NULL    |       |
| AUTO_INCREMENT  | bigint(21)   | YES  |     | NULL    |       |
| CREATE_TIME     | datetime     | YES  |     | NULL    |       |
| UPDATE_TIME     | datetime     | YES  |     | NULL    |       |
| CHECK_TIME      | datetime     | YES  |     | NULL    |       |
| TABLE_COLLATION | varchar(64)  | YES  |     | NULL    |       |
| CHECKSUM        | bigint(21)   | YES  |     | NULL    |       |
| CREATE_OPTIONS  | varchar(255) | YES  |     | NULL    |       |
| TABLE_COMMENT   | varchar(80)  | NO   |     |         |       |
+-----------------+--------------+------+-----+---------+-------+

-- now you can do this or some such
select table_name, engine from information_schema.tables where table_name like "MY_TABLE%";

MySQL : Find out and change charset of a table

Posted in mysql by imsaar on January 10th, 2008

show create table TABLE_IN_QUESTION;
-- show create table can tell you more than
-- describe TABLE_IN_QUESTION including database engine
-- look towards the end for CHARSET=XXX
alter table TABLE_IN_QUESTION CONVERT TO CHARACTER SET latin1;

Ruby : Deep Copy

Posted in code, ruby by imsaar on January 9th, 2008

# problem
a = [[0, 1], [10], 19]
b = a.dup
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 100], [10], 19]

# solution
a = [[0, 1], [10], 19]
b = Marshal.load(Marshal.dump(a))
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 1], [10], 19]

Ruby: Grid Computing Golf

Posted in code, golf, ruby by imsaar on January 8th, 2008

My first attempt at really playing code golf, that is solving a problem in minimum characters of code and I did not do very well (200 character compared to the best 43 perl or 63 ruby characters).
http://codegolf.com/grid-computing


m = []
r = []
c = Array.new(10, 0)
0.upto(9) {|n|m<<gets.chomp.split.map {|x|x.to_i};r<<m[n].inject(0){|s,e| s += e}}
0.upto(9) {|x|m.each_with_index{|v,z|c[x] += v[x]}}
puts [r.max, c.max].max

Update:
I further squeezed some white-space out of the code and it is starting to look uglier (now it is 184 characters). I am not sure I like the code golf concept although I like to solve coding problems.


m=[]
r=[]
c=Array.new(10,0)
0.upto(9){|n|m<<gets.chomp.split.map{|x|x.to_i};r<<m[n].inject(0){|s,e|s+=e}}
0.upto(9){|x|m.each_with_index{|v,z|c[x]+=v[x]}}
puts [r.max,c.max].max

Ruby: Oblongular Number Spirals Solution

Posted in code, ruby by imsaar on January 4th, 2008

<pre>
  # Oblongular Number Spirals
  # http://codegolf.com/oblongular-number-spirals

  class Spiral
    attr_accessor :row_num, :col_num, :direction

    def initialize(m, n)
      @spiral = []
      @value =
     @row_num = m
     @col_num = n
     @direction = :right
   end

   def get(row, col)
     if (row = row_num || col = col_num)
         raise RangeError.new("Out of Bound : row = #{row} col = #{col}")
     end
     @spiral[row] ||= Array.new
     @spiral[row][col] ||= nil
     @spiral[row][col]
   end

   def increment_fill(row, col)
     @value +=
     fill(row, col, @value)
   end

   def fill(row, col, value)
     if (row = row_num || col = col_num)
       raise RangeError.new("Out of Bound : row = #{row} col = #{col}")
     end
     @spiral[row] ||= Array.new
     @spiral[row][col] = value
   end

   def populate(x =, y =)
     count =
     while ()
       increment_fill(x, y)
       count +=
       # exit the loop once iteration == total_cells
       break if count == row_num * col_num
       x, y = next_cell(x, y)
     end
   @spiral
   end

   def next_cell(row, col)
     new_row = row
     new_col = col

     case @direction
     when :right
       new_col = col + 1
     when :left
       new_col = col - 1
     when :down
       new_row = row + 1
     when :up
       new_row = row - 1
     end

     if ((new_col >= col_num || new_col = row_num || new_row < 0))
       change_direction
       next_cell(row, col)
     elsif (get(new_row, new_col) != nil)
       change_direction
       next_cell(row, col)
     else
       return [new_row, new_col]
     end
   end

   def change_direction
     case @direction
     when :right
       @direction = :down
     when :down
       @direction = :left
     when :left
       @direction = :up
     when :up
       @direction = :right
     else
       raise ArgumentError.new("Illegal value  for current_direction #{@direction}")
     end
   end

   def dump
    display = ''
    @spiral.each do |row|
      row.each do |col|
        display << sprintf("%d ", col)
      end
      display << "\n"
    end
    puts display
   end
 end

 if $ == __FILE__
   s = Spiral.new(ARGV[].to_i, ARGV[].to_i)
   s.populate
   s.dump
 end

# test file below
  $:.unshift File.join(File.dirname(__FILE__),'..','lib')

  require 'test/unit'
  require 'spiral'

  class TestSpiral &lt; Test::Unit::TestCase
    def setup
      @spiral = Spiral.new(,)
    end

   def test_increment_fill_0_0_first
     @spiral.increment_fill(0, 0)
     assert_equal(1, @spiral.get(0, 0))
   end

   def test_increment_fill_1_1_first
     @spiral.increment_fill(1, 1)
     assert_equal(1, @spiral.get(1, 1))
   end

   def test_increment_fill_successive
     @spiral.increment_fill(0, 0)
     @spiral.increment_fill(0, 1)
     @spiral.increment_fill(1, 1)
     assert_equal(3, @spiral.get(1, 1))
   end

   def test_get_beyond_limit_raises_exception
     assert_raise(RangeError) {
       @spiral.get(@spiral.row_num + 1, @spiral.col_num + 1)
     }
   end

   def test_get_below_zero_raises_exception
     assert_raise(RangeError) {
       @spiral.get(-,)
     }
   end

   def test_get_at_limit_raises_exception
     assert_raise(RangeError) {
       @spiral.get(@spiral.row_num, @spiral.col_num)
     }
   end

   def test_unfill_get_with_in_range_does_not_raise_exception
     assert_nothing_raised() {
       @spiral.get(@spiral.row_num -, @spiral.col_num - 1)
     }
   end

   def test_unfill_get_with_in_range_returns_nil
     assert_equal(nil, @spiral.get(@spiral.row_num - 1, @spiral.col_num - 1))
   end

   def test_fill_beyond_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(@spiral.row_num + 1, @spiral.col_num + 1)
     }
   end

   def test_fill_beyond_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(0, -1)
     }
   end

   def test_fill_at_range_raises_exception
     assert_raise(RangeError) {
       @spiral.increment_fill(@spiral.row_num, @spiral.col_num)
     }
   end

   def test_populate
     spiral = [
           [1, 2, 3, 4, 5],
           [14, 15, 16, 17, 6],
           [13, 20, 19, 18, 7],
           [12, 11, 10, 9, 8]
          ]
     s = @spiral.populate
     assert_equal(spiral, s)
   end

   def test_change_directon_right
     @spiral.direction = :right
     @spiral.change_direction
     assert_equal(:down, @spiral.direction)
   end

   def test_change_directon_up
     @spiral.direction = :up
     @spiral.change_direction
     assert_equal(:right, @spiral.direction)
   end

   def test_next_cell_0_0
     x, y = @spiral.next_cell(0,0)
     assert_equal(0, x)
     assert_equal(1, y)
   end

   def test_next_cell_0_4
     x, y = @spiral.next_cell(0,4)
     assert_equal(1, x)
     assert_equal(4, y)
   end

 end
</pre>

Ruby : Time Math Interview Problem With Bug Fixed (still writing test first)

Posted in code, ruby by imsaar on January 2nd, 2008

While discussing my friend Arsalan’s C# (seemlessly compiled on my linux machine using mcs)
solution and testing it out I found a bug in my own code that. The problem was when adding
more than 12 hours (> 720 minutes) it was not doing the right thing. The code can still be
refactored for cleaner solution but it is too late at night to do that now. Also, my wife
gave me another idea to convert the time to minutes before adding which I will try out later.


   # Without using any built in date or time functions, write a function or method
   # that accepts two mandatory arguments. The first argument is a string of the
   # format "[H]H:MM {AM|PM}" and the second argument is an integer. Assume the
   # integer is the number of minutes to add to the string. The return value of
   # the function should be a string of the same format as the first argument.
   # For example AddMinutes(" AM",) would return " AM". The exercise
   # isn't meant to be too hard. I just want to see how you code. Feel free to
   # do it procedurally or in an object oriented way, whichever you prefer. Use
   # any language you want. Write production quality code.
  # Question Source: http://blist.com/blog/

  # the following solution was developed using TDD

  require 'test/unit'

  class TestTimeCalc < Test::Unit::TestCase

      def setup
          @time = " AM"
      end

      def test_new_time_cal
          assert_not_nil(TimeCalc.new)
      end

      def test_add_minute_zero
          assert_equal(@time, TimeCalc.add_minutes(@time,))
      end

      def test_add_minute_ten
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_minute_thirteen
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_hour
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_two_hours_fifteen_minutes
          assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_past_meridiem
          # minutes = hours and minutes
          assert_equal(" PM", TimeCalc.add_minutes(@time,))
      end

      def test_alpha_hour_min_format_throws_exception
          assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD AM",) }
      end

      def test_bad_meridiem_throws_exception
          assert_raise(ArgumentError) { TimeCalc.add_minutes("AB:CD TM",) }
      end

      def test_hr_greater_than_twelve
          assert_raise(ArgumentError) { TimeCalc.add_minutes(" PM",) }
      end

      def test_min_greater_than_fifty_nine
          assert_raise(ArgumentError) { TimeCalc.add_minutes(" PM",) }
      end

      def test_add_lot_of_minutes
        # minutes = hours and minutes
        assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

      def test_add_up_to_noon
        # AM plus hr min ()
        assert_equal(" PM", TimeCalc.add_minutes(@time,))
      end

      def test_add_whole_lot_of_minutes
        # minutes = hours and minutes
        assert_equal(" AM", TimeCalc.add_minutes(@time,))
      end

  end # end class TestTimeCalc

  class TimeCalc

      def self.add_minutes(time, minutes)
        (hour, min, meridiem) = parse_time_string(time)

        hour_increment = (min + minutes)/
        min_increment = (min + minutes)% - min

        while (hour + hour_increment >)
          meridiem = (meridiem == 'AM' ? 'PM' : 'AM')
          hour_increment -=
        end

        hour += hour_increment
        # special case forth hour
        meridiem = (meridiem == 'AM' ? 'PM' : 'AM') if hour ==
        min += min_increment

        hour.to_s + ":" + sprintf('%d', min) + " " + meridiem
      end

      private

      def self.parse_time_string(time)
          raise ArgumentError unless (matches = time.match(/(\d{,}):(\d{,})\s+(\w{})/))
          matches = time.match(/^(\d{,}):(\d{,})\s+([A|P]M)$/)
          hour = matches[].to_i
          min = matches[].to_i
          meridiem = matches[]
          raise ArgumentError unless (hour <=)
          raise ArgumentError unless (min <)
          return [hour, min, meridiem]
      end
  end # end class TimeCalc

 if __FILE__ == $
   puts TimeCalc.add_minutes(ARGV[], ARGV[].to_i)
 end