initial commit
This commit is contained in:
126
backend/internal/store/memory.go
Normal file
126
backend/internal/store/memory.go
Normal file
@@ -0,0 +1,126 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"reichard.io/aethera/pkg/slices"
|
||||
)
|
||||
|
||||
var _ Store = (*InMemoryStore)(nil)
|
||||
|
||||
// InMemoryStore implements Store interface using in-memory storage
|
||||
type InMemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
chats map[uuid.UUID]*Chat
|
||||
settings *Settings
|
||||
}
|
||||
|
||||
// NewInMemoryStore creates a new InMemoryStore
|
||||
func NewInMemoryStore() *InMemoryStore {
|
||||
return &InMemoryStore{
|
||||
chats: make(map[uuid.UUID]*Chat),
|
||||
}
|
||||
}
|
||||
|
||||
// SaveChat creates or updates a chat
|
||||
func (s *InMemoryStore) SaveChat(c *Chat) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
c.ensureDefaults()
|
||||
s.chats[c.ID] = c
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetChat retrieves a chat by ID
|
||||
func (s *InMemoryStore) GetChat(chatID uuid.UUID) (*Chat, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chat, exists := s.chats[chatID]
|
||||
if !exists {
|
||||
return nil, ErrChatNotFound
|
||||
}
|
||||
|
||||
// Return a copy to avoid concurrent modification
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
// DeleteChat removes a chat by ID
|
||||
func (s *InMemoryStore) DeleteChat(chatID uuid.UUID) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, exists := s.chats[chatID]; !exists {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
delete(s.chats, chatID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListChats returns all chat
|
||||
func (s *InMemoryStore) ListChats() ([]*Chat, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
// Convert Map
|
||||
var chats []*Chat
|
||||
for _, chat := range s.chats {
|
||||
chats = append(chats, chat)
|
||||
}
|
||||
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
// SaveChatMessage creates or updates a chat message to a chat
|
||||
func (s *InMemoryStore) SaveChatMessage(m *Message) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if m.ChatID == uuid.Nil {
|
||||
return ErrNilChatID
|
||||
}
|
||||
m.ensureDefaults()
|
||||
|
||||
// Get Chat
|
||||
chat, exists := s.chats[m.ChatID]
|
||||
if !exists {
|
||||
return ErrChatNotFound
|
||||
}
|
||||
|
||||
// Find Existing
|
||||
existingMsg, found := slices.FindFirst(chat.Messages, func(item *Message) bool {
|
||||
return item.ID == m.ID
|
||||
})
|
||||
|
||||
// Upsert
|
||||
if found {
|
||||
*existingMsg = *m
|
||||
} else {
|
||||
chat.Messages = append(chat.Messages, m)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SaveSettings saves settings to in-memory storage
|
||||
func (s *InMemoryStore) SaveSettings(settings *Settings) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
s.settings = settings
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetSettings retrieves settings from in-memory storage
|
||||
func (s *InMemoryStore) GetSettings() (*Settings, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
if s.settings == nil {
|
||||
return &Settings{}, nil
|
||||
}
|
||||
return s.settings, nil
|
||||
}
|
||||
Reference in New Issue
Block a user