Skip to content

Commit

Permalink
s/geocouch/refuge_spatial/g
Browse files Browse the repository at this point in the history
  • Loading branch information
nrdufour committed Mar 23, 2012
1 parent 11edf5b commit 7fcc13e
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 100 deletions.
4 changes: 2 additions & 2 deletions Makefile
@@ -1,8 +1,8 @@
all: compile

compile:
@./rebar compile
@rebar compile

clean:
@./rebar clean
@rebar clean

36 changes: 23 additions & 13 deletions README.md
@@ -1,22 +1,32 @@
Welcome to the world of GeoCouch
================================

GeoCouch is a spatial extension for Apache CouchDB.
GeoCouch is a spatial extension for Apache CouchDB and Couchbase.

Prerequisites
-------------

A working installation of CouchDB with corresponding source
code. GeoCouch works best with the latest stable releases of CouchDB
(must be >= 1.0.2).
code. GeoCouch works best with Couchbase and the latest stable releases of
CouchDB (should be >= 1.1.0).

### Understanding the branches:

This repository contains several branches, please make sure you use
the correct one:

- master: works with the CouchDB master branch from Couchbase's repo
(https://github.com/couchbase/couchdb)
- couchdb1.1.x: works with Apache CouchDB 1.1.x
- coucudb1.2.x: should work with the current Apache CouchDB trunk version,
but currently doesn't.

Installation
------------

### Get GeoCouch:

git clone https://github.com/couchone/geocouch.git
git clone https://github.com/couchbase/geocouch.git
cd geocouch

### Compilation
Expand Down Expand Up @@ -47,7 +57,7 @@ over (from `<geocouch>/share/www/script/test` to

cp <geocouch>/share/www/script/test/* <vanilla-couch>/share/www/script/test/

Add the test to `<vanilla-couch>/share/www/script/test/couch_tests.js`
Add the test to `<vanilla-couch>/share/www/script/couch_tests.js`

loadTest("spatial.js");
loadTest("list_spatial.js");
Expand Down Expand Up @@ -93,7 +103,7 @@ Make a bounding box request:
It should return:

{"update_seq":3,"rows":[
{"id":"augsburg","bbox":[10.898333,48.371667,10.898333,48.371667],"value":["augsburg",[10.898333,48.371667]]}
{"id":"augsburg","bbox":[10.898333,48.371667,10.898333,48.371667],"geometry":{"type":"Point","coordinates":[10.898333,48.371667]},"value":["augsburg",[10.898333,48.371667]]}
]}

The Design Document Function
Expand Down Expand Up @@ -146,8 +156,8 @@ And request only Australia and Brasilia:
The result is as expected:

{"update_seq":6,"rows":[
{"id":"australia","bbox":[135,-25,135,-25],"value":["australia",[135,-25]]},
{"id":"brasilia","bbox":[-52.95,-10.65,-52.95,-10.65],"value":["brasilia",[-52.95,-10.65]]}
{"id":"australia","bbox":[135,-25,135,-25],"geometry":{"type":"Point","coordinates":[135,-25]},"value":["australia",[135,-25]]},
{"id":"brasilia","bbox":[-52.95,-10.65,-52.95,-10.65],"geometry":{"type":"Point","coordinates":[-52.95,-10.65]},"value":["brasilia",[-52.95,-10.65]]}
]}

The bounding with the same numbers, but different order
Expand All @@ -156,7 +166,7 @@ The bounding with the same numbers, but different order
curl -X GET 'http://localhost:5984/places/_design/main/_spatial/points?bbox=-30,-60,110,15&plane_bounds=-180,-90,180,90'

{"update_seq":6,"rows":[
{"id":"namibia","bbox":[17.15,-22.566667,17.15,-22.566667],"value":["namibia",[17.15,-22.566667]]}
{"id":"namibia","bbox":[17.15,-22.566667,17.15,-22.566667],"geometry":{"type":"Point","coordinates":[17.15,-22.566667]},"value":["namibia",[17.15,-22.566667]]}
]}

List function support
Expand All @@ -169,7 +179,7 @@ As an example we output the points as WKT. Add a new Design Document
with an additional List function (the rest is the same as above). Make
sure you use the right `_rev`:

curl -X PUT -d '{"_rev": "1-121efc747b00743b8c7621ffccf1ac40", "lists": {"wkt": "function(head, req) {\n var row;\n while (row = getRow()) {\n send(\"POINT(\" + row.value[1].join(\" \") + \")\\n\");\n }\n};"}, "spatial":{"points":"function(doc) {\n if (doc.loc) {\n emit({\n type: \"Point\",\n coordinates: [doc.loc[0], doc.loc[1]]\n }, [doc._id, doc.loc]);\n }};"}}' http://127.0.0.1:5984/places/_design/main
curl -X PUT -d '{"_rev": "1-121efc747b00743b8c7621ffccf1ac40", "lists": {"wkt": "function(head, req) {\n var row;\n while (row = getRow()) {\n send(\"POINT(\" + row.geometry.coordinates.join(\" \") + \")\\n\");\n }\n};"}, "spatial":{"points":"function(doc) {\n if (doc.loc) {\n emit({\n type: \"Point\",\n coordinates: [doc.loc[0], doc.loc[1]]\n }, [doc._id, doc.loc]);\n }};"}}' http://127.0.0.1:5984/places/_design/main

Now you can request this List function as you would do for CouchDB,
though with a different Design handler (`_spatial/_list` instead of
Expand All @@ -179,8 +189,8 @@ though with a different Design handler (`_spatial/_list` instead of

The result is:

POINT(-122.270833 37.804444)
POINT(10.898333 48.371667)
POINT(-122.270833 37.804444)
POINT(17.15 -22.566667)
POINT(135 -25)
POINT(-52.95 -10.65)
Expand All @@ -189,7 +199,7 @@ Using List functions from Design Documents other than the one containing the
Spatial functions is supported as well. This time we add the Document
ID in parenthesis:

curl -X PUT -d '{"lists": {"wkt": "function(head, req) {\n var row;\n while (row = getRow()) {\n send(\"POINT(\" + row.value[1].join(\" \") + \") (\" + row.id + \")\\n\");\n }\n};"}}' http://127.0.0.1:5984/places/_design/listfunonly
curl -X PUT -d '{"lists": {"wkt": "function(head, req) {\n var row;\n while (row = getRow()) {\n send(\"POINT(\" + row.geometry.coordinates.join(\" \") + \") (\" + row.id + \")\\n\");\n }\n};"}}' http://127.0.0.1:5984/places/_design/listfunonly

curl -X GET 'http://localhost:5984/places/_design/listfunonly/_spatial/_list/wkt/main/points?bbox=-180,-90,180,90'

Expand All @@ -206,7 +216,7 @@ as for the spatial List functions.
`count` is a boolean. `count=true` will only return the number of geometries
the query will return, not the geometry themselves.

curl -X GET 'http://localhost:5984/places/_design/main/_spatial/points?bbox=0,0,180,90'
curl -X GET 'http://localhost:5984/places/_design/main/_spatial/points?bbox=0,0,180,90&count=true'

{"count":1}

Expand Down
4 changes: 2 additions & 2 deletions include/geocouch.hrl → include/refuge_spatial.hrl
Expand Up @@ -10,7 +10,7 @@
% License for the specific language governing permissions and limitations under
% the License.

-define(GEOCOUCH_DISK_VSN, 2).
-define(REFUGE_SPATIAL_DISK_VSN, 2).


-record(gcst, {
Expand Down Expand Up @@ -53,7 +53,7 @@
purge_seq=0,
id_btree_state=nil,
idx_states=[],
disk_version = ?GEOCOUCH_DISK_VSN
disk_version = ?REFUGE_SPATIAL_DISK_VSN
}).


Expand Down
Binary file removed rebar
Binary file not shown.
2 changes: 1 addition & 1 deletion rebar.config
@@ -1,5 +1,5 @@
{sub_dirs, ["apps/vtree"]}.

{lib_dirs, ["apps"]}.
{lib_dirs, ["apps", "../../refuge/deps"]}.

{require_otp_vsn, "R14|R15"}.
11 changes: 0 additions & 11 deletions src/geocouch.app.src

This file was deleted.

11 changes: 11 additions & 0 deletions src/refuge_spatial.app.src
@@ -0,0 +1,11 @@
%%% -*- erlang -*-
%%%
%%% This file is part of refuge_spatial released under the Apache license 2.
%%% See the NOTICE for more information.

{application, refuge_spatial, [
{description, "GeoCouch in Refuge"},
{vsn, git},
{registered, []},
{applications, [kernel]}
]}.
22 changes: 11 additions & 11 deletions src/geocouch.erl → src/refuge_spatial.erl
@@ -1,14 +1,14 @@
%%% -*- erlang -*-
%%%
%%% This file is part of geocouch released under the Apache license 2.
%%% This file is part of refuge_spatial released under the Apache license 2.
%%% See the NOTICE for more information.

-module(geocouch).
-module(refuge_spatial).

-export([spatial_query/3, spatial_query/4, spatial_query/6]).
-export([count/4, get_info/2, compact/2, cleanup/1]).

-include_lib("geocouch/include/geocouch.hrl").
-include_lib("refuge_spatial/include/refuge_spatial.hrl").


-record(gcacc, {
Expand All @@ -35,7 +35,7 @@ spatial_query(Db, DDoc, SName, Args) ->
spatial_query(Db, DDoc, SName, Args, Callback, Acc) when is_list(Args) ->
spatial_query(Db, DDoc, SName, to_gcargs(Args), Callback, Acc);
spatial_query(Db, DDoc, SName, Args0, Callback, Acc0) ->
{ok, Idx, Sig, Args} = geocouch_util:get_index(Db, DDoc, SName, Args0),
{ok, Idx, Sig, Args} = refuge_spatial_util:get_index(Db, DDoc, SName, Args0),
{ok, Acc1} = case Args#gcargs.preflight_fun of
PFFun when is_function(PFFun, 2) -> PFFun(Sig, Acc0);
_ -> {ok, Acc0}
Expand All @@ -50,22 +50,22 @@ default_cb(Row, Acc) ->


count(Db, DDoc, SName, Args0) ->
{ok, Idx, _, Args} = geocouch_util:get_index(Db, DDoc, SName, Args0),
geocouch_util:count(Idx, Args).
{ok, Idx, _, Args} = refuge_spatial_util:get_index(Db, DDoc, SName, Args0),
refuge_spatial_util:count(Idx, Args).


get_info(Db, DDoc) ->
{ok, Pid} = geocouch_util:get_indexer_pid(Db, DDoc),
{ok, Pid} = refuge_spatial_util:get_indexer_pid(Db, DDoc),
couch_index:get_info(Pid).


compact(Db, DDoc) ->
{ok, Pid} = geocouch_util:get_indexer_pid(Db, DDoc),
{ok, Pid} = refuge_spatial_util:get_indexer_pid(Db, DDoc),
couch_index:compact(Pid).


cleanup(Db) ->
geocouch_cleanup:run(Db).
refuge_spatial_cleanup:run(Db).


spatial_fold(Db, Idx, Args, Callback, Acc) ->
Expand All @@ -80,7 +80,7 @@ spatial_fold(Db, Idx, Args, Callback, Acc) ->
bbox=BBox,
bounds=Bounds
} = Args,
{ok, Acc1} = geocouch_util:fold(Idx, fun spatial_fold/2, Acc0, BBox,Bounds),
{ok, Acc1} = refuge_spatial_util:fold(Idx, fun spatial_fold/2, Acc0, BBox,Bounds),
finish_fold(Acc1, Idx).


Expand Down Expand Up @@ -125,7 +125,7 @@ finish_fold(#gcacc{user_acc=UAcc}, _Idx) ->


make_meta(Idx) ->
{ok, Total} = geocouch_util:get_row_count(Idx),
{ok, Total} = refuge_spatial_util:get_row_count(Idx),
{meta, [
{total, Total},
{update_seq, Idx#gcidx.update_seq}
Expand Down
12 changes: 6 additions & 6 deletions src/geocouch_cleanup.erl → src/refuge_spatial_cleanup.erl
@@ -1,14 +1,14 @@
%%% -*- erlang -*-
%%%
%%% This file is part of geocouch released under the Apache license 2.
%%% This file is part of refuge_spatial released under the Apache license 2.
%%% See the NOTICE for more information.

-module(geocouch_cleanup).
-module(refuge_spatial_cleanup).

-export([run/1]).

-include_lib("couch/include/couch_db.hrl").
-include_lib("geocouch/include/geocouch.hrl").
-include_lib("refuge_spatial/include/refuge_spatial.hrl").


run(Db) ->
Expand All @@ -18,10 +18,10 @@ run(Db) ->

{ok, DesignDocs} = couch_db:get_design_docs(Db),
SigFiles = lists:foldl(fun(DDoc, SFAcc) ->
InitState = geocouch_util:ddoc_to_gcst(DbName, DDoc),
InitState = refuge_spatial_util:ddoc_to_gcst(DbName, DDoc),
Sig = InitState#gcst.sig,
IFName = geocouch_util:index_file(IdxDir, DbName, Sig),
CFName = geocouch_util:compaction_file(IdxDir, DbName, Sig),
IFName = refuge_spatial_util:index_file(IdxDir, DbName, Sig),
CFName = refuge_spatial_util:compaction_file(IdxDir, DbName, Sig),
[IFName, CFName | SFAcc]
end, [], [DD || DD <- DesignDocs, DD#doc.deleted == false]),

Expand Down
20 changes: 10 additions & 10 deletions src/geocouch_compactor.erl → src/refuge_spatial_compactor.erl
@@ -1,13 +1,13 @@
%%% -*- erlang -*-
%%%
%%% This file is part of geocouch released under the Apache license 2.
%%% This file is part of refuge_spatial released under the Apache license 2.
%%% See the NOTICE for more information.


-module(geocouch_compactor).
-module(refuge_spatial_compactor).

-include_lib("couch/include/couch_db.hrl").
-include_lib("geocouch/include/geocouch.hrl").
-include_lib("refuge_spatial/include/refuge_spatial.hrl").

-export([compact/3, swap_compacted/2]).

Expand All @@ -30,9 +30,9 @@ compact(State) ->
} = State,

EmptyState = couch_util:with_db(DbName, fun(Db) ->
CompactFName = geocouch_util:compaction_file(RootDir, DbName, Sig),
{ok, Fd} = geocouch_util:open_file(CompactFName),
geocouch_util:reset_index(Db, Fd, State)
CompactFName = refuge_spatial_util:compaction_file(RootDir, DbName, Sig),
{ok, Fd} = refuge_spatial_util:open_file(CompactFName),
refuge_spatial_util:reset_index(Db, Fd, State)
end),

#gcst{
Expand Down Expand Up @@ -102,7 +102,7 @@ recompact(State) ->
link(State#gcst.fd),
?LOG_INFO("Recompacting index ~s ~s at ~p", [DbName, IdxName, UpdateSeq]),
{_Pid, Ref} = erlang:spawn_monitor(fun() ->
couch_index_updater:update(geocouch_index, State)
couch_index_updater:update(refuge_spatial_index, State)
end),
receive
{'DOWN', Ref, _, _, {updated, State2}} ->
Expand All @@ -112,7 +112,7 @@ recompact(State) ->


compact_index(Idx, #gcidx{fd=EFd}=EmptyIdx, BufferSize) ->
{ok, Count} = geocouch_util:get_row_count(Idx),
{ok, Count} = refuge_spatial_util:get_row_count(Idx),
FoldFun = fun(Node, {Vt, Vh, Acc, AccSize, Copied}) ->
AccSize2 = AccSize + ?term_size(Node),
case AccSize2 >= BufferSize of
Expand Down Expand Up @@ -145,8 +145,8 @@ swap_compacted(OldState, NewState) ->
unlink(OldState#gcst.fd),
link(NewState#gcst.fd),

IndexFName = geocouch_util:index_file(RootDir, DbName, Sig),
CompactFName = geocouch_util:compaction_file(RootDir, DbName, Sig),
IndexFName = refuge_spatial_util:index_file(RootDir, DbName, Sig),
CompactFName = refuge_spatial_util:compaction_file(RootDir, DbName, Sig),

io:format("~p~n~p~n", [IndexFName, CompactFName]),
couch_file:close(OldState#gcst.fd),
Expand Down

0 comments on commit 7fcc13e

Please sign in to comment.