Files
mtusi2026lab2-implementation/internal/repository/memory/store.go
T

134 lines
3.4 KiB
Go

package memory
import (
"context"
"slices"
"sync"
"time"
"uuid"
"messenger/internal/domain"
)
type Store struct {
mu sync.RWMutex
users map[uuid.UUID]domain.User
usernames map[string]uuid.UUID
chats map[uuid.UUID]domain.Chat
userChats map[uuid.UUID][]domain.Chat
chatHistory map[uuid.UUID][]domain.Message
}
func New() *Store {
return &Store{
users: make(map[uuid.UUID]domain.User),
usernames: make(map[string]uuid.UUID),
chats: make(map[uuid.UUID]domain.Chat),
userChats: make(map[uuid.UUID][]domain.Chat),
chatHistory: make(map[uuid.UUID][]domain.Message),
}
}
func (s *Store) CreateUser(_ context.Context, u domain.User) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, taken := s.usernames[u.Username]; taken {
return domain.ErrConflict
}
s.users[u.ID] = u
s.usernames[u.Username] = u.ID
return nil
}
func (s *Store) GetUser(_ context.Context, id uuid.UUID) (domain.User, error) {
s.mu.RLock()
defer s.mu.RUnlock()
u, ok := s.users[id]
if !ok {
return domain.User{}, domain.ErrNotFound
}
return u, nil
}
func (s *Store) CreateChat(_ context.Context, c domain.Chat) error {
c.MemberIDs = slices.Clone(c.MemberIDs)
s.mu.Lock()
defer s.mu.Unlock()
for _, id := range c.MemberIDs {
if _, ok := s.users[id]; !ok {
return domain.ErrNotFound
}
}
s.chats[c.ID] = c
for _, id := range c.MemberIDs {
s.userChats[id] = insertSorted(s.userChats[id], c, chatKey)
}
return nil
}
func (s *Store) GetChat(_ context.Context, id uuid.UUID) (domain.Chat, error) {
s.mu.RLock()
defer s.mu.RUnlock()
c, ok := s.chats[id]
if !ok {
return domain.Chat{}, domain.ErrNotFound
}
c.MemberIDs = slices.Clone(c.MemberIDs)
return c, nil
}
func (s *Store) ListUserChats(_ context.Context, userID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Chat, bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
items, more := pageAfter(s.userChats[userID], after, limit, chatKey)
for i := range items {
items[i].MemberIDs = slices.Clone(items[i].MemberIDs)
}
return items, more, nil
}
func (s *Store) CreateMessage(_ context.Context, m domain.Message) error {
s.mu.Lock()
defer s.mu.Unlock()
if _, ok := s.chats[m.ChatID]; !ok {
return domain.ErrNotFound
}
s.chatHistory[m.ChatID] = insertSorted(s.chatHistory[m.ChatID], m, messageKey)
return nil
}
func (s *Store) ListMessages(_ context.Context, chatID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Message, bool, error) {
s.mu.RLock()
defer s.mu.RUnlock()
items, more := pageAfter(s.chatHistory[chatID], after, limit, messageKey)
return items, more, nil
}
func chatKey(c domain.Chat) (time.Time, uuid.UUID) { return c.CreatedAt, c.ID }
func messageKey(m domain.Message) (time.Time, uuid.UUID) { return m.CreatedAt, m.ID }
func insertSorted[T any](list []T, item T, key func(T) (time.Time, uuid.UUID)) []T {
t, id := key(item)
i, _ := slices.BinarySearchFunc(list, item, func(e T, _ T) int {
et, eid := key(e)
return domain.CompareKey(et, eid, t, id)
})
return slices.Insert(list, i, item)
}
func pageAfter[T any](list []T, after *domain.Cursor, limit int, key func(T) (time.Time, uuid.UUID)) ([]T, bool) {
start := 0
if after != nil {
start, _ = slices.BinarySearchFunc(list, after, func(e T, c *domain.Cursor) int {
et, eid := key(e)
if r := domain.CompareKey(et, eid, c.CreatedAt, c.ID); r != 0 {
return r
}
return -1
})
}
end := min(start+limit, len(list))
return slices.Clone(list[start:end]), end < len(list)
}