Skip to content

Commit

Permalink
lots of endpoint and http_client refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
erichanson committed May 22, 2015
1 parent 5ac50d9 commit f6b6488
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 61 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -31,5 +31,5 @@ postgres=# \q
postgres@34f81a644855:~$
```
3. Run `./build.sh` as the user who has superuser access
4. Build and install the webserver. See core/004-www/servers/background_worker/README.
4. Build and install the webserver. See core/004-aquameta_endpoint/servers/background_worker/README.
5. http://localhost:8080/
39 changes: 13 additions & 26 deletions core/004-aquameta_endpoint/001-endpoint_client.sql
Expand Up @@ -24,15 +24,16 @@ set search_path=endpoint;
* table remote_endpoint, a known endpoint out in the universe
*******************************************************************************/
create table remote_endpoint (
id uuid default public.uuid_generate_v4() primary key,
url text
id uuid not null default public.uuid_generate_v4() primary key,
name text,
url text not null
);


/*******************************************************************************
* rows_select
* client_rows_select
*******************************************************************************/
create or replace function endpoint.client_rows_select(remote_endpoint_id uuid, relation_id meta.relation_id, args json, out response http_client.http_response)
create or replace function endpoint.client_rows_select(remote_endpoint_id uuid, relation_id meta.relation_id, out response http_client.http_response)
as $$

select http_client.http_get (
Expand All @@ -47,7 +48,7 @@ $$ language sql;


/*******************************************************************************
* row_select
* client_row_select
*******************************************************************************/
create or replace function endpoint.client_row_select(remote_endpoint_id uuid, row_id meta.row_id, out response http_client.http_response)
as $$
Expand All @@ -65,10 +66,9 @@ $$ language sql;


/*******************************************************************************
* field_select
* client_field_select
*******************************************************************************/
/*
create or replace function endpoint.client_field_select(remote_endpoint_id uuid, field_id meta.field_id) returns text
create or replace function endpoint.client_field_select(remote_endpoint_id uuid, field_id meta.field_id, out response http_client.http_response)
as $$

select http_client.http_get (
Expand All @@ -82,14 +82,12 @@ select http_client.http_get (
);

$$ language sql;
*/


/*******************************************************************************
* row_delete
*******************************************************************************/
/*
create or replace function endpoint.client_row_delete(remote_endpoint_id uuid, row_id meta.row_id) returns text
create or replace function endpoint.client_row_delete(remote_endpoint_id uuid, row_id meta.row_id, out response http_client.http_response)
as $$

select http_client.http_delete (
Expand All @@ -102,7 +100,6 @@ select http_client.http_delete (
);

$$ language sql;
*/



Expand All @@ -112,50 +109,40 @@ $$ language sql;
*******************************************************************************/
create or replace function endpoint.client_rows_select_function(remote_endpoint_id uuid, function_id meta.function_id, arg_vals text[], out http_client.http_response)
as $$
declare
qs text;
begin

select http_client.http_get (
(select url from endpoint.remote_endpoint where id=remote_endpoint_id)
|| '/' || (function_id).schema_id.name
|| '/function'
|| '/' || (function_id).name
|| '/rows'
|| '?' || endpoint.array_to_querystring((function_id).parameters, arg_vals)
|| '?' || http_client.array_to_querystring((function_id).parameters, arg_vals)
);

end;
$$ language plpgsql;
$$ language sql;



/*******************************************************************************
* rows_insert
*******************************************************************************/
/*
create or replace function endpoint.client_rows_insert(remote_endpoint_id uuid, args json, out response http_client.http_response)
as $$
begin

-- TOOD: fixme
select http_client.http_post (
(select url || '/insert' from endpoint.remote_endpoint where id=remote_endpoint_id),
args::text -- fixme? does a post expect x=7&y=p&z=3 ?
args::text
);
end;
$$ language plpgsql;
*/


--
-- TODO
--
-- row_insert(remote_id uuid, relation_id meta.relation_id, row_object json)
-- row_update(remote_id uuid, row_id meta.row_id, args json)
--
-- rows_select(remote_id uuid, relation_id meta.relation_id, args json)
--
--
--

commit;
33 changes: 27 additions & 6 deletions core/004-aquameta_endpoint/test/001-endpoint_client_tests.sql
Expand Up @@ -8,15 +8,36 @@ select * from no_plan();
create schema endpoint_test;
set search_path=endpoint_test,public;

-- \set test_url '\'http://demo.aquameta.org/\''
insert into endpoint.remote_endpoint(id,url) values ('67f7d009-52d8-4a01-9b13-00188c904249', 'http://demo.aquameta.org/endpoint');
-- test remote
insert into endpoint.remote_endpoint(id,url, name)
values ('67f7d009-52d8-4a01-9b13-00188c904249', 'http://demo.aquameta.org/endpoint', 'Test Server');

-------------------------------------------------------------------------------
-- TEST 1: rows_select
-------------------------------------------------------------------------------
select is (r.status_code, 200, 'GET status_code')
from endpoint.client_rows_select('67f7d009-52d8-4a01-9b13-00188c904249', meta.relation_id('widget','input')) r;


-------------------------------------------------------------------------------
-- TEST 2: row_select
-------------------------------------------------------------------------------
select is (r.status_code, 200, 'GET status_code')
from endpoint.client_row_select('67f7d009-52d8-4a01-9b13-00188c904249', meta.row_id('bundle','bundle','id','0c2aa87b-0a66-48cb-ac9d-733d0a740bde')) r;


-------------------------------------------------------------------------------
-- TEST 3: field_select
-------------------------------------------------------------------------------
select is (r.status_code, 200, 'GET status_code')
from endpoint.client_field_select('67f7d009-52d8-4a01-9b13-00188c904249', meta.field_id('bundle','bundle','id','0c2aa87b-0a66-48cb-ac9d-733d0a740bde', 'name')) r;


-------------------------------------------------------------------------------
-- TEST 1: GET status_code
-- TEST 4: row_select
-------------------------------------------------------------------------------
/*
select is (r.status_code, 200, 'GET status_code')
from endpoint.rows_select('http://demo.aquameta.org/endpoint') r;
*/
from endpoint.client_rows_select_function('67f7d009-52d8-4a01-9b13-00188c904249', meta.function_id('bundle','commit_log',ARRAY['bundle_name']), ARRAY['com.aquameta.bundle']) r;



Expand Down
2 changes: 1 addition & 1 deletion core/006-bundle/000-data_model.sql
Expand Up @@ -598,7 +598,7 @@ create table remote_webrtc (
);
*/

create table bundle_endpoint (
create table remote (
id uuid default public.uuid_generate_v4() primary key,
bundle_id uuid not null references bundle.bundle(id) on delete cascade,
endpoint_id uuid not null references endpoint.remote_endpoint(id) on delete cascade,
Expand Down
55 changes: 28 additions & 27 deletions core/006-bundle/002-remotes.sql
Expand Up @@ -135,56 +135,57 @@ $$ language plpgsql;
* diffs the set of local commits with the set of remote commits
*******************************************************************************/

create or replace function bundle.remote_compare_commits(in bundle_endpoint_id uuid)
create or replace function bundle.remote_compare_commits(in remote_id uuid)
returns table(local_commit_id uuid, remote_commit_id uuid)
as $$
declare
local_bundle_id uuid;
begin
select into local_bundle_id bundle_id from bundle.bundle_endpoint rh where rh.id = bundle_endpoint_id;
select into local_bundle_id bundle_id from bundle.remote r where r.id = remote_id;
return query
with remote_commit as (select (json_array_elements(
http_client.http_get(
e.url
-- 'http://demo.aquameta.org/endpoint'
|| '/bundle/table/commit/rows?bundle_id='
|| be.bundle_id
|| r.bundle_id
)::json->'result')->'row'->>'id')::uuid as id
from bundle.bundle_endpoint be
join endpoint.remote_endpoint e on be.endpoint_id=e.id
where be.id = bundle_endpoint_id
from bundle.remote r
join endpoint.remote_endpoint e on r.endpoint_id=e.id
where r.id = remote_id
)
select c.id as local_commit_id, rc.id as remote_id
from bundle.commit c
full outer join remote_commit rc on rc.id=c.id
where c.bundle_id = local_bundle_id or c.bundle_id is null;

end;
$$ language plpgsql;
$$ language plpgsql;



/*******************************************************************************
* bundle.remote_compare_commits
* bundle.has_bundle
* checks a remote to see if it also has a bundle with the same id installed
*******************************************************************************/

create or replace function bundle.remote_has_bundle(in bundle_endpoint_id uuid, out has_bundle boolean)
create or replace function bundle.remote_has_bundle(in remote_id uuid, out has_bundle boolean)
as $$
declare
local_bundle_id uuid;
begin
select into has_bundle count(*) > 0 from (
select (json_array_elements(
http_client.http_get(
be.endpoint_url
r.endpoint_url
|| '/bundle/table/bundle/rows/'
|| be.bundle_id
|| r.bundle_id
)::json->'result')->'row'->>'id')::uuid as id
from bundle.bundle_endpoint be
where be.id = bundle_endpoint_id
from bundle.remote r
where r.id = remote_id
) has;
end;
$$ language plpgsql;
$$ language plpgsql;



Expand All @@ -208,11 +209,11 @@ begin
temp_table_name,
('{ "schema_name": "bundle", "relation_name": "bundle", "label": "b", "local_id": "id", "where_clause": "b.id = ''' || bundle_id::text || '''", "position": 1, "exclude": true }')::json,
('[
{"schema_name": "bundle", "relation_name": "commit", "label": "c", "local_id": "bundle_id", "related_label": "b", "related_field": "id", "position": 5, "where_clause": "c.id in (' || new_commits_str || ')"},
{"schema_name": "bundle", "relation_name": "commit", "label": "c", "local_id": "bundle_id", "related_label": "b", "related_field": "id", "position": 6, "where_clause": "c.id in (' || new_commits_str || ')"},
{"schema_name": "bundle", "relation_name": "rowset", "label": "r", "local_id": "id", "related_label": "c", "related_field": "rowset_id", "position": 2},
{"schema_name": "bundle", "relation_name": "rowset_row", "label": "rr", "local_id": "rowset_id", "related_label": "r", "related_field": "id", "position": 3},
{"schema_name": "bundle", "relation_name": "rowset_row_field", "label": "rrf", "local_id": "rowset_row_id", "related_label": "rr", "related_field": "id", "position": 6},
{"schema_name": "bundle", "relation_name": "blob", "label": "blb", "local_id": "hash", "related_label": "rrf", "related_field": "value_hash", "position": 5}
{"schema_name": "bundle", "relation_name": "rowset_row_field", "label": "rrf", "local_id": "rowset_row_id", "related_label": "rr", "related_field": "id", "position": 5},
{"schema_name": "bundle", "relation_name": "blob", "label": "blb", "local_id": "hash", "related_label": "rrf", "related_field": "value_hash", "position": 4}
]')::json
);

Expand All @@ -229,7 +230,7 @@ $$ language plpgsql;
* transfer to a remote repository any local commits not present in the remote
*******************************************************************************/

create or replace function bundle.remote_push(in bundle_endpoint_id uuid)
create or replace function bundle.remote_push(in remote_id uuid)
returns void -- table(_row_id meta.row_id)
as $$
declare
Expand All @@ -241,11 +242,11 @@ declare
r row_graph_row;
begin
raise notice '################################### PUSH ##########################';
select into bundle_id be.bundle_id from bundle.bundle_endpoint be where be.id = bundle_endpoint_id;
select into bundle_id be.bundle_id from bundle.remote be where be.id = remote_id;

-- get the array of new remote commits
select into new_commits array_agg(local_commit_id)
from bundle.remote_compare_commits(bundle_endpoint_id)
from bundle.remote_compare_commits(remote_id)
where remote_commit_id is null;

raise notice 'NEW COMMITS: %', new_commits::text;
Expand All @@ -260,7 +261,7 @@ begin
raise notice 'PUUUUUUUUUSH result: %', result::text;

-- http://hashrocket.com/blog/posts/faster-json-generation-with-postgresql
perform http_client.endpoint_rows_insert (bundle_endpoint_id, result);
perform http_client.endpoint_rows_insert (remote_id, result);
-- from (select * from bundle_push_1234 order by position) as b;

drop table _bundle_push_1234;
Expand All @@ -279,7 +280,7 @@ $$ language plpgsql;
* download from remote repository any commits not present in the local repository
*******************************************************************************/

create or replace function bundle.remote_fetch(in bundle_endpoint_id uuid)
create or replace function bundle.remote_fetch(in remote_id uuid)
returns void -- table(_row_id meta.row_id)
as $$
declare
Expand All @@ -289,17 +290,17 @@ declare
json_results text;
begin
raise notice '################################### FETCH ##########################';
select into bundle_id be.bundle_id from bundle.bundle_endpoint be where be.id = bundle_endpoint_id;
select into bundle_id be.bundle_id from bundle.remote be where be.id = remote_id;

-- get the array of new remote commits
select into new_commits array_agg(remote_commit_id)
from bundle.remote_compare_commits(bundle_endpoint_id)
from bundle.remote_compare_commits(remote_id)
where local_commit_id is null;

raise notice 'NEW COMMITS: %', new_commits::text;

select into json_results http_client.endpoint_rows_select_function(
bundle_endpoint_id,
remote_id,
meta.function_id('bundle','construct_bundle_diff', ARRAY['bundle_id','new_commits','temp_table_name']),
ARRAY[bundle_id::text, new_commits::text, 'bundle_diff_1234'::text]
);
Expand All @@ -308,7 +309,7 @@ begin

-- create a join_graph on the remote via the construct_bundle_diff function
select into json_results result::json->'result' from http_client.endpoint_rows_select_function(
bundle_endpoint_id,
remote_id,
meta.function_id('bundle','construct_bundle_diff', ARRAY['bundle_id','new_commits','temp_table_name']),
ARRAY[bundle_id::text, new_commits::text, 'bundle_diff_1234'::text]
);
Expand All @@ -318,7 +319,7 @@ begin
/*
-- http://hashrocket.com/blog/posts/faster-json-generation-with-postgresql
perform http_client.endpoint_rows_insert (
bundle_endpoint_id,
remote_id,
array_to_json(
array_agg(
row_to_json(b)
Expand Down

0 comments on commit f6b6488

Please sign in to comment.