Skip to content

Commit

Permalink
can now specify which attributes to return for query and scan
Browse files Browse the repository at this point in the history
  • Loading branch information
surt666 committed Apr 4, 2012
1 parent 3ed90fe commit 1dd5379
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ region=dynamodb.eu-west-1.amazonaws.com

(scan client "events")

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

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

(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
22 changes: 12 additions & 10 deletions src/dynamo4clj/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,27 @@
(= 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]
"Find items with key and optional range. Range has the form [operator param1 param2] or [operator param1]"
(let [condition (create-condition (first range))
req (cond
(empty? range) (doto (QueryRequest.) (.withTableName table) (.withHashKeyValue (to-attr-value key)) (.withConsistentRead consistent))
(not (empty? range)) (doto (QueryRequest.) (.withTableName table) (.withHashKeyValue (to-attr-value key)) (.withRangeKeyCondition condition) (.withConsistentRead consistent)))]
(defn find-items [^AmazonDynamoDBClient client table key consistent & [range-condition return-attributes]]
"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 (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 the items in a DynamoDB table. Conditions is vector of tuples like [field operator param1 param2] or [field operator param1]"
(defn scan [^AmazonDynamoDBClient client table & [conditions return-attributes]]
"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 [req (cond
(let [reqq (cond
(empty? conds) (doto (ScanRequest.) (.withTableName table))
(not (empty? conds)) (doto (ScanRequest.) (.withTableName table) (.withScanFilter conds)))]
(not (empty? conds)) (doto (ScanRequest.) (.withTableName table) (.withScanFilter conds)))
req (if (empty? return-attributes) reqq (doto reqq (.withAttributesToGet (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)})))))

0 comments on commit 1dd5379

Please sign in to comment.