Permalink
Browse files

changed removes interface to use Filter instances

  • Loading branch information...
1 parent 95fcd23 commit bf810ac8b6cd71a6a4c71be7b38371338fbb444d @necrobious committed Mar 16, 2011
Showing with 38 additions and 28 deletions.
  1. +38 −28 src/Database/Cassandra.hs
View
@@ -1,3 +1,5 @@
+{-# LANGUAGE RankNTypes #-}
+
module Database.Cassandra
( Column(..)
, (=:)
@@ -62,7 +64,7 @@ insert :: (BS key) => ColumnFamily -> key -> [Column] -> Cassandra ()
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
@@ -72,64 +74,69 @@ insert column_family key columns = do
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" [col "fn", "address" =| [col "state", col "city"], col "ln"]
-remove :: (BS key) => ColumnFamily -> key -> [Column] -> Cassandra ()
-remove column_family key columns = do
+-- 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
- if length columns < 1 -- no columns? remove the whole row from the column family
- then liftIO $ Cas.remove conn (bs key) (Thrift.ColumnPath (Just column_family) Nothing Nothing) now consistency
- else liftIO $ Cas.batch_mutate conn (Map.singleton (bs key) (Map.singleton column_family (mutations now))) consistency
+ 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 = map (q now) columns
- q now c = Thrift.Mutation Nothing (Just $ f now c)
- f now (Super sc cs)
- | length cs < 1 = Thrift.Deletion (Just now) (Just sc) Nothing
- | otherwise = Thrift.Deletion (Just now) (Just sc) (Just $ s (map x cs) )
- f now (Column c _ ) = Thrift.Deletion (Just now) Nothing (Just $ s [c])
- s cs = Thrift.SlicePredicate (Just cs) Nothing
- x (Column c _) = c
+ 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
, rangeReverse :: Bool
, 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
+ 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
@@ -160,14 +167,17 @@ c2c (Thrift.Column (Just n) (Just v) _ _) acc = (Column n v) : acc
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)))
-

0 comments on commit bf810ac

Please sign in to comment.