couchdb icon indicating copy to clipboard operation
couchdb copied to clipboard

Add key range check for _all_docs

Open jiahuili430 opened this issue 1 year ago • 3 comments

Overview

Using key and start/end_key in a different order will produce different responses when querying _all_docs. To reduce confusion, add a key range check for _all_docs.

  • If direction=fwd, start_key > end_key throws an error
  • If direction=rev, start_key < end_key throws an error
  • Otherwise, return relevant responses

Previous behavior:

$ curl -XPUT $DB/db
$ curl -XPOST $DB/db/_bulk_docs -H 'Content-Type: application/json' -d '{"docs": [{"_id": "a"},{"_id": "b"},{"_id": "c"}]}'

$ curl $DB/db/_all_docs'?key="c"&endkey="a"'
{"total_rows":3,"offset":2,"rows":[ ]}

$ curl $DB/db/_all_docs'?key="c"&endkey="a"&descending=true'
{"total_rows":3,"offset":0,"rows":[
  {"id":"c","key":"c","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}},
  {"id":"b","key":"b","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}},
  {"id":"a","key":"a","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}}
]}

After the change:

$ curl $DB/db/_all_docs'?key="c"&endkey="a"'
{"error":"query_parse_error","reason":"No rows can match your key range, reverse your start_key and end_key or set descending=true"}

$ curl $DB/db/_all_docs'?key="c"&endkey="a"&descending=true'
{"total_rows":3,"offset":0,"rows":[
  {"id":"c","key":"c","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}},
  {"id":"b","key":"b","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}},
  {"id":"a","key":"a","value":{"rev":"1-967a00dff5e02add41819138abb3284d"}}
]}

Testing recommendations

make eunit apps=chttpd,couch_mrview

Related Issues or Pull Requests

https://github.com/apache/couchdb/issues/3977

Checklist

  • [ ] Code is written and works correctly
  • [x] Changes are covered by tests
  • [ ] Any new configurable parameters are documented in rel/overlay/etc/default.ini
  • [ ] Documentation changes were made in the src/docs folder
  • [ ] Documentation changes were backported (separated PR) to affected branches

jiahuili430 avatar Apr 07 '23 21:04 jiahuili430