You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Fred Lemasson <dj...@gmail.com> on 2010/04/05 14:14:48 UTC

delete Docs

I everyone

I could use some advice on CouchDB "Best Pratice" for deleting documents.

Lets say I know the id of some docs I want to delete, I still have to
retreive their _rev number before sending the DELETE. So, do I really
have to read(GET request) an entire document just to get its _rev or
should I create a _view to get _id + _rev pairs ?
Both ways seem non ideal to me, because why loading an entire document
just to read its _rev (it might be a really big doc and u might have a
lot of them) and why create a _view that will suffer from indexation
process, reading a single value is supposed to be quick ...
Or may be there is a way to get "just" the _rev from some doc _id that
I don't know about ?

Feel free to show me the way ... and let me know how u proceed ...

Fred

Re: delete Docs

Posted by Jan Lehnardt <ja...@apache.org>.
On 6 Apr 2010, at 16:22, Noah Slater wrote:

> 
> On 6 Apr 2010, at 15:15, Jan Lehnardt wrote:
> 
>> 
>> On 6 Apr 2010, at 09:53, wolfgang haefelinger wrote:
>>>> and indeed couchdb-python uses this :
>>>> 
>>>>      status, headers, data = self.resource.head(id)
>>>>      self.resource.delete_json(id, rev=headers['etag'].strip('"'))
>>> 
>>> Strange. Wouldn't we expect that deleting a resource is independent
>>> from its representation, would we?
>> 
>> No we wouldn't :) — When writing, you need to prove that you don't
>> accidentally overwrite anybody else's data. This is not a REST but
>> a CouchDB constraint, but it doesn't violate REST really. Also,
>> deleting data is bad, mmkay :)
> 
> The distinction here is that you're not deleting the resource at all, you are deleting the representation. So CouchDB asks you to demonstrate that you know which version of the representation you're wanting to delete. It's a core part of CouchDB's conflict protection mechanism.


Ah yes, thanks Noah :)

Cheers
Jan
--



Re: delete Docs

Posted by Jan Lehnardt <ja...@apache.org>.
On 7 Apr 2010, at 12:55, wolfgang haefelinger wrote:

> On Wed, Apr 7, 2010 at 12:25 PM, Jan Lehnardt <ja...@apache.org> wrote:
> 
>>> of the resource in order to delete it. After all, the
>>> following appears to work fine (still using version 0.10 ):
>>> 
>>> $ curl -X DELETE http://localhost:5984/mydb/myid?rev=whatever
>> 
>> You are saying are are able to delete a document by passing in a rev that doesn't match the rev in the document? Can you create a reproducing test case for that?
> 
> No, I'm not saying this. Where do you read that?

Sorry, I got that wrong. I interpreted `rev=whatever` as `rev=nonmatchingrev`.


> I'm stating that there is something fishy with "couchdb-python". As an
> example, I taking this line of code
> 
> self.resource.delete_json(id, rev=headers['etag'].strip('"'))
> 
> and I wonder about "delete_json()" - which does not make sense. I
> would expect  something like
> 
> self.resource.delete(id, rev=headers['etag'].strip('"'))
> 
> because a document's representation does not matter when deleting it.
> The original code is equivalent with
> 
> $ curl -H "content-type: application/json" -X DELETE  ${url}
> 
> Providing that mime-type is unnecessary however, cause this works fine:
> 
> $ curl -X DELETE  ${url}

By default CouchDB replies with the `text/plain` mime-type to have browsers
render the JSON instead of offering it for download. If you send the
`application/json` mime-type with the request, CouchDB will also use it
with the response. So this is couchdb-python just making sure it gets
the correct mime-type returned. CouchDB's behaviour is nonstandard, 
but significantly better for usability.

Sorry for the earlier confusion.

Cheers
Jan
--


Re: delete Docs

Posted by wolfgang haefelinger <wh...@gmail.com>.
On Wed, Apr 7, 2010 at 12:25 PM, Jan Lehnardt <ja...@apache.org> wrote:

>> of the resource in order to delete it. After all, the
>> following appears to work fine (still using version 0.10 ):
>>
>> $ curl -X DELETE http://localhost:5984/mydb/myid?rev=whatever
>
> You are saying are are able to delete a document by passing in a rev that doesn't match the rev in the document? Can you create a reproducing test case for that?

No, I'm not saying this. Where do you read that?

I'm stating that there is something fishy with "couchdb-python". As an
example, I taking this line of code

self.resource.delete_json(id, rev=headers['etag'].strip('"'))

and I wonder about "delete_json()" - which does not make sense. I
would expect  something like

