48 lines
1.4 KiB
Go
48 lines
1.4 KiB
Go
// Package memory implements synchronized storage within one bounded process.
|
|
package memory
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/config"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/model/dto"
|
|
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab3/internal/store"
|
|
)
|
|
|
|
// Store owns all mutable state and its indexes under one lock.
|
|
// Records are immutable once published; membership slices are copied on both
|
|
// write and read boundaries. Its zero value is not usable; construct it with New.
|
|
type Store struct {
|
|
mu sync.RWMutex
|
|
limits config.Memory
|
|
users map[uuid.UUID]dto.UserDTO
|
|
usernames map[string]uuid.UUID
|
|
chats map[uuid.UUID]dto.ChatDTO
|
|
userChats map[uuid.UUID][]dto.ChatDTO
|
|
messages map[uuid.UUID][]dto.MessageDTO
|
|
messageCount int
|
|
usedBytes int
|
|
lastTime time.Time
|
|
}
|
|
|
|
var _ store.Interface = (*Store)(nil)
|
|
|
|
// New validates and copies capacity settings, then initializes an empty store.
|
|
// It creates no files, goroutines or external connections.
|
|
func New(cfg *config.Memory) (*Store, error) {
|
|
if err := cfg.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
return &Store{
|
|
limits: *cfg,
|
|
users: make(map[uuid.UUID]dto.UserDTO),
|
|
usernames: make(map[string]uuid.UUID),
|
|
chats: make(map[uuid.UUID]dto.ChatDTO),
|
|
userChats: make(map[uuid.UUID][]dto.ChatDTO),
|
|
messages: make(map[uuid.UUID][]dto.MessageDTO),
|
|
}, nil
|
|
}
|