Skip to content

Instantly share code, notes, and snippets.

@mikewallace1979
Created November 6, 2012 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikewallace1979/4028144 to your computer and use it in GitHub Desktop.
Save mikewallace1979/4028144 to your computer and use it in GitHub Desktop.
Erlang view for base64-encoded zipped data
fun({Doc}) ->
case couch_util:get_value(<<"type">>, Doc) of
undefined ->
ok;
Type when Type == <<"results">> ->
TaskId = couch_util:get_value(<<"task_id">>, Doc),
JobId = couch_util:get_value(<<"job_id">>, Doc),
EmitCellData = fun(Data, [Key | Rest], CB) ->
case Key of
<<"soil_type">> ->
try
SoilData = couch_util:get_nested_json_value(Data, [Key, <<"value">>]),
Emit([TaskId, Key, JobId, 0], SoilData)
catch
{not_found, _} ->
ok
end;
_ ->
Decompress = fun(CellData) ->
Decoded = base64:decode(CellData),
Z = zlib:open(),
ok = zlib:inflateInit(Z),
Uncompressed = zlib:inflate(Z, Decoded),
ok = zlib:inflateEnd(Z),
AsList = bitstring_to_list(iolist_to_binary(Uncompressed)),
{ok, Tokens, _} = erl_scan:string(AsList),
{ok, Term} = erl_parse:parse_term(lists:append(Tokens, [{dot, 1}])),
Term
end,
EmitCellsTimestep = fun([CellData | Rest], Acc, CB) ->
Emit([TaskId, Key, JobId, Acc], CellData),
case Rest of
[] ->
ok;
_ ->
CB(Rest, Acc + 1, CB)
end
end,
try
CellData = couch_util:get_nested_json_value(Data, [Key, <<"value">>]),
Decompressed = Decompress(CellData),
EmitCellsTimestep(Decompressed, 0, EmitCellsTimestep)
catch
{not_found, _} ->
ok
end
end,
case Rest of
[] ->
ok;
_ ->
CB(Data, Rest, CB)
end
end,
KeysToEmit = [<<"moisture_content">>, <<"soil_type">>, <<"pressure_head">>],
case couch_util:get_value(<<"chasm_output">>, Doc) of
undefined ->
ok;
Output when Output == <<"unavailable">> ->
ok;
Output when is_list(Output) ->
ok;
Output ->
EmitCellData(Output, KeysToEmit, EmitCellData);
_ ->
ok
end;
_ ->
ok
end
end.
@mikewallace1979
Copy link
Author

Out of context I'd say this was batshit insane, but it's actually a really neat solution to a certain problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment