packages feed

hscassandra 0.0.5 → 0.0.7

raw patch · 4 files changed

+72/−17 lines, 4 files

Files

− README
+ README.mkd view
@@ -0,0 +1,23 @@+#### Connecting+Connecting to cassandra is done with the withCassandra function, which takes a CassandraConfig instance, and a callback function.++    withCassandra initConfig{cassandraKeyspace="MyApplication"} (do+        getTime+        )+++#### Inserting +Inserts are done via the _insert_ function, taking A Column Family string name, a Key, and a a list of columns to add.++    insert "Users" "kirk@glyphsoftware.com" +        [ "fn" =: "Kirk"+        , "ln" =: "Peterson"+        , "address" =| ["street" =: "2020 NE Going", "city"=:"Portland", "state" =: "OR"]+        , "gender" =: "M"+        ]++#### Retrieving+you can then pull the data back out via the _get_ function.++    get "Users" "kirk@glyphsoftware.com" AllColumns+
hscassandra.cabal view
@@ -1,5 +1,5 @@ name               : hscassandra -version            : 0.0.5+version            : 0.0.7 license            : BSD3  cabal-version      : >= 1.6 author             : Kirk Peterson 
src/Database/Cassandra.hs view
@@ -1,8 +1,11 @@+{-# LANGUAGE RankNTypes #-}+ module Database.Cassandra   ( Column(..)   , (=:)   , (=|)   , insert+  , remove   , Filter(..)   , get   , multiget@@ -43,11 +46,11 @@             | Super  ColumnName [Column]              deriving (Show) --- | Build up a column's insert values+-- Build up a column's insert values (=:) :: (BS column, BS value) => column -> value -> Column (=:) col val = Column (bs col) (bs val) --- | Build up a super column's insert values+-- Build up a super column's insert values (=|) :: (BS supercolumn) => supercolumn -> [Column] -> Column  (=|) sup  = Super (bs sup) @@ -61,7 +64,7 @@ insert column_family key columns = do    consistency <- getConsistencyLevel   conn <- getConnection-  now <- getTime+  now  <- getTime   liftIO $ Cas.batch_mutate conn (Map.singleton (bs key) (Map.singleton column_family (mutations now))) consistency   where   mutations now = map (q now) columns@@ -71,8 +74,31 @@   col c v now = Thrift.Column (Just c) (Just v) (Just now) Nothing   m now (Column c v) = col c v now +-- remove "Users" "necrobious@gmail.com" (columns ["fn", "address" , "ln"])   +remove :: (BS key) => ColumnFamily -> key -> Filter  -> Cassandra ()+remove column_family key fltr = do +  consistency <- getConsistencyLevel+  conn <- getConnection+  now  <- getTime+  case fltr of+    AllColumns     -> liftIO $ Cas.remove conn (bs key) (Thrift.ColumnPath (Just column_family) Nothing Nothing) now consistency+    ColNames []    -> liftIO $ Cas.remove conn (bs key) (Thrift.ColumnPath (Just column_family) Nothing Nothing) now consistency+    ColNames cs    -> liftIO $ Cas.batch_mutate conn (Map.singleton (bs key) (Map.singleton column_family (mutations now Nothing   cs))) consistency+    SupNames sc cs -> liftIO $ Cas.batch_mutate conn (Map.singleton (bs key) (Map.singleton column_family (mutations now (Just sc) cs))) consistency+    _              -> return ()+  where+  mutations  now sup columns = [m now sup columns]+  m now sup cs = Thrift.Mutation Nothing (Just $ d now sup cs) +  d now sup@(Just _) [] = Thrift.Deletion (Just now) sup Nothing           -- delete the whole supercolumn +  d now sup@(Just _) cs = Thrift.Deletion (Just now) sup (Just $ s cs)     -- delete the supercolumn's columns by name +  d now Nothing      [] = Thrift.Deletion (Just now) Nothing Nothing       -- delete ??? not sure what this will do +  d now Nothing      cs = Thrift.Deletion (Just now) Nothing (Just $ s cs) -- delete jsut the column names +  s cs     = Thrift.SlicePredicate (Just cs) Nothing++ data Filter  = AllColumns 	     | ColNames [ByteString]+	     | SupNames ByteString [ByteString] 	     | ColRange 	         { rangeStart   :: ByteString 	         , rangeEnd     :: ByteString@@ -80,35 +106,38 @@ 	         , rangeLimit   :: Int32 	         } -- | a smarter constructor for building a Range filter-range :: (BS column_name) => column_name -> column_name -> Bool -> Int32 -> Filter+range :: forall column_name. (BS column_name) => column_name -> column_name -> Bool -> Int32 -> Filter range start finish = ColRange (bs start) (bs finish)   -- | a smarter constructor for building a Columns filter -columns :: (BS column_name) => [column_name] -> Filter+columns :: forall column_name. (BS column_name) => [column_name] -> Filter columns = ColNames . (map bs)  +supercolumns :: forall column_name. (BS column_name) => column_name -> [column_name] -> Filter+supercolumns sc cs = SupNames (bs sc) (map bs cs)+ -- | for the given key, within the column family, retrieve all columns, unless filtered get :: (BS key) => ColumnFamily -> key -> Filter -> Cassandra [Column]-get column_family key filters = do+get column_family key fltr = do   consistency <- getConsistencyLevel   conn        <- getConnection-  results     <- liftIO $ Cas.get_slice conn (bs key) (column_parent column_family) (slice_predicate filters) consistency +  results     <- liftIO $ Cas.get_slice conn (bs key) (column_parent column_family fltr) (slice_predicate fltr) consistency    return $ foldr rewrap [] results  get_count :: (BS key) => ColumnFamily -> key -> Filter -> Cassandra Int32-get_count column_family key filters = do+get_count column_family key fltr = do   consistency <- getConsistencyLevel   conn        <- getConnection-  liftIO $ Cas.get_count conn (bs key) (column_parent column_family) (slice_predicate filters) consistency +  liftIO $ Cas.get_count conn (bs key) (column_parent column_family fltr) (slice_predicate fltr) consistency      multiget :: (BS key) => ColumnFamily -> [key] -> Filter -> Cassandra (Map key [Column])-multiget column_family keys filters = do+multiget column_family keys fltr = do   let byBs = keys2map keys   consistency <- getConsistencyLevel   conn        <- getConnection-  results     <- liftIO $ Cas.multiget_slice conn (Map.keys byBs) (column_parent column_family) (slice_predicate filters) consistency -  return $ map2map byBs $ Map.foldrWithKey remap Map.empty results -- $ foldr rewrap [] results+  results     <- liftIO $ Cas.multiget_slice conn (Map.keys byBs) (column_parent column_family fltr) (slice_predicate fltr) consistency +  return $ map2map byBs $ Map.foldrWithKey remap Map.empty results    keys2map :: (BS key) => [key] -> Map ByteString key@@ -138,14 +167,17 @@ c2c _ acc = acc  -column_parent :: ColumnFamily -> Thrift.ColumnParent-column_parent  column_family = Thrift.ColumnParent (Just column_family) Nothing+column_parent :: ColumnFamily -> Filter -> Thrift.ColumnParent+column_parent  column_family (SupNames sc _) = Thrift.ColumnParent (Just column_family) (Just sc) +column_parent  column_family _               = Thrift.ColumnParent (Just column_family) Nothing  + slice_predicate :: Filter -> Thrift.SlicePredicate slice_predicate AllColumns =   Thrift.SlicePredicate Nothing (Just $ Thrift.SliceRange (Just Lazy.empty) (Just Lazy.empty) (Just False) (Just 100)) -slice_predicate (ColNames bs) = +slice_predicate (ColNames   bs) =    Thrift.SlicePredicate (Just bs) Nothing+slice_predicate (SupNames _ bs) = +  Thrift.SlicePredicate (Just bs) Nothing slice_predicate (ColRange rs re rr rl) =    Thrift.SlicePredicate Nothing (Just (Thrift.SliceRange (Just rs) (Just re) (Just rr) (Just rl)))-