forked from mtusi2026/mtusi2026lab6
39 lines
1.3 KiB
Go
39 lines
1.3 KiB
Go
package memory
|
|
|
|
import "gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
|
|
|
// Conservative logical charges, not measurements of Go allocations or RSS.
|
|
// The budget includes record metadata and index entries; HTTP buffers are separate.
|
|
const (
|
|
userRecordOverheadBytes = 256
|
|
chatRecordOverheadBytes = 256
|
|
messageRecordOverheadBytes = 256
|
|
chatMemberIndexBytes = 128
|
|
usernameIndexCopies = 2
|
|
)
|
|
|
|
// userCost calculates the logical storage charge for a user and its index entries.
|
|
func userCost(user dto.UserDTO) int {
|
|
return userRecordOverheadBytes + len(user.Username)*usernameIndexCopies + len(user.DisplayName)
|
|
}
|
|
|
|
// chatCost calculates the logical storage charge for a chat and its membership index.
|
|
func chatCost(chat dto.ChatDTO) int {
|
|
return chatRecordOverheadBytes + len(chat.Title) + chatMemberIndexBytes*len(chat.MemberIDs)
|
|
}
|
|
|
|
// messageCost calculates the logical storage charge for a message.
|
|
func messageCost(message dto.MessageDTO) int {
|
|
return messageRecordOverheadBytes + len(message.Text)
|
|
}
|
|
|
|
// reserve checks and charges capacity while the caller holds the write lock.
|
|
// Callers must not perform a fallible operation after reserving this budget.
|
|
func (s *Store) reserve(bytes int) bool {
|
|
if bytes > s.limits.MaxStateBytes-s.usedBytes {
|
|
return false
|
|
}
|
|
s.usedBytes += bytes
|
|
return true
|
|
}
|