memory

package
v0.295.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 2, 2025 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	CreateEvent     = `Create`
	SaveEvent       = `Save`
	UpdateEvent     = `Update`
	DeleteAllEvent  = `DeleteAll`
	DeleteByIDEvent = `DeleteByID`
)

Name Types

View Source
const ErrMissingID errorkit.Error = "ErrMissingID"

Variables

This section is empty.

Functions

func LogHistoryOnFailure

func LogHistoryOnFailure(tb testingTB, el EventViewer)

func MakeID

func MakeID[ID any](context.Context) (ID, error)

func RewriteEventLog

func RewriteEventLog[EventType any](el *EventLog, rewrite func(es []EventType) []EventType)

func Scheduler added in v0.247.0

func Scheduler() tasker.Scheduler

Types

type CacheRepository

type CacheRepository[Entity, ID any] struct {
	Memory *Memory
	// contains filtered or unexported fields
}

func NewCacheRepository

func NewCacheRepository[Entity, ID any](m *Memory) *CacheRepository[Entity, ID]

func (*CacheRepository[Entity, ID]) BeginTx

func (cr *CacheRepository[Entity, ID]) BeginTx(ctx context.Context) (context.Context, error)

func (*CacheRepository[Entity, ID]) CommitTx

func (cr *CacheRepository[Entity, ID]) CommitTx(ctx context.Context) error

func (*CacheRepository[Entity, ID]) Entities

func (cr *CacheRepository[Entity, ID]) Entities() cache.EntityRepository[Entity, ID]

func (*CacheRepository[Entity, ID]) Hits

func (cr *CacheRepository[Entity, ID]) Hits() cache.HitRepository[ID]

func (*CacheRepository[Entity, ID]) Init added in v0.285.0

func (cr *CacheRepository[Entity, ID]) Init()

func (*CacheRepository[Entity, ID]) RollbackTx

func (cr *CacheRepository[Entity, ID]) RollbackTx(ctx context.Context) error

type Event

type Event = interface{}

type EventLog

type EventLog struct {
	Options struct {
		DisableAsyncSubscriptionHandling bool
	}
	// contains filtered or unexported fields
}

EventLog is an event source principles based in memory resource, that allows easy debugging and tracing during development for fast and descriptive feedback loops.

func NewEventLog

func NewEventLog() *EventLog

func (*EventLog) Append

func (el *EventLog) Append(ctx context.Context, event Event) error

func (*EventLog) Atomic

func (el *EventLog) Atomic(ctx context.Context, fn func(tx *EventLogTx) error) error

func (*EventLog) BeginTx

func (el *EventLog) BeginTx(ctx context.Context) (context.Context, error)

func (*EventLog) CommitTx

func (el *EventLog) CommitTx(ctx context.Context) error

func (*EventLog) Compress

func (el *EventLog) Compress()

func (*EventLog) Events

func (el *EventLog) Events() []Event

func (*EventLog) EventsInContext

func (el *EventLog) EventsInContext(ctx context.Context) []Event

func (*EventLog) LookupMeta

func (el *EventLog) LookupMeta(ctx context.Context, key string, ptr interface{}) (_found bool, _err error)

func (*EventLog) LookupTx

func (el *EventLog) LookupTx(ctx context.Context) (*EventLogTx, bool)

func (*EventLog) Rewrite

func (el *EventLog) Rewrite(mapper func(es []Event) []Event)

func (*EventLog) RollbackTx

func (el *EventLog) RollbackTx(ctx context.Context) error

func (*EventLog) SetMeta

func (el *EventLog) SetMeta(ctx context.Context, key string, value interface{}) (context.Context, error)

type EventLogEvent

type EventLogEvent struct {
	Type  string
	Name  string
	Trace []Stack
}

func (EventLogEvent) GetTrace

func (et EventLogEvent) GetTrace() []Stack

func (EventLogEvent) SetTrace

func (et EventLogEvent) SetTrace(trace []Stack)

func (EventLogEvent) String

func (et EventLogEvent) String() string

type EventLogRepository

type EventLogRepository[ENT, ID any] struct {
	EventLog *EventLog
	MakeID   func(ctx context.Context) (ID, error)

	// Namespace separates different repository events in the event log.
	// By default same entities reside under the same Namespace through their fully qualified name used as namespace ID.
	// If you want create multiple EventLogRepository that works with the same entity but act as separate repositories,
	// you need to assign a unique Namespace for each of these EventLogRepository.
	Namespace string

	Options struct {
		CompressEventLog bool
	}
	// contains filtered or unexported fields
}

EventLogRepository is an EventLog based development in memory repository, that allows easy debugging and tracing during development for fast and descriptive feedback loops.

func NewEventLogRepository

func NewEventLogRepository[ENT, ID any](m *EventLog) *EventLogRepository[ENT, ID]

func NewEventLogRepositoryWithNamespace

func NewEventLogRepositoryWithNamespace[ENT, ID any](m *EventLog, ns string) *EventLogRepository[ENT, ID]

func (*EventLogRepository[ENT, ID]) BeginTx

func (s *EventLogRepository[ENT, ID]) BeginTx(ctx context.Context) (context.Context, error)

func (*EventLogRepository[ENT, ID]) CommitTx

func (s *EventLogRepository[ENT, ID]) CommitTx(ctx context.Context) error

func (*EventLogRepository[ENT, ID]) Compress

func (s *EventLogRepository[ENT, ID]) Compress()

func (*EventLogRepository[ENT, ID]) Create

func (s *EventLogRepository[ENT, ID]) Create(ctx context.Context, ptr *ENT) error

func (*EventLogRepository[ENT, ID]) DeleteAll

func (s *EventLogRepository[ENT, ID]) DeleteAll(ctx context.Context) error

func (*EventLogRepository[ENT, ID]) DeleteByID

func (s *EventLogRepository[ENT, ID]) DeleteByID(ctx context.Context, id ID) error

func (*EventLogRepository[ENT, ID]) Events

func (s *EventLogRepository[ENT, ID]) Events(ctx context.Context) []EventLogRepositoryEvent[ENT, ID]

func (*EventLogRepository[ENT, ID]) FindAll

func (s *EventLogRepository[ENT, ID]) FindAll(ctx context.Context) (iter.Seq2[ENT, error], error)

func (*EventLogRepository[ENT, ID]) FindByID

func (s *EventLogRepository[ENT, ID]) FindByID(ctx context.Context, id ID) (_ent ENT, _found bool, _err error)

func (*EventLogRepository[ENT, ID]) FindByIDs

func (s *EventLogRepository[ENT, ID]) FindByIDs(ctx context.Context, ids ...ID) (iter.Seq2[ENT, error], error)

func (*EventLogRepository[ENT, ID]) GetNamespace

func (s *EventLogRepository[ENT, ID]) GetNamespace() string

func (*EventLogRepository[ENT, ID]) LookupTx

func (s *EventLogRepository[ENT, ID]) LookupTx(ctx context.Context) (*EventLogTx, bool)

func (*EventLogRepository[ENT, ID]) RollbackTx

func (s *EventLogRepository[ENT, ID]) RollbackTx(ctx context.Context) error

func (*EventLogRepository[ENT, ID]) Save added in v0.242.0

func (s *EventLogRepository[ENT, ID]) Save(ctx context.Context, ptr *ENT) (rErr error)

func (*EventLogRepository[ENT, ID]) Update

func (s *EventLogRepository[ENT, ID]) Update(ctx context.Context, ptr *ENT) error

func (*EventLogRepository[ENT, ID]) View

func (s *EventLogRepository[ENT, ID]) View(ctx context.Context) EventLogRepositoryView[ENT, ID]

type EventLogRepositoryEvent

type EventLogRepositoryEvent[ENT, ID any] struct {
	Namespace string
	Name      string
	Value     ENT
	Trace     []Stack
}

func (EventLogRepositoryEvent[ENT, ID]) GetTrace

func (e EventLogRepositoryEvent[ENT, ID]) GetTrace() []Stack

func (EventLogRepositoryEvent[ENT, ID]) SetTrace

func (e EventLogRepositoryEvent[ENT, ID]) SetTrace(trace []Stack)

func (EventLogRepositoryEvent[ENT, ID]) String

func (e EventLogRepositoryEvent[ENT, ID]) String() string

type EventLogRepositoryView

type EventLogRepositoryView[ENT, ID any] map[string]ENT

func (EventLogRepositoryView[ENT, ID]) FindByID

func (v EventLogRepositoryView[ENT, ID]) FindByID(id ID) (ENT, bool)

type EventLogSubscriber

type EventLogSubscriber interface {
	// Handle handles the the subscribed event.
	// Context may or may not have meta information about the received event.
	// To ensure expectations, define a resource specification <contract> about what must be included in the context.
	Handle(ctx context.Context, event interface{}) error
	HandleError(ctx context.Context, err error) error
}

