Skip to content
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

Make it possible to choose the index type (fixed selection) #522

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

jeltsch
Copy link
Collaborator

@jeltsch jeltsch commented Jan 12, 2025

This pull requests enables choice of index types via table configurations. It solves the same problem as #502 and #506 but using an alternative implementation, one that supports only a fixed selection of index types.

@jeltsch jeltsch added the enhancement New feature or request label Jan 12, 2025
@jeltsch jeltsch self-assigned this Jan 12, 2025
@jeltsch jeltsch changed the base branch from main to jeltsch/index-choice-with-opacity January 16, 2025 21:38
@jeltsch jeltsch marked this pull request as ready for review January 21, 2025 17:09
@jeltsch jeltsch changed the base branch from jeltsch/index-choice-with-opacity to main January 22, 2025 15:24
Copy link
Collaborator

@jorisdral jorisdral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost LGTM! Some small suggestions and then after that we can merge

There are also some merge conflicts that have to be resolved, and we should probably squash the commits or rewrite the history a bit

src/Database/LSMTree/Internal/MergingRun.hs Outdated Show resolved Hide resolved
src/Database/LSMTree/Internal/Run.hs Outdated Show resolved Hide resolved
src/Database/LSMTree/Internal/RunAcc.hs Outdated Show resolved Hide resolved
src/Database/LSMTree/Internal/Snapshot.hs Outdated Show resolved Hide resolved
src-extras/Database/LSMTree/Extras/NoThunks.hs Outdated Show resolved Hide resolved
Comment on lines 52 to +101
{-|
Adds information about appended pages to an index and outputs newly
available chunks.
available chunks, using primitives specific to the type of the index.

See the documentation of the 'IndexAcc' class for constraints to adhere to.
See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
append :: IndexAcc j => Append -> j s -> ST s [Chunk]
append instruction indexAcc = case instruction of
appendWith :: ((SerialisedKey, SerialisedKey) -> j s -> ST s (Maybe Chunk))
-> ((SerialisedKey, Word32) -> j s -> ST s [Chunk])
-> Append
-> j s
-> ST s [Chunk]
appendWith appendSingle appendMulti instruction indexAcc = case instruction of
AppendSinglePage minKey maxKey
-> toList <$> appendSingle (minKey, maxKey) indexAcc
AppendMultiPage key overflowPageCount
-> appendMulti (key, overflowPageCount) indexAcc
{-# INLINABLE append #-}
{-# INLINABLE appendWith #-}

{-|
Adds information about appended pages to a compact index and outputs newly
available chunks.

See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
appendToCompact :: Append -> IndexCompactAcc s -> ST s [Chunk]
appendToCompact = appendWith IndexCompact.appendSingle
IndexCompact.appendMulti
{-# INLINE appendToCompact #-}

{-|
Adds information about appended pages to an ordinary index and outputs newly
available chunks.

See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
appendToOrdinary :: Append -> IndexOrdinaryAcc s -> ST s [Chunk]
appendToOrdinary = appendWith IndexOrdinary.appendSingle
IndexOrdinary.appendMulti
{-# INLINE appendToOrdinary #-}

{-|
Adds information about appended pages to an index and outputs newly
available chunks.

See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
append :: Append -> IndexAcc s -> ST s [Chunk]
append = appendWith Index.appendSingle
Index.appendMulti
{-# INLINE append #-}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be simpler to have:

{-|
    Adds information about appended pages to an index and outputs newly
    available chunks.

    See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
append :: Append -> IndexAcc s -> ST s [Chunk]
append instruction indexAcc = case instruction of
    AppendSinglePage minKey maxKey
        -> toList <$> Index.appendSingle (minKey, maxKey) indexAcc
    AppendMultiPage key overflowPageCount
        -> Index.appendMulti (key, overflowPageCount) indexAcc
{-# INLINE append #-}

{-|
    Adds information about appended pages to a compact index and outputs newly
    available chunks.

    See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
appendToCompact :: Append -> IndexCompactAcc s -> ST s [Chunk]
appendToCompact instruction indexAcc =
    append instruction (CompactIndexAcc indexAcc)
{-# INLINE appendToCompact #-}

{-|
    Adds information about appended pages to an ordinary index and outputs newly
    available chunks.

    See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
appendToOrdinary :: Append -> IndexOrdinaryAcc s -> ST s [Chunk]
appendToOrdinary instruction indexAcc =
    append instruction (OrdinaryIndexAcc indexAcc)
{-# INLINE appendToOrdinary #-}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or even:

class AppendIndex j where
  {- |
       Adds information about appended pages to an index and outputs newly
       available chunks.

       See the documentation of the 'IndexAcc' type for constraints to adhere to.
  -}
  append :: Append -> j s -> ST s [Chunk]

instance AppendIndex IndexAcc where
  append instruction indexAcc = case instruction of
      AppendSinglePage minKey maxKey
        -> toList <$> Index.appendSingle (minKey, maxKey) indexAcc
      AppendMultiPage key overflowPageCount
        -> Index.appendMulti (key, overflowPageCount) indexAcc
  {-# INLINE append #-}

instance AppendIndex IndexCompactAcc where
  append instruction indexAcc =
      append instruction (CompactIndexAcc indexAcc)
  {-# INLINE append #-}

instance AppendIndex IndexOrdinaryAcc where
  append instruction indexAcc =
      append instruction (OrdinaryIndexAcc indexAcc)
  {-# INLINE append #-}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first solution would be simpler but would also involve the indirection of going through the Index type even when the concrete index accumulator type is known. I considered something like the second solution before but in the end didn’t want to introduce a type class again just for this little bit of auxiliary code. All in all, I have a bias towards leaving the code in its current state. What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be bad to have an additional indirection for option 1, or an extra class for option 2. I'm fine with keeping it as is

test/Test/Database/LSMTree/Internal/Lookup.hs Show resolved Hide resolved
@jeltsch
Copy link
Collaborator Author

jeltsch commented Feb 1, 2025

There are also some merge conflicts that have to be resolved, and we should probably squash the commits or rewrite the history a bit.

I plan to squash the commits, including the ones inherited from #506, after the reviewing is complete, in order to not make reviewing harder. 🙂

@jeltsch
Copy link
Collaborator Author

jeltsch commented Feb 1, 2025

And I plan to rebase the branch only after squashing, in order to make rebasing easier.

Comment on lines +58 to +62
appendWith :: ((SerialisedKey, SerialisedKey) -> j s -> ST s (Maybe Chunk))
-> ((SerialisedKey, Word32) -> j s -> ST s [Chunk])
-> Append
-> j s
-> ST s [Chunk]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
appendWith :: ((SerialisedKey, SerialisedKey) -> j s -> ST s (Maybe Chunk))
-> ((SerialisedKey, Word32) -> j s -> ST s [Chunk])
-> Append
-> j s
-> ST s [Chunk]
appendWith ::
((SerialisedKey, SerialisedKey) -> j s -> ST s (Maybe Chunk))
-> ((SerialisedKey, Word32) -> j s -> ST s [Chunk])
-> Append
-> j s
-> ST s [Chunk]

Comment on lines 52 to +101
{-|
Adds information about appended pages to an index and outputs newly
available chunks.
available chunks, using primitives specific to the type of the index.

See the documentation of the 'IndexAcc' class for constraints to adhere to.
See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
append :: IndexAcc j => Append -> j s -> ST s [Chunk]
append instruction indexAcc = case instruction of
appendWith :: ((SerialisedKey, SerialisedKey) -> j s -> ST s (Maybe Chunk))
-> ((SerialisedKey, Word32) -> j s -> ST s [Chunk])
-> Append
-> j s
-> ST s [Chunk]
appendWith appendSingle appendMulti instruction indexAcc = case instruction of
AppendSinglePage minKey maxKey
-> toList <$> appendSingle (minKey, maxKey) indexAcc
AppendMultiPage key overflowPageCount
-> appendMulti (key, overflowPageCount) indexAcc
{-# INLINABLE append #-}
{-# INLINABLE appendWith #-}

{-|
Adds information about appended pages to a compact index and outputs newly
available chunks.

See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
appendToCompact :: Append -> IndexCompactAcc s -> ST s [Chunk]
appendToCompact = appendWith IndexCompact.appendSingle
IndexCompact.appendMulti
{-# INLINE appendToCompact #-}

{-|
Adds information about appended pages to an ordinary index and outputs newly
available chunks.

See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
appendToOrdinary :: Append -> IndexOrdinaryAcc s -> ST s [Chunk]
appendToOrdinary = appendWith IndexOrdinary.appendSingle
IndexOrdinary.appendMulti
{-# INLINE appendToOrdinary #-}

{-|
Adds information about appended pages to an index and outputs newly
available chunks.

See the documentation of the 'IndexAcc' type for constraints to adhere to.
-}
append :: Append -> IndexAcc s -> ST s [Chunk]
append = appendWith Index.appendSingle
Index.appendMulti
{-# INLINE append #-}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it would be bad to have an additional indirection for option 1, or an extra class for option 2. I'm fine with keeping it as is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants