Commit 352331b2c610411a5132a349fc61492f50353664

Authored by comboy
Committed by Julian Langschaedel
1 parent d356c7b045

do one query instead of N to fetch existing addr ids

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

lib/bitcoin/storage/sequel/sequel_store.rb
... ... @@ -124,9 +124,10 @@
124 124 # bulk-store addresses and txout mappings
125 125 def persist_addrs addrs
126 126 addr_txouts, new_addrs = [], []
  127 + existing_addr = Hash[*@db[:addr].filter(hash160: addrs.map(&:last).uniq).map {|addr| [addr[:hash160], addr[:id]] }.flatten]
127 128 addrs.group_by {|_, a| a }.each do |hash160, txouts|
128   - if existing = @db[:addr][:hash160 => hash160]
129   - txouts.each {|id, _| addr_txouts << [existing[:id], id] }
  129 + if existing_id = existing_addr[hash160]
  130 + txouts.each {|id, _| addr_txouts << [existing_id, id] }
130 131 else
131 132 new_addrs << [hash160, txouts.map {|id, _| id }]
132 133 end