From f2275fd25463764ea059b32ffb689baeeebc1ca1 Mon Sep 17 00:00:00 2001 From: "Damien F. Katz" Date: Fri, 8 Aug 2008 21:06:29 +0000 Subject: Idempotent document creation support, new HTTP api to generate UUIDs and support in the couch.js library for using them. Creating uuids client side ensure that document creation happens only once, despite automatic network retries. git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@684092 13f79535-47bb-0310-9956-ffa450edef68 --- share/www/script/couch.js | 50 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 10 deletions(-) (limited to 'share/www/script/couch.js') diff --git a/share/www/script/couch.js b/share/www/script/couch.js index 38c9cf34..2f15737d 100644 --- a/share/www/script/couch.js +++ b/share/www/script/couch.js @@ -42,18 +42,14 @@ function CouchDB(name) { this.save = function(doc, options) { var req; if (doc._id == undefined) - req = request("POST", this.uri + encodeOptions(options), { - body: JSON.stringify(doc) - }); - else - req = request("PUT", this.uri + encodeURIComponent(doc._id) + encodeOptions(options), { - body: JSON.stringify(doc) - }); + doc._id = CouchDB.newUuids(1)[0]; + + req = request("PUT", this.uri + encodeURIComponent(doc._id) + encodeOptions(options), { + body: JSON.stringify(doc) + }); var result = JSON.parse(req.responseText); if (req.status != 201) throw result; - // set the _id and _rev members on the input object, for caller convenience. - doc._id = result.id; doc._rev = result.rev; return result; } @@ -91,6 +87,18 @@ function CouchDB(name) { } this.bulkSave = function(docs, options) { + // first prepoulate the UUIDs for new documents + var newCount = 0 + for (var i=0; i= n) { + var uuids = CouchDB.uuids_cache.slice(CouchDB.uuids_cache.length - n); + if(CouchDB.uuids_cache.length - n == 0) { + CouchDB.uuids_cache = []; + } else { + CouchDB.uuids_cache = + CouchDB.uuids_cache.slice(0, CouchDB.uuids_cache.length - n); + } + return uuids; + } else { + var req = CouchDB.request("POST", "/_uuids?count=" + (100 + n)); + var result = JSON.parse(req.responseText); + if (req.status != 200) + throw result; + CouchDB.uuids_cache = + CouchDB.uuids_cache.concat(result.uuids.slice(0, 100)); + return result.uuids.slice(100); + } + } -- cgit v1.2.3