MB-6860: Fix spatial index file descriptors leaks

On the spatial indexes the file descriptors leaked, when a design
document was updated. The issue (and fix) is similar to the
Apache CouchDB issue COUCHDB-1309 [1].

In Couchbase the design document only lives in the master database,
hence the ddoc_updated event is only triggered for that database,
but we need to clean up the spatial indexes for all vBuckets.

The solution is to save not only a reference of the spatial view
group but also a reference to the database that contains the
design document. This way a cleanup on all vBuckets can be done.

[1] https://issues.apache.org/jira/browse/COUCHDB-1309

Change-Id: Ia9fc277f163a7adbce8a47b0a4212d6ec606858c
Reviewed-on: http://review.couchbase.org/21592
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
MB-6860: Don't leak spatial file descriptors on DB files

When a database was compacted, there were still file descriptors
of the spatial indexes still kept open on the old, pre-compaction
database files.

This commit is based on commits [1][2][3][4] from Apache CouchDB.

[1] https://github.com/apache/couchdb/commit/0ce2c544b632cb6b405738d5e83d618e079a5d9c
[2] https://github.com/apache/couchdb/commit/c0de37df72b240622faf819457485f694c391e21
[3] https://github.com/apache/couchdb/commit/152f2b9bc20e4078173c69652b0bc54038528c75
[4] https://github.com/apache/couchdb/commit/12a593c61dc0f154f3ade65a50706a019103d680

Change-Id: I2ff9ee18a8ae5d07ddb3ffbdd7f086bfa91a1d65
Reviewed-on: http://review.couchbase.org/21714
Tested-by: buildbot <build@couchbase.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
MB-6990: Add validation of spatial function

The spatial function in a Design Document now has the same
validation as the mapreduce funtions in normal views. You'll
get errors when the spatial function isn't a valid one, or
the name of the function has leading/trailing whitespace.

This commit needs http://review.couchbase.org/21891
else the 300-validation.t tests will fail.

Change-Id: I88b7e20ba32ae66058da4d7d122190153f66eeba
Reviewed-on: http://review.couchbase.org/21890
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Farshid Ghods <farshid@couchbase.com>
Tested-by: Farshid Ghods <farshid@couchbase.com>
MB-7013: There is a branch for Apache CouchDB 1.3.x now

Change-Id: Ie865dc966db5be59b89138685e67f48017dbe50a
Reviewed-on: http://review.couchbase.org/21960
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: Volker Mische <volker.mische@gmail.com>
MB-7193: Make Spatial Views work that contain slashes

Spatial Views that contained slashed in either the Design Document
or the function name didn't work.

Those three commits need to be merged at the same time:
http://review.couchbase.org/23138
http://review.couchbase.org/23175
http://review.couchbase.org/23139

Change-Id: I9142509f8fe9f2afab681f549a145856c065652d
Reviewed-on: http://review.couchbase.org/23175
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
Merge remote-tracking branch 'couchbase/2.0.1' into HEAD

* couchbase/2.0.1:
  MB-7193: Make Spatial Views work that contain slashes

Change-Id: I1dff1822ceba6be425b7e78fa2cfed80786c194b
GC-3: Make COUCH_SRC parsing less fragile

Newer version of rebar support a file called
rebar.config.script to be run before the
actual compiling is done. This way we can
inject the value of COUCH_SRC and the MAKECHECK
environment variable in a less fragile way.

Change-Id: Ifb0082ac1b1272f590897ab6dacdcbf4a87ca6fd
Reviewed-on: http://review.couchbase.org/23397
Reviewed-by: Bin Cui <bin.cui@gmail.com>
Tested-by: Bin Cui <bin.cui@gmail.com>
Merge remote-tracking branch 'origin/2.0.1'

* origin/2.0.1:
  GC-3: Make COUCH_SRC parsing less fragile

Change-Id: I1680f1661a716018a9da2b3963db80e0bdb96af1
MB-7084: Pass on limit parameter in spatial merger

