Skip to content

Commit 83bba75

Browse files
committed
acid-experiment: init
1 parent d942002 commit 83bba75

File tree

6 files changed

+189
-0
lines changed

6 files changed

+189
-0
lines changed

acid-experiment/ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for acid-experiment
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.

acid-experiment/LICENSE

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Copyright (c) 2018, Vaibhav Sagar
2+
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
* Redistributions of source code must retain the above copyright
9+
notice, this list of conditions and the following disclaimer.
10+
11+
* Redistributions in binary form must reproduce the above
12+
copyright notice, this list of conditions and the following
13+
disclaimer in the documentation and/or other materials provided
14+
with the distribution.
15+
16+
* Neither the name of Vaibhav Sagar nor the names of other
17+
contributors may be used to endorse or promote products derived
18+
from this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

acid-experiment/Main.hs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{-# LANGUAGE DeriveDataTypeable #-}
2+
{-# LANGUAGE TemplateHaskell #-}
3+
{-# LANGUAGE TypeFamilies #-}
4+
5+
module Main (main) where
6+
7+
import Data.Acid
8+
import Data.Acid.Remote
9+
10+
import Control.Applicative
11+
import Control.Monad.Reader
12+
import Control.Monad.State
13+
import Data.ByteString (ByteString(..))
14+
import Data.ByteString.UTF8 (toString, fromString)
15+
import Data.SafeCopy
16+
import Network
17+
import System.Environment
18+
import System.Exit
19+
import System.IO
20+
21+
import Data.Typeable
22+
23+
import qualified Data.Map as Map
24+
25+
------------------------------------------------------
26+
-- The Haskell structure that we want to encapsulate
27+
28+
type Key = ByteString
29+
type Value = ByteString
30+
31+
newtype KeyValue_v0 = KeyValue_v0 (Map.Map String String)
32+
deriving (Typeable)
33+
34+
newtype KeyValue = KeyValue (Map.Map Key Value)
35+
deriving (Typeable)
36+
37+
$(deriveSafeCopy 0 'base ''KeyValue_v0)
38+
$(deriveSafeCopy 1 'extension ''KeyValue)
39+
40+
------------------------------------------------------
41+
-- The transaction we will execute over the state.
42+
43+
insertKey_v0 :: String -> String -> Update KeyValue_v0 ()
44+
insertKey_v0 key value
45+
= do KeyValue_v0 m <- get
46+
put (KeyValue_v0 (Map.insert key value m))
47+
48+
lookupKey_v0 :: String -> Query KeyValue_v0 (Maybe String)
49+
lookupKey_v0 key
50+
= do KeyValue_v0 m <- ask
51+
return (Map.lookup key m)
52+
53+
$(makeAcidic ''KeyValue_v0 ['insertKey_v0, 'lookupKey_v0])
54+
55+
insertKey :: Key -> Value -> Update KeyValue ()
56+
insertKey key value
57+
= do KeyValue m <- get
58+
put (KeyValue (Map.insert key value m))
59+
60+
lookupKey :: Key -> Query KeyValue (Maybe Value)
61+
lookupKey key
62+
= do KeyValue m <- ask
63+
return (Map.lookup key m)
64+
65+
$(makeAcidic ''KeyValue ['insertKey, 'lookupKey])
66+
67+
instance Migrate KeyValue where
68+
type MigrateFrom KeyValue = KeyValue_v0
69+
migrate (KeyValue_v0 mp) = KeyValue $ Map.foldrWithKey (\k a -> Map.insert (fromString k) (fromString a)) Map.empty mp
70+
71+
------------------------------------------------------
72+
-- This is how AcidState is used:
73+
74+
main :: IO ()
75+
main = do args <- getArgs
76+
acid <- openLocalState (KeyValue Map.empty)
77+
case args of
78+
[key]
79+
-> do mbKey <- query acid (LookupKey (fromString key))
80+
case mbKey of
81+
Nothing -> putStrLn $ key ++ " has no associated value."
82+
Just value -> putStrLn $ key ++ " = " ++ (toString value)
83+
[key,val]
84+
-> do update acid (InsertKey (fromString key) (fromString val))
85+
putStrLn "Done."
86+
_ -> do putStrLn "Usage:"
87+
putStrLn " key Lookup the value of 'key'."
88+
putStrLn " key value Set the value of 'key' to 'value'."
89+
closeAcidState acid

acid-experiment/Setup.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import Distribution.Simple
2+
main = defaultMain

acid-experiment/acid-experiment.cabal

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Initial acid-experiment.cabal generated by cabal init. For further
2+
-- documentation, see http://haskell.org/cabal/users-guide/
3+
4+
name: acid-experiment
5+
version: 0.1.0.0
6+
-- synopsis:
7+
-- description:
8+
license: BSD3
9+
license-file: LICENSE
10+
author: Vaibhav Sagar
11+
maintainer: [email protected]
12+
-- copyright:
13+
-- category:
14+
build-type: Simple
15+
extra-source-files: ChangeLog.md
16+
cabal-version: >=1.10
17+
18+
executable acid-experiment
19+
main-is: Main.hs
20+
-- other-modules:
21+
-- other-extensions:
22+
build-depends: base >=4.9 && <4.10
23+
, acid-state
24+
, bytestring
25+
, containers
26+
, mtl
27+
, network
28+
, safecopy
29+
, unordered-containers
30+
, utf8-string
31+
-- hs-source-dirs:
32+
default-language: Haskell2010

acid-experiment/default.nix

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{ nixpkgs ? import <nixpkgs> {}, compiler ? "default" }:
2+
3+
let
4+
5+
inherit (nixpkgs) pkgs;
6+
7+
f = { mkDerivation, acid-state, base, bytestring, containers, mtl
8+
, network, safecopy, stdenv, unordered-containers, utf8-string
9+
}:
10+
mkDerivation {
11+
pname = "acid-experiment";
12+
version = "0.1.0.0";
13+
src = ./.;
14+
isLibrary = false;
15+
isExecutable = true;
16+
executableHaskellDepends = [
17+
acid-state base bytestring containers mtl network safecopy
18+
unordered-containers utf8-string
19+
];
20+
license = stdenv.lib.licenses.bsd3;
21+
};
22+
23+
haskellPackages = if compiler == "default"
24+
then pkgs.haskellPackages
25+
else pkgs.haskell.packages.${compiler};
26+
27+
drv = haskellPackages.callPackage f {};
28+
29+
in
30+
31+
if pkgs.lib.inNixShell then drv.env else drv

0 commit comments

Comments
 (0)