pg_data_encoder 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
    
        data/lib/pg_data_encoder.rb
    CHANGED
    
    
| @@ -3,24 +3,36 @@ require 'stringio' | |
| 3 3 | 
             
            module PgDataEncoder
         | 
| 4 4 | 
             
              POSTGRES_EPOCH_TIME = (Time.utc(2000,1,1).to_f * 1_000_000).to_i
         | 
| 5 5 |  | 
| 6 | 
            +
              
         | 
| 6 7 | 
             
              class EncodeForCopy
         | 
| 7 8 | 
             
                def initialize(options = {})
         | 
| 8 9 | 
             
                  @options = options
         | 
| 9 10 | 
             
                  @closed = false
         | 
| 10 11 | 
             
                  options[:column_types] ||= {}
         | 
| 11 12 | 
             
                  @io = nil
         | 
| 13 | 
            +
                  @buffer = TempBuffer.new
         | 
| 12 14 | 
             
                end
         | 
| 13 15 |  | 
| 14 16 | 
             
                def add(row)
         | 
| 15 17 | 
             
                  setup_io if !@io
         | 
| 16 18 | 
             
                  @io.write([row.size].pack("n"))
         | 
| 17 19 | 
             
                  row.each_with_index {|col, index|
         | 
| 18 | 
            -
                    encode_field(@ | 
| 20 | 
            +
                    encode_field(@buffer, col, index)
         | 
| 21 | 
            +
                    if @buffer.size > (1024 * 100)
         | 
| 22 | 
            +
                      @buffer.rewind
         | 
| 23 | 
            +
                      @io.write(@buffer.read)
         | 
| 24 | 
            +
                      @buffer.reopen
         | 
| 25 | 
            +
                    end
         | 
| 19 26 | 
             
                  }
         | 
| 20 27 | 
             
                end
         | 
| 21 28 |  | 
| 22 29 | 
             
                def close
         | 
| 23 30 | 
             
                  @closed = true
         | 
| 31 | 
            +
                  if @buffer.size > 0
         | 
| 32 | 
            +
                    @buffer.rewind
         | 
| 33 | 
            +
                    @io.write(@buffer.read)
         | 
| 34 | 
            +
                    @buffer.reopen
         | 
| 35 | 
            +
                  end
         | 
| 24 36 | 
             
                  @io.write([-1].pack("n")) rescue raise Exception.new("No rows have been added to the encoder!")
         | 
| 25 37 | 
             
                  @io.rewind
         | 
| 26 38 | 
             
                end
         | 
| @@ -86,7 +98,7 @@ module PgDataEncoder | |
| 86 98 | 
             
                      io.write(buf)
         | 
| 87 99 | 
             
                    end
         | 
| 88 100 | 
             
                  when Array
         | 
| 89 | 
            -
                    array_io =  | 
| 101 | 
            +
                    array_io = TempBuffer.new
         | 
| 90 102 | 
             
                    field.compact!
         | 
| 91 103 | 
             
                    completed = false
         | 
| 92 104 | 
             
                    case field[0]
         | 
| @@ -147,7 +159,7 @@ module PgDataEncoder | |
| 147 159 | 
             
                    end
         | 
| 148 160 | 
             
                  when Hash
         | 
| 149 161 | 
             
                    raise Exception.new("Hash's can't contain hashes") if depth > 0
         | 
| 150 | 
            -
                    hash_io =  | 
| 162 | 
            +
                    hash_io = TempBuffer.new
         | 
| 151 163 |  | 
| 152 164 | 
             
                    hash_io.write([field.size].pack("N"))
         | 
| 153 165 | 
             
                    field.each_pair {|key,val|
         | 
| @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            class TempBuffer
         | 
| 2 | 
            +
              def initialize
         | 
| 3 | 
            +
                @st = ""
         | 
| 4 | 
            +
              end
         | 
| 5 | 
            +
              def size 
         | 
| 6 | 
            +
                @st.size
         | 
| 7 | 
            +
              end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def write(st)
         | 
| 10 | 
            +
                @st << st
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
              def rewind
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
              def reopen
         | 
| 16 | 
            +
                @st = ""
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
              def read
         | 
| 19 | 
            +
                @st
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
              def pos
         | 
| 22 | 
            +
                @st.size
         | 
| 23 | 
            +
              end
         | 
| 24 | 
            +
              def string
         | 
| 25 | 
            +
                @st
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
            end
         | 
| @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
         | 
| 2 | 
            +
            require 'benchmark'
         | 
| 3 | 
            +
            describe "testing changes with large imports and speed issues" do
         | 
| 4 | 
            +
              it 'should import lots of data quickly' do
         | 
| 5 | 
            +
                encoder = PgDataEncoder::EncodeForCopy.new(temp_file: true)
         | 
| 6 | 
            +
                puts Benchmark.measure {
         | 
| 7 | 
            +
                    0.upto(100_000) {|r|
         | 
| 8 | 
            +
                        encoder.add [1, "text", {a: 1, b: "asdf"}]
         | 
| 9 | 
            +
                    }
         | 
| 10 | 
            +
                }
         | 
| 11 | 
            +
                encoder.close
         | 
| 12 | 
            +
                io = encoder.get_io
         | 
| 13 | 
            +
             | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
    
        metadata
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: pg_data_encoder
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.1. | 
| 4 | 
            +
              version: 0.1.1
         | 
| 5 5 | 
             
              prerelease: 
         | 
| 6 6 | 
             
            platform: ruby
         | 
| 7 7 | 
             
            authors:
         | 
| @@ -9,7 +9,7 @@ authors: | |
| 9 9 | 
             
            autorequire: 
         | 
| 10 10 | 
             
            bindir: bin
         | 
| 11 11 | 
             
            cert_chain: []
         | 
| 12 | 
            -
            date: 2014- | 
| 12 | 
            +
            date: 2014-05-06 00:00:00.000000000 Z
         | 
| 13 13 | 
             
            dependencies:
         | 
| 14 14 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 15 15 | 
             
              name: rspec
         | 
| @@ -59,8 +59,10 @@ files: | |
| 59 59 | 
             
            - examples/fast_load.rb
         | 
| 60 60 | 
             
            - lib/pg_data_encoder.rb
         | 
| 61 61 | 
             
            - lib/pg_data_encoder/encode_for_copy.rb
         | 
| 62 | 
            +
            - lib/pg_data_encoder/temp_buffer.rb
         | 
| 62 63 | 
             
            - lib/pg_data_encoder/version.rb
         | 
| 63 64 | 
             
            - pg_data_encoder.gemspec
         | 
| 65 | 
            +
            - spec/big_write_spec.rb
         | 
| 64 66 | 
             
            - spec/errors_spec.rb
         | 
| 65 67 | 
             
            - spec/fixtures/3_col_array.txt
         | 
| 66 68 | 
             
            - spec/fixtures/3_col_hstore.dat
         | 
| @@ -111,6 +113,7 @@ specification_version: 3 | |
| 111 113 | 
             
            summary: for faster input of data into postgres you can use this to generate the binary
         | 
| 112 114 | 
             
              import and run COPY FROM
         | 
| 113 115 | 
             
            test_files:
         | 
| 116 | 
            +
            - spec/big_write_spec.rb
         | 
| 114 117 | 
             
            - spec/errors_spec.rb
         | 
| 115 118 | 
             
            - spec/fixtures/3_col_array.txt
         | 
| 116 119 | 
             
            - spec/fixtures/3_col_hstore.dat
         |