Skip to content

Adp/lookup range #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ TODO
.emacs.desktop
dist
dist-newstyle
.stack-work/
b-tree
2 changes: 0 additions & 2 deletions Setup.hs

This file was deleted.

158 changes: 85 additions & 73 deletions b-tree.cabal
Original file line number Diff line number Diff line change
@@ -1,81 +1,93 @@
name: b-tree
version: 0.1.4
synopsis: Immutable disk-based B* trees
description: Immutable disk-based B* trees
homepage: http://github.com/bgamari/b-tree
license: BSD3
license-file: LICENSE
author: Ben Gamari
maintainer: [email protected]
copyright: (c) 2014 Ben Gamari
category: Data
build-type: Simple
cabal-version: >=1.10
cabal-version: 1.12

source-repository head
type: git
location: [email protected]:bgamari/b-tree
-- This file has been generated from package.yaml by hpack version 0.31.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: 75e5224834801bdcd562d1165007ce48fe2da076410bcca562bfe7670ee02342

library
exposed-modules: BTree,
BTree.BinaryList
other-modules: BTree.Types,
BTree.Merge,
BTree.Builder,
BTree.Lookup,
BTree.Walk,
BTree.BinaryFile,
BTree.BuildUnordered
name: b-tree
version: 0.1.4
synopsis: Immutable disk-based B* trees
description: Immutable disk-based B* trees
category: Data
homepage: http://github.com/bgamari/b-tree
author: Ben Gamari
maintainer: [email protected]
copyright: (c) 2014 Ben Gamari
license: BSD3
license-file: LICENSE
build-type: Simple

other-extensions: DeriveGeneric,
FlexibleContexts,
TemplateHaskell,
UndecidableInstances,
StandaloneDeriving,
BangPatterns,
GeneralizedNewtypeDeriving
source-repository head
type: git
location: [email protected]:bgamari/b-tree

hs-source-dirs: src
build-depends: base >=4.6 && <4.13,
mtl >=2.0 && <3.0,
pipes >=4.1 && <4.4,
pipes-interleave >= 1.0 && <2.2,
bytestring >=0.10 && <0.11,
binary >=0.8.4 && <0.11,
transformers >=0.3 && <0.6,
lens >=3.10 && <4.18,
containers >=0.5 && <0.7,
vector >=0.10 && <0.13,
vector-binary-instances >= 0.2 && < 0.3,
errors >=2.0 && <2.4,
exceptions >= 0.8 && < 0.11,
filepath >=1.3 && <1.5,
directory >=1.2 && <1.4,
mmap >=0.5 && <0.6
default-language: Haskell2010
ghc-options: -Wall
library
exposed-modules:
BTree
BTree.BinaryList
other-modules:
BTree.BinaryFile
BTree.Builder
BTree.BuildUnordered
BTree.Lookup
BTree.Merge
BTree.Types
BTree.Walk
Paths_b_tree
hs-source-dirs:
src
other-extensions: DeriveGeneric FlexibleContexts TemplateHaskell UndecidableInstances StandaloneDeriving BangPatterns GeneralizedNewtypeDeriving
ghc-options: -Wall
build-depends:
base >=4.6 && <4.13
, binary >=0.8.4 && <0.11
, bytestring >=0.10 && <0.11
, containers >=0.5 && <0.7
, directory >=1.2 && <1.4
, errors >=2.0 && <2.4
, exceptions >=0.8 && <0.11
, filepath >=1.3 && <1.5
, lens >=3.10 && <4.18
, mmap >=0.5 && <0.6
, mtl >=2.0 && <3.0
, pipes >=4.1 && <4.4
, pipes-interleave >=1.0 && <2.2
, transformers >=0.3 && <0.6
, vector >=0.10 && <0.13
, vector-binary-instances >=0.2 && <0.3
default-language: Haskell2010

test-suite btree-quickcheck
type: exitcode-stdio-1.0
main-is: QuickCheck.hs
hs-source-dirs: tests
default-language: Haskell2010
build-depends: base,
containers,
pipes,
binary,
b-tree,
QuickCheck >= 2.8.2,
tasty,
tasty-quickcheck
type: exitcode-stdio-1.0
main-is: QuickCheck.hs
other-modules:
TestUnorderedMerge
Paths_b_tree
hs-source-dirs:
tests
build-depends:
QuickCheck >=2.8.2
, b-tree
, base
, binary
, containers
, pipes
, tasty
, tasty-quickcheck
default-language: Haskell2010

benchmark btree-benchmark
type: exitcode-stdio-1.0
main-is: Benchmark.hs
hs-source-dirs: benchmarks
default-language: Haskell2010
build-depends: base,
b-tree,
pipes,
criterion >=0.8 && <2.0

type: exitcode-stdio-1.0
main-is: Benchmark.hs
other-modules:
Paths_b_tree
hs-source-dirs:
benchmarks
build-depends:
b-tree
, base
, criterion >=0.8 && <2.0
, pipes
default-language: Haskell2010
65 changes: 65 additions & 0 deletions package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: b-tree
version: '0.1.4'
synopsis: Immutable disk-based B* trees
description: Immutable disk-based B* trees
category: Data
author: Ben Gamari
maintainer: [email protected]
copyright: (c) 2014 Ben Gamari
license: BSD3
homepage: http://github.com/bgamari/b-tree
git: [email protected]:bgamari/b-tree
library:
source-dirs: src
other-extensions:
- BangPatterns
- DeriveGeneric
- FlexibleContexts
- GeneralizedNewtypeDeriving
- LambdaCase
- StandaloneDeriving
- TemplateHaskell
- UndecidableInstances
ghc-options: -Wall
exposed-modules:
- BTree
- BTree.BinaryList
dependencies:
- base >=4.6 && <4.13
- mtl >=2.0 && <3.0
- pipes >=4.1 && <4.4
- pipes-interleave >=1.0 && <2.2
- bytestring >=0.10 && <0.11
- binary >=0.8.4 && <0.11
- transformers >=0.3 && <0.6
- lens >=3.10 && <4.18
- containers >=0.5 && <0.7
- vector >=0.10 && <0.13
- vector-binary-instances >=0.2 && <0.3
- errors >=2.0 && <2.4
- exceptions >=0.8 && <0.11
- filepath >=1.3 && <1.5
- directory >=1.2 && <1.4
- mmap >=0.5 && <0.6
tests:
btree-quickcheck:
main: QuickCheck.hs
source-dirs: tests
dependencies:
- base
- containers
- pipes
- binary
- b-tree
- QuickCheck >=2.8.2
- tasty
- tasty-quickcheck
benchmarks:
btree-benchmark:
main: Benchmark.hs
source-dirs: benchmarks
dependencies:
- base
- b-tree
- pipes
- criterion >=0.8 && <2.0
15 changes: 12 additions & 3 deletions src/BTree/BinaryFile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
-- a trailing "header".
module BTree.BinaryFile
( writeWithHeader
, hWriteWithHeader
, readWithHeader
, hReadWithHeader
) where

import Control.Monad (when)
Expand Down Expand Up @@ -79,8 +81,16 @@ readWithHeader :: (MonadMask m, MonadIO m, B.Binary hdr)
=> FilePath
-> (hdr -> Handle -> m a)
-> ExceptT String m a
readWithHeader fname action = do
r <- lift $ bracket (liftIO $ openFile fname ReadMode) (liftIO . hClose) $ \h -> runExceptT $ do
readWithHeader fname action = ExceptT $ bracket (liftIO $ openFile fname ReadMode) (liftIO . hClose)
$ \h -> runExceptT $ hReadWithHeader h action


hReadWithHeader :: (MonadMask m, MonadIO m, B.Binary hdr)
=> Handle
-> (hdr -> Handle -> m a)
-> ExceptT String m a

hReadWithHeader h action = do
-- read epilogue
liftIO $ hSeek h SeekFromEnd (-epiLength)
epiBytes <- liftIO (LBS.hGet h $ fromIntegral epiLength)
Expand All @@ -96,4 +106,3 @@ readWithHeader fname action = do
liftIO $ hSeek h AbsoluteSeek 0
lift $ action hdr h

ExceptT $ return r
23 changes: 11 additions & 12 deletions src/BTree/BinaryList.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module BTree.BinaryList
, stream
-- * Other queries
, length
, filePath
) where

import Control.Applicative
Expand All @@ -31,13 +30,9 @@ import Pipes
import BTree.BinaryFile

-- | A file containing a finite list of binary encoded items
newtype BinaryList a = BinList FilePath
newtype BinaryList a = BinList Handle
deriving (Show)

-- | Get the path to the @BinaryList@ file
filePath :: BinaryList a -> FilePath
filePath (BinList f) = f

data Header = Header { hdrLength :: Word64 }
deriving (Show)

Expand All @@ -47,9 +42,9 @@ instance B.Binary Header where

-- | Encode the items of the given producer
toBinaryList :: forall m a r. (MonadMask m, MonadIO m, B.Binary a)
=> FilePath -> Producer a m r -> m (BinaryList a, r)
=> Handle -> Producer a m r -> m (BinaryList a, r)
toBinaryList fname producer = do
writeWithHeader fname (go 0 producer BB.empty)
hWriteWithHeader fname (go 0 producer BB.empty)
where
go :: Int -> Producer a m r -> BB.Builder
-> Producer LBS.ByteString m (Header, (BinaryList a, r))
Expand All @@ -71,12 +66,16 @@ toBinaryList fname producer = do
-- | Open a 'BinaryList' file.
--
-- TODO: Sanity checking at open time.
open :: FilePath -> BinaryList a
open = BinList
open :: Handle -> BinaryList a
open = BinList
{-# INLINE open #-}

withHeader :: (MonadMask m, MonadIO m)
=> BinaryList a -> (Header -> Handle -> m b) -> ExceptT String m b
withHeader (BinList fname) action = readWithHeader fname action
=> BinaryList a
-> (Header -> Handle -> m b)
-> ExceptT String m b

withHeader (BinList fhandle) action = hReadWithHeader fhandle action

length :: (MonadMask m, MonadIO m)
=> BinaryList a -> ExceptT String m Word64
Expand Down
Loading