module Aws.DynamoDb.Commands.Scan
( Scan (..)
, scan
, ScanResponse (..)
) where
import Control.Applicative
import Data.Aeson
import Data.Default
import Data.Maybe
import qualified Data.Text as T
import Data.Typeable
import qualified Data.Vector as V
import Aws.Core
import Aws.DynamoDb.Core
data Scan = Scan {
sTableName :: T.Text
, sFilter :: Conditions
, sStartKey :: Maybe [Attribute]
, sLimit :: Maybe Int
, sSelect :: QuerySelect
, sRetCons :: ReturnConsumption
, sSegment :: Int
, sTotalSegments :: Int
} deriving (Eq,Show,Read,Ord,Typeable)
scan :: T.Text
-> Scan
scan tn = Scan tn def Nothing Nothing def def 0 1
data ScanResponse = ScanResponse {
srItems :: V.Vector Item
, srLastKey :: Maybe [Attribute]
, srCount :: Int
, srScanned :: Int
, srConsumed :: Maybe ConsumedCapacity
} deriving (Eq,Show,Read,Ord)
instance ToJSON Scan where
toJSON Scan{..} = object $
catMaybes
[ (("ExclusiveStartKey" .= ) . attributesJson) <$> sStartKey
, ("Limit" .= ) <$> sLimit
] ++
conditionsJson "ScanFilter" sFilter ++
querySelectJson sSelect ++
[ "TableName".= sTableName
, "ReturnConsumedCapacity" .= sRetCons
, "Segment" .= sSegment
, "TotalSegments" .= sTotalSegments
]
instance FromJSON ScanResponse where
parseJSON (Object v) = ScanResponse
<$> v .:? "Items" .!= V.empty
<*> ((do o <- v .: "LastEvaluatedKey"
Just <$> parseAttributeJson o)
<|> pure Nothing)
<*> v .: "Count"
<*> v .: "ScannedCount"
<*> v .:? "ConsumedCapacity"
parseJSON _ = fail "ScanResponse must be an object."
instance Transaction Scan ScanResponse
instance SignQuery Scan where
type ServiceConfiguration Scan = DdbConfiguration
signQuery gi = ddbSignQuery "Scan" gi
instance ResponseConsumer r ScanResponse where
type ResponseMetadata ScanResponse = DdbResponse
responseConsumer _ ref resp = ddbResponseConsumer ref resp
instance AsMemoryResponse ScanResponse where
type MemoryResponse ScanResponse = ScanResponse
loadToMemory = return
instance ListResponse ScanResponse Item where
listResponse = V.toList . srItems
instance IteratedTransaction Scan ScanResponse where
nextIteratedRequest request response =
case srLastKey response of
Nothing -> Nothing
key -> Just request { sStartKey = key }