type EventLogTx

type EventLogTx struct {
	// contains filtered or unexported fields
}

func (*EventLogTx) Append

func (tx *EventLogTx) Append(ctx context.Context, event Event) error

func (*EventLogTx) Events

func (tx *EventLogTx) Events() []Event

type EventManager

type EventManager interface {
	Append(context.Context, Event) error
	EventViewer
}

type EventViewer

type EventViewer interface {
	Events() []Event
}

type FanOutExchange

type FanOutExchange[Data any] struct {
	// Queues contain every Queue that suppose to be bound to the FanOut Exchange
	Queues []*Queue[Data]
}

FanOutExchange delivers messages to all the queues that are bound to it. This is useful when you want to broadcast a message to multiple consumers.

func (*FanOutExchange[Data]) MakeQueue

func (e *FanOutExchange[Data]) MakeQueue() *Queue[Data]

MakeQueue creates a unique queue which is bound to the FanOut exchange.

func (*FanOutExchange[Data]) Publish

func (e *FanOutExchange[Data]) Publish(ctx context.Context, data ...Data) (rErr error)

Publish will publish all data to all FanOutExchange.Queues in an atomic fashion. It will either all succeed or all fail together.

func (*FanOutExchange[Data]) Purge

func (e *FanOutExchange[Data]) Purge(ctx context.Context) (rErr error)

Purge will flush all data from the exchange's queues

type FileSystem

type FileSystem struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"fmt"
	"io"
	"io/fs"
	"os"
	"path/filepath"

	"go.llib.dev/frameless/adapter/memory"

	"go.llib.dev/frameless/port/filesystem"
)

func main() {
	fsys := &memory.FileSystem{}

	file, err := fsys.OpenFile("test", os.O_RDWR|os.O_CREATE|os.O_EXCL, filesystem.ModeUserRWX)
	if err != nil {
		panic(err)
	}
	defer file.Close()

	file.Write([]byte("Hello world!"))
	file.Seek(0, io.SeekStart)

	bs, err := io.ReadAll(file)
	if err != nil {
		panic(err)
	}

	fmt.Println(string(bs)) // "Hello world!"

	file.Close()
	fsys.Remove("test")

	fsys.Mkdir("a", filesystem.ModeUserRWX)

	file2Name := filepath.Join("a", "test.txt")
	file2, err := filesystem.Create(fsys, file2Name)
	if err != nil {
		panic(err)
	}
	file2.Close()

	file2, err = filesystem.Open(fsys, file2Name)
	if err != nil {
		panic(err)
	}
	file2.Close()

	filesystem.WalkDir(fsys, ".", func(path string, d fs.DirEntry, err error) error {
		return fs.SkipDir
	})
}

func (*FileSystem) Mkdir

func (mfs *FileSystem) Mkdir(name string, perm fs.FileMode) error

func (*FileSystem) OpenFile

func (mfs *FileSystem) OpenFile(name string, flag int, perm fs.FileMode) (filesystem.File, error)

func (*FileSystem) Remove

func (mfs *FileSystem) Remove(name string) error

func (*FileSystem) Stat

func (mfs *FileSystem) Stat(name string) (fs.FileInfo, error)

type FileSystemFile

type FileSystemFile struct {
	// contains filtered or unexported fields
}

func (*FileSystemFile) Close

func (f *FileSystemFile) Close() error

func (*FileSystemFile) Read

func (f *FileSystemFile) Read(bytes []byte) (int, error)

func (*FileSystemFile) ReadDir

func (f *FileSystemFile) ReadDir(n int) ([]fs.DirEntry, error)

func (*FileSystemFile) Seek

func (f *FileSystemFile) Seek(offset int64, whence int) (int64, error)

func (*FileSystemFile) Stat

func (f *FileSystemFile) Stat() (fs.FileInfo, error)

func (*FileSystemFile) Sync

func (f *FileSystemFile) Sync() error

func (*FileSystemFile) Write

func (f *FileSystemFile) Write(p []byte) (n int, err error)

type Lock added in v0.247.0

type Lock struct {
	// contains filtered or unexported fields
}

Lock is a memory-based implementation of guard.Lock. Lock is not safe to call from different application instances. Lock is meant to be used in a single application instance.

func NewLocker

func NewLocker() *Lock

func (*Lock) Lock added in v0.247.0

