Skip to content

Instantly share code, notes, and snippets.

@ian-kent
Created November 13, 2014 17:57
Show Gist options
  • Save ian-kent/93e7d33dd413d85876ae to your computer and use it in GitHub Desktop.
Save ian-kent/93e7d33dd413d85876ae to your computer and use it in GitHub Desktop.
crowdmob countscan
diff --git a/dynamodb/scan.go b/dynamodb/scan.go
index 8e08eb9..3c46933 100644
--- a/dynamodb/scan.go
+++ b/dynamodb/scan.go
@@ -109,6 +109,48 @@ func (t *Table) Scan(attributeComparisons []AttributeComparison) ([]map[string]*
return t.FetchResults(q)
}
+func (t *Table) CountScan() (int64, error) {
+ var lastEvaluatedKey *Key
+ totalCount := int64(0)
+
+ for {
+ q := NewQuery(t)
+ q.AddSelect("COUNT")
+ if lastEvaluatedKey != nil {
+ q.AddExclusiveStartKey(t, lastEvaluatedKey)
+ }
+ jsonResponse, err := t.Server.queryServer("DynamoDB_20120810.Scan", q)
+ if err != nil {
+ return 0, err
+ }
+ json, err := simplejson.NewJson(jsonResponse)
+ if err != nil {
+ return 0, err
+ }
+
+ itemCount, err := json.Get("Count").Int64()
+ if err != nil {
+ return 0, err
+ }
+
+ totalCount += itemCount
+
+ if lastKeyMap := json.Get("LastEvaluatedKey").MustMap(); lastKeyMap != nil {
+ lastEvaluatedKey = parseKey(t, lastKeyMap)
+ } else {
+ lastEvaluatedKey = nil
+ }
+
+ log.Println(lastEvaluatedKey)
+
+ if lastEvaluatedKey == nil {
+ break
+ }
+ }
+
+ return totalCount, nil
+}
+
func (t *Table) ParallelScan(attributeComparisons []AttributeComparison, segment int, totalSegments int) ([]map[string]*Attribute, error) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment