Skip to content

Commit

Permalink
Merge pull request #172 from IntersectMBO/mheinzel/remove-field-prefix
Browse files Browse the repository at this point in the history
Remove lsm- prefix of Run record fields
  • Loading branch information
mheinzel committed Apr 16, 2024
2 parents a7b8bd4 + 90e9f43 commit f3bbdd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
46 changes: 23 additions & 23 deletions src/Database/LSMTree/Internal/Run.hs
Expand Up @@ -70,42 +70,42 @@ import System.FS.API (HasFS)
-- | The in-memory representation of a completed LSM run.
--
data Run fhandle = Run {
lsmRunNumEntries :: !NumEntries
runNumEntries :: !NumEntries
-- | The reference count for the LSM run. This counts the
-- number of references from LSM handles to this run. When
-- this drops to zero the open files will be closed.
, lsmRunRefCount :: !RefCount
, runRefCount :: !RefCount
-- | The file system paths for all the files used by the run.
, lsmRunFsPaths :: !RunFsPaths
, runRunFsPaths :: !RunFsPaths
-- | The bloom filter for the set of keys in this run.
, lsmRunFilter :: !(Bloom SerialisedKey)
, runFilter :: !(Bloom SerialisedKey)
-- | The in-memory index mapping keys to page numbers in the
-- Key\/Ops file. In future we may support alternative index
-- representations.
, lsmRunIndex :: !IndexCompact
, runIndex :: !IndexCompact
-- | The file handle for the Key\/Ops file. This file is opened
-- read-only and is accessed in a page-oriented way, i.e. only
-- reading whole pages, at page offsets. It will be opened with
-- 'O_DIRECT' on supported platforms.
, lsmRunKOpsFile :: !fhandle
, runKOpsFile :: !fhandle
-- | The file handle for the BLOBs file. This file is opened
-- read-only and is accessed in a normal style using buffered
-- I\/O, reading arbitrary file offset and length spans.
, lsmRunBlobFile :: !fhandle
, runBlobFile :: !fhandle
}

-- | Increase the reference count by one.
addReference :: HasFS IO h -> Run (FS.Handle h) -> IO ()
addReference _ Run {..} =
atomicModifyIORef lsmRunRefCount (\n -> (n+1, ()))
atomicModifyIORef runRefCount (\n -> (n+1, ()))

-- | Decrease the reference count by one.
-- After calling this operation, the run must not be used anymore.
-- If the reference count reaches zero, the run is closed, removing all its
-- associated files from disk.
removeReference :: HasFS IO h -> Run (FS.Handle h) -> IO ()
removeReference fs run@Run {..} = do
count <- atomicModifyIORef' lsmRunRefCount (\n -> (n-1, n-1))
count <- atomicModifyIORef' runRefCount (\n -> (n-1, n-1))
when (count <= 0) $
close fs run

Expand All @@ -116,17 +116,17 @@ removeReference fs run@Run {..} = do
-- we want to be able to re-open closed runs from disk.
close :: HasFS IO h -> Run (FS.Handle h) -> IO ()
close fs Run {..} = do
FS.hClose fs lsmRunKOpsFile
FS.hClose fs runKOpsFile
`finally`
FS.hClose fs lsmRunBlobFile
FS.hClose fs runBlobFile

-- | Create a run by finalising a mutable run.
fromMutable :: HasFS IO h -> RunBuilder (FS.Handle h) -> IO (Run (FS.Handle h))
fromMutable fs builder = do
(lsmRunRefCount, lsmRunFsPaths, lsmRunFilter, lsmRunIndex, lsmRunNumEntries) <-
(runRefCount, runRunFsPaths, runFilter, runIndex, runNumEntries) <-
Builder.unsafeFinalise fs builder
lsmRunKOpsFile <- FS.hOpen fs (runKOpsPath lsmRunFsPaths) FS.ReadMode
lsmRunBlobFile <- FS.hOpen fs (runBlobPath lsmRunFsPaths) FS.ReadMode
runKOpsFile <- FS.hOpen fs (runKOpsPath runRunFsPaths) FS.ReadMode
runBlobFile <- FS.hOpen fs (runBlobPath runRunFsPaths) FS.ReadMode
return Run {..}


Expand Down Expand Up @@ -169,27 +169,27 @@ instance Exception FileFormatError
-- Exceptions will be raised when any of the file's contents don't match their
-- checksum ('ChecksumError') or can't be parsed ('FileFormatError').
openFromDisk :: HasFS IO h -> RunFsPaths -> IO (Run (FS.Handle h))
openFromDisk fs lsmRunFsPaths = do
openFromDisk fs runRunFsPaths = do
expectedChecksums <-
expectValidFile (runChecksumsPath lsmRunFsPaths) . fromChecksumsFile
=<< CRC.readChecksumsFile fs (runChecksumsPath lsmRunFsPaths)
expectValidFile (runChecksumsPath runRunFsPaths) . fromChecksumsFile
=<< CRC.readChecksumsFile fs (runChecksumsPath runRunFsPaths)

-- verify checksums of files we don't read yet
let paths = runFsPaths lsmRunFsPaths
let paths = runFsPaths runRunFsPaths
checkCRC (forRunKOps expectedChecksums) (forRunKOps paths)
checkCRC (forRunBlob expectedChecksums) (forRunBlob paths)

-- read and try parsing files
lsmRunFilter <-
runFilter <-
expectValidFile (forRunFilter paths) . bloomFilterFromSBS
=<< readCRC (forRunFilter expectedChecksums) (forRunFilter paths)
(lsmRunNumEntries, lsmRunIndex) <-
(runNumEntries, runIndex) <-
expectValidFile (forRunIndex paths) . Index.fromSBS
=<< readCRC (forRunIndex expectedChecksums) (forRunIndex paths)

lsmRunKOpsFile <- FS.hOpen fs (runKOpsPath lsmRunFsPaths) FS.ReadMode
lsmRunBlobFile <- FS.hOpen fs (runBlobPath lsmRunFsPaths) FS.ReadMode
lsmRunRefCount <- newIORef 1
runKOpsFile <- FS.hOpen fs (runKOpsPath runRunFsPaths) FS.ReadMode
runBlobFile <- FS.hOpen fs (runBlobPath runRunFsPaths) FS.ReadMode
runRefCount <- newIORef 1
return Run {..}
where
checkCRC :: CRC.CRC32C -> FS.FsPath -> IO ()
Expand Down
18 changes: 9 additions & 9 deletions test/Test/Database/LSMTree/Internal/Run.hs
Expand Up @@ -207,19 +207,19 @@ prop_WriteAndLoad wb = ioProperty $ do
written <- fromWriteBuffer fs fsPaths wb
loaded <- openFromDisk fs fsPaths

(1 @=?) =<< readIORef (lsmRunRefCount written)
(1 @=?) =<< readIORef (lsmRunRefCount loaded)
(1 @=?) =<< readIORef (runRefCount written)
(1 @=?) =<< readIORef (runRefCount loaded)

lsmRunNumEntries written @=? lsmRunNumEntries loaded
lsmRunFilter written @=? lsmRunFilter loaded
lsmRunIndex written @=? lsmRunIndex loaded
runNumEntries written @=? runNumEntries loaded
runFilter written @=? runFilter loaded
runIndex written @=? runIndex loaded

assertEqual "k/ops file"
(FS.handlePath (lsmRunKOpsFile written))
(FS.handlePath (lsmRunKOpsFile loaded))
(FS.handlePath (runKOpsFile written))
(FS.handlePath (runKOpsFile loaded))
assertEqual "blob file"
(FS.handlePath (lsmRunBlobFile written))
(FS.handlePath (lsmRunBlobFile loaded))
(FS.handlePath (runBlobFile written))
(FS.handlePath (runBlobFile loaded))

{-------------------------------------------------------------------------------
Utilities
Expand Down

0 comments on commit f3bbdd1

Please sign in to comment.