You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Kevin Burton <rk...@charter.net> on 2012/11/05 19:22:36 UTC

Exist test?

I am calling CreateDocument<Document>() but I suspect that testing if the document exists first may perform better in the long run. I am using DreamSeat for my driver but I suspect other drivers have a similar "test". My problem is that I don't know what to test for and I am unfamiliar with the available methods. Any one successfully use such a pattern (preferably with DreamSeat) that tests for existence then creates if the document doesn't exist? Keep in mind I don't initially have an id. Thank you.


Re: Exist test?

Posted by Alex G <al...@solarapex.com>.
On Mon, November 5, 2012 23:29, Kevin Burton wrote:
> I understand the HTTP verb HEAD. But I was unaware that that verb was part
> of CRUD and a REST interface, particularly in the CouchDB interface. Where
> can I learn more about HEAD and the REST interface to CouchDB?

http://wiki.apache.org/couchdb/HTTP_Document_API#HEAD

Alex



Re: Exist test?

Posted by Jens Alfke <je...@couchbase.com>.
I already pointed you to the Complete Guide book (see URL in my last post.) please read at least the first few chapters, as I think they'll answer a lot of your questions.

--Jens     [via iPhone]

On Nov 5, 2012, at 8:30 PM, "Kevin Burton" <rk...@charter.net> wrote:

> I understand the HTTP verb HEAD. But I was unaware that that verb was part of CRUD and a REST interface, particularly in the CouchDB interface. Where can I learn more about HEAD and the REST interface to CouchDB?
> 
> On Nov 5, 2012, at 10:06 PM, Dustin Sallings <du...@spy.net> wrote:
> 
>> "Kevin Burton" <rk...@charter.net>
>> writes:
>> 
>>> This tells me about the HTTP verbs but I don't know how that relates to
>>> CouchDB.
>> 
>> It specifically answers the question you asked.  You seemed to not
>> understand what HEAD means.  After reading that part of the spec, do you
>> still not understand?  If you don't, then please describe what more you
>> need, or what parts of the spec you found unclear.
>> 
>> -- 
>> dustin
>> 

Re: Exist test?

Posted by Dustin Sallings <du...@spy.net>.
Kevin Burton <rk...@charter.net>
writes:

> I understand the HTTP verb HEAD. But I was unaware that that verb was
> part of CRUD and a REST interface, particularly in the CouchDB
> interface. Where can I learn more about HEAD and the REST interface to
> CouchDB?

  HEAD does exactly what the spec says it should do.  Pretty much all of
CouchDB works the way you'd think it should work based on HTTP spec,
though you need to mind the rev when doing writes or it will reject them
as conflicted.

-- 
dustin


Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
I understand the HTTP verb HEAD. But I was unaware that that verb was part of CRUD and a REST interface, particularly in the CouchDB interface. Where can I learn more about HEAD and the REST interface to CouchDB?

On Nov 5, 2012, at 10:06 PM, Dustin Sallings <du...@spy.net> wrote:

> "Kevin Burton" <rk...@charter.net>
> writes:
> 
>> This tells me about the HTTP verbs but I don't know how that relates to
>> CouchDB.
> 
>  It specifically answers the question you asked.  You seemed to not
> understand what HEAD means.  After reading that part of the spec, do you
> still not understand?  If you don't, then please describe what more you
> need, or what parts of the spec you found unclear.
> 
> -- 
> dustin
> 

Re: Exist test?

Posted by Dustin Sallings <du...@spy.net>.
"Kevin Burton" <rk...@charter.net>
writes:

> This tells me about the HTTP verbs but I don't know how that relates to
> CouchDB.

  It specifically answers the question you asked.  You seemed to not
understand what HEAD means.  After reading that part of the spec, do you
still not understand?  If you don't, then please describe what more you
need, or what parts of the spec you found unclear.

-- 
dustin


RE: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
This tells me about the HTTP verbs but I don't know how that relates to
CouchDB.

-----Original Message-----
From: Dustin Sallings [mailto:dustin@spy.net] 
Sent: Monday, November 05, 2012 9:59 PM
To: user@couchdb.apache.org
Subject: Re: Exist test?

"Kevin Burton" <rk...@charter.net>
writes:

