Skip to content

Commit

Permalink
removed the need for nil attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
surt666 committed Apr 4, 2012
1 parent 8bc8999 commit 0645c0e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ region=dynamodb.eu-west-1.amazonaws.com

(scan client "events")

(scan client "events" [["author" "eq" "steen"]] nil)
(scan client "events" [["author" "eq" "steen"]] [:attr])

(find-items client "events" "NORMAL" true)

(find-items client "events" "NORMAL" true ["between" 715 815] [attr1 attr2])
(find-items client "events" "NORMAL" true ["between" 715 815])

(find-items client "events" "NORMAL" true ["between" 715 815] [:attr1 :attr2])

Return values have meta data containing consumed units, count and lastkey where applicable.

Expand Down
54 changes: 32 additions & 22 deletions src/dynamo4clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,37 @@
(= operator "null") (doto (Condition.) (.withComparisonOperator ComparisonOperator/NULL) (.withAttributeValueList ^java.util.List (vector (to-attr-value param1))))
(= operator "in") (doto (Condition.) (.withComparisonOperator ComparisonOperator/IN) (.withAttributeValueList ^java.util.List (vector (to-attr-value param1)))))))

(defn find-items [^AmazonDynamoDBClient client table key consistent & [range-condition return-attributes]]
(defn find-items
"Find items with key and optional range. Range has the form [operator param1 param2] or [operator param1], and return-attributes is a vector of attributes to return as in [attr1 attr2]"
(let [condition (create-condition range-condition)
reqq (cond
(empty? range-condition) (doto (QueryRequest.) (.withTableName table) (.withHashKeyValue (to-attr-value key)) (.withConsistentRead consistent))
(not (empty? range-condition)) (doto (QueryRequest.) (.withTableName table) (.withHashKeyValue (to-attr-value key)) (.withRangeKeyCondition condition) (.withConsistentRead consistent)))
req (if (empty? return-attributes) reqq (doto reqq (.withAttributesToGet ^java.util.Collection (map #(name %) return-attributes))))]
(let [qres (. client (query req))]
(with-meta (keywordize-keys (map to-map (.getItems qres)))
{:consumed-capacity-units (.getConsumedCapacityUnits qres) :count (.getCount qres) :last-key (.getLastEvaluatedKey qres)}))))

(defn scan [^AmazonDynamoDBClient client table & [conditions return-attributes]]
([^AmazonDynamoDBClient client table key consistent range-condition return-attributes]
(let [condition (create-condition range-condition)
reqq (cond
(empty? range-condition) (doto (QueryRequest.) (.withTableName table) (.withHashKeyValue (to-attr-value key)) (.withConsistentRead consistent))
(not (empty? range-condition)) (doto (QueryRequest.) (.withTableName table) (.withHashKeyValue (to-attr-value key)) (.withRangeKeyCondition condition) (.withConsistentRead consistent)))
req (if (empty? return-attributes) reqq (doto reqq (.withAttributesToGet ^java.util.Collection (map #(name %) return-attributes))))]
(let [qres (. client (query req))]
(with-meta (keywordize-keys (map to-map (.getItems qres)))
{:consumed-capacity-units (.getConsumedCapacityUnits qres) :count (.getCount qres) :last-key (.getLastEvaluatedKey qres)}))))
([^AmazonDynamoDBClient client table key consistent range-condition]
(find-items client table key consistent range-condition nil))
([^AmazonDynamoDBClient client table key consistent]
(find-items client table key consistent nil nil)))

(defn scan
"Return the items in a DynamoDB table. Conditions is vector of tuples like [field operator param1 param2] or [field operator param1]. Return-attributes is a vector of attributes to return as in [attr1 attr2]"
(let [conds (loop [c (first conditions) res {}]
(if (empty? c)
res
(recur (rest c) (assoc res (first (first c)) (create-condition (vec (rest (first c))))))))]
(let [reqq (cond
(empty? conds) (doto (ScanRequest.) (.withTableName table))
(not (empty? conds)) (doto (ScanRequest.) (.withTableName table) (.withScanFilter conds)))
req (if (empty? return-attributes) reqq (doto reqq (.withAttributesToGet ^java.util.Collection (map #(name %) return-attributes))))]
(let [sres (. client (scan req))]
(with-meta (keywordize-keys (map to-map (.getItems sres)))
{:consumed-capacity-units (.getConsumedCapacityUnits sres) :count (.getCount sres) :last-key (.getLastEvaluatedKey sres)})))))
([^AmazonDynamoDBClient client table conditions return-attributes]
(let [conds (loop [c (first conditions) res {}]
(if (empty? c)
res
(recur (rest c) (assoc res (first (first c)) (create-condition (vec (rest (first c))))))))]
(let [reqq (cond
(empty? conds) (doto (ScanRequest.) (.withTableName table))
(not (empty? conds)) (doto (ScanRequest.) (.withTableName table) (.withScanFilter conds)))
req (if (empty? return-attributes) reqq (doto reqq (.withAttributesToGet ^java.util.Collection (map #(name %) return-attributes))))]
(let [sres (. client (scan req))]
(with-meta (keywordize-keys (map to-map (.getItems sres)))
{:consumed-capacity-units (.getConsumedCapacityUnits sres) :count (.getCount sres) :last-key (.getLastEvaluatedKey sres)})))))
([^AmazonDynamoDBClient client table conditions]
(scan client table conditions nil))
([^AmazonDynamoDBClient client table]
(scan client table nil nil)))

0 comments on commit 0645c0e

Please sign in to comment.