The limit parameter is now passed on to the other nodes when
the spatial view merger is used. Note that the limit is set
to the original limit plus the original skip. The reason is
that you never know which node needs to skip the results to
which amount (but the maximum number of documents needed is
definitely <= limit + skip).

Change-Id: I7fa9884ef673bbe88c034353ca418ef6843702ed
Reviewed-on: http://review.couchbase.org/22228
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Volker Mische <volker.mische@gmail.com>
Tested-by: Volker Mische <volker.mische@gmail.com>
MB-6895 Deal with new mapreduce API

When a map call succeeds, the returned value is no longer
only a list of lists of KVs. An outter list element can
also be an error tuple now, which tells us there was an
error for a specific map function of a view.

Change-Id: If4a19c0d21e21756f749747841c07ec00b798d85
Reviewed-on: http://review.couchbase.org/24206
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Merge remote-tracking branch 'origin/2.0.1'

* origin/2.0.1:
  MB-6895 Deal with new mapreduce API

Change-Id: I945cd2d7d68b3041c688fd1793d144fe980d994a
CBD-829:  create 2.0.2 branch

Change-Id: Id53732ad5b4158ad3a5789c0bb5c4caa65884788
Reviewed-on: http://review.couchbase.org/24527
Reviewed-by: Philip Labee <philip@couchbase.com>
Tested-by: Philip Labee <philip@couchbase.com>
MB-7406: Add updater barrier for spatial indexer

Currently there's one spatial indexer for every vBucket,
hence a lot of updaters are running at the same time.
This commit adds a barrier, so that you can set the
maximum number of updaters that should run at the same
time. The default value is 4. The MapReduce indexer
already uses such a barrier.

Those two commits should me merged at the same time:
http://review.couchbase.org/24830
http://review.couchbase.org/24831

Change-Id: Ibcf423073b0e01055f5d8952f4d3656ab8959464
Reviewed-on: http://review.couchbase.org/24831
Tested-by: Volker Mische <volker.mische@gmail.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Tested-by: Filipe David Borba Manana <fdmanana@gmail.com>
Merge remote-tracking branch 'origin/2.0.2'

* origin/2.0.2:
  MB-7406: Add updater barrier for spatial indexer
  CBD-829:  create 2.0.2 branch

Change-Id: Icca00c51c3801498a0b6f2ada9d3ac3d74881ad9
MB-8648: Adapt the spatial merger for the changes

Change-Id: If45cc878f46d967e32762905a21c4027f5d2ec16
Reviewed-on: http://review.couchbase.org/27553
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
MB-10138: Writing a header now also returns the position

The function to write a header now also returns the position. This change
was introduce in commit /79038773b2ed4beaa1e1bf2e889fcff4141ff49e [1][2].

This fixes the testrunner spatialquery.conf tests (tested on single
and 4 nodes).

[1] https://github.com/couchbase/couchdb/commit/79038773b2ed4beaa1e1bf2e889fcff4141ff49e
[2] http://review.couchbase.org/28816

Change-Id: Ied2f89ff72990e1f93e5eb7cbe91338684e9e06f
Reviewed-on: http://review.couchbase.org/33284
Reviewed-by: Filipe David Borba Manana <fdmanana@gmail.com>
Tested-by: Volker Mische <volker.mische@gmail.com>
Converting geocouch to CMake.

The following CMake targets exist:

"geocouch" -just compiles the geocouch source, using rebar as
always. ("make all" is the default, and "all" depend on "geocouch", so
"make" by itself will compile the source.)

"geocouch_makecheck" - same as old "compileforcheck" target.

"geocouch_check" - same as old "check" target; cleans, runs
geocouch_makecheck, and then runs the tests.

"realclean" - same as old "clean".

I specifically did not include .tar.gz or "dist" targets as they
support the old, undesirable way of creating source tarballs.

Change-Id: I9098c47db8b50fdde760130391b529f987e74d03