> So by HEAD you mean the HTTP headers? If I include HTTP headers along 
> with the PUT/POST how does CouchDB's REST interface interpret these
headers?

  http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

--
dustin



Re: Exist test?

Posted by Dustin Sallings <du...@spy.net>.
"Kevin Burton" <rk...@charter.net>
writes:

> So by HEAD you mean the HTTP headers? If I include HTTP headers along with
> the PUT/POST how does CouchDB's REST interface interpret these headers?

  http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

-- 
dustin


RE: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
So by HEAD you mean the HTTP headers? If I include HTTP headers along with
the PUT/POST how does CouchDB's REST interface interpret these headers?

-----Original Message-----
From: Benoit Chesneau [mailto:bchesneau@gmail.com] 
Sent: Monday, November 05, 2012 9:42 PM
To: user@couchdb.apache.org
Subject: Re: Exist test?

On Tue, Nov 6, 2012 at 1:45 AM, Kevin Burton <rk...@charter.net>
wrote:
> Sorry but how do I send a "HEAD with the id I create"?
>
by using an HTTP client.

- benoît


Re: Exist test?

Posted by Benoit Chesneau <bc...@gmail.com>.
On Tue, Nov 6, 2012 at 1:45 AM, Kevin Burton <rk...@charter.net> wrote:
> Sorry but how do I send a "HEAD with the id I create"?
>
by using an HTTP client.

- benoît

Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
Sorry but how do I send a "HEAD with the id I create"?

On Nov 5, 2012, at 5:08 PM, Benoit Chesneau <bc...@gmail.com> wrote:

> You can send an HEAD with the the id you will create. It is a cheap
> way to do it. However it is generally faster to just try and see what
> happen. If you generate good doc ids it will likely happen.
> 
> On Mon, Nov 5, 2012 at 7:22 PM, Kevin Burton <rk...@charter.net> wrote:
>> I am calling CreateDocument<Document>() but I suspect that testing if the document exists first may perform better in the long run. I am using DreamSeat for my driver but I suspect other drivers have a similar "test". My problem is that I don't know what to test for and I am unfamiliar with the available methods. Any one successfully use such a pattern (preferably with DreamSeat) that tests for existence then creates if the document doesn't exist? Keep in mind I don't initially have an id. Thank you.
>> 

Re: Exist test?

Posted by Benoit Chesneau <bc...@gmail.com>.
You can send an HEAD with the the id you will create. It is a cheap
way to do it. However it is generally faster to just try and see what
happen. If you generate good doc ids it will likely happen.

On Mon, Nov 5, 2012 at 7:22 PM, Kevin Burton <rk...@charter.net> wrote:
> I am calling CreateDocument<Document>() but I suspect that testing if the document exists first may perform better in the long run. I am using DreamSeat for my driver but I suspect other drivers have a similar "test". My problem is that I don't know what to test for and I am unfamiliar with the available methods. Any one successfully use such a pattern (preferably with DreamSeat) that tests for existence then creates if the document doesn't exist? Keep in mind I don't initially have an id. Thank you.
>

Initial Bulk Upload (was Re: Exist test?)

Posted by Kevin Coombes <ke...@gmail.com>.
Hi Dave,

Special thanks for your suggestion on initial bulk upload.  Point [2] 
explains why I always had to compact immediately afterwards, and reduced 
disk space usage ten-fold....

(And the subject change is so that I and others can maybe find this 
advice again in the future.)

     Kevin

On 11/6/2012 2:15 AM, Dave Cottlehuber wrote:
> On 5 November 2012 19:22, Kevin Burton <rk...@charter.net> wrote:
>> [SNIP]
>>
> Hi Kevin,
>
> [SNIP]
> If you're initially bulk uploading data, I would do 3 things
> differently to what you're currently doing.
>
> 1. assign UUIDs myself
> This is the only enforced unique indexed attribute in a DB, so use it
> well. Put something you want in it. It's basically free text ** within
> reason.
>
> 2. insert them in sorted UUID order
> CouchDB is a database and sorting matters. Couch uses a B~tree ** and
> so if you insert randomly you spend a lot of time forcing the re-write
> of intermediate nodes for no gain. As Couch is an append-only
> datastore this means several things -
> - wasted space until you compact
> - slower insert performance as you have multiple writes instead of one
> http://horicky.blogspot.co.at/2008/10/couchdb-implementation.html
>
> 3. try inserting the first few docs by hand with curl. And read up on
> the _bulk_docs API, this is much much faster.
>
> Re your drivers, there are several but I personally don't use any of
> them. There are more popular ones (based on my dodgy recollection)
> here http://wiki.apache.org/couchdb/Related_Projects hopefully some of
> the other Windows folk will pipe up.
>
> A+
> Dave
>
> ** handwavey


