Skip to content

Commit

Permalink
SUGGESTION: add a 'ResolveAsFirst' newtype wrapper
Browse files Browse the repository at this point in the history
This is one way to recover some desirable behaviour from the `Normal`/`Monoidal`
API split. In particular, if a user does not intend to use `Mupserts`, then in
the full API they still have to define `ResolveValue` instances. The
`ResolveAsFirst` wrapper provides a sensible default instance when this is the
case.
  • Loading branch information
jorisdral committed Nov 18, 2024
1 parent 5b41076 commit 986bec6
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Database/LSMTree.hs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ module Database.LSMTree (
-- ** Properties
, resolveValueValidOutput
, resolveValueAssociativity
-- ** DerivingVia wrappers
, ResolveAsFirst (..)

-- * Utility types
, IOLike
Expand All @@ -113,6 +115,7 @@ import qualified Database.LSMTree.Common as Common
import qualified Database.LSMTree.Internal as Internal
import qualified Database.LSMTree.Internal.BlobRef as Internal
import qualified Database.LSMTree.Internal.Entry as Entry
import qualified Database.LSMTree.Internal.RawBytes as RB
import qualified Database.LSMTree.Internal.Serialise as Internal
import qualified Database.LSMTree.Internal.Snapshot as Internal
import qualified Database.LSMTree.Internal.Vector as V
Expand Down Expand Up @@ -536,3 +539,20 @@ union = error "union: not yet implemented" $ union @m @k @v @b

resolve :: forall v. ResolveValue v => Proxy v -> Internal.ResolveSerialisedValue
resolve = coerce . resolveValue

-- | Newtype wrapper for use with deriving via, which resolves values as a
-- 'Data.Semigroup.First' semigroup.
--
-- The instance makes 'Insert's isomorphic to 'Mupsert's. This can be a sensible
-- default if there is no intent to use 'Mupsert's.
newtype ResolveAsFirst v = ResolveAsFirst v
deriving stock (Show, Eq, Ord)
deriving newtype SerialiseValue

instance ResolveValue (ResolveAsFirst v) where
resolveValue ::
Proxy (ResolveAsFirst v)
-> RB.RawBytes
-> RB.RawBytes
-> RB.RawBytes
resolveValue _ x _ = x

0 comments on commit 986bec6

Please sign in to comment.