Skip to content

Commit

Permalink
Merge pull request #16 from kadena-io/edmundnoble/table-abstraction
Browse files Browse the repository at this point in the history
Add a Table abstraction replacing the Cas abstraction
  • Loading branch information
edmundnoble authored Jan 5, 2023
2 parents 034cef6 + 0eb5697 commit d691dd0
Show file tree
Hide file tree
Showing 12 changed files with 607 additions and 730 deletions.
20 changes: 10 additions & 10 deletions vendored/chainweb-storage/.github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Build

on:
workflow_dispatch:
push:
schedule:
- cron: '0 8 * * *'
Expand All @@ -13,21 +14,21 @@ jobs:
strategy:
fail-fast: false
matrix:
ghc: ['8.8.4', '8.10.5', '9.0.1']
cabal: ['3.4']
ghc: ['8.10.7', '9.0.2', '9.2']
cabal: ['3.6']
os:
- 'ubuntu-18.04'
- 'ubuntu-20.04'
- 'ubuntu-22.04'
- 'macOS-latest'
# - 'windows-latest' # windows builds generally work but are flaky on the github runners

steps:

# Setup
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Install GHC and Cabal
uses: haskell/actions/setup@v1.2.3
uses: haskell/actions/setup@v2
with:
ghc-version: ${{ matrix.ghc }}
cabal-version: ${{ matrix.cabal }}
Expand All @@ -36,7 +37,7 @@ jobs:
run: |
case "${{ runner.os }}" in
Linux)
sudo apt-get install -y librocksdb-dev
sudo apt-get install -y libgflags-dev liblz4-dev libzstd-dev libsnappy-dev libbz2-dev
;;
mac*)
brew install rocksdb
Expand All @@ -54,20 +55,19 @@ jobs:
documentation: True
benchmarks: True
tests: True
write-ghc-environment-files: always
EOF
# Restore Packages from Caches
- uses: actions/cache@v2.1.6
- uses: actions/cache@v3
name: Cache ~/.cabal/packages
with:
path: |
~/.cabal/packages
~/.cabal/store
dist-newstyle
key: ${{ matrix.os }}-${{ matrix.ghc }}-cabal-cache-${{ github.sha }}
key: ${{ matrix.os }}-${{ matrix.ghc }}-cabal-${{ github.sha }}
restore-keys: |
${{ matrix.os }}-${{ matrix.ghc }}-cabal-cache-
${{ matrix.os }}-${{ matrix.ghc }}-cabal-
# Build
- name: Update package database
Expand Down
2 changes: 1 addition & 1 deletion vendored/chainweb-storage/cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ packages: chainweb-storage.cabal
source-repository-package
type: git
location: https://github.com/kadena-io/rocksdb-haskell.git
tag: 2f0bae730d137b7edee090c4014d891675933ba5
tag: 42cfb81426fe1cadfe411a723942c55bdb54285e
15 changes: 8 additions & 7 deletions vendored/chainweb-storage/chainweb-storage.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ library
ghc-options:
-Wall
exposed-modules:
Data.CAS
Data.CAS.Forgetful
Data.CAS.HashMap
Data.CAS.RocksDB
Data.DedupStore
Chainweb.Storage.Table
Chainweb.Storage.Table.Forgetful
Chainweb.Storage.Table.HashMap
Chainweb.Storage.Table.RocksDB
Chainweb.Storage.DedupStore
build-depends:
, base >=4.10 && <4.16
, base >=4.10 && <5
, bytestring >=0.10
, containers >=0.5
, cryptonite >= 0.25
Expand All @@ -48,6 +48,7 @@ library
, hashable >=1.2
, lens >=4.16
, memory >=0.14
, mtl >= 2.2
, nothunks >= 0.1.0.0
, rocksdb-haskell-kadena >=1.1.0
, stm >=2.4
Expand All @@ -74,7 +75,7 @@ test-suite rocksdb-tests

-- external
, async >=2.2
, base >=4.10 && <4.16
, base >=4.10 && <5
, bytestring >=0.10
, exceptions >=0.10
, lens >=4.16
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}

-- |
-- Module: Data.DedupStore
-- Module: Chainweb.Storage.DedupStore
-- Copyright: Copyright © 2019 Kadena LLC.
-- License: MIT
-- Maintainer: Lars Kuhtz <[email protected]>
-- Stability: experimental
--
-- A Deduplicated Key-Value Store
--
module Data.DedupStore
module Chainweb.Storage.DedupStore
(
-- * Deduplicated Key-Value Store
DedupStore(..)
Expand Down Expand Up @@ -46,7 +47,6 @@ import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as BB
import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Unsafe as BU
import Data.CAS
import Data.Hashable
import Data.Int
import qualified Data.List as L
Expand All @@ -64,7 +64,8 @@ import System.IO.Unsafe

-- internal modules

import Data.CAS.RocksDB
import Chainweb.Storage.Table
import Chainweb.Storage.Table.RocksDB

-- -------------------------------------------------------------------------- --
-- Utils
Expand Down Expand Up @@ -111,7 +112,7 @@ instance Exception DedupStoreException
--
data DedupStore k v = DedupStore
{ _dedupRoots :: !(RocksDbTable k DedupHash)
, _dedupChunks :: !(RocksDbCas Chunk)
, _dedupChunks :: !(Casify RocksDbTable Chunk)
, _dedupValueCodec :: !(Codec v)
}

Expand All @@ -126,7 +127,7 @@ newDedupStore
-> DedupStore k v
newDedupStore rdb vc kc n = DedupStore
{ _dedupRoots = newTable rdb dedupHashCodec kc (n <> ["roots"])
, _dedupChunks = newCas rdb dedupChunkCodec dedupHashCodec (n <> ["chunks"])
, _dedupChunks = Casify $ newTable rdb dedupChunkCodec dedupHashCodec (n <> ["chunks"])
, _dedupValueCodec = vc
}
where
Expand Down Expand Up @@ -155,7 +156,7 @@ dedupInsert :: DedupStore k v -> k -> v -> IO ()
dedupInsert store k v = do
dedupKey <- dedupStore (_dedupChunks store)
$ BL.fromStrict $ _codecEncode (_dedupValueCodec store) v
tableInsert (_dedupRoots store) k dedupKey
tableInsert (_dedupRoots store) k dedupKey
{-# INLINE dedupInsert #-}

-- | @dedupLookup db k@ returns 'Just' the value at key @k@ in the
Expand Down Expand Up @@ -185,7 +186,7 @@ instance IsCasValue Chunk where
type CasKeyType Chunk = DedupHash
casKey (Chunk _ h _) = h

type DedupCas cas = (IsCas cas, CasValueType cas ~ Chunk)
type DedupCas cas = Cas cas Chunk

-- | Store a sequence of bytes in a deduplicated content addressed store.
-- Returns the hash of the bytes, that can be used to query the data from the
Expand Down Expand Up @@ -217,15 +218,15 @@ dedupStore' store = go0
where
go0 :: HasCallStack => BL.ByteString -> IO (Int, Int, DedupHash)
go0 bytes = mapM (hashAndStore 0x0) (toChunks $ roll bytes) >>= \case
[] -> error "Data.DedupStore.dedupStore.go0: Data.ByteString.Lazy.toChunks must not return an empty list"
[] -> error "Chainweb.Storage.DedupStore.dedupStore.go0: Data.ByteString.Lazy.toChunks must not return an empty list"
[x] -> return x
l -> do
(h, m, r) <- go1 (third3 <$> l)
return (sum (fst3 <$> l) + h, sum (snd3 <$> l) + m, r)

go1 :: HasCallStack => [DedupHash] -> IO (Int, Int, DedupHash)
go1 hashes = mapM (hashAndStore 0x1) (rollHashes hashes) >>= \case
[] -> error "Data.DedupStore.dedupStore.go1: Data.ByteString.Lazy.toChunks must not return an empty list"
[] -> error "Chainweb.Storage.DedupStore.dedupStore.go1: Data.ByteString.Lazy.toChunks must not return an empty list"
[x] -> return x
l -> do
(h, m, r) <- go1 (third3 <$> l)
Expand All @@ -245,10 +246,10 @@ dedupStore' store = go0
hashAndStore :: Word8 -> B.ByteString -> IO (Int, Int, DedupHash)
hashAndStore tag c = do
let h = DedupHash $ BA.convert $ C.hash @_ @DedupHashAlg (B.cons tag c)
(hit, miss) <- casMember store h >>= \x -> if x
(hit, miss) <- tableMember store h >>= \x -> if x
then return (1, 0)
else do
casInsert store (Chunk tag h c)
casInsert store (Chunk tag h c)
return (0, 1)
return (hit, miss, h)
{-# INLINE hashAndStore #-}
Expand All @@ -275,15 +276,15 @@ dedupRestore
-> DedupHash
-> IO (Maybe BL.ByteString)
dedupRestore store key = fmap BB.toLazyByteString <$> do
casLookup store key >>= \case
tableLookup store key >>= \case
Nothing -> return Nothing
Just (Chunk 0x0 _ b) -> return $ Just $ BB.byteString b
Just (Chunk 0x1 _ b) -> Just <$> splitHashes b
_ -> throwM $ DedupStoreCorruptedData "Unknown chunk tag"

where
go h = do
casLookup store h >>= \case
tableLookup store h >>= \case
Nothing -> throwM $ DedupStoreCorruptedData "Missing chunk from store"
Just (Chunk 0x0 _ b) -> return (BB.byteString b)
Just (Chunk 0x1 _ b) -> splitHashes b
Expand All @@ -310,7 +311,7 @@ roll :: BL.ByteString -> BL.ByteString
roll z = BL.fromChunks $ go seed 0 z za ze
where
z' = BL.unpack z
za = (BL.unpack $ BL.replicate window 0) <> z'
za = BL.unpack (BL.replicate window 0) <> z'
{-# SCC za #-}
ze = z'
{-# SCC ze #-}
Expand Down
Loading

0 comments on commit d691dd0

Please sign in to comment.