memory

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package memory provides an in-memory storage implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Storage

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

Storage represents an in-memory storage for events.

func NewStorage

func NewStorage(maxEvents int) (*Storage, error)

NewStorage creates a new in-memory Storage instance with a maximum event limit. If maxEvents is 0 or negative, a default limit of 10,000 is used. No initialization of data structures is performed until Connect is called.

func (*Storage) Close

func (s *Storage) Close(_ context.Context)

Close clears the in-memory storage and releases resources. It is safe to call multiple times.

func (*Storage) Connect

func (s *Storage) Connect(ctx context.Context) error

Connect initializes the in-memory storage by creating the event slice and indexes. It checks the context before applying initialization. If the context is canceled, no changes are applied.

func (*Storage) CreateEvent

func (s *Storage) CreateEvent(ctx context.Context, event *types.Event) (*types.Event, error)

CreateEvent adds a new event to the in-memory storage. Method is imitation transactional behaviour, checking the context before applying changes.

If the event already exists or the storage is full, it returns ErrDataExists or ErrStorageFull respectively. If the event overlaps with another event, it returns ErrDateBusy.

The event is inserted in a sorted order by Datetime, and if Datetime is equal, it uses ID for deterministic ordering.

func (*Storage) DeleteEvent

func (s *Storage) DeleteEvent(ctx context.Context, id uuid.UUID) error

DeleteEvent deletes the event with the given ID from the in-memory storage. Method is imitation transactional behaviour, checking the context before applying changes.

If the event does not exist, it returns ErrEventNotFound.

func (*Storage) DeleteOldEvents

func (s *Storage) DeleteOldEvents(ctx context.Context, date time.Time) (int64, error)

DeleteOldEvents deletes all events older than the given date from the database. Returns the number of deleted events and nil on success, 0 and any error otherwise.

func (*Storage) GetAllUserEvents

func (s *Storage) GetAllUserEvents(ctx context.Context, userID string) ([]*types.Event, error)

GetAllUserEvents retrieves all events for the given user from the in-memory storage. Method imitates transactional behavior, checking the context before returning the result.

Returns a slice of events sorted by Datetime. If the user has no events, it returns an nil and ErrEventNotFound.

func (*Storage) GetEvent

func (s *Storage) GetEvent(ctx context.Context, id uuid.UUID) (*types.Event, error)

GetEvent retrieves the event with the given ID from the in-memory storage. Method imitates transactional behavior, checking the context before returning the result.

If the event does not exist, it returns nil and ErrEventNotFound.

func (*Storage) GetEventsForDay

func (s *Storage) GetEventsForDay(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)

GetEventsForDay retrieves events for the specified day from the in-memory storage. If userID is provided, it filters events for that user; otherwise, it returns events for all users. Method imitates transactional behavior, checking the context before returning the result.

Returns a slice of events sorted by Datetime. If no events are found, it returns nil and ErrEventNotFound.

func (*Storage) GetEventsForMonth

func (s *Storage) GetEventsForMonth(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)

GetEventsForMonth retrieves events for the month containing the specified date from the in-memory storage. If userID is provided, it filters events for that user; otherwise, it returns events for all users. Method imitates transactional behavior, checking the context before returning the result.

Returns a slice of events sorted by Datetime. If no events are found, it returns nil and ErrEventNotFound.

func (*Storage) GetEventsForNotification

func (s *Storage) GetEventsForNotification(ctx context.Context) ([]*types.Event, error)

GetEventsForNotification retrieves events that need to be notified from the in-memory storage. Method imitates transactional behavior, checking the context before returning the result.

Returns a slice of events sorted by Datetime.

func (*Storage) GetEventsForPeriod

func (s *Storage) GetEventsForPeriod(ctx context.Context,
	dateStart, dateEnd time.Time,
	userID *string,
) ([]*types.Event, error)

GetEventsForPeriod retrieves events within the specified time period from the in-memory storage. If userID is provided, it filters events for that user; otherwise, it returns events for all users. Method imitates transactional behavior, checking the context before returning the result.

Returns a slice of events sorted by Datetime, considering only events that start within [dateStart, dateEnd]. If no events are found, it returns nil and ErrEventNotFound.

func (*Storage) GetEventsForWeek

func (s *Storage) GetEventsForWeek(ctx context.Context, date time.Time, userID *string) ([]*types.Event, error)

GetEventsForWeek retrieves events for the week containing the specified date from the in-memory storage. If userID is provided, it filters events for that user; otherwise, it returns events for all users. Method imitates transactional behavior, checking the context before returning the result.

Returns a slice of events sorted by Datetime. If no events are found, it returns nil and ErrEventNotFound.

func (*Storage) UpdateEvent

func (s *Storage) UpdateEvent(ctx context.Context, id uuid.UUID, data *types.EventData) (*types.Event, error)

UpdateEvent updates the event with the given ID in the in-memory storage. Method is imitation transactional behaviour, checking the context before applying changes.

If the event does not exist, it returns ErrEventNotFound. If it overlaps with another event, it returns ErrDateBusy.

func (*Storage) UpdateNotifiedEvents

func (s *Storage) UpdateNotifiedEvents(ctx context.Context, notifiedEvents []uuid.UUID) (int64, error)

UpdateNotifiedEvents updates the events with the given IDs in the storage as notified ones.

If some of the IDs are not found in the DB, they will be ignored.

Returns the number of updated events and nil on success, 0 and any error otherwise.

Jump to

Keyboard shortcuts

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