import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.UTF8 as BUL
import Database.TokyoCabinet.HDB
import System.Environment (getArgs)
printUsage = do
putStrLn "Simple TokyoCabinet hash storage interface."
putStrLn "Usage: tchcli <db file> <action> <args>"
putStrLn "Examples: tchcli mydb.tch get mykey # gets value of mykey and prints it to STDOUT"
putStrLn " tchcli mydb.tch put mykey # stores data from STDIN as mykey in database"
putStrLn " tchcli mydb.tch putcat mykey # appends data from STDIN to mykey in database"
performTricks dbname action arg = do
case action of
"get" -> do
db <- new
open db dbname [OCREAT, OREADER]
result <- get db (BUL.fromString arg)
maybe (putStr "") (BSL.putStr) result
"put" -> do
db <- new
open db dbname [OCREAT, OWRITER]
put db (BUL.fromString arg) =<< BSL.getContents
return ()
"putcat" -> do
db <- new
open db dbname [OCREAT, OWRITER]
putcat db (BUL.fromString arg) =<< BSL.getContents
return ()
"num" -> do
db <- new
open db dbname [OCREAT, OREADER]
print =<< rnum db
otherwise -> printUsage
main = do
args <- getArgs
if (length args < 3) then
printUsage
else do
let [dbname, action, arg] = args
performTricks dbname action arg