self.resource.delete(id, rev=headers['etag'].strip('"'))

because a document's representation does not matter when deleting it.
The original code is equivalent with

 $ curl -H "content-type: application/json" -X DELETE  ${url}

Providing that mime-type is unnecessary however, cause this works fine:

$ curl -X DELETE  ${url}

Re: delete Docs

Posted by Jan Lehnardt <ja...@apache.org>.
On 7 Apr 2010, at 07:53, wolfgang haefelinger wrote:

> I would like to disagree ..
> 
> My remark was about why one needs to know the representation (here
> Javascript)

You mean JSON? :)

> of the resource in order to delete it. After all, the
> following appears to work fine (still using version 0.10 ):
> 
> $ curl -X DELETE http://localhost:5984/mydb/myid?rev=whatever

You are saying are are able to delete a document by passing in a rev that doesn't match the rev in the document? Can you create a reproducing test case for that?

Cheers
Jan
--


> 
> 
> 
> On Tue, Apr 6, 2010 at 4:22 PM, Noah Slater <ns...@me.com> wrote:
>> 
>> On 6 Apr 2010, at 15:15, Jan Lehnardt wrote:
>> 
>>> 
>>> On 6 Apr 2010, at 09:53, wolfgang haefelinger wrote:
>>>>> and indeed couchdb-python uses this :
>>>>> 
>>>>>       status, headers, data = self.resource.head(id)
>>>>>       self.resource.delete_json(id, rev=headers['etag'].strip('"'))
>>>> 
>>>> Strange. Wouldn't we expect that deleting a resource is independent
>>>> from its representation, would we?
>>> 
>>> No we wouldn't :) — When writing, you need to prove that you don't
>>> accidentally overwrite anybody else's data. This is not a REST but
>>> a CouchDB constraint, but it doesn't violate REST really. Also,
>>> deleting data is bad, mmkay :)
>> 
>> The distinction here is that you're not deleting the resource at all, you are deleting the representation. So CouchDB asks you to demonstrate that you know which version of the representation you're wanting to delete. It's a core part of CouchDB's conflict protection mechanism.
> 
> 
> 
> -- 
> Wolfgang Häfelinger
> häfelinger IT - Applied Software Architecture
> http://www.haefelinger.it
> +31 648 27 61 59


Re: delete Docs

Posted by wolfgang haefelinger <wh...@gmail.com>.
I would like to disagree ..

My remark was about why one needs to know the representation (here
Javascript) of the resource in order to delete it. After all, the
following appears to work fine (still using version 0.10 ):

$ curl -X DELETE http://localhost:5984/mydb/myid?rev=whatever



On Tue, Apr 6, 2010 at 4:22 PM, Noah Slater <ns...@me.com> wrote:
>
> On 6 Apr 2010, at 15:15, Jan Lehnardt wrote:
>
>>
>> On 6 Apr 2010, at 09:53, wolfgang haefelinger wrote:
>>>> and indeed couchdb-python uses this :
>>>>
>>>>       status, headers, data = self.resource.head(id)
>>>>       self.resource.delete_json(id, rev=headers['etag'].strip('"'))
>>>
>>> Strange. Wouldn't we expect that deleting a resource is independent
>>> from its representation, would we?
>>
>> No we wouldn't :) — When writing, you need to prove that you don't
>> accidentally overwrite anybody else's data. This is not a REST but
>> a CouchDB constraint, but it doesn't violate REST really. Also,
>> deleting data is bad, mmkay :)
>
> The distinction here is that you're not deleting the resource at all, you are deleting the representation. So CouchDB asks you to demonstrate that you know which version of the representation you're wanting to delete. It's a core part of CouchDB's conflict protection mechanism.



-- 
Wolfgang Häfelinger
häfelinger IT - Applied Software Architecture
http://www.haefelinger.it
+31 648 27 61 59

Re: delete Docs

Posted by Noah Slater <ns...@me.com>.
On 6 Apr 2010, at 15:15, Jan Lehnardt wrote:

> 
> On 6 Apr 2010, at 09:53, wolfgang haefelinger wrote:
>>> and indeed couchdb-python uses this :
>>> 
>>>       status, headers, data = self.resource.head(id)
>>>       self.resource.delete_json(id, rev=headers['etag'].strip('"'))
>> 
>> Strange. Wouldn't we expect that deleting a resource is independent
>> from its representation, would we?
> 
> No we wouldn't :) — When writing, you need to prove that you don't
> accidentally overwrite anybody else's data. This is not a REST but
> a CouchDB constraint, but it doesn't violate REST really. Also,
> deleting data is bad, mmkay :)