func (l *Lock) Lock(ctx context.Context) (context.Context, error)

func (*Lock) TryLock added in v0.247.0

func (l *Lock) TryLock(ctx context.Context) (context.Context, bool, error)

func (*Lock) Unlock added in v0.247.0

func (l *Lock) Unlock(ctx context.Context) error

type LockerFactory

type LockerFactory[Key comparable] struct {
	// contains filtered or unexported fields
}

func NewLockerFactory

func NewLockerFactory[Key comparable]() *LockerFactory[Key]

func NewTaskerSchedulerLocks added in v0.246.0

func NewTaskerSchedulerLocks() *LockerFactory[tasker.ScheduleID]

func (*LockerFactory[Key]) LockerFor

func (lf *LockerFactory[Key]) LockerFor(key Key) guard.Locker

func (*LockerFactory[Key]) NonBlockingLockerFor added in v0.247.0

func (lf *LockerFactory[Key]) NonBlockingLockerFor(key Key) guard.NonBlockingLocker

type Memory

type Memory struct {
	// contains filtered or unexported fields
}

func NewMemory

func NewMemory() *Memory

func (*Memory) All

func (m *Memory) All(T any, ctx context.Context, namespace string) (sliceOfT interface{})

func (*Memory) BeginTx

func (m *Memory) BeginTx(ctx context.Context) (context.Context, error)

func (*Memory) CommitTx

func (m *Memory) CommitTx(ctx context.Context) error

func (*Memory) Del

func (m *Memory) Del(ctx context.Context, namespace string, key string) bool

func (*Memory) Get

func (m *Memory) Get(ctx context.Context, namespace string, key string) (interface{}, bool)

func (*Memory) LookupMeta

func (m *Memory) LookupMeta(ctx context.Context, key string, ptr interface{}) (_found bool, _err error)

func (*Memory) LookupTx

func (m *Memory) LookupTx(ctx context.Context) (*MemoryTx, bool)

func (*Memory) RollbackTx

func (m *Memory) RollbackTx(ctx context.Context) error

func (*Memory) Set

func (m *Memory) Set(ctx context.Context, namespace, key string, value interface{})

func (*Memory) SetMeta

func (m *Memory) SetMeta(ctx context.Context, key string, value interface{}) (context.Context, error)

type MemoryNamespace

type MemoryNamespace map[string]interface{}

type MemoryTx

type MemoryTx struct {
	// contains filtered or unexported fields
}

type Queue

type Queue[Data any] struct {
	// LIFO is a flag to change element ordering from FIFO to LIFO
	LIFO bool
	// Volatile will flag the Queue to act like a Volatile queue
	Volatile bool
	// blocking will cause the Queue to wait until the published messages are ACK -ed.
	Blocking bool
	// SortLessFunc will define how to sort data, when we look for what message to handle next.
	// if not supplied FIFO is the default ordering.
	SortLessFunc func(i Data, j Data) bool
	// contains filtered or unexported fields
}

func (*Queue[Data]) BeginTx added in v0.244.0

func (q *Queue[Data]) BeginTx(ctx context.Context) (context.Context, error)

BeginTx creates a context with a transaction. All statements that receive this context should be executed within the given transaction in the context. After a BeginTx command will be executed in a single transaction until an explicit COMMIT or ROLLBACK is given.

In case the resource support some form of isolation level, or other ACID related property of the transaction, then it is advised to prepare this information in the context before calling BeginTx. e.g.:

...
var err error
ctx = r.ContextWithIsolationLevel(ctx, sql.LevelSerializable)
ctx, err = r.BeginTx(ctx)

func (*Queue[Data]) CommitTx added in v0.244.0

func (q *Queue[Data]) CommitTx(ctx context.Context) error

CommitTx Commit commits the current transaction. All changes made by the transaction become visible to others and are guaranteed to be durable if a crash occurs.

func (*Queue[Data]) Publish

func (q *Queue[Data]) Publish(ctx context.Context, vs ...Data) (rErr error)

func (*Queue[Data]) Purge

func (q *Queue[Data]) Purge(ctx context.Context) error

func (*Queue[Data]) RollbackTx added in v0.244.0

func (q *Queue[Data]) RollbackTx(ctx context.Context) error

RollbackTx rolls back the current transaction and causes all the updates made by the transaction to be discarded.

func (*Queue[Data]) Subscribe

func (q *Queue[Data]) Subscribe(ctx context.Context) (pubsub.Subscription[Data], error)

