Jim Cain
4/2/2007 9:08:00 PM
I've updated my previous example to include transactions:
BDB::Hash.open('models.db', nil, BDB::CREATE | BDB::TRUNCATE) {}
BDB::Hash.open('makes.db', nil, BDB::CREATE | BDB::TRUNCATE,
:set_flags => BDB::DUP | BDB::DUPSORT) {}
env = BDB::Env.new('/home/jim/eclipse/testRuby',
BDB::CREATE|BDB::INIT_TRANSACTION)
models = env.open_db(BDB::HASH, 'models.db', nil, BDB::CREATE)
makes = env.open_db(BDB::HASH, 'makes.db', nil, BDB::CREATE,
:set_flags => BDB::DUP | BDB::DUPSORT)
env.begin(models, makes) do |tx, tx_models, tx_makes|
tx_models.associate(tx_makes) { |sdb, key, value| value }
tx.commit
end
puts '===== Before'
puts '----- models'
models.each { |key, value| puts "#{key} = #{value}" }
puts '----- makes'
makes.each_primary { |skey, pkey, pvalue| p [skey, pkey, pvalue] }
env.begin(BDB::TXN_COMMIT, models, makes) do |tx, tx_models, tx_makes|
tx_models['Civic'] = 'Honda'
tx_models['Accord'] = 'Honda'
tx_models['S2000'] = 'Honda'
tx_models['Camry'] = 'Toyota'
tx_models['Solara'] = 'Toyota'
end
env.begin(models, makes) do |tx, tx_models, tx_makes|
puts '===== After'
puts '----- models'
tx_models.each { |key, value| puts "#{key} = #{value}" }
puts '----- makes'
tx_makes.each_primary { |skey, pkey, pvalue| p [skey, pkey, pvalue] }
end
env.begin(BDB::TXN_COMMIT, models, makes) do |tx, tx_models, tx_makes|
tx_makes.close
tx_models.close
end
env.close
Here is the output:
===== Before
----- models
----- makes
===== After
----- models
Civic = Honda
Accord = Honda
Camry = Toyota
Solara = Toyota
S2000 = Honda
----- makes
It doesn't appear to be updating the secondary index. What have I done wrong?
Cheers,
Jim