The distinction here is that you're not deleting the resource at all, you are deleting the representation. So CouchDB asks you to demonstrate that you know which version of the representation you're wanting to delete. It's a core part of CouchDB's conflict protection mechanism.

Re: delete Docs

Posted by Jan Lehnardt <ja...@apache.org>.
On 6 Apr 2010, at 09:53, wolfgang haefelinger wrote:
>> and indeed couchdb-python uses this :
>> 
>>        status, headers, data = self.resource.head(id)
>>        self.resource.delete_json(id, rev=headers['etag'].strip('"'))
> 
> Strange. Wouldn't we expect that deleting a resource is independent
> from its representation, would we?

No we wouldn't :) — When writing, you need to prove that you don't
accidentally overwrite anybody else's data. This is not a REST but
a CouchDB constraint, but it doesn't violate REST really. Also,
deleting data is bad, mmkay :)

Cheers
Jan
--


Re: delete Docs

Posted by wolfgang haefelinger <wh...@gmail.com>.
> I'm not using couchdb-python yet, still playing with httplib2 to try
> and understand the low levels

Use 'curl' to look behind the scenes of that REST api.

> and indeed couchdb-python uses this :
>
>        status, headers, data = self.resource.head(id)
>        self.resource.delete_json(id, rev=headers['etag'].strip('"'))

Strange. Wouldn't we expect that deleting a resource is independent
from its representation, would we?

Re: delete Docs

Posted by Fred Lemasson <dj...@gmail.com>.
Thanks a lot Nils

thats an elegant, effective and lightweight solution that goes way
beyond my expectations :)
I'm not using couchdb-python yet, still playing with httplib2 to try
and understand the low levels

and indeed couchdb-python uses this :

        status, headers, data = self.resource.head(id)
        self.resource.delete_json(id, rev=headers['etag'].strip('"'))


Fred

2010/4/5 Nils Breunese <N....@vpro.nl>:
> You don't need to GET the full document to retrieve the current revision. You can just do a HEAD request and look at the Etag header, which contains the document _rev.
>
> ----
> $ curl --head http://example.com/database/document
> HTTP/1.1 200 OK
> Date: Mon, 05 Apr 2010 12:26:39 GMT
> Server: CouchDB/0.10.1 (Erlang OTP/R13B)
> Etag: "86-ff15272d40894c8d7ece25891a874230"
> (...)
> ----
>
> I usually work with a CouchDB library which abstracts this away (I like couchdb-python for scripting), but I guess they all use HEAD requests under the hood.
>
> Nils Breunese.

RE: delete Docs

Posted by Nils Breunese <N....@vpro.nl>.
You don't need to GET the full document to retrieve the current revision. You can just do a HEAD request and look at the Etag header, which contains the document _rev.

----
$ curl --head http://example.com/database/document
HTTP/1.1 200 OK
Date: Mon, 05 Apr 2010 12:26:39 GMT
Server: CouchDB/0.10.1 (Erlang OTP/R13B)
Etag: "86-ff15272d40894c8d7ece25891a874230"
(...)
----

I usually work with a CouchDB library which abstracts this away (I like couchdb-python for scripting), but I guess they all use HEAD requests under the hood.

Nils Breunese.
________________________________________
Van: Fred Lemasson [djassper@gmail.com]
Verzonden: maandag 5 april 2010 14:14
Aan: user@couchdb.apache.org
Onderwerp: delete Docs

I everyone

I could use some advice on CouchDB "Best Pratice" for deleting documents.

Lets say I know the id of some docs I want to delete, I still have to
retreive their _rev number before sending the DELETE. So, do I really
have to read(GET request) an entire document just to get its _rev or
should I create a _view to get _id + _rev pairs ?
Both ways seem non ideal to me, because why loading an entire document
just to read its _rev (it might be a really big doc and u might have a
lot of them) and why create a _view that will suffer from indexation
process, reading a single value is supposed to be quick ...
Or may be there is a way to get "just" the _rev from some doc _id that
I don't know about ?

Feel free to show me the way ... and let me know how u proceed ...

Fred

De informatie vervat in deze  e-mail en meegezonden bijlagen is uitsluitend bedoeld voor gebruik door de geadresseerde en kan vertrouwelijke informatie bevatten. Openbaarmaking, vermenigvuldiging, verspreiding en/of verstrekking van deze informatie aan derden is voorbehouden aan geadresseerde. De VPRO staat niet in voor de juiste en volledige overbrenging van de inhoud van een verzonden e-mail, noch voor tijdige ontvangst daarvan.