hindsight-memory-store
Copyright(c) 2024
LicenseBSD3
Maintainermaintainer@example.com
Stabilityexperimental
Safe HaskellNone
LanguageGHC2021

Hindsight.Store.Memory

Description

Overview

In-memory event store using STM (Software Transactional Memory) for fast, concurrent access. Ideal for testing, development, and scenarios where events don't need to survive process restarts.

⚠️ Data is lost on process termination - not suitable for production use.

Quick Start

import Hindsight.Store.Memory (newMemoryStore)
import Hindsight

main :: IO ()
main = do
  -- Create store
  store <- newMemoryStore

  -- Insert events (see Hindsight.Store for details)
  streamId <- StreamId <$> UUID.nextRandom
  let event = mkEvent MyEvent myData
  result <- insertEvents store Nothing $ singleEvent streamId NoStream event

  -- Subscribe to events
  handle <- subscribe store matcher (EventSelector AllStreams FromBeginning)
  -- ... process events ...

Use Cases

When to use Memory store:

  • Unit and integration tests (fast, isolated)
  • Development and prototyping
  • Temporary event processing pipelines
  • Scenarios where persistence isn't required

When NOT to use Memory store:

  • Production systems requiring durability
  • Multi-process applications (each process has separate state)
  • Long-running services that can't afford data loss

Trade-offs

Advantages:

  • Fastest performance (no I/O)
  • No external dependencies
  • Simple setup (single function call)
  • Thread-safe via STM

Limitations:

  • Data lost on process termination or crash
  • Memory usage grows with event count
  • Single-process only (no sharing between instances)
  • No built-in persistence or snapshots

Implementation

Events and stream metadata stored in-memory using STM TVars. Subscriptions use STM retry mechanism for efficient event notification.

Synopsis

Core Types

data MemoryStoreHandle Source #

Handle for the memory store

newtype MemoryCursor Source #

Cursor implementation for the memory store

Constructors

MemoryCursor 

Instances

Instances details
FromJSON MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

ToJSON MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

Generic MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

Associated Types

type Rep MemoryCursor 
Instance details

Defined in Hindsight.Store.Memory

type Rep MemoryCursor = D1 ('MetaData "MemoryCursor" "Hindsight.Store.Memory" "hindsight-memory-store-0.1.0.0-inplace" 'True) (C1 ('MetaCons "MemoryCursor" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSequenceNo") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Integer)))
Show MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

Eq MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

Ord MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

type Rep MemoryCursor Source # 
Instance details

Defined in Hindsight.Store.Memory

type Rep MemoryCursor = D1 ('MetaData "MemoryCursor" "Hindsight.Store.Memory" "hindsight-memory-store-0.1.0.0-inplace" 'True) (C1 ('MetaCons "MemoryCursor" 'PrefixI 'True) (S1 ('MetaSel ('Just "getSequenceNo") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Integer)))

Store Operations

newMemoryStore :: IO MemoryStoreHandle Source #

Creates a new memory store instance

Re-exports