change module name 3
This commit is contained in:
@@ -4,7 +4,9 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// LastIDManager управляет последними ID для коллекций
|
||||
// LastIDManager управляет последними ID для коллекций.
|
||||
// Ключ — полное имя коллекции (как в NextID/Insert), без обрезки по «_»:
|
||||
// иначе user_data и events_A ломались бы на первый сегмент.
|
||||
type LastIDManager struct {
|
||||
lastIDs map[string]int
|
||||
mutex sync.RWMutex
|
||||
@@ -23,53 +25,32 @@ func GetLastIDManagerInstance() *LastIDManager {
|
||||
return lastIDManagerInstance
|
||||
}
|
||||
|
||||
// GetLastID получает последний ID для коллекции
|
||||
func (l *LastIDManager) GetLastID(filename string) int {
|
||||
// GetLastID получает последний ID для коллекции (ключ — полное имя коллекции).
|
||||
func (l *LastIDManager) GetLastID(collectionKey string) int {
|
||||
l.mutex.RLock()
|
||||
defer l.mutex.RUnlock()
|
||||
|
||||
baseFileName := l.getBaseFileName(filename)
|
||||
return l.lastIDs[baseFileName]
|
||||
return l.lastIDs[collectionKey]
|
||||
}
|
||||
|
||||
// SetLastID устанавливает последний ID для коллекции
|
||||
func (l *LastIDManager) SetLastID(filename string, id int) {
|
||||
// SetLastID устанавливает последний ID для коллекции, если id больше текущего.
|
||||
func (l *LastIDManager) SetLastID(collectionKey string, id int) {
|
||||
l.mutex.Lock()
|
||||
defer l.mutex.Unlock()
|
||||
|
||||
baseFileName := l.getBaseFileName(filename)
|
||||
currentID := l.lastIDs[baseFileName]
|
||||
currentID := l.lastIDs[collectionKey]
|
||||
if currentID < id {
|
||||
l.lastIDs[baseFileName] = id
|
||||
l.lastIDs[collectionKey] = id
|
||||
}
|
||||
}
|
||||
|
||||
// IncrementLastID увеличивает последний ID для коллекции
|
||||
func (l *LastIDManager) IncrementLastID(filename string) int {
|
||||
func (l *LastIDManager) IncrementLastID(collectionKey string) int {
|
||||
l.mutex.Lock()
|
||||
defer l.mutex.Unlock()
|
||||
|
||||
baseFileName := l.getBaseFileName(filename)
|
||||
currentID := l.lastIDs[baseFileName]
|
||||
currentID := l.lastIDs[collectionKey]
|
||||
newID := currentID + 1
|
||||
l.lastIDs[baseFileName] = newID
|
||||
l.lastIDs[collectionKey] = newID
|
||||
return newID
|
||||
}
|
||||
|
||||
// getBaseFileName извлекает базовое имя файла
|
||||
func (l *LastIDManager) getBaseFileName(filename string) string {
|
||||
if idx := l.findPartitionSeparator(filename); idx != -1 {
|
||||
return filename[:idx]
|
||||
}
|
||||
return filename
|
||||
}
|
||||
|
||||
// findPartitionSeparator находит разделитель партиции
|
||||
func (l *LastIDManager) findPartitionSeparator(filename string) int {
|
||||
for i, char := range filename {
|
||||
if char == '_' {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user