Commit b7e2d151cdea00ee7107abc6856da2a323b93e70

Authored by comboy
Committed by Julian Langschaedel
1 parent 352331b2c6

use copy_into for postgres inserts where it's simple enough

Showing 1 changed file with 16 additions and 4 deletions Side-by-side Diff

lib/bitcoin/storage/sequel/sequel_store.rb
... ... @@ -74,8 +74,13 @@
74 74 new_tx_ids = @db[:tx].insert_multiple(new_tx.map {|tx, _| tx_data(tx) })
75 75 new_tx_ids.each.with_index {|tx_id, idx| blk_tx[new_tx[idx][1]] = tx_id }
76 76  
77   - @db[:blk_tx].insert_multiple(blk_tx.map.with_index {|id, idx|
78   - { blk_id: block_id, tx_id: id, idx: idx } })
  77 + if @db.adapter_scheme == :postgres
  78 + csv_data = blk_tx.map.with_index {|id, idx| [block_id, id, idx].join(',')}.join("\n")
  79 + @db.copy_into(:blk_tx, format: :csv, columns: [:blk_id, :tx_id, :idx], data: csv_data)
  80 + else
  81 + @db[:blk_tx].insert_multiple(blk_tx.map.with_index {|id, idx|
  82 + { blk_id: block_id, tx_id: id, idx: idx } })
  83 + end
79 84  
80 85 # store txins
81 86 txin_ids = @db[:txin].insert_multiple(new_tx.map.with_index {|tx, tx_idx|
82 87  
... ... @@ -134,13 +139,20 @@
134 139 end
135 140 new_addr_ids = @db[:addr].insert_multiple(new_addrs.map {|hash160, txout_id|
136 141 { hash160: hash160 } })
  142 +
137 143 new_addr_ids.each.with_index do |addr_id, idx|
138 144 new_addrs[idx][1].each do |txout_id|
139 145 addr_txouts << [addr_id, txout_id]
140 146 end
141 147 end
142   - @db[:addr_txout].insert_multiple(addr_txouts.map {|addr_id, txout_id|
143   - { addr_id: addr_id, txout_id: txout_id }})
  148 +
  149 + if @db.adapter_scheme == :postgres
  150 + csv = addr_txouts.map{|x| x.join(',')}.join("\n")
  151 + @db.copy_into(:addr_txout, format: :csv, columns: [:addr_id, :txout_id], data: csv)
  152 + else
  153 + @db[:addr_txout].insert_multiple(addr_txouts.map {|addr_id, txout_id|
  154 + { addr_id: addr_id, txout_id: txout_id }})
  155 + end
144 156 end
145 157  
146 158 # prepare transaction data for storage