我有以下存储或更新事件 json 对象列表的方法。我找不到 couchdb 的批量 create_or_update 函数,我必须查询每个对象并查看它是否存在于数据库中并相应地创建/更新。不幸的是,这是非常低效的,处理 1725 个事件需要 6 分钟。有人可以提出更好的设计吗?它必须在几秒钟内完成。我的 couchdb 实际上是一个 ssl cloudant 数据库,我的应用程序托管在 Heroku 上,这与 heroku 上实际与 cloudant 结合的应用程序不同。
def self.store(bulk, resource)
JSON::Validator.validate!(SCHEMA, bulk, :list => true)
bulk.each{ |event|
response = resource.get("/database-dev/_design/Event/_view/byEID?key=\"#{event['eid']}\"")
if (response["rows"].nil? || response["rows"].empty?) then
o = [('a'..'z'),('A'..'Z'),(0..9)].map{|i| i.to_a}.flatten
o.push('-','_')
event['_id'] = (0..50).map{ o[rand(o.length)] }.join
event['resource'] = 'Event'
resource.post('/database-dev', event.to_json)
else
resource.put("/database-dev/#{response['rows'][0]['id']}", event.to_json)
end
}
end