Re: Exist test?

Posted by Wordit <wo...@gmail.com>.
Kevin,

As Dave did, I also understood couchdb far better after spending a few
hours at the command line with curl. See what put, post, get, delete do.
Use Futon to view changes. It's all far simpler to understand at the lowest
level in plain HTTP requests.

You will figure out testing for existence yourself after you understand the
curl approach. There are several ways.

Max Ogden made a very useful tutorial video. He shows curl usage so you
understand what frameworks do behind the scenes.

http://vimeo.com/m/18808177

HTH,

Marcus

Re: Exist test?

Posted by Dave Cottlehuber <dc...@jsonified.com>.
On 5 November 2012 19:22, Kevin Burton <rk...@charter.net> wrote:
> I am calling CreateDocument<Document>() but I suspect that testing if the document exists first may perform better in the long run. I am using DreamSeat for my driver but I suspect other drivers have a similar "test". My problem is that I don't know what to test for and I am unfamiliar with the available methods. Any one successfully use such a pattern (preferably with DreamSeat) that tests for existence then creates if the document doesn't exist? Keep in mind I don't initially have an id. Thank you.
>

Hi Kevin,

A number of folk have said "read the guide first" and it's sound
advice http://guide.couchdb.org/draft/index.html as you're stuck with
conceptual stuff that's well covered in the book. I recommend skipping
the sofa chapter (it was written some time ago).

Secondly, I recommend having a play at first with pure HTTP i.e. curl
or similar. This is simply so you get a real feel for how your data is
actually stored and manipulated, before layering an abstraction on
top. It *will* save you time in the short run, and it's not scary. I
learned a huge amount about HTTP itself along the way and I'm
definitely not done yet/ So it's all good. You can also watch the HTTP
calls in & out of futon using Chrome Debugger or some other proxy like
CharlesProxy in between.

I'm assuming you are using Windows (yay!) so
http://wiki.apache.org/couchdb/Quirks_on_Windows has some tips on
using curl successfully, and I'm happy to help out if you get stuck.
Let me know if anything is not clear or out of date.

If you're initially bulk uploading data, I would do 3 things
differently to what you're currently doing.

1. assign UUIDs myself
This is the only enforced unique indexed attribute in a DB, so use it
well. Put something you want in it. It's basically free text ** within
reason.

2. insert them in sorted UUID order
CouchDB is a database and sorting matters. Couch uses a B~tree ** and
so if you insert randomly you spend a lot of time forcing the re-write
of intermediate nodes for no gain. As Couch is an append-only
datastore this means several things -
- wasted space until you compact
- slower insert performance as you have multiple writes instead of one
http://horicky.blogspot.co.at/2008/10/couchdb-implementation.html

3. try inserting the first few docs by hand with curl. And read up on
the _bulk_docs API, this is much much faster.

Re your drivers, there are several but I personally don't use any of
them. There are more popular ones (based on my dodgy recollection)
here http://wiki.apache.org/couchdb/Related_Projects hopefully some of
the other Windows folk will pipe up.

A+
Dave

** handwavey

Re: Exist test?

Posted by Alex G <al...@solarapex.com>.
On Mon, November 5, 2012 14:40, Kevin Burton wrote:
> I am mainly using it the context of initially populating a database. So
> there isn't any danger of a race because there is only one user.

I understand this does not answer your question directly. I guess, it
depends on your particular use case, but I thought I'd mention this.
Assuming you construct your key based on your unique attribute(s) I'm
wondering whether it makes sense instead of preventing a new document from
being created to go ahead and update the existing record. Then you could
reconcile data discrepancies based on revisions.