type QueueSubscription added in v0.244.0

type QueueSubscription[Data any] struct {
	// contains filtered or unexported fields
}

func (*QueueSubscription[Data]) Close added in v0.244.0

func (pss *QueueSubscription[Data]) Close() error

func (*QueueSubscription[Data]) Err added in v0.244.0

func (s *QueueSubscription[Data]) Err() error

func (*QueueSubscription[Data]) Next added in v0.244.0

func (s *QueueSubscription[Data]) Next() bool

func (*QueueSubscription[Data]) Value added in v0.244.0

func (s *QueueSubscription[Data]) Value() pubsub.Message[Data]

type Repository

type Repository[ENT, ID any] struct {
	// Memory [optional] is the backing store for this Repository.
	//
	// default: NewMemory()
	Memory *Memory
	// Namespace [optional]
	Namespace string
	// IDA [optional] is the ID Accessor that maps the ID to the ENT field.
	IDA extid.Accessor[ENT, ID]
	// MakeID [optional] is an optional field if you need a specific way of generating new IDs during creation.
	MakeID func(context.Context) (ID, error)
	// ExpectID [optional] is a test simulator flag that allows you to enforce the user of this Repository,
	// to populate the ID field of ENT prior to calling Create or Save, else the operation will fail.
	ExpectID bool
	// contains filtered or unexported fields
}

func NewRepository

func NewRepository[ENT, ID any](m *Memory) *Repository[ENT, ID]

func NewRepositoryWithNamespace

func NewRepositoryWithNamespace[ENT, ID any](m *Memory, ns string) *Repository[ENT, ID]

func NewTaskerSchedulerStateRepository added in v0.246.0

func NewTaskerSchedulerStateRepository() *Repository[tasker.ScheduleState, tasker.ScheduleID]

func (*Repository[ENT, ID]) BeginTx added in v0.238.0

func (r *Repository[ENT, ID]) BeginTx(ctx context.Context) (context.Context, error)

func (*Repository[ENT, ID]) CommitTx added in v0.238.0

func (r *Repository[ENT, ID]) CommitTx(ctx context.Context) error

func (*Repository[ENT, ID]) Create

func (r *Repository[ENT, ID]) Create(ctx context.Context, ptr *ENT) error

func (*Repository[ENT, ID]) DeleteAll

func (r *Repository[ENT, ID]) DeleteAll(ctx context.Context) error

func (*Repository[ENT, ID]) DeleteByID

func (r *Repository[ENT, ID]) DeleteByID(ctx context.Context, id ID) error

func (*Repository[ENT, ID]) FindAll

func (r *Repository[ENT, ID]) FindAll(ctx context.Context) (iter.Seq2[ENT, error], error)

func (*Repository[ENT, ID]) FindByID

func (r *Repository[ENT, ID]) FindByID(ctx context.Context, id ID) (_ent ENT, _found bool, _err error)

func (*Repository[ENT, ID]) FindByIDs

func (r *Repository[ENT, ID]) FindByIDs(ctx context.Context, ids ...ID) (iter.Seq2[ENT, error], error)

func (*Repository[ENT, ID]) IDToMemoryKey

func (r *Repository[ENT, ID]) IDToMemoryKey(id any) string

func (*Repository[ENT, ID]) QueryMany added in v0.244.0

func (r *Repository[ENT, ID]) QueryMany(ctx context.Context, filter func(v ENT) bool) (iter.Seq2[ENT, error], error)

func (*Repository[ENT, ID]) QueryOne added in v0.244.0

func (r *Repository[ENT, ID]) QueryOne(ctx context.Context, filter func(v ENT) bool) (ENT, bool, error)

func (*Repository[ENT, ID]) RollbackTx added in v0.238.0

func (r *Repository[ENT, ID]) RollbackTx(ctx context.Context) error

func (*Repository[ENT, ID]) Save

func (r *Repository[ENT, ID]) Save(ctx context.Context, ptr *ENT) (rErr error)

func (*Repository[ENT, ID]) Update

func (r *Repository[ENT, ID]) Update(ctx context.Context, ptr *ENT) error

type Stack

type Stack struct {
	Path string
	Line int
}

func NewTrace

func NewTrace(offset int) []Stack

func (Stack) RelPath

func (te Stack) RelPath() string

type Traceable

type Traceable interface {
	GetTrace() []Stack
	SetTrace([]Stack)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL