@@ -262,7 +262,7 @@ data LSMTreeTrace =
262262 CursorTrace
263263 -- Unions
264264 | TraceUnions
265- Int -- ^ Number of unioned tables
265+ ( NonEmpty Word64 ) -- ^ Table identifiers
266266 deriving stock Show
267267
268268data TableTrace =
@@ -553,6 +553,12 @@ data Table m h = Table {
553553 , tableState :: ! (RWVar m (TableState m h ))
554554 , tableArenaManager :: ! (ArenaManager (PrimState m ))
555555 , tableTracer :: ! (Tracer m TableTrace )
556+ -- | Session-unique identifier for this table.
557+ --
558+ -- INVARIANT: a table's identifier is never changed during the lifetime of
559+ -- the table.
560+ , tableId :: ! Word64
561+
556562 -- === Session-inherited
557563
558564 -- | The session that this table belongs to.
@@ -563,8 +569,8 @@ data Table m h = Table {
563569 }
564570
565571instance NFData (Table m h ) where
566- rnf (Table a b c d e) =
567- rnf a `seq` rnf b `seq` rnf c `seq` rwhnf d `seq` rwhnf e
572+ rnf (Table a b c d e f ) =
573+ rnf a `seq` rnf b `seq` rnf c `seq` rwhnf d `seq` rnf e `seq` rwhnf f
568574
569575-- | A table may assume that its corresponding session is still open as
570576-- long as the table is open. A session's global resources, and therefore
@@ -583,8 +589,6 @@ data TableEnv m h = TableEnv {
583589
584590 -- === Table-specific
585591
586- -- | Session-unique identifier for this table.
587- , tableId :: ! Word64
588592 -- | All of the state being in a single 'StrictMVar' is a relatively simple
589593 -- solution, but there could be more concurrency. For example, while inserts
590594 -- are in progress, lookups could still look at the old state without
@@ -615,12 +619,12 @@ tableSessionUniqCounter :: TableEnv m h -> UniqCounter m
615619tableSessionUniqCounter = sessionUniqCounter . tableSessionEnv
616620
617621{-# INLINE tableSessionUntrackTable #-}
618- {-# SPECIALISE tableSessionUntrackTable :: TableEnv IO h -> IO () #-}
622+ {-# SPECIALISE tableSessionUntrackTable :: Word64 -> TableEnv IO h -> IO () #-}
619623-- | Open tables are tracked in the corresponding session, so when a table is
620624-- closed it should become untracked (forgotten).
621- tableSessionUntrackTable :: MonadMVar m => TableEnv m h -> m ()
622- tableSessionUntrackTable thEnv =
623- modifyMVar_ (sessionOpenTables (tableSessionEnv thEnv)) $ pure . Map. delete ( tableId thEnv)
625+ tableSessionUntrackTable :: MonadMVar m => Word64 -> TableEnv m h -> m ()
626+ tableSessionUntrackTable tableId thEnv =
627+ modifyMVar_ (sessionOpenTables (tableSessionEnv thEnv)) $ pure . Map. delete tableId
624628
625629-- | 'withOpenTable' ensures that the table stays open for the duration of the
626630-- provided continuation.
@@ -718,10 +722,10 @@ newWith reg sesh seshEnv conf !am !tc = do
718722 contentVar <- RW. new $ tc
719723 tableVar <- RW. new $ TableOpen $ TableEnv {
720724 tableSessionEnv = seshEnv
721- , tableId = uniqueToWord64 tableId
722725 , tableContent = contentVar
723726 }
724- let ! t = Table conf tableVar am tr sesh
727+ let ! tid = uniqueToWord64 tableId
728+ ! t = Table conf tableVar am tr tid sesh
725729 -- Track the current table
726730 freeTemp reg $ modifyMVar_ (sessionOpenTables seshEnv)
727731 $ pure . Map. insert (uniqueToWord64 tableId) t
@@ -743,7 +747,7 @@ close t = do
743747 -- Since we have a write lock on the table state, we know that we are the
744748 -- only thread currently closing the table. We can safely make the session
745749 -- forget about this table.
746- freeTemp reg (tableSessionUntrackTable thEnv)
750+ freeTemp reg (tableSessionUntrackTable (tableId t) thEnv)
747751 RW. withWriteAccess_ (tableContent thEnv) $ \ tc -> do
748752 releaseTableContent reg tc
749753 pure tc
@@ -969,7 +973,7 @@ newCursor !offsetKey t = withOpenTable t $ \thEnv -> do
969973 cursorId <- uniqueToWord64 <$>
970974 incrUniqCounter (sessionUniqCounter cursorSessionEnv)
971975 let cursorTracer = TraceCursor cursorId `contramap` sessionTracer cursorSession
972- traceWith cursorTracer $ TraceCreateCursor (tableId thEnv )
976+ traceWith cursorTracer $ TraceCreateCursor (tableId t )
973977
974978 -- We acquire a read-lock on the session open-state to prevent races, see
975979 -- 'sessionOpenTables'.
@@ -1336,7 +1340,7 @@ unions ts = do
13361340 Left (i, j) -> throwIO $ ErrUnionsSessionMismatch i j
13371341 Right sesh -> pure sesh
13381342
1339- traceWith (sessionTracer sesh) $ TraceUnions n
1343+ traceWith (sessionTracer sesh) $ TraceUnions ( NE. map tableId ts)
13401344
13411345 -- TODO: Do we really need the configs to match exactly? It's okay as a
13421346 -- requirement for now, but we might want to revisit it. Some settings don't
@@ -1377,8 +1381,6 @@ unions ts = do
13771381 content
13781382
13791383 pure (seshState, t)
1380- where
1381- n = NE. length ts
13821384
13831385-- | Like 'matchBy', but the match function is @(==)@.
13841386match :: Eq a => NonEmpty a -> Either (Int , Int ) a
0 commit comments