I assume it depends on how those duplicates got in your data in the first
place. If those duplicates meant to be updates (or upserts rather), then
revisions seem to me a better approach.

Alex


RE: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
I understand the concepts of CouchDB that I want but I don't know how to
implement them. Like views. I understand that you can have a view that is
part of the CouchDB database but how do I create a design document that
contains the view without creating it many times. Or the initial id. From
the documentation I understand that if an id is not supplied then CouchDB
automatically generates the ID. For some drivers like Hammock the ID is
completely hidden from the user and managed "behind the scenes". In Hammock
also the design document is automatically updated from the queries. Other
drivers require an explicit ID. My point is that there is a difference
between the raw REST interface and the drivers used to develop applications.
My purpose in posing this question is to see how others have developed
applications. I know that everyone is not only using REST. And if they were
I would still be interested in how they generated and maintained their
database(s). I initially started with DreamSeat so naturally I was more
interested in those users that have experience with this driver. Especially
as there is little or no documentation and not discussion group dedicated to
this driver or any of the individual drivers.  Since this was a CouchDB
group it was more likely that this group would have experience with using
this driver. If there is another group or documentation that I don't know
about that would be more appropriate please direct me there.

-----Original Message-----
From: Jens Alfke [mailto:jens@couchbase.com] 
Sent: Monday, November 05, 2012 6:52 PM
To: user@couchdb.apache.org
Subject: Re: Exist test?


On Nov 5, 2012, at 3:02 PM, Kevin Burton <rk...@charter.net> wrote:

> This sounds like a good idea but I am not sure how to implement it. Ideas?

I hate to say "RTFM", but . RTFM. It sounds like your next step should be to
read more about either CouchDB or the framework you're using, or both. It's
not a good use of people's time to re-explain the fundamentals when there
are already multiple sources of documentation.

For CouchDB, http://guide.couchdb.org is a decent place to start.

If it's the DreamSeat framework you need help with, it doesn't seem that
this is the right place to ask for it. I have never heard anyone mention it
here before. Hopefully it has its own mailing list or help forum.

-Jens


Re: Exist test?

Posted by Jens Alfke <je...@couchbase.com>.
On Nov 5, 2012, at 3:02 PM, Kevin Burton <rk...@charter.net> wrote:

> This sounds like a good idea but I am not sure how to implement it. Ideas?

I hate to say “RTFM”, but … RTFM. It sounds like your next step should be to read more about either CouchDB or the framework you’re using, or both. It’s not a good use of people’s time to re-explain the fundamentals when there are already multiple sources of documentation.

For CouchDB, http://guide.couchdb.org is a decent place to start.

If it’s the DreamSeat framework you need help with, it doesn’t seem that this is the right place to ask for it. I have never heard anyone mention it here before. Hopefully it has its own mailing list or help forum.

—Jens

Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
This sounds like a good idea but I am not sure how to implement it. Ideas?

On Nov 5, 2012, at 4:31 PM, Jens Alfke <je...@couchbase.com> wrote:

> 
> On Nov 5, 2012, at 12:17 PM, Kevin Burton <rk...@charter.net> wrote:
> 
>> When I create a new document both the id and rev will be null. So in order to determine if the create is a duplicate another field must be involved.
> 
> Gotcha. In general, if you have an attribute of a document that must be unique within the database, it’s a good idea to use that attribute (or a transformed/canonicalized version of it) as the document ID. That way the database itself can enforce the uniqueness. Otherwise you can end up in situations where there are multiple documents with that same attribute value, if your app supports multiple clients or does replication.
> 
>> For example say my document had a field 'title' I could construct a view to return all documents that have a given 'title'. The reason that I brought this up in this context is that if I could check to see if there were any documents that had a particular title then I would be closer to finding out if the document had already been inserted in the db.
> 
> That’s very easy to do. In generic CouchDB terms (I don’t know DreamSeat or .NET), you would
> * Create a design document containing a view with a map function:
>    function(doc) {if (doc.title) emit(doc.title, null);}
> * Query the view for a particular title by setting the startkey and endkey parameters to the title. You’ll get back a list of the document(s), if any, with that title.
> * If you get back zero rows from the query, create a new document.
> 
> —Jens

Re: Exist test?

