Commit 035a312fc1d408ff57fd2f5b723d2a9162fdd0b2

Authored by comboy
Committed by Julian Langschaedel
1 parent b7e2d151cd

use COPY to store addr and tx for postgres

Showing 1 changed file with 28 additions and 3 deletions Side-by-side Diff

lib/bitcoin/storage/sequel/sequel_store.rb
... ... @@ -71,7 +71,20 @@
71 71 existing ? blk_tx[idx] = existing : new_tx << [tx, idx]
72 72 end
73 73  
74   - new_tx_ids = @db[:tx].insert_multiple(new_tx.map {|tx, _| tx_data(tx) })
  74 + if @db.adapter_scheme == :postgres
  75 + new_tx_ids = []
  76 + db.transaction do
  77 + first_id = db.fetch("SELECT nextval('tx_id_seq') AS id").first[:id]
  78 + csv = new_tx.map {|tx, _| tx_data(tx) }.map.with_index {|tx,i| [i+first_id, "\\x#{tx[:hash].hth}", tx[:version], tx[:lock_time], tx[:coinbase], tx[:tx_size], (tx[:nhash] ? "\\x#{tx[:nhash].hth}" : '')].join(',') }.join("\n")
  79 + db.copy_into(:tx, columns: [:id, :hash, :version, :lock_time, :coinbase, :tx_size, :nhash], format: :csv, data: csv)
  80 + last_id = first_id + new_tx.size - 1
  81 + db.execute("SELECT setval('tx_id_seq', #{last_id}, true)")
  82 + new_tx_ids = (first_id..last_id).to_a
  83 + end
  84 + else
  85 + new_tx_ids = @db[:tx].insert_multiple(new_tx.map {|tx, _| tx_data(tx) })
  86 + end
  87 +
75 88 new_tx_ids.each.with_index {|tx_id, idx| blk_tx[new_tx[idx][1]] = tx_id }
76 89  
77 90 if @db.adapter_scheme == :postgres
... ... @@ -137,8 +150,20 @@
137 150 new_addrs << [hash160, txouts.map {|id, _| id }]
138 151 end
139 152 end
140   - new_addr_ids = @db[:addr].insert_multiple(new_addrs.map {|hash160, txout_id|
141   - { hash160: hash160 } })
  153 + new_addr_ids = []
  154 + if @db.adapter_scheme == :postgres
  155 + db.transaction do
  156 + first_id = db.fetch("SELECT nextval('addr_id_seq') AS id").first[:id]
  157 + csv = new_addrs.map(&:first).map.with_index {|hash160, i| [hash160, i+first_id].join(',')}.join("\n")
  158 + db.copy_into(:addr, columns: [:hash160, :id], format: :csv, data: csv)
  159 + last_id = first_id + new_addrs.size - 1
  160 + db.execute("SELECT setval('addr_id_seq', #{last_id}, true)")
  161 + new_addr_ids = (first_id..last_id).to_a
  162 + end
  163 + else
  164 + new_addr_ids = @db[:addr].insert_multiple(new_addrs.map {|hash160, txout_id|
  165 + { hash160: hash160 } })
  166 + end
142 167  
143 168 new_addr_ids.each.with_index do |addr_id, idx|
144 169 new_addrs[idx][1].each do |txout_id|