Skip to content

Commit

Permalink
base
Browse files Browse the repository at this point in the history
  • Loading branch information
joelreymont committed Feb 3, 2012
0 parents commit 43dfb48
Show file tree
Hide file tree
Showing 14 changed files with 1,104 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ebin/
deps/
*.plt
*.beam
app.config
.eunit
log*
23 changes: 23 additions & 0 deletions COPYRIGHT
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (C) 2012 Issuu ApS. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

55 changes: 55 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
REBAR = ./rebar
DIALYZER = dialyzer
TOUCH = touch

.PHONY: all deps compile escripize clean doc eunit ct test \
run plt analyze get-deps compile-deps

all: deps compile

deps: get-deps compile-deps

compile:
@$(REBAR) compile skip_deps=true

escriptize:
@$(REBAR) escriptize

clean:
@$(REBAR) clean
@rm -f test/*.beam erl_crash.dump ./deps/.compile-deps

eunit: deps compile
@$(REBAR) skip_deps=true eunit

ct: deps compile
@$(REBAR) skip_deps=true ct

test: eunit ct

plt:
@$(DIALYZER) --build_plt --output_plt .backend-api.plt \
-pa deps/lager/ebin \
-pa deps/mochiweb/ebin \
-c deps/mochiweb/ebin \
--apps kernel stdlib sasl inets crypto \
public_key ssl mnesia runtime_tools erts \
compiler tools syntax_tools xmerl hipe webtool

analyze: compile
@$(DIALYZER) --no_check_plt \
-c ebin \
--plt .ddb.plt \
-pa deps/lager/ebin \
-pa deps/mochiweb/ebin \
-Werror_handling \
-Wunmatched_returns #-Wunderspecs

docs:
@$(REBAR) doc skip_deps=true

get-deps:
@$(REBAR) get-deps

compile-deps:
@$(REBAR) compile
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Accessing Amazon DynamoDB

Authenticating

ddb_iam:credentials("access key", "secret").
{'ok', Key, Secret, Token} = ddb_iam:token(129600).
ddb:credentials(Key, Secret, Token).


Creating a table with a hash key

ddb:create(<<"foo">>, ddb:key_type(<<"hashkey">>, 'string'), 10, 10).

Creating a table with hash and range keys

ddb:create(<<"bar">>, ddb:key_type(<<"hashkey">>, 'string', <<"rangekey">>, 'number'), 10, 10).

Adding a record to a table with a hash key

ddb:put(<<"foo">>, [{<<"hashkey">>, <<"hash key value">>, 'string'},
{<<"field1">>, <<"string value">>, 'string'},
{<<"field2">>, <<"100">>, 'number'}]).

Adding a record to a table with hash and range keys

ddb:put(<<"bar">>, [{<<"hashkey">>, <<"hash key value">>, 'string'},
{<<"rangekey">>, <<"1000">>, 'number'},
{<<"field1">>, <<"string value">>, 'string'},
{<<"field2">>, <<"100">>, 'number'}]).

ddb:put(<<"bar">>, [{<<"hashkey">>, <<"hash key value">>, 'string'},
{<<"rangekey">>, <<"2000">>, 'number'},
{<<"field3">>, <<"string value">>, 'string'}]).

Fetching a record from a table using a hash key

ddb:get(<<"foo">>, ddb:key_value(<<"hash key value">>, 'string')).

Fetching a record from a table using hash and range keys

ddb:get(<<"bar">>, ddb:key_value(<<"hash key value">>, 'string', <<"1000">>, 'number')).

Querying a table

ddb:find(<<"bar">>, {<<"hash key value">>, 'string'}, {'between', 'number', [<<"1000">>, <<"2000">>]}).

Changing value (and type) of one field while deleting another

ddb:update(<<"foo">>, ddb:key_value(<<"hash key value">>, 'string'),
[{<<"field1">>, <<"1">>, 'number', 'put'},
{<<"field2">>, 'delete'}]).

Adding to a string set field and returning pre-update values of updated fields

ddb:update(<<"foo">>, ddb:key_value(<<"hash key value">>, 'string'),
[{<<"field2">>, [<<"1">>, <<"2">>], 'string_set', 'add'}],
'updated_old').

Deleting an item from a string set field and returning the values of all fields before the update

ddb:update(<<"foo">>, ddb:key_value(<<"hash key value">>, 'string'),
[{<<"field2">>, [<<"1">>], 'string_set', 'delete'}],
'all_old').

Update field1 only when field2 does not exist

ddb:cond_update(<<"foo">>, ddb:key_value(<<"hash key value">>, 'string'),
[{<<"field1">>, <<"1">>, 'number', 'put'}],
{'does_not_exist', <<"field2">>}).

Update field1 only when field2 exists and has a numerical value of 1

ddb:cond_update(<<"foo">>, ddb:key_value(<<"hash key value">>, 'string'),
[{<<"field1">>, <<"1">>, 'number', 'put'}],
{'exists', <<"field2">>, <<"1">>, 'number'}).

See `src/ddb.erl` for the rest of the API.

Note that dates in Dynamo are represented as seconds since Unix epoch!

All data is stored as strings but you have to specify whether each field is a string or a number.
Binary file added rebar
Binary file not shown.
30 changes: 30 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et

{lib_dirs, ["deps"]}.

{deps, [
{lager, ".*", {git, "git://github.com/basho/lager.git", {branch, "master"}}},
{jsx, ".*", {git, "git://github.com/talentdeficit/jsx.git", {branch, "master"}}},
{mochiweb, ".*", {git, "git://github.com/mochi/mochiweb.git", {branch, "master"}}},
{lhttpc, ".*", {git, "git://github.com/oscarh/lhttpc.git", {branch, "master"}}}
]}.

{require_otp_vsn, "R14"}.

{erl_opts, [
fail_on_warning,
debug_info,
%%warn_missing_spec,
{parse_transform, lager_transform}
]}.

{cover_enabled, true}.

{clean_files, ["*.eunit", "ebin/*.beam"]}.

{eunit_opts, [verbose, {report, {eunit_surefire, [{dir, "."}]}}]}.

{validate_app_modules, false}.

{sub_dirs, ["src"]}.
19 changes: 19 additions & 0 deletions src/ddb.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
%% ex: ts=4 sw=4 ft=erlang et

{application, ddb,
[
{description, "AWS DynamoDB client"},
{vsn, git},
{registered, [ddb_sup]},
{applications, [
kernel,
stdlib,
lager,
crypto,
inets,
ssl
]},
{mod, {ddb_app, []}},
{env, []}
]}.
Loading

0 comments on commit 43dfb48

Please sign in to comment.