Posted by Jens Alfke <je...@couchbase.com>.
On Nov 5, 2012, at 12:17 PM, Kevin Burton <rk...@charter.net> wrote:

> When I create a new document both the id and rev will be null. So in order to determine if the create is a duplicate another field must be involved.

Gotcha. In general, if you have an attribute of a document that must be unique within the database, it’s a good idea to use that attribute (or a transformed/canonicalized version of it) as the document ID. That way the database itself can enforce the uniqueness. Otherwise you can end up in situations where there are multiple documents with that same attribute value, if your app supports multiple clients or does replication.

> For example say my document had a field 'title' I could construct a view to return all documents that have a given 'title'. The reason that I brought this up in this context is that if I could check to see if there were any documents that had a particular title then I would be closer to finding out if the document had already been inserted in the db.

That’s very easy to do. In generic CouchDB terms (I don’t know DreamSeat or .NET), you would
* Create a design document containing a view with a map function:
	function(doc) {if (doc.title) emit(doc.title, null);}
* Query the view for a particular title by setting the startkey and endkey parameters to the title. You’ll get back a list of the document(s), if any, with that title.
* If you get back zero rows from the query, create a new document.

—Jens

Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
When I create a new document both the id and rev will be null. So in order to determine if the create is a duplicate another field must be involved.
A view (this is my crude understanding) is a function in the database that test for programmable field values in the document. A design document is basically a document of views that can be persisted to the db. For example say my document had a field 'title' I could construct a view to return all documents that have a given 'title'. The reason that I brought this up in this context is that if I could check to see if there were any documents that had a particular title then I would be closer to finding out if the document had already been inserted in the db. Problem is that I don't know how to do all this with DreamSeat. Maybe I should use another .NET driver.

On Nov 5, 2012, at 2:00 PM, Jens Alfke <je...@couchbase.com> wrote:

> 
> On Nov 5, 2012, at 11:53 AM, Kevin Burton <rk...@charter.net> wrote:
> 
>> So if I create a document that already exists how does the database know that it already exists to give me a 409 error?
> 
> If you PUT a document, and the body of your request has no ‘_rev’ property, it’s an initial creation of a new document. The request will fail if there is already a document with that ID in the database. (CouchDB can tell this apart from an update request because you didn’t include the document’s existing ‘_rev’ in the request.)
> 
> If you’re using POST, that by definition always creates a new document (with a randomly-assigned ID), so it’s meaningless to say whether the document already exists.
> 
>> How do I establish a view? Won't I run into the same problem by creating a view that already exists (via the DesignDocument)?
> 
> Sorry, I don’t understand the question.
> 
> Maybe you should back up and describe what you’re trying to do in more detail.
> 
> —Jens

Re: Exist test?

Posted by Jens Alfke <je...@couchbase.com>.
On Nov 5, 2012, at 11:53 AM, Kevin Burton <rk...@charter.net> wrote:

> So if I create a document that already exists how does the database know that it already exists to give me a 409 error?

If you PUT a document, and the body of your request has no ‘_rev’ property, it’s an initial creation of a new document. The request will fail if there is already a document with that ID in the database. (CouchDB can tell this apart from an update request because you didn’t include the document’s existing ‘_rev’ in the request.)

If you’re using POST, that by definition always creates a new document (with a randomly-assigned ID), so it’s meaningless to say whether the document already exists.

> How do I establish a view? Won't I run into the same problem by creating a view that already exists (via the DesignDocument)?

Sorry, I don’t understand the question.

Maybe you should back up and describe what you’re trying to do in more detail.

—Jens

Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
So if I create a document that already exists how does the database know that it already exists to give me a 409 error? How do I establish a view? Won't I run into the same problem by creating a view that already exists (via the DesignDocument)?

On Nov 5, 2012, at 1:45 PM, Robert Newson <rn...@apache.org> wrote:

> Easiest to just create the document, you'll get a 409 response to indicate
> that it already existed. If your documents don't have user assigned id's,
> then I don't see how you could check for existence before writing anyway.
> 
> 
> On 5 November 2012 19:40, Kevin Burton <rk...@charter.net> wrote:
> 
>> I am mainly using it the context of initially populating a database. So
>> there isn't any danger of a race because there is only one user.
>> 
>> On Nov 5, 2012, at 12:47 PM, Jens Alfke <je...@couchbase.com> wrote:
>> 
>>> 
>>> On Nov 5, 2012, at 10:22 AM, Kevin Burton <rk...@charter.net>
>> wrote:
>>> 
>>>> I am calling CreateDocument<Document>() but I suspect that testing if
>> the document exists first may perform better in the long run. I am using
>> DreamSeat for my driver but I suspect other drivers have a similar "test".
>> My problem is that I don't know what to test for and I am unfamiliar with
>> the available methods. Any one successfully use such a pattern (preferably
>> with DreamSeat) that tests for existence then creates if the document
>> doesn't exist? Keep in mind I don't initially have an id. Thank you.
>>> 
>>> I don’t know anything about that particular API, but in general,
>> check-then-create doesn’t work well in a concurrent environment. It’s prone
>> to race conditions where something else creates the resource in between
>> your check call and your create call. (The canonical example is checking
>> whether a file exists, then creating the file, which is a classic old
>> security hole in privileged Unix tools.)
>>> 
>>> —Jens
>> 

Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
So the update handler sets some global variable indicating that the document already exists? How do I tell that the update handler has run to completion?

On Nov 5, 2012, at 1:50 PM, Mark Hahn <ma...@hahnca.com> wrote:

> You can use an update handler.  It works with non-existent docs.  It's a
> very clean way to do it that I use.
> 
> On Mon, Nov 5, 2012 at 11:45 AM, Robert Newson <rn...@apache.org> wrote:
> 
>> Easiest to just create the document, you'll get a 409 response to indicate
>> that it already existed. If your documents don't have user assigned id's,
>> then I don't see how you could check for existence before writing anyway.
>> 
>> 
>> On 5 November 2012 19:40, Kevin Burton <rk...@charter.net> wrote:
>> 
>>> I am mainly using it the context of initially populating a database. So
>>> there isn't any danger of a race because there is only one user.
>>> 
>>> On Nov 5, 2012, at 12:47 PM, Jens Alfke <je...@couchbase.com> wrote:
>>> 
>>>> 
>>>> On Nov 5, 2012, at 10:22 AM, Kevin Burton <rk...@charter.net>
>>> wrote:
>>>> 
>>>>> I am calling CreateDocument<Document>() but I suspect that testing if
>>> the document exists first may perform better in the long run. I am using
>>> DreamSeat for my driver but I suspect other drivers have a similar
>> "test".
>>> My problem is that I don't know what to test for and I am unfamiliar with
>>> the available methods. Any one successfully use such a pattern
>> (preferably
>>> with DreamSeat) that tests for existence then creates if the document
>>> doesn't exist? Keep in mind I don't initially have an id. Thank you.
>>>> 
>>>> I don’t know anything about that particular API, but in general,
>>> check-then-create doesn’t work well in a concurrent environment. It’s
>> prone
>>> to race conditions where something else creates the resource in between
>>> your check call and your create call. (The canonical example is checking
>>> whether a file exists, then creating the file, which is a classic old
>>> security hole in privileged Unix tools.)
>>>> 
>>>> —Jens
>> 

Re: Exist test?

Posted by Mark Hahn <ma...@hahnca.com>.
You can use an update handler.  It works with non-existent docs.  It's a
very clean way to do it that I use.

On Mon, Nov 5, 2012 at 11:45 AM, Robert Newson <rn...@apache.org> wrote:

> Easiest to just create the document, you'll get a 409 response to indicate
> that it already existed. If your documents don't have user assigned id's,
> then I don't see how you could check for existence before writing anyway.
>
>
> On 5 November 2012 19:40, Kevin Burton <rk...@charter.net> wrote:
>
> > I am mainly using it the context of initially populating a database. So
> > there isn't any danger of a race because there is only one user.
> >
> > On Nov 5, 2012, at 12:47 PM, Jens Alfke <je...@couchbase.com> wrote:
> >
> > >
> > > On Nov 5, 2012, at 10:22 AM, Kevin Burton <rk...@charter.net>
> > wrote:
> > >
> > >> I am calling CreateDocument<Document>() but I suspect that testing if
> > the document exists first may perform better in the long run. I am using
> > DreamSeat for my driver but I suspect other drivers have a similar
> "test".
> > My problem is that I don't know what to test for and I am unfamiliar with
> > the available methods. Any one successfully use such a pattern
> (preferably
> > with DreamSeat) that tests for existence then creates if the document
> > doesn't exist? Keep in mind I don't initially have an id. Thank you.
> > >
> > > I don’t know anything about that particular API, but in general,
> > check-then-create doesn’t work well in a concurrent environment. It’s
> prone
> > to race conditions where something else creates the resource in between
> > your check call and your create call. (The canonical example is checking
> > whether a file exists, then creating the file, which is a classic old
> > security hole in privileged Unix tools.)
> > >
> > > —Jens
> >
>

Re: Exist test?

Posted by Robert Newson <rn...@apache.org>.
Easiest to just create the document, you'll get a 409 response to indicate
that it already existed. If your documents don't have user assigned id's,
then I don't see how you could check for existence before writing anyway.


On 5 November 2012 19:40, Kevin Burton <rk...@charter.net> wrote:

> I am mainly using it the context of initially populating a database. So
> there isn't any danger of a race because there is only one user.
>
> On Nov 5, 2012, at 12:47 PM, Jens Alfke <je...@couchbase.com> wrote:
>
> >
> > On Nov 5, 2012, at 10:22 AM, Kevin Burton <rk...@charter.net>
> wrote:
> >
> >> I am calling CreateDocument<Document>() but I suspect that testing if
> the document exists first may perform better in the long run. I am using
> DreamSeat for my driver but I suspect other drivers have a similar "test".
> My problem is that I don't know what to test for and I am unfamiliar with
> the available methods. Any one successfully use such a pattern (preferably
> with DreamSeat) that tests for existence then creates if the document
> doesn't exist? Keep in mind I don't initially have an id. Thank you.
> >
> > I don’t know anything about that particular API, but in general,
> check-then-create doesn’t work well in a concurrent environment. It’s prone
> to race conditions where something else creates the resource in between
> your check call and your create call. (The canonical example is checking
> whether a file exists, then creating the file, which is a classic old
> security hole in privileged Unix tools.)
> >
> > —Jens
>

Re: Exist test?

Posted by Kevin Burton <rk...@charter.net>.
I am mainly using it the context of initially populating a database. So there isn't any danger of a race because there is only one user.

On Nov 5, 2012, at 12:47 PM, Jens Alfke <je...@couchbase.com> wrote:

> 
> On Nov 5, 2012, at 10:22 AM, Kevin Burton <rk...@charter.net> wrote:
> 
>> I am calling CreateDocument<Document>() but I suspect that testing if the document exists first may perform better in the long run. I am using DreamSeat for my driver but I suspect other drivers have a similar "test". My problem is that I don't know what to test for and I am unfamiliar with the available methods. Any one successfully use such a pattern (preferably with DreamSeat) that tests for existence then creates if the document doesn't exist? Keep in mind I don't initially have an id. Thank you.
> 
> I don’t know anything about that particular API, but in general, check-then-create doesn’t work well in a concurrent environment. It’s prone to race conditions where something else creates the resource in between your check call and your create call. (The canonical example is checking whether a file exists, then creating the file, which is a classic old security hole in privileged Unix tools.)
> 
> —Jens

Re: Exist test?

Posted by Jens Alfke <je...@couchbase.com>.
On Nov 5, 2012, at 10:22 AM, Kevin Burton <rk...@charter.net> wrote:

> I am calling CreateDocument<Document>() but I suspect that testing if the document exists first may perform better in the long run. I am using DreamSeat for my driver but I suspect other drivers have a similar "test". My problem is that I don't know what to test for and I am unfamiliar with the available methods. Any one successfully use such a pattern (preferably with DreamSeat) that tests for existence then creates if the document doesn't exist? Keep in mind I don't initially have an id. Thank you.

I don’t know anything about that particular API, but in general, check-then-create doesn’t work well in a concurrent environment. It’s prone to race conditions where something else creates the resource in between your check call and your create call. (The canonical example is checking whether a file exists, then creating the file, which is a classic old security hole in privileged Unix tools.)

—Jens