Skip to content

Commit 3deb012

Browse files
authored
Merge pull request #455 from IntersectMBO/jeltsch/page-module
Separate utilities related to pages
2 parents 49c234d + cf31a37 commit 3deb012

File tree

15 files changed

+94
-77
lines changed

15 files changed

+94
-77
lines changed

bench/micro/Bench/Database/LSMTree/Internal/Lookup.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import Database.LSMTree.Extras.Random (frequency,
2121
import Database.LSMTree.Extras.UTxO
2222
import Database.LSMTree.Internal.BlobRef (BlobSpan (..))
2323
import Database.LSMTree.Internal.Entry (Entry (..), unNumEntries)
24-
import Database.LSMTree.Internal.IndexCompact (getNumPages)
2524
import Database.LSMTree.Internal.Lookup (bloomQueries, indexSearches,
2625
intraPageLookups, lookupsIO, prepLookups)
26+
import Database.LSMTree.Internal.Page (getNumPages)
2727
import Database.LSMTree.Internal.Paths (RunFsPaths (..))
2828
import Database.LSMTree.Internal.Run (Run)
2929
import qualified Database.LSMTree.Internal.Run as Run

lsm-tree.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ library
135135
Database.LSMTree.Internal.Lookup
136136
Database.LSMTree.Internal.Merge
137137
Database.LSMTree.Internal.MergeSchedule
138+
Database.LSMTree.Internal.Page
138139
Database.LSMTree.Internal.PageAcc
139140
Database.LSMTree.Internal.PageAcc1
140141
Database.LSMTree.Internal.Paths

src-extras/Database/LSMTree/Extras/Generators.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@ import Database.LSMTree.Extras
5151
import Database.LSMTree.Extras.Orphans ()
5252
import Database.LSMTree.Internal.BlobRef (BlobSpan (..))
5353
import Database.LSMTree.Internal.Entry (Entry (..), NumEntries (..))
54-
import Database.LSMTree.Internal.IndexCompact (PageNo (..))
5554
import Database.LSMTree.Internal.IndexCompactAcc (Append (..))
5655
import qualified Database.LSMTree.Internal.Merge as Merge
56+
import Database.LSMTree.Internal.Page (PageNo (..))
5757
import Database.LSMTree.Internal.RawBytes as RB
5858
import Database.LSMTree.Internal.Serialise
5959
import qualified Database.LSMTree.Internal.Serialise.Class as S.Class

src-extras/Database/LSMTree/Extras/NoThunks.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import Database.LSMTree.Internal.IndexCompactAcc
4343
import Database.LSMTree.Internal.Merge hiding (Level)
4444
import qualified Database.LSMTree.Internal.Merge as Merge
4545
import Database.LSMTree.Internal.MergeSchedule
46+
import Database.LSMTree.Internal.Page
4647
import Database.LSMTree.Internal.PageAcc
4748
import Database.LSMTree.Internal.Paths
4849
import Database.LSMTree.Internal.RawBytes

src/Database/LSMTree/Internal/IndexCompact.hs

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55
module Database.LSMTree.Internal.IndexCompact (
66
-- $compact
77
IndexCompact (..)
8-
, PageNo (..)
9-
, nextPageNo
10-
, NumPages
11-
, getNumPages
128
-- * Queries
13-
, PageSpan (..)
14-
, singlePage
15-
, multiPage
16-
, pageSpanSize
179
, search
1810
, countClashes
1911
, hasClashes
@@ -57,6 +49,7 @@ import Database.LSMTree.Internal.BitMath
5749
import Database.LSMTree.Internal.ByteString (byteArrayFromTo,
5850
byteArrayToByteString)
5951
import Database.LSMTree.Internal.Entry (NumEntries (..))
52+
import Database.LSMTree.Internal.Page
6053
import Database.LSMTree.Internal.Serialise
6154
import Database.LSMTree.Internal.Unsliced
6255
import Database.LSMTree.Internal.Vector
@@ -382,60 +375,10 @@ instance NFData IndexCompact where
382375
rnf ic = rnf a `seq` rnf b `seq` rnf c `seq` rnf d
383376
where IndexCompact a b c d = ic
384377

385-
-- | A 0-based number identifying a disk page.
386-
newtype PageNo = PageNo { unPageNo :: Int }
387-
deriving stock (Show, Eq, Ord)
388-
deriving newtype NFData
389-
390-
-- | Increment the page number.
391-
--
392-
-- Note: This does not encure that the incremented page number exists within a given page span.
393-
{-# INLINE nextPageNo #-}
394-
nextPageNo :: PageNo -> PageNo
395-
nextPageNo = PageNo . succ . unPageNo
396-
397-
-- | The number of pages contained by an index or other paging data-structure.
398-
--
399-
-- Note: This is a 0-based number; take care to ensure arithmetic underflow
400-
-- does not occur during subtraction operations!
401-
newtype NumPages = NumPages Word
402-
deriving stock (Eq, Ord, Show)
403-
deriving newtype (NFData)
404-
405-
-- | A type-safe "unwrapper" for 'NumPages'. Use this accessor whenever you want
406-
-- to convert 'NumPages' to a more versatile number type.
407-
{-# INLINE getNumPages #-}
408-
getNumPages :: Integral i => NumPages -> i
409-
getNumPages (NumPages w) = fromIntegral w
410-
411378
{-------------------------------------------------------------------------------
412379
Queries
413380
-------------------------------------------------------------------------------}
414381

415-
-- | A span of pages, representing an inclusive interval of page numbers.
416-
--
417-
-- Typlically used to denote the contiguous page span for a database entry.
418-
data PageSpan = PageSpan {
419-
pageSpanStart :: {-# UNPACK #-} !PageNo
420-
, pageSpanEnd :: {-# UNPACK #-} !PageNo
421-
}
422-
deriving stock (Show, Eq)
423-
424-
instance NFData PageSpan where
425-
rnf (PageSpan x y) = rnf x `seq` rnf y
426-
427-
{-# INLINE singlePage #-}
428-
singlePage :: PageNo -> PageSpan
429-
singlePage i = PageSpan i i
430-
431-
{-# INLINE multiPage #-}
432-
multiPage :: PageNo -> PageNo -> PageSpan
433-
multiPage i j = PageSpan i j
434-
435-
pageSpanSize :: PageSpan -> NumPages
436-
pageSpanSize pspan = NumPages . toEnum $
437-
unPageNo (pageSpanEnd pspan) - unPageNo (pageSpanStart pspan) + 1
438-
439382
-- | Searches for a page span that contains a key–value pair with the given key.
440383
--
441384
-- If there is indeed such a pair, the result is the corresponding page span; if

src/Database/LSMTree/Internal/IndexCompactAcc.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ module Database.LSMTree.Internal.IndexCompactAcc (
1313
-- * Construction
1414
-- $construction-invariants
1515
IndexCompactAcc (..)
16-
, PageNo (..)
1716
, new
1817
, Append (..)
1918
, Chunk (..)
@@ -51,6 +50,7 @@ import qualified Data.Vector.Unboxed.Mutable as VUM
5150
import Data.Word
5251
import Database.LSMTree.Internal.BitMath
5352
import Database.LSMTree.Internal.IndexCompact
53+
import Database.LSMTree.Internal.Page
5454
import Database.LSMTree.Internal.Serialise
5555
import Database.LSMTree.Internal.Unsliced
5656

src/Database/LSMTree/Internal/IndexOrdinary.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import qualified Data.Vector.Primitive as Primitive (Vector (Vector), drop,
2828
force, length, null, splitAt, take)
2929
import Data.Word (Word16, Word32, Word64, Word8, byteSwap32)
3030
import Database.LSMTree.Internal.Entry (NumEntries (NumEntries))
31-
import Database.LSMTree.Internal.IndexCompact (PageNo (PageNo),
31+
import Database.LSMTree.Internal.Page (PageNo (PageNo),
3232
PageSpan (PageSpan))
3333
import Database.LSMTree.Internal.Serialise
3434
(SerialisedKey (SerialisedKey'))

src/Database/LSMTree/Internal/Lookup.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import Control.Monad.ST.Strict
4040

4141
import Database.LSMTree.Internal.BlobRef (WeakBlobRef (..))
4242
import Database.LSMTree.Internal.Entry
43-
import Database.LSMTree.Internal.IndexCompact (IndexCompact,
44-
PageSpan (..))
43+
import Database.LSMTree.Internal.IndexCompact (IndexCompact)
4544
import qualified Database.LSMTree.Internal.IndexCompact as Index
45+
import Database.LSMTree.Internal.Page (PageSpan (..), getNumPages,
46+
pageSpanSize, unPageNo)
4647
import Database.LSMTree.Internal.RawBytes (RawBytes (..))
4748
import qualified Database.LSMTree.Internal.RawBytes as RB
4849
import Database.LSMTree.Internal.RawPage
@@ -99,18 +100,18 @@ indexSearches !arena !indexes !kopsFiles !ks !rkixs = V.generateM n $ \i -> do
99100
!h = kopsFiles `V.unsafeIndex` rix
100101
!k = ks `V.unsafeIndex` kix
101102
!pspan = Index.search k c
102-
!size = Index.pageSpanSize pspan
103+
!size = pageSpanSize pspan
103104
-- The current allocation strategy is to allocate a new pinned
104105
-- byte array for each 'IOOp'. One optimisation we are planning to
105106
-- do is to use a cache of re-usable buffers, in which case we
106107
-- decrease the GC load. TODO: re-usable buffers.
107-
(!off, !buf) <- allocateFromArena arena (Index.getNumPages size * 4096) 4096
108+
(!off, !buf) <- allocateFromArena arena (getNumPages size * 4096) 4096
108109
pure $! IOOpRead
109110
h
110-
(fromIntegral $ Index.unPageNo (pageSpanStart pspan) * 4096)
111+
(fromIntegral $ unPageNo (pageSpanStart pspan) * 4096)
111112
buf
112113
(fromIntegral off)
113-
(Index.getNumPages size * 4096)
114+
(getNumPages size * 4096)
114115
where
115116
!n = VP.length rkixs
116117

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-- | Utilities related to pages.
2+
module Database.LSMTree.Internal.Page (
3+
PageNo (..)
4+
, nextPageNo
5+
, NumPages (..)
6+
, getNumPages
7+
, PageSpan (..)
8+
, singlePage
9+
, multiPage
10+
, pageSpanSize
11+
) where
12+
13+
import Control.DeepSeq (NFData (..))
14+
15+
-- | A 0-based number identifying a disk page.
16+
newtype PageNo = PageNo { unPageNo :: Int }
17+
deriving stock (Show, Eq, Ord)
18+
deriving newtype NFData
19+
20+
-- | Increment the page number.
21+
--
22+
-- Note: This does not ensure that the incremented page number exists within a given page span.
23+
{-# INLINE nextPageNo #-}
24+
nextPageNo :: PageNo -> PageNo
25+
nextPageNo = PageNo . succ . unPageNo
26+
27+
-- | The number of pages contained by an index or other paging data-structure.
28+
--
29+
-- Note: This is a 0-based number; take care to ensure arithmetic underflow
30+
-- does not occur during subtraction operations!
31+
newtype NumPages = NumPages Word
32+
deriving stock (Eq, Ord, Show)
33+
deriving newtype (NFData)
34+
35+
-- | A type-safe "unwrapper" for 'NumPages'. Use this accessor whenever you want
36+
-- to convert 'NumPages' to a more versatile number type.
37+
{-# INLINE getNumPages #-}
38+
getNumPages :: Integral i => NumPages -> i
39+
getNumPages (NumPages w) = fromIntegral w
40+
41+
-- | A span of pages, representing an inclusive interval of page numbers.
42+
--
43+
-- Typlically used to denote the contiguous page span for a database entry.
44+
data PageSpan = PageSpan {
45+
pageSpanStart :: {-# UNPACK #-} !PageNo
46+
, pageSpanEnd :: {-# UNPACK #-} !PageNo
47+
}
48+
deriving stock (Show, Eq)
49+
50+
instance NFData PageSpan where
51+
rnf (PageSpan x y) = rnf x `seq` rnf y
52+
53+
{-# INLINE singlePage #-}
54+
singlePage :: PageNo -> PageSpan
55+
singlePage i = PageSpan i i
56+
57+
{-# INLINE multiPage #-}
58+
multiPage :: PageNo -> PageNo -> PageSpan
59+
multiPage i j = PageSpan i j
60+
61+
{-# INLINE pageSpanSize #-}
62+
pageSpanSize :: PageSpan -> NumPages
63+
pageSpanSize pspan = NumPages . toEnum $
64+
unPageNo (pageSpanEnd pspan) - unPageNo (pageSpanStart pspan) + 1

src/Database/LSMTree/Internal/Run.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ import Database.LSMTree.Internal.BlobRef (BlobRef (..), BlobSpan (..))
7070
import Database.LSMTree.Internal.BloomFilter (bloomFilterFromSBS)
7171
import qualified Database.LSMTree.Internal.CRC32C as CRC
7272
import Database.LSMTree.Internal.Entry (NumEntries (..))
73-
import Database.LSMTree.Internal.IndexCompact (IndexCompact, NumPages)
73+
import Database.LSMTree.Internal.IndexCompact (IndexCompact)
7474
import qualified Database.LSMTree.Internal.IndexCompact as Index
75+
import Database.LSMTree.Internal.Page (NumPages)
7576
import Database.LSMTree.Internal.Paths
7677
import Database.LSMTree.Internal.RunAcc (RunBloomFilterAlloc)
7778
import Database.LSMTree.Internal.RunBuilder (RunBuilder)

0 commit comments

Comments
 (0)