Skip to content

Commit

Permalink
adding digraph exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Maclear committed Feb 10, 2011
1 parent afe0337 commit ddfa023
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 3 deletions.
10 changes: 10 additions & 0 deletions function-contrib-gollum/digraph_exporter.textile
@@ -0,0 +1,10 @@
h1. Exporting to an Erlang digraph

+Contributed By:+ [[Ryan Maclear|https://github.com/ryanmaclear]]

[[Source File on GitHub|https://github.com/basho/riak_function_contrib/blob/master/other/erlang/digraph_export.erl]]





6 changes: 3 additions & 3 deletions function-contrib-gollum/digraph_importer.textile
Expand Up @@ -11,11 +11,11 @@ digraph_importer:import_digraph(address of server, port, bucket, digraph, conten

h3. Notes

The function uses the Erlang PBC client, so the correct ip address and port must be specified.
The function uses the Erlang Protocol Buffer client, so the correct ip address and port must be specified.

There are currently some restrictions regarding the construction of the digraph:
The vertices should be created with digraph:add_vertex/2 or digraph:add_vertex/3
and the edges need to be created with digraph:add_edge/5
#The vertices should be created with digraph:add_vertex/2 or digraph:add_vertex/3
#and the edges need to be created with digraph:add_edge/5

h3. Examples

Expand Down
1 change: 1 addition & 0 deletions function-contrib-gollum/other-functions.textile
Expand Up @@ -16,5 +16,6 @@ h2. Contributed Functions or Scripts
[[Read Repair Bucket (Erlang)|bucket_inspector]]
[[YAML Data Importer (Ruby)|yaml_importer]]
[[Digraph Importer (Erlang)|digraph_importer]]
[[Digraph Exporter (Erlang)|digraph_exporter]]


88 changes: 88 additions & 0 deletions other/erlang/digraph_exporter.erl
@@ -0,0 +1,88 @@
%%
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% -------------------------------------------------------------------

-module(digraph_exporter).

-export([export_digraph/6]).

export_digraph(Server, Port, Bucket, FilterList, Ref, UseValue) ->
{ok, Client} = riakc_pb_socket:start(Server, Port),

Input = {Bucket, [FilterList]},

MapFun = build_map_fun(),

MapPhase = {map, {qfun, MapFun}, notused, true},
Query = [MapPhase],

{ok, [{_, Data}]} = riakc_pb_socket:mapred(Client, Input, Query),

build_vertices(Ref, UseValue, Data),
add_edges(Ref, Data).


build_vertices(_Ref, _UseValue, []) -> ok;
build_vertices(Ref, UseValue, [{Key, Value, ContentType, _}|T]) ->
Label =
case UseValue of
true ->
case ContentType of
"application/x-erlang-binary" ->
binary_to_term(Value);
_ ->
binary_to_list(Value)
end;
false -> ""
end,

Vertex = binary_to_list(Key),

digraph:add_vertex(Ref, Vertex, Label),
build_vertices(Ref, UseValue, T).


add_edges(_Ref, []) -> ok;
add_edges(Ref, [{Key, _, _, Links}|T]) ->
lists:foreach(fun({{_, Dest}, Tag}) ->
Edge = binary_to_list(Tag),
VSrc = binary_to_list(Key),
VDest = binary_to_list(Dest),
digraph:add_edge(Ref, Edge, VSrc, VDest, "")
end, Links),
add_edges(Ref, T).


build_map_fun() ->
MapFun = "fun(Object, _KeyData, _Args) ->
[{MetaDataDict, Value}] = riak_object:get_contents(Object),
MetaData = dict:to_list(MetaDataDict),
ContentType = proplists:get_value(<<\"content-type\">>, MetaData, \"\"),
Links = proplists:get_value(<<\"Links\">>, MetaData, []),
[{riak_object:key(Object), Value, ContentType, Links}]
end.",

{ok, Tokens, _} = erl_scan:string(MapFun),
{ok, [Form]} = erl_parse:parse_exprs(Tokens),
Bindings = erl_eval:new_bindings(),
{value, Fun, _} = erl_eval:expr(Form, Bindings),
Fun.

%%% EOF

0 comments on commit ddfa023

Please sign in to comment.