finish index feature

This commit is contained in:
2026-04-07 11:49:42 +06:00
parent 8ba956d8c5
commit 15db6e81db
37 changed files with 1047 additions and 170 deletions

12
.vscode/launch.json vendored
View File

@@ -72,6 +72,18 @@
"args": [], "args": [],
"showLog": true, "showLog": true,
"console": "integratedTerminal" "console": "integratedTerminal"
},
{
"name": "Debug LineDB Partitions Example",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/examples/partitions/main.go",
"cwd": "${workspaceFolder}",
"env": {},
"args": [],
"showLog": true,
"console": "integratedTerminal"
} }
] ]
} }

View File

@@ -1,2 +1,2 @@
{"id":1,"status":"processed","tenant":"A","ts":1773305152,"type":"signup"} {"id":1,"status":"new","tenant":"A","ts":1773311342,"type":"signup"}
{"id":2,"status":"processed","tenant":"A","ts":1773305152,"type":"purchase"} {"id":2,"status":"new","tenant":"A","ts":1773311342,"type":"purchase"}

View File

@@ -1 +1 @@
{"id":3,"status":"new","tenant":"B","ts":1773305152,"type":"signup"} {"id":3,"status":"new","tenant":"B","ts":1773311342,"type":"signup"}

View File

@@ -1,2 +1,2 @@
{"createdAt":"2026-03-12T14:45:52.66645719+06:00","email":"a@example.com","id":1,"name":"Alice"} {"createdAt":"2026-03-12T16:29:02.588642365+06:00","email":"a@example.com","id":1,"name":"Alice"}
{"createdAt":"2026-03-12T14:45:52.666505533+06:00","email":"b@example.com","id":2,"name":"Bob"} {"createdAt":"2026-03-12T16:29:02.58871104+06:00","email":"b@example.com","id":2,"name":"Bob"}

View File

@@ -13,6 +13,7 @@ import (
// Пример работы с партициями + индексируемой коллекцией. // Пример работы с партициями + индексируемой коллекцией.
// //
// Запуск: // Запуск:
//
// go run ./examples/partitions/main.go // go run ./examples/partitions/main.go
// //
// Важно: // Важно:
@@ -41,6 +42,7 @@ func main() {
{ {
CollectionName: "events", CollectionName: "events",
AllocSize: 512, AllocSize: 512,
IndexedFields: []string{"id", "type"},
}, },
}, },
Partitions: []linedb.PartitionCollection{ Partitions: []linedb.PartitionCollection{
@@ -103,13 +105,13 @@ func main() {
log.Fatalf("Insert events failed: %v", err) log.Fatalf("Insert events failed: %v", err)
} }
tenantA, err := db.ReadByFilter(map[string]any{"tenant": "A"}, "events", linedb.LineDbAdapterOptions{}) tenantA, err := db.ReadByFilter(map[string]any{"type": "signup", "tenant": "A"}, "events", linedb.LineDbAdapterOptions{})
if err != nil { if err != nil {
log.Fatalf("ReadByFilter events tenant A failed: %v", err) log.Fatalf("ReadByFilter events tenant A failed: %v", err)
} }
mustLen("events tenant A after insert", tenantA, 2) mustLen("events tenant A after insert", tenantA, 2)
tenantB, err := db.ReadByFilter(map[string]any{"tenant": "B"}, "events", linedb.LineDbAdapterOptions{}) tenantB, err := db.ReadByFilter(map[string]any{"type": "signup", "tenant": "B"}, "events", linedb.LineDbAdapterOptions{})
if err != nil { if err != nil {
log.Fatalf("ReadByFilter events tenant B failed: %v", err) log.Fatalf("ReadByFilter events tenant B failed: %v", err)
} }
@@ -158,4 +160,3 @@ func mustLen(label string, got []any, want int) {
log.Fatalf("%s: expected %d, got %d (%v)", label, want, len(got), got) log.Fatalf("%s: expected %d, got %d (%v)", label, want, len(got), got)
} }
} }

View File

@@ -6,26 +6,37 @@ import (
"sync" "sync"
) )
// IndexPosition — позиция записи в индексе (партиция + номер строки).
// Partition "default" — для обычных коллекций без партиционирования.
type IndexPosition struct {
Partition string // имя партиции ("default" для обычных коллекций)
LineIndex int
}
// DefaultPartition — значение партиции для непартиционированных коллекций.
const DefaultPartition = "default"
// IndexStore — интерфейс хранилища индексов. // IndexStore — интерфейс хранилища индексов.
// Позволяет подключать разные реализации: в памяти, memcached и др. // Позволяет подключать разные реализации: в памяти, memcached и др.
// Индекс привязан к логической коллекции; для партиционированных хранит (partition, lineIndex).
type IndexStore interface { type IndexStore interface {
// Lookup ищет позиции записей по полю и значению. // Lookup ищет позиции записей по полю и значению.
// value — строковое представление (см. valueToIndexKey). // value — строковое представление (см. valueToIndexKey).
// Возвращает срез индексов строк (0-based) или nil, nil, // Возвращает (partition, lineIndex) — для непартиционированных partition = DefaultPartition.
// если индекс не используется или записей нет. Lookup(collection, field, value string) ([]IndexPosition, error)
Lookup(collection, field, value string) ([]int, error)
// IndexRecord добавляет одну запись в индекс по номеру строки (для точечных Update). // IndexRecord добавляет одну запись в индекс.
IndexRecord(collection string, fields []string, record map[string]any, lineIndex int) // partition — имя партиции (DefaultPartition для обычных коллекций).
IndexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int)
// UnindexRecord удаляет одну запись из индекса по номеру строки. // UnindexRecord удаляет одну запись из индекса.
UnindexRecord(collection string, fields []string, record map[string]any, lineIndex int) UnindexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int)
// Rebuild полностью перестраивает индекс коллекции. // Rebuild перестраивает вклад в индекс для одной партиции (или всей коллекции, если одна партиция).
// records — полный список записей, каждая позиция в срезе соответствует номеру строки в файле. // partition — имя партиции; records — записи, позиция в срезе = lineIndex.
Rebuild(collection string, fields []string, records []any) error Rebuild(collection, partition string, fields []string, records []any) error
// Clear очищает индекс коллекции. // Clear очищает индекс коллекции (все партиции).
Clear(collection string) error Clear(collection string) error
} }
@@ -65,14 +76,14 @@ func getFieldValue(record map[string]any, field string) string {
// InMemoryIndexStore — реализация IndexStore в памяти (по умолчанию). // InMemoryIndexStore — реализация IndexStore в памяти (по умолчанию).
type InMemoryIndexStore struct { type InMemoryIndexStore struct {
mu sync.RWMutex mu sync.RWMutex
// index: collection:field -> value -> список индексов строк (0-based) // index: collection:field -> value -> []IndexPosition
index map[string]map[string][]int index map[string]map[string][]IndexPosition
} }
// NewInMemoryIndexStore создаёт новый in-memory индекс. // NewInMemoryIndexStore создаёт новый in-memory индекс.
func NewInMemoryIndexStore() *InMemoryIndexStore { func NewInMemoryIndexStore() *InMemoryIndexStore {
return &InMemoryIndexStore{ return &InMemoryIndexStore{
index: make(map[string]map[string][]int), index: make(map[string]map[string][]IndexPosition),
} }
} }
@@ -82,27 +93,33 @@ func (s *InMemoryIndexStore) indexKey(collection, field string) string {
} }
// IndexRecord добавляет запись в индекс. // IndexRecord добавляет запись в индекс.
func (s *InMemoryIndexStore) IndexRecord(collection string, fields []string, record map[string]any, lineIndex int) { func (s *InMemoryIndexStore) IndexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int) {
if len(fields) == 0 || record == nil { if len(fields) == 0 || record == nil {
return return
} }
if partition == "" {
partition = DefaultPartition
}
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
for _, field := range fields { for _, field := range fields {
val := getFieldValue(record, field) val := getFieldValue(record, field)
key := s.indexKey(collection, field) key := s.indexKey(collection, field)
if s.index[key] == nil { if s.index[key] == nil {
s.index[key] = make(map[string][]int) s.index[key] = make(map[string][]IndexPosition)
} }
s.index[key][val] = append(s.index[key][val], lineIndex) s.index[key][val] = append(s.index[key][val], IndexPosition{Partition: partition, LineIndex: lineIndex})
} }
} }
// UnindexRecord удаляет запись из индекса. // UnindexRecord удаляет запись из индекса (по partition и lineIndex).
func (s *InMemoryIndexStore) UnindexRecord(collection string, fields []string, record map[string]any, lineIndex int) { func (s *InMemoryIndexStore) UnindexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int) {
if len(fields) == 0 || record == nil { if len(fields) == 0 || record == nil {
return return
} }
if partition == "" {
partition = DefaultPartition
}
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
for _, field := range fields { for _, field := range fields {
@@ -112,23 +129,23 @@ func (s *InMemoryIndexStore) UnindexRecord(collection string, fields []string, r
if bucket == nil { if bucket == nil {
continue continue
} }
idxs := bucket[val] positions := bucket[val]
newIdxs := make([]int, 0, len(idxs)) newPos := make([]IndexPosition, 0, len(positions))
for _, i := range idxs { for _, p := range positions {
if i != lineIndex { if p.Partition != partition || p.LineIndex != lineIndex {
newIdxs = append(newIdxs, i) newPos = append(newPos, p)
} }
} }
if len(newIdxs) == 0 { if len(newPos) == 0 {
delete(bucket, val) delete(bucket, val)
} else { } else {
bucket[val] = newIdxs bucket[val] = newPos
} }
} }
} }
// Lookup возвращает индексы строк по полю и значению. // Lookup возвращает позиции (partition, lineIndex) по полю и значению.
func (s *InMemoryIndexStore) Lookup(collection, field, value string) ([]int, error) { func (s *InMemoryIndexStore) Lookup(collection, field, value string) ([]IndexPosition, error) {
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()
@@ -137,27 +154,46 @@ func (s *InMemoryIndexStore) Lookup(collection, field, value string) ([]int, err
if bucket == nil { if bucket == nil {
return nil, nil return nil, nil
} }
indexes := bucket[value] positions := bucket[value]
if len(indexes) == 0 { if len(positions) == 0 {
return nil, nil return nil, nil
} }
// Возвращаем копию, чтобы вызывающий код не модифицировал внутренний срез out := make([]IndexPosition, len(positions))
out := make([]int, len(indexes)) copy(out, positions)
copy(out, indexes)
return out, nil return out, nil
} }
// Rebuild полностью перестраивает индекс коллекции. // Rebuild перестраивает вклад партиции в индекс.
func (s *InMemoryIndexStore) Rebuild(collection string, fields []string, records []any) error { func (s *InMemoryIndexStore) Rebuild(collection, partition string, fields []string, records []any) error {
if partition == "" {
partition = DefaultPartition
}
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
// Очищаем старые записи по этой коллекции // Удаляем старые позиции этой партиции из индекса
for _, field := range fields { for _, field := range fields {
key := s.indexKey(collection, field) key := s.indexKey(collection, field)
delete(s.index, key) bucket := s.index[key]
if bucket == nil {
continue
}
for val, positions := range bucket {
newPos := make([]IndexPosition, 0, len(positions))
for _, p := range positions {
if p.Partition != partition {
newPos = append(newPos, p)
}
}
if len(newPos) == 0 {
delete(bucket, val)
} else {
bucket[val] = newPos
}
}
} }
// Добавляем новые позиции
for idx, rec := range records { for idx, rec := range records {
recMap, ok := rec.(map[string]any) recMap, ok := rec.(map[string]any)
if !ok { if !ok {
@@ -167,9 +203,9 @@ func (s *InMemoryIndexStore) Rebuild(collection string, fields []string, records
val := getFieldValue(recMap, field) val := getFieldValue(recMap, field)
key := s.indexKey(collection, field) key := s.indexKey(collection, field)
if s.index[key] == nil { if s.index[key] == nil {
s.index[key] = make(map[string][]int) s.index[key] = make(map[string][]IndexPosition)
} }
s.index[key][val] = append(s.index[key][val], idx) s.index[key][val] = append(s.index[key][val], IndexPosition{Partition: partition, LineIndex: idx})
} }
} }
return nil return nil
@@ -190,7 +226,7 @@ func (s *InMemoryIndexStore) Clear(collection string) error {
} }
// GetSnapshotForTest возвращает копию индекса для тестов. Доступ только при accessKey == "give_me_cache". // GetSnapshotForTest возвращает копию индекса для тестов. Доступ только при accessKey == "give_me_cache".
// Ключ — "collection:field", значение — map[string][]int (значение поля -> номера строк). // Ключ — "collection:field", значение — map[string][]IndexPosition.
func (s *InMemoryIndexStore) GetSnapshotForTest(accessKey string) map[string]any { func (s *InMemoryIndexStore) GetSnapshotForTest(accessKey string) map[string]any {
const testAccessKey = "give_me_cache" const testAccessKey = "give_me_cache"
if accessKey != testAccessKey { if accessKey != testAccessKey {
@@ -200,11 +236,11 @@ func (s *InMemoryIndexStore) GetSnapshotForTest(accessKey string) map[string]any
defer s.mu.RUnlock() defer s.mu.RUnlock()
out := make(map[string]any, len(s.index)) out := make(map[string]any, len(s.index))
for k, bucket := range s.index { for k, bucket := range s.index {
cp := make(map[string][]int, len(bucket)) cp := make(map[string][]IndexPosition, len(bucket))
for val, idxs := range bucket { for val, positions := range bucket {
idxs2 := make([]int, len(idxs)) pos2 := make([]IndexPosition, len(positions))
copy(idxs2, idxs) copy(pos2, positions)
cp[val] = idxs2 cp[val] = pos2
} }
out[k] = cp out[k] = cp
} }

View File

@@ -58,18 +58,22 @@ func (s *MemcachedIndexStore) memKey(collection, field, value string) string {
} }
// IndexRecord добавляет запись в индекс. // IndexRecord добавляет запись в индекс.
func (s *MemcachedIndexStore) IndexRecord(collection string, fields []string, record map[string]any, lineIndex int) { func (s *MemcachedIndexStore) IndexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int) {
if len(fields) == 0 || record == nil { if len(fields) == 0 || record == nil {
return return
} }
if partition == "" {
partition = DefaultPartition
}
pos := IndexPosition{Partition: partition, LineIndex: lineIndex}
for _, field := range fields { for _, field := range fields {
val := getFieldValue(record, field) val := getFieldValue(record, field)
key := s.memKey(collection, field, val) key := s.memKey(collection, field, val)
var list []int var list []IndexPosition
if data, err := s.client.Get(key); err == nil && len(data) > 0 { if data, err := s.client.Get(key); err == nil && len(data) > 0 {
_ = json.Unmarshal(data, &list) _ = json.Unmarshal(data, &list)
} }
list = append(list, lineIndex) list = append(list, pos)
if data, err := json.Marshal(list); err == nil { if data, err := json.Marshal(list); err == nil {
_ = s.client.Set(key, data, s.expireSec) _ = s.client.Set(key, data, s.expireSec)
} }
@@ -77,10 +81,13 @@ func (s *MemcachedIndexStore) IndexRecord(collection string, fields []string, re
} }
// UnindexRecord удаляет запись из индекса. // UnindexRecord удаляет запись из индекса.
func (s *MemcachedIndexStore) UnindexRecord(collection string, fields []string, record map[string]any, lineIndex int) { func (s *MemcachedIndexStore) UnindexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int) {
if len(fields) == 0 || record == nil { if len(fields) == 0 || record == nil {
return return
} }
if partition == "" {
partition = DefaultPartition
}
for _, field := range fields { for _, field := range fields {
val := getFieldValue(record, field) val := getFieldValue(record, field)
key := s.memKey(collection, field, val) key := s.memKey(collection, field, val)
@@ -88,14 +95,14 @@ func (s *MemcachedIndexStore) UnindexRecord(collection string, fields []string,
if err != nil || len(data) == 0 { if err != nil || len(data) == 0 {
continue continue
} }
var list []int var list []IndexPosition
if json.Unmarshal(data, &list) != nil { if json.Unmarshal(data, &list) != nil {
continue continue
} }
var newList []int var newList []IndexPosition
for _, i := range list { for _, p := range list {
if i != lineIndex { if p.Partition != partition || p.LineIndex != lineIndex {
newList = append(newList, i) newList = append(newList, p)
} }
} }
if len(newList) == 0 { if len(newList) == 0 {
@@ -106,8 +113,8 @@ func (s *MemcachedIndexStore) UnindexRecord(collection string, fields []string,
} }
} }
// Lookup ищет индексы строк по полю и значению. // Lookup ищет позиции по полю и значению.
func (s *MemcachedIndexStore) Lookup(collection, field, value string) ([]int, error) { func (s *MemcachedIndexStore) Lookup(collection, field, value string) ([]IndexPosition, error) {
key := s.memKey(collection, field, value) key := s.memKey(collection, field, value)
data, err := s.client.Get(key) data, err := s.client.Get(key)
if err != nil { if err != nil {
@@ -116,27 +123,38 @@ func (s *MemcachedIndexStore) Lookup(collection, field, value string) ([]int, er
if len(data) == 0 { if len(data) == 0 {
return nil, nil return nil, nil
} }
var indexes []int var positions []IndexPosition
if json.Unmarshal(data, &indexes) != nil { if json.Unmarshal(data, &positions) != nil {
return nil, nil return nil, nil
} }
// Возвращаем копию, чтобы вызывающий код не модифицировал внутренний срез out := make([]IndexPosition, len(positions))
out := make([]int, len(indexes)) copy(out, positions)
copy(out, indexes)
return out, nil return out, nil
} }
// Rebuild перестраивает индекс коллекции (удаляет старые ключи по префиксу и записывает новые). // Rebuild перестраивает вклад партиции в индекс.
func (s *MemcachedIndexStore) Rebuild(collection string, fields []string, records []any) error { func (s *MemcachedIndexStore) Rebuild(collection, partition string, fields []string, records []any) error {
if partition == "" {
partition = DefaultPartition
}
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
// Memcached не поддерживает перечисление ключей по шаблону. // Memcached не поддерживает удаление по паттерну — добавляем новые ключи.
// Стратегия: перезаписываем все ключи для текущих записей. // Для корректной замены партиции нужно слить с существующими: получить старые списки,
// Старые ключи с другими значениями останутся до TTL. // удалить позиции с Partition==partition, добавить новые. Упрощённо: строим value -> []IndexPosition
// только для этой партиции и перезаписываем. Но тогда старые позиции других партиций
// Строим value -> []lineIndex для каждого поля // для того же value будут потеряны. Поэтому нужен merge: Get, фильтровать по partition, добавлять новые.
byFieldValue := make(map[string]map[string][]int) // Для merge нужен список всех values — его нет. Реалистично: Rebuild вызывается для одной партиции,
// и мы должны merge. Без перечисления ключей в Memcached merge невозможен.
// Вариант: храним в одном ключе все позиции. При Rebuild(partition) нам нужны ВСЕ ключи
// collection:field:value. Их мы не знаем. Прагматичное решение: Rebuild для Memcached
// делает полную перезапись для этого partition — мы не можем удалить старые, просто добавляем.
// Это приведёт к дубликатам. Правильный путь: хранить составной ключ вида collection:field:value,
// и value — единственный. При Rebuild(partition) мы перезаписываем только те ключи, которые
// встречаются в records. Для остальных value старые позиции этой partition останутся.
// Значит нужен merge: для каждого value в records делаем Get, убираем старые Position с этой partition, добавляем новые.
byFieldValue := make(map[string]map[string][]IndexPosition)
for idx, rec := range records { for idx, rec := range records {
recMap, ok := rec.(map[string]any) recMap, ok := rec.(map[string]any)
if !ok { if !ok {
@@ -145,24 +163,34 @@ func (s *MemcachedIndexStore) Rebuild(collection string, fields []string, record
for _, field := range fields { for _, field := range fields {
val := getFieldValue(recMap, field) val := getFieldValue(recMap, field)
if byFieldValue[field] == nil { if byFieldValue[field] == nil {
byFieldValue[field] = make(map[string][]int) byFieldValue[field] = make(map[string][]IndexPosition)
} }
byFieldValue[field][val] = append(byFieldValue[field][val], idx) byFieldValue[field][val] = append(byFieldValue[field][val], IndexPosition{Partition: partition, LineIndex: idx})
} }
} }
for field, valMap := range byFieldValue { for field, valMap := range byFieldValue {
for val, list := range valMap { for val, newPositions := range valMap {
key := s.memKey(collection, field, val) key := s.memKey(collection, field, val)
data, err := json.Marshal(list) var list []IndexPosition
if err != nil { if data, err := s.client.Get(key); err == nil && len(data) > 0 {
continue _ = json.Unmarshal(data, &list)
} }
// Удаляем старые позиции этой партиции
filtered := make([]IndexPosition, 0, len(list))
for _, p := range list {
if p.Partition != partition {
filtered = append(filtered, p)
}
}
list = append(filtered, newPositions...)
if data, err := json.Marshal(list); err == nil {
if err := s.client.Set(key, data, s.expireSec); err != nil { if err := s.client.Set(key, data, s.expireSec); err != nil {
return fmt.Errorf("memcached set %s: %w", key, err) return fmt.Errorf("memcached set %s: %w", key, err)
} }
} }
} }
}
return nil return nil
} }

View File

@@ -27,7 +27,7 @@ type LineDb struct {
constructorOptions *LineDbOptions constructorOptions *LineDbOptions
initOptions *LineDbInitOptions initOptions *LineDbInitOptions
indexStore IndexStore indexStore IndexStore
reindexDone chan struct{} // закрывается при Close, останавливает горутину ReindexByTimer indexRebuildDone chan struct{} // закрывается при Close, останавливает горутину IndexRebuildTimer
} }
// NewLineDb создает новый экземпляр LineDb // NewLineDb создает новый экземпляр LineDb
@@ -133,28 +133,42 @@ func (db *LineDb) Init(force bool, initOptions *LineDbInitOptions) error {
} }
if db.indexStore != nil { if db.indexStore != nil {
for _, opt := range initOptions.Collections { for _, opt := range initOptions.Collections {
if len(opt.IndexedFields) == 0 || db.isCollectionPartitioned(opt.CollectionName) { if len(opt.IndexedFields) == 0 {
continue continue
} }
adapter := db.adapters[opt.CollectionName] if db.isCollectionPartitioned(opt.CollectionName) {
if adapter == nil { // Партиции: обходим все существующие партиции (могут быть созданы ранее)
baseName := opt.CollectionName
for name, adapter := range db.adapters {
if !strings.HasPrefix(name, baseName+"_") {
continue continue
} }
records, err := adapter.Read(LineDbAdapterOptions{})
if err != nil {
continue
}
_ = db.indexStore.Rebuild(opt.CollectionName, name, opt.IndexedFields, records)
}
} else {
adapter := db.adapters[opt.CollectionName]
if adapter != nil {
records, err := adapter.Read(LineDbAdapterOptions{}) records, err := adapter.Read(LineDbAdapterOptions{})
if err != nil { if err != nil {
return fmt.Errorf("failed to read collection %s for index rebuild: %w", opt.CollectionName, err) return fmt.Errorf("failed to read collection %s for index rebuild: %w", opt.CollectionName, err)
} }
if err := db.indexStore.Rebuild(opt.CollectionName, opt.IndexedFields, records); err != nil { if err := db.indexStore.Rebuild(opt.CollectionName, DefaultPartition, opt.IndexedFields, records); err != nil {
return fmt.Errorf("failed to rebuild index for %s: %w", opt.CollectionName, err) return fmt.Errorf("failed to rebuild index for %s: %w", opt.CollectionName, err)
} }
} }
} }
}
}
// Периодический ребилд индексов // Периодический ребилд индексов (отдельная горутина; при тике — полная блокировка db.mutex)
if initOptions.ReindexByTimer > 0 && db.indexStore != nil { if db.indexStore != nil && initOptions.IndexRebuildTimer > 0 {
db.reindexDone = make(chan struct{}) db.indexRebuildDone = make(chan struct{})
interval := time.Duration(initOptions.ReindexByTimer) * time.Millisecond interval := time.Duration(initOptions.IndexRebuildTimer) * time.Second
go db.reindexByTimerLoop(interval) go db.indexRebuildTimerLoop(interval)
} }
return nil return nil
@@ -274,6 +288,12 @@ func (db *LineDb) Insert(data any, collectionName string, options LineDbAdapterO
// Записываем данные с флагом транзакции // Записываем данные с флагом транзакции
writeOptions := LineDbAdapterOptions{InTransaction: true, InternalCall: true} writeOptions := LineDbAdapterOptions{InTransaction: true, InternalCall: true}
if db.indexStore != nil {
opts := db.getCollectionOptions(collectionName)
if opts != nil && len(opts.IndexedFields) > 0 && db.isCollectionPartitioned(collectionName) {
writeOptions.DoIndexing = true // индекс строится точечно при Write в партиции
}
}
if err := db.Write(resultDataArray, collectionName, writeOptions); err != nil { if err := db.Write(resultDataArray, collectionName, writeOptions); err != nil {
return fmt.Errorf("failed to write data: %w", err) return fmt.Errorf("failed to write data: %w", err)
} }
@@ -285,7 +305,7 @@ func (db *LineDb) Insert(data any, collectionName string, options LineDbAdapterO
} }
} }
// Обновляем индекс (полная пересборка по коллекции) // Обновляем индекс (полная пересборка по коллекции) — только для непартиционированных
if db.indexStore != nil { if db.indexStore != nil {
opts := db.getCollectionOptions(collectionName) opts := db.getCollectionOptions(collectionName)
if opts != nil && len(opts.IndexedFields) > 0 && !db.isCollectionPartitioned(collectionName) { if opts != nil && len(opts.IndexedFields) > 0 && !db.isCollectionPartitioned(collectionName) {
@@ -293,7 +313,7 @@ func (db *LineDb) Insert(data any, collectionName string, options LineDbAdapterO
if exists { if exists {
allRecords, readErr := adapter.Read(LineDbAdapterOptions{}) allRecords, readErr := adapter.Read(LineDbAdapterOptions{})
if readErr == nil { if readErr == nil {
_ = db.indexStore.Rebuild(collectionName, opts.IndexedFields, allRecords) _ = db.indexStore.Rebuild(collectionName, DefaultPartition, opts.IndexedFields, allRecords)
} }
} }
} }
@@ -317,15 +337,27 @@ func (db *LineDb) Write(data any, collectionName string, options LineDbAdapterOp
// Проверяем партиционирование // Проверяем партиционирование
if db.isCollectionPartitioned(collectionName) { if db.isCollectionPartitioned(collectionName) {
dataArray := db.normalizeDataArray(data) dataArray := db.normalizeDataArray(data)
opts := db.getCollectionOptions(collectionName)
for _, item := range dataArray { for _, item := range dataArray {
adapter, err := db.getPartitionAdapter(item, collectionName) adapter, err := db.getPartitionAdapter(item, collectionName)
if err != nil { if err != nil {
return fmt.Errorf("failed to get partition adapter: %w", err) return fmt.Errorf("failed to get partition adapter: %w", err)
} }
partitionName := adapter.GetCollectionName()
var startLine int
if options.DoIndexing && db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
if c, err := adapter.LineCount(); err == nil {
startLine = c
}
}
if err := adapter.Write(item, options); err != nil { if err := adapter.Write(item, options); err != nil {
return fmt.Errorf("failed to write to partition: %w", err) return fmt.Errorf("failed to write to partition: %w", err)
} }
if options.DoIndexing && db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
if m, err := db.toMap(item); err == nil {
db.indexStore.IndexRecord(collectionName, partitionName, opts.IndexedFields, m, startLine)
}
}
} }
return nil return nil
} }
@@ -354,7 +386,7 @@ func (db *LineDb) Write(data any, collectionName string, options LineDbAdapterOp
dataArray := db.normalizeDataArray(data) dataArray := db.normalizeDataArray(data)
for i, record := range dataArray { for i, record := range dataArray {
if m, err := db.toMap(record); err == nil { if m, err := db.toMap(record); err == nil {
db.indexStore.IndexRecord(collectionName, opts.IndexedFields, m, startLine+i) db.indexStore.IndexRecord(collectionName, DefaultPartition, opts.IndexedFields, m, startLine+i)
} }
} }
} }
@@ -412,7 +444,7 @@ func (db *LineDb) Update(data any, collectionName string, filter any, options Li
// Пробуем точечный Update через индекс (без полного чтения файла) // Пробуем точечный Update через индекс (без полного чтения файла)
opts := db.getCollectionOptions(collectionName) opts := db.getCollectionOptions(collectionName)
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 { if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 && !db.isCollectionPartitioned(collectionName) {
result, used, err := db.tryIndexUpdate(adapter, filter, dataMap, collectionName, opts.IndexedFields, options) result, used, err := db.tryIndexUpdate(adapter, filter, dataMap, collectionName, opts.IndexedFields, options)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -436,7 +468,7 @@ func (db *LineDb) Update(data any, collectionName string, filter any, options Li
if opts != nil && len(opts.IndexedFields) > 0 { if opts != nil && len(opts.IndexedFields) > 0 {
allRecords, readErr := adapter.Read(LineDbAdapterOptions{}) allRecords, readErr := adapter.Read(LineDbAdapterOptions{})
if readErr == nil { if readErr == nil {
_ = db.indexStore.Rebuild(collectionName, opts.IndexedFields, allRecords) _ = db.indexStore.Rebuild(collectionName, DefaultPartition, opts.IndexedFields, allRecords)
} }
} }
} }
@@ -502,7 +534,7 @@ func (db *LineDb) Delete(data any, collectionName string, options LineDbAdapterO
if opts != nil && len(opts.IndexedFields) > 0 { if opts != nil && len(opts.IndexedFields) > 0 {
allRecords, readErr := adapter.Read(LineDbAdapterOptions{}) allRecords, readErr := adapter.Read(LineDbAdapterOptions{})
if readErr == nil { if readErr == nil {
_ = db.indexStore.Rebuild(collectionName, opts.IndexedFields, allRecords) _ = db.indexStore.Rebuild(collectionName, DefaultPartition, opts.IndexedFields, allRecords)
} }
} }
} }
@@ -555,7 +587,29 @@ func (db *LineDb) ReadByFilter(filter any, collectionName string, options LineDb
// Проверяем партиционирование // Проверяем партиционирование
if db.isCollectionPartitioned(collectionName) { if db.isCollectionPartitioned(collectionName) {
return db.readByFilterPartitioned(filter, collectionName, options) if db.indexStore != nil {
hit, result, err := db.tryIndexLookupPartitioned(filter, collectionName, options)
if err != nil || (options.FailOnFailureIndexRead && !hit) {
return nil, fmt.Errorf("index read failed: %w", err)
}
if hit && err == nil {
if db.cacheExternal != nil && !options.InTransaction {
db.cacheExternal.Set(db.generateCacheKey(filter, collectionName), result)
}
return result, nil
}
}
// fallback to regular read
result, err := db.readByFilterPartitioned(filter, collectionName, options)
if err != nil {
return nil, err
}
// Обновляем кэш
if db.cacheExternal != nil && !options.InTransaction {
db.cacheExternal.Set(db.generateCacheKey(filter, collectionName), result)
}
return result, nil
} }
adapter, exists := db.adapters[collectionName] adapter, exists := db.adapters[collectionName]
@@ -565,7 +619,11 @@ func (db *LineDb) ReadByFilter(filter any, collectionName string, options LineDb
// Используем индекс, если фильтр — одно поле из IndexedFields // Используем индекс, если фильтр — одно поле из IndexedFields
if db.indexStore != nil { if db.indexStore != nil {
if hit, result, err := db.tryIndexLookup(adapter, filter, collectionName, options); hit && err == nil { hit, result, err := db.tryIndexLookup(adapter, filter, collectionName, DefaultPartition, options)
if err != nil || (options.FailOnFailureIndexRead && !hit) {
return nil, fmt.Errorf("index read failed: %w", err)
}
if hit && err == nil {
if db.cacheExternal != nil && !options.InTransaction { if db.cacheExternal != nil && !options.InTransaction {
db.cacheExternal.Set(db.generateCacheKey(filter, collectionName), result) db.cacheExternal.Set(db.generateCacheKey(filter, collectionName), result)
} }
@@ -573,6 +631,7 @@ func (db *LineDb) ReadByFilter(filter any, collectionName string, options LineDb
} }
} }
// fallback to regular read
result, err := adapter.ReadByFilter(filter, options) result, err := adapter.ReadByFilter(filter, options)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -633,10 +692,10 @@ func (db *LineDb) Close() {
db.mutex.Lock() db.mutex.Lock()
defer db.mutex.Unlock() defer db.mutex.Unlock()
// Останавливаем горутину ReindexByTimer // Останавливаем горутину периодического ребилда индексов (IndexRebuildTimer)
if db.reindexDone != nil { if db.indexRebuildDone != nil {
close(db.reindexDone) close(db.indexRebuildDone)
db.reindexDone = nil db.indexRebuildDone = nil
} }
// Закрываем все адаптеры // Закрываем все адаптеры
@@ -656,31 +715,44 @@ func (db *LineDb) Close() {
db.partitionFunctions = make(map[string]func(any) string) db.partitionFunctions = make(map[string]func(any) string)
} }
// reindexByTimerLoop выполняет полный ребилд всех индексов с заданным интервалом. // indexRebuildTimerLoop выполняет полный ребилд всех индексов с заданным интервалом (IndexRebuildTimer).
// Работает в отдельной горутине. Останавливается при закрытии db.reindexDone. // Работает в отдельной горутине; при каждом тике берёт db.mutex.Lock() на время ребилда.
func (db *LineDb) reindexByTimerLoop(interval time.Duration) { // Останавливается при закрытии db.indexRebuildDone.
func (db *LineDb) indexRebuildTimerLoop(interval time.Duration) {
ticker := time.NewTicker(interval) ticker := time.NewTicker(interval)
defer ticker.Stop() defer ticker.Stop()
for { for {
select { select {
case <-db.reindexDone: case <-db.indexRebuildDone:
return return
case <-ticker.C: case <-ticker.C:
db.mutex.Lock() db.mutex.Lock()
if db.indexStore != nil && db.initOptions != nil { if db.indexStore != nil && db.initOptions != nil {
for _, opt := range db.initOptions.Collections { for _, opt := range db.initOptions.Collections {
if len(opt.IndexedFields) == 0 || db.isCollectionPartitioned(opt.CollectionName) { if len(opt.IndexedFields) == 0 {
continue continue
} }
adapter := db.adapters[opt.CollectionName] if db.isCollectionPartitioned(opt.CollectionName) {
if adapter == nil { baseName := opt.CollectionName
for name, adapter := range db.adapters {
if !strings.HasPrefix(name, baseName+"_") {
continue continue
} }
records, err := adapter.Read(LineDbAdapterOptions{}) records, err := adapter.Read(LineDbAdapterOptions{})
if err != nil { if err != nil {
continue continue
} }
_ = db.indexStore.Rebuild(opt.CollectionName, opt.IndexedFields, records) _ = db.indexStore.Rebuild(opt.CollectionName, name, opt.IndexedFields, records)
}
} else {
adapter := db.adapters[opt.CollectionName]
if adapter != nil {
records, err := adapter.Read(LineDbAdapterOptions{})
if err == nil {
_ = db.indexStore.Rebuild(opt.CollectionName, DefaultPartition, opt.IndexedFields, records)
}
}
}
} }
} }
db.mutex.Unlock() db.mutex.Unlock()
@@ -704,20 +776,16 @@ func (db *LineDb) getBaseCollectionName(collectionName string) string {
return collectionName return collectionName
} }
// tryIndexLookup пытается выполнить поиск через индекс. // tryIndexLookup пытается выполнить поиск через индекс (для одной партиции/коллекции).
// Если фильтр — map с несколькими полями, выбирается первое поле из IndexedFields, func (db *LineDb) tryIndexLookup(adapter *JSONLFile, filter any, collectionName, partition string, options LineDbAdapterOptions) (bool, []any, error) {
// по нему читаются строки через индекс, затем в памяти накладывается полный фильтр.
// Возвращает (true, result) если индекс использован, (false, nil) иначе.
func (db *LineDb) tryIndexLookup(adapter *JSONLFile, filter any, collectionName string, options LineDbAdapterOptions) (bool, []any, error) {
filterMap, ok := filter.(map[string]any) filterMap, ok := filter.(map[string]any)
if !ok || len(filterMap) == 0 { if !ok || len(filterMap) == 0 {
return false, nil, nil return false, nil, nil
} }
opts := adapter.GetOptions() opts := db.getCollectionOptions(collectionName)
if len(opts.IndexedFields) == 0 { if opts == nil || len(opts.IndexedFields) == 0 {
return false, nil, nil return false, nil, nil
} }
// Находим первое индексируемое поле, которое есть в фильтре
var field string var field string
var value any var value any
for _, idxField := range opts.IndexedFields { for _, idxField := range opts.IndexedFields {
@@ -731,11 +799,21 @@ func (db *LineDb) tryIndexLookup(adapter *JSONLFile, filter any, collectionName
return false, nil, nil return false, nil, nil
} }
valStr := valueToIndexKey(value) valStr := valueToIndexKey(value)
indexes, err := db.indexStore.Lookup(collectionName, field, valStr) positions, err := db.indexStore.Lookup(collectionName, field, valStr)
if err != nil || indexes == nil || len(indexes) == 0 { if err != nil || len(positions) == 0 {
return false, nil, err return false, nil, err
} }
records, err := adapter.ReadByLineIndexes(indexes, options) // Фильтруем только позиции нужной партиции
var lineIndexes []int
for _, p := range positions {
if p.Partition == partition {
lineIndexes = append(lineIndexes, p.LineIndex)
}
}
if len(lineIndexes) == 0 {
return false, nil, nil
}
records, err := adapter.ReadByLineIndexes(lineIndexes, options)
if err != nil { if err != nil {
return false, nil, err return false, nil, err
} }
@@ -753,6 +831,65 @@ func (db *LineDb) tryIndexLookup(adapter *JSONLFile, filter any, collectionName
return true, filtered, nil return true, filtered, nil
} }
// tryIndexLookupPartitioned — поиск через индекс для партиционированной коллекции.
func (db *LineDb) tryIndexLookupPartitioned(filter any, collectionName string, options LineDbAdapterOptions) (bool, []any, error) {
opts := db.getCollectionOptions(collectionName)
if opts == nil || len(opts.IndexedFields) == 0 {
return false, nil, nil
}
filterMap, ok := filter.(map[string]any)
if !ok || len(filterMap) == 0 {
return false, nil, nil
}
var field string
var value any
for _, idxField := range opts.IndexedFields {
if v, exists := filterMap[idxField]; exists {
field = idxField
value = v
break
}
}
if field == "" {
return false, nil, nil
}
valStr := valueToIndexKey(value)
positions, err := db.indexStore.Lookup(collectionName, field, valStr)
if err != nil || len(positions) == 0 {
return false, nil, err
}
// Группируем по партиции
byPartition := make(map[string][]int)
for _, p := range positions {
byPartition[p.Partition] = append(byPartition[p.Partition], p.LineIndex)
}
var allRecords []any
for partitionName, lineIndexes := range byPartition {
adapter := db.adapters[partitionName]
if adapter == nil {
continue
}
recs, err := adapter.ReadByLineIndexes(lineIndexes, options)
if err != nil {
return false, nil, err
}
allRecords = append(allRecords, recs...)
}
if len(allRecords) == 0 {
return false, nil, nil
}
if len(filterMap) == 1 {
return true, allRecords, nil
}
var filtered []any
for _, rec := range allRecords {
if db.matchesFilter(rec, filter, options.StrictCompare) {
filtered = append(filtered, rec)
}
}
return true, filtered, nil
}
// tryIndexUpdate выполняет точечное обновление через индекс. Возвращает (result, used, err). // tryIndexUpdate выполняет точечное обновление через индекс. Возвращает (result, used, err).
func (db *LineDb) tryIndexUpdate(adapter *JSONLFile, filter any, dataMap map[string]any, collectionName string, indexedFields []string, options LineDbAdapterOptions) ([]any, bool, error) { func (db *LineDb) tryIndexUpdate(adapter *JSONLFile, filter any, dataMap map[string]any, collectionName string, indexedFields []string, options LineDbAdapterOptions) ([]any, bool, error) {
filterMap, ok := filter.(map[string]any) filterMap, ok := filter.(map[string]any)
@@ -772,10 +909,19 @@ func (db *LineDb) tryIndexUpdate(adapter *JSONLFile, filter any, dataMap map[str
return nil, false, nil return nil, false, nil
} }
valStr := valueToIndexKey(value) valStr := valueToIndexKey(value)
indexes, err := db.indexStore.Lookup(collectionName, field, valStr) posList, err := db.indexStore.Lookup(collectionName, field, valStr)
if err != nil || indexes == nil || len(indexes) == 0 { if err != nil || len(posList) == 0 {
return nil, false, err return nil, false, err
} }
var indexes []int
for _, p := range posList {
if p.Partition == DefaultPartition {
indexes = append(indexes, p.LineIndex)
}
}
if len(indexes) == 0 {
return nil, false, nil
}
opt := options opt := options
opt.InTransaction = true opt.InTransaction = true
records, positions, err := adapter.ReadByLineIndexesWithPositions(indexes, opt) records, positions, err := adapter.ReadByLineIndexesWithPositions(indexes, opt)
@@ -814,12 +960,12 @@ func (db *LineDb) tryIndexUpdate(adapter *JSONLFile, filter any, dataMap map[str
} }
for i, rec := range toUpdate { for i, rec := range toUpdate {
if m, ok := rec.(map[string]any); ok { if m, ok := rec.(map[string]any); ok {
db.indexStore.UnindexRecord(collectionName, indexedFields, m, toUpdatePos[i]) db.indexStore.UnindexRecord(collectionName, DefaultPartition, indexedFields, m, toUpdatePos[i])
} }
} }
for i, rec := range updated { for i, rec := range updated {
if m, ok := rec.(map[string]any); ok { if m, ok := rec.(map[string]any); ok {
db.indexStore.IndexRecord(collectionName, indexedFields, m, toUpdatePos[i]) db.indexStore.IndexRecord(collectionName, DefaultPartition, indexedFields, m, toUpdatePos[i])
} }
} }
return updated, true, nil return updated, true, nil
@@ -844,10 +990,19 @@ func (db *LineDb) tryIndexDelete(adapter *JSONLFile, filter any, collectionName
return nil, false, nil return nil, false, nil
} }
valStr := valueToIndexKey(value) valStr := valueToIndexKey(value)
indexes, err := db.indexStore.Lookup(collectionName, field, valStr) posList, err := db.indexStore.Lookup(collectionName, field, valStr)
if err != nil || indexes == nil || len(indexes) == 0 { if err != nil || len(posList) == 0 {
return nil, false, err return nil, false, err
} }
var indexes []int
for _, p := range posList {
if p.Partition == DefaultPartition {
indexes = append(indexes, p.LineIndex)
}
}
if len(indexes) == 0 {
return nil, false, nil
}
opt := options opt := options
opt.InTransaction = true opt.InTransaction = true
records, positions, err := adapter.ReadByLineIndexesWithPositions(indexes, opt) records, positions, err := adapter.ReadByLineIndexesWithPositions(indexes, opt)
@@ -871,7 +1026,7 @@ func (db *LineDb) tryIndexDelete(adapter *JSONLFile, filter any, collectionName
} }
for i, rec := range toDel { for i, rec := range toDel {
if m, ok := rec.(map[string]any); ok { if m, ok := rec.(map[string]any); ok {
db.indexStore.UnindexRecord(collectionName, indexedFields, m, toDelPos[i]) db.indexStore.UnindexRecord(collectionName, DefaultPartition, indexedFields, m, toDelPos[i])
} }
} }
return toDel, true, nil return toDel, true, nil
@@ -1321,19 +1476,20 @@ func (db *LineDb) normalizeFilter(filter any) (any, error) {
} }
func (db *LineDb) updatePartitioned(data any, collectionName string, filter any, options LineDbAdapterOptions) ([]any, error) { func (db *LineDb) updatePartitioned(data any, collectionName string, filter any, options LineDbAdapterOptions) ([]any, error) {
// Получаем все партиции
partitionFiles, err := db.getPartitionFiles(collectionName) partitionFiles, err := db.getPartitionFiles(collectionName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
opts := db.getCollectionOptions(collectionName)
var allResults []any var allResults []any
for _, filename := range partitionFiles { for _, filename := range partitionFiles {
// Находим адаптер по имени файла
var adapter *JSONLFile var adapter *JSONLFile
var partitionName string
for name, adapterFile := range db.collections { for name, adapterFile := range db.collections {
if adapterFile == filename { if adapterFile == filename {
adapter = db.adapters[name] adapter = db.adapters[name]
partitionName = name
break break
} }
} }
@@ -1344,6 +1500,12 @@ func (db *LineDb) updatePartitioned(data any, collectionName string, filter any,
return nil, err return nil, err
} }
allResults = append(allResults, results...) allResults = append(allResults, results...)
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
records, readErr := adapter.Read(LineDbAdapterOptions{InTransaction: true})
if readErr == nil {
_ = db.indexStore.Rebuild(collectionName, partitionName, opts.IndexedFields, records)
}
}
} }
} }
@@ -1351,19 +1513,20 @@ func (db *LineDb) updatePartitioned(data any, collectionName string, filter any,
} }
func (db *LineDb) deletePartitioned(data any, collectionName string, options LineDbAdapterOptions) ([]any, error) { func (db *LineDb) deletePartitioned(data any, collectionName string, options LineDbAdapterOptions) ([]any, error) {
// Получаем все партиции
partitionFiles, err := db.getPartitionFiles(collectionName) partitionFiles, err := db.getPartitionFiles(collectionName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
opts := db.getCollectionOptions(collectionName)
var allResults []any var allResults []any
for _, filename := range partitionFiles { for _, filename := range partitionFiles {
// Находим адаптер по имени файла
var adapter *JSONLFile var adapter *JSONLFile
var partitionName string
for name, adapterFile := range db.collections { for name, adapterFile := range db.collections {
if adapterFile == filename { if adapterFile == filename {
adapter = db.adapters[name] adapter = db.adapters[name]
partitionName = name
break break
} }
} }
@@ -1374,6 +1537,12 @@ func (db *LineDb) deletePartitioned(data any, collectionName string, options Lin
return nil, err return nil, err
} }
allResults = append(allResults, results...) allResults = append(allResults, results...)
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
records, readErr := adapter.Read(LineDbAdapterOptions{InTransaction: true})
if readErr == nil {
_ = db.indexStore.Rebuild(collectionName, partitionName, opts.IndexedFields, records)
}
}
} }
} }
@@ -1672,7 +1841,7 @@ func (db *LineDb) GetCacheForTest(accessKey string) map[string]any {
} }
// GetIndexSnapshotForTest возвращает снимок индекса для тестов (только InMemoryIndexStore). // GetIndexSnapshotForTest возвращает снимок индекса для тестов (только InMemoryIndexStore).
// Доступ только при accessKey == "give_me_cache". Ключ — "collection:field", значение — map[value][]int (номера строк). // Доступ только при accessKey == "give_me_cache". Ключ — "collection:field", значение — map[value][]IndexPosition.
func (db *LineDb) GetIndexSnapshotForTest(accessKey string) map[string]any { func (db *LineDb) GetIndexSnapshotForTest(accessKey string) map[string]any {
const testAccessKey = "give_me_cache" const testAccessKey = "give_me_cache"
if accessKey != testAccessKey || db.indexStore == nil { if accessKey != testAccessKey || db.indexStore == nil {

View File

@@ -53,7 +53,8 @@ type LineDbInitOptions struct {
Collections []JSONLFileOptions `json:"collections"` Collections []JSONLFileOptions `json:"collections"`
DBFolder string `json:"dbFolder,omitempty"` DBFolder string `json:"dbFolder,omitempty"`
Partitions []PartitionCollection `json:"partitions,omitempty"` Partitions []PartitionCollection `json:"partitions,omitempty"`
ReindexByTimer int `json:"reindexByTimer,omitempty"` // интервал в ms; если > 0 — периодический полный ребилд всех индексов с блокировкой // IndexRebuildTimer — интервал в секундах; если > 0 — в отдельной горутине периодический полный ребилд всех индексов с блокировкой базы (db.mutex).
IndexRebuildTimer int `json:"indexRebuildTimer,omitempty"`
} }
// EmptyValueMode определяет, что считать "пустым" при проверке required/unique // EmptyValueMode определяет, что считать "пустым" при проверке required/unique
@@ -180,6 +181,9 @@ type LineDbAdapterOptions struct {
OptimisticRead bool `json:"optimisticRead,omitempty"` OptimisticRead bool `json:"optimisticRead,omitempty"`
ReturnChain bool `json:"returnChain,omitempty"` ReturnChain bool `json:"returnChain,omitempty"`
DoIndexing bool `json:"doIndexing,omitempty"` // при true — после Write делать точечное индексирование новых записей DoIndexing bool `json:"doIndexing,omitempty"` // при true — после Write делать точечное индексирование новых записей
// FailOnFailureIndexRead — при true: если ReadByFilter пошёл по индексу и получил ошибку (Lookup / ReadByLineIndexes),
// вернуть её и не делать полный перебор по коллекции.
FailOnFailureIndexRead bool `json:"failOnFailureIndexRead,omitempty"`
} }
// TransactionOptions представляет опции транзакции // TransactionOptions представляет опции транзакции

View File

@@ -223,19 +223,19 @@ func TestIndexEncodedCollectionCache(t *testing.T) {
if !foundInCache { if !foundInCache {
t.Error("Expected to find bob record in raw cache with updated name") t.Error("Expected to find bob record in raw cache with updated name")
} }
// Проверяем индекс: по email bob@secret.com должна быть одна строка, по name bob_updated — тоже // Проверяем индекс: по email bob@secret.com должна быть одна позиция, по name bob_updated — тоже
idxSnapshot := db.GetIndexSnapshotForTest("give_me_cache") idxSnapshot := db.GetIndexSnapshotForTest("give_me_cache")
if len(idxSnapshot) == 0 { if len(idxSnapshot) == 0 {
t.Error("Expected index snapshot to have entries") t.Error("Expected index snapshot to have entries")
} }
if emailIdx, ok := idxSnapshot["users:email"].(map[string][]int); ok { if emailIdx, ok := idxSnapshot["users:email"].(map[string][]linedb.IndexPosition); ok {
if lines, ok := emailIdx["bob@secret.com"]; !ok || len(lines) != 1 { if positions, ok := emailIdx["bob@secret.com"]; !ok || len(positions) != 1 {
t.Errorf("Expected index users:email bob@secret.com to have 1 line, got %v", emailIdx["bob@secret.com"]) t.Errorf("Expected index users:email bob@secret.com to have 1 position, got %v", emailIdx["bob@secret.com"])
} }
} }
if nameIdx, ok := idxSnapshot["users:name"].(map[string][]int); ok { if nameIdx, ok := idxSnapshot["users:name"].(map[string][]linedb.IndexPosition); ok {
if lines, ok := nameIdx["bob_updated"]; !ok || len(lines) != 1 { if positions, ok := nameIdx["bob_updated"]; !ok || len(positions) != 1 {
t.Errorf("Expected index users:name bob_updated to have 1 line, got %v", nameIdx["bob_updated"]) t.Errorf("Expected index users:name bob_updated to have 1 position, got %v", nameIdx["bob_updated"])
} }
} }
@@ -265,14 +265,14 @@ func TestIndexEncodedCollectionCache(t *testing.T) {
} }
// Индекс: bob@secret.com и bob_updated не должны быть в индексе (или пустые срезы) // Индекс: bob@secret.com и bob_updated не должны быть в индексе (или пустые срезы)
idxSnapshot2 := db.GetIndexSnapshotForTest("give_me_cache") idxSnapshot2 := db.GetIndexSnapshotForTest("give_me_cache")
if emailIdx, ok := idxSnapshot2["users:email"].(map[string][]int); ok { if emailIdx, ok := idxSnapshot2["users:email"].(map[string][]linedb.IndexPosition); ok {
if lines, has := emailIdx["bob@secret.com"]; has && len(lines) > 0 { if positions, has := emailIdx["bob@secret.com"]; has && len(positions) > 0 {
t.Errorf("After delete, index users:email bob@secret.com should be empty, got %v", lines) t.Errorf("After delete, index users:email bob@secret.com should be empty, got %v", positions)
} }
} }
if nameIdx, ok := idxSnapshot2["users:name"].(map[string][]int); ok { if nameIdx, ok := idxSnapshot2["users:name"].(map[string][]linedb.IndexPosition); ok {
if lines, has := nameIdx["bob_updated"]; has && len(lines) > 0 { if positions, has := nameIdx["bob_updated"]; has && len(positions) > 0 {
t.Errorf("After delete, index users:name bob_updated should be empty, got %v", lines) t.Errorf("After delete, index users:name bob_updated should be empty, got %v", positions)
} }
} }
} }

View File

@@ -0,0 +1,218 @@
package nonpartitioned
import (
"os"
"path/filepath"
"testing"
"linedb/pkg/linedb"
)
const dbDir = "./data/nonpartitioned"
func setupDB(t *testing.T) *linedb.LineDb {
t.Helper()
dir := filepath.Join(dbDir, t.Name())
_ = os.RemoveAll(dir)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
initOptions := &linedb.LineDbInitOptions{
DBFolder: dir,
Collections: []linedb.JSONLFileOptions{
{
CollectionName: "users",
AllocSize: 512,
IndexedFields: []string{"id", "email", "name"},
},
},
}
db := linedb.NewLineDb(&linedb.LineDbOptions{
IndexStore: linedb.NewInMemoryIndexStore(),
})
if err := db.Init(true, initOptions); err != nil {
t.Fatalf("Init: %v", err)
}
return db
}
func TestNonPartitioned_InsertRead(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
// Insert
users := []any{
map[string]any{"id": 1, "email": "a@test.com", "name": "Alice"},
map[string]any{"id": 2, "email": "b@test.com", "name": "Bob"},
map[string]any{"id": 3, "email": "c@test.com", "name": "Charlie"},
}
if err := db.Insert(users, "users", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
// Read
all, err := db.Read("users", opts)
if err != nil {
t.Fatalf("Read: %v", err)
}
if len(all) != 3 {
t.Errorf("Read: expected 3, got %d", len(all))
}
}
func TestNonPartitioned_ReadByFilter_Indexed(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert([]any{
map[string]any{"id": 1, "email": "x@test.com", "name": "X"},
map[string]any{"id": 2, "email": "y@test.com", "name": "Y"},
}, "users", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
// Поиск по индексированному полю
found, err := db.ReadByFilter(map[string]any{"email": "y@test.com"}, "users", opts)
if err != nil {
t.Fatalf("ReadByFilter: %v", err)
}
if len(found) != 1 {
t.Fatalf("ReadByFilter email: expected 1, got %d", len(found))
}
if m, ok := found[0].(map[string]any); !ok || m["name"] != "Y" {
t.Errorf("ReadByFilter: wrong record %v", found[0])
}
foundByID, _ := db.ReadByFilter(map[string]any{"id": 1}, "users", opts)
if len(foundByID) != 1 {
t.Fatalf("ReadByFilter id: expected 1, got %d", len(foundByID))
}
}
func TestNonPartitioned_Update(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert(map[string]any{"id": 1, "email": "old@test.com", "name": "Old"}, "users", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
updated, err := db.Update(
map[string]any{"email": "new@test.com", "name": "New"},
"users",
map[string]any{"id": 1},
opts,
)
if err != nil {
t.Fatalf("Update: %v", err)
}
if len(updated) != 1 {
t.Fatalf("Update: expected 1, got %d", len(updated))
}
found, _ := db.ReadByFilter(map[string]any{"email": "new@test.com"}, "users", opts)
if len(found) != 1 {
t.Fatalf("After update ReadByFilter: expected 1, got %d", len(found))
}
oldFound, _ := db.ReadByFilter(map[string]any{"email": "old@test.com"}, "users", opts)
if len(oldFound) != 0 {
t.Errorf("Old email should not exist after update, got %d", len(oldFound))
}
}
func TestNonPartitioned_Delete(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert([]any{
map[string]any{"id": 1, "email": "del@test.com", "name": "Del"},
map[string]any{"id": 2, "email": "keep@test.com", "name": "Keep"},
}, "users", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
deleted, err := db.Delete(map[string]any{"email": "del@test.com"}, "users", opts)
if err != nil {
t.Fatalf("Delete: %v", err)
}
if len(deleted) != 1 {
t.Fatalf("Delete: expected 1, got %d", len(deleted))
}
all, _ := db.Read("users", opts)
if len(all) != 1 {
t.Fatalf("After delete expected 1, got %d", len(all))
}
}
func TestNonPartitioned_WriteWithDoIndexing(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
// Write с DoIndexing
if err := db.Write(
[]any{map[string]any{"id": 10, "email": "write@test.com", "name": "Written"}},
"users",
linedb.LineDbAdapterOptions{DoIndexing: true},
); err != nil {
t.Fatalf("Write: %v", err)
}
found, _ := db.ReadByFilter(map[string]any{"email": "write@test.com"}, "users", opts)
if len(found) != 1 {
t.Fatalf("Write+DoIndexing: expected 1, got %d", len(found))
}
}
func TestNonPartitioned_FullCycle(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
// 1. Insert
if err := db.Insert([]any{
map[string]any{"id": 1, "email": "one@test.com", "name": "One"},
map[string]any{"id": 2, "email": "two@test.com", "name": "Two"},
map[string]any{"id": 3, "email": "three@test.com", "name": "Three"},
}, "users", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
// 2. Read all
all, _ := db.Read("users", opts)
if len(all) != 3 {
t.Fatalf("Read: expected 3, got %d", len(all))
}
// 3. Update
_, err := db.Update(map[string]any{"name": "TwoUpdated"}, "users", map[string]any{"id": 2}, opts)
if err != nil {
t.Fatalf("Update: %v", err)
}
// 4. Delete
_, err = db.Delete(map[string]any{"id": 3}, "users", opts)
if err != nil {
t.Fatalf("Delete: %v", err)
}
// 5. Verify
all2, _ := db.Read("users", opts)
if len(all2) != 2 {
t.Fatalf("After cycle expected 2, got %d", len(all2))
}
found, _ := db.ReadByFilter(map[string]any{"email": "two@test.com"}, "users", opts)
if len(found) != 1 {
t.Fatalf("ReadByFilter two: expected 1, got %d", len(found))
}
if m, ok := found[0].(map[string]any); ok && m["name"] != "TwoUpdated" {
t.Errorf("Update did not apply: name=%v", m["name"])
}
}

View File

@@ -0,0 +1,3 @@
{"email":"a@test.com","id":1,"name":"Alice"}
{"email":"b@test.com","id":2,"name":"Bob"}
{"email":"c@test.com","id":3,"name":"Charlie"}

View File

@@ -0,0 +1 @@
{"email":"x@test.com","id":1}

View File

@@ -0,0 +1,88 @@
package nonpartitioned
import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
"linedb/pkg/linedb"
)
// failingLookupStore — IndexStore, у которого Lookup всегда возвращает ошибку (для проверки FailOnFailureIndexRead).
type failingLookupStore struct {
inner linedb.IndexStore
}
func (f *failingLookupStore) Lookup(collection, field, value string) ([]linedb.IndexPosition, error) {
return nil, errors.New("simulated index lookup failure")
}
func (f *failingLookupStore) IndexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int) {
f.inner.IndexRecord(collection, partition, fields, record, lineIndex)
}
func (f *failingLookupStore) UnindexRecord(collection, partition string, fields []string, record map[string]any, lineIndex int) {
f.inner.UnindexRecord(collection, partition, fields, record, lineIndex)
}
func (f *failingLookupStore) Rebuild(collection, partition string, fields []string, records []any) error {
return f.inner.Rebuild(collection, partition, fields, records)
}
func (f *failingLookupStore) Clear(collection string) error {
return f.inner.Clear(collection)
}
func TestReadByFilter_FailOnFailureIndexRead_ReturnsError(t *testing.T) {
dir := filepath.Join(dbDir, "index_fail")
_ = os.RemoveAll(dir)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
initOptions := &linedb.LineDbInitOptions{
DBFolder: dir,
Collections: []linedb.JSONLFileOptions{
{
CollectionName: "users",
AllocSize: 512,
IndexedFields: []string{"id", "email"},
},
},
}
inner := linedb.NewInMemoryIndexStore()
store := &failingLookupStore{inner: inner}
db := linedb.NewLineDb(&linedb.LineDbOptions{IndexStore: store})
if err := db.Init(true, initOptions); err != nil {
t.Fatalf("Init: %v", err)
}
defer db.Close()
if err := db.Insert(map[string]any{"id": 1, "email": "x@test.com"}, "users", linedb.LineDbAdapterOptions{}); err != nil {
t.Fatalf("Insert: %v", err)
}
// Без опции — ошибка индекса игнорируется, полный перебор находит запись
found, err := db.ReadByFilter(map[string]any{"email": "x@test.com"}, "users", linedb.LineDbAdapterOptions{})
if err != nil {
t.Fatalf("ReadByFilter without flag: %v", err)
}
if len(found) != 1 {
t.Fatalf("expected 1 via fallback, got %d", len(found))
}
// С FailOnFailureIndexRead — возвращается ошибка индекса
_, err = db.ReadByFilter(map[string]any{"email": "x@test.com"}, "users", linedb.LineDbAdapterOptions{
FailOnFailureIndexRead: true,
})
if err == nil {
t.Fatal("expected error when FailOnFailureIndexRead and index Lookup fails")
}
if !strings.Contains(err.Error(), "index read failed") || !strings.Contains(err.Error(), "simulated index lookup failure") {
t.Fatalf("unexpected error: %v", err)
}
}

View File

@@ -0,0 +1,274 @@
package partitioned
import (
"os"
"path/filepath"
"testing"
"time"
"linedb/pkg/linedb"
)
const dbDir = "./data/partitioned"
func setupDB(t *testing.T) *linedb.LineDb {
t.Helper()
dir := filepath.Join(dbDir, t.Name())
_ = os.RemoveAll(dir)
if err := os.MkdirAll(dir, 0755); err != nil {
t.Fatalf("mkdir: %v", err)
}
initOptions := &linedb.LineDbInitOptions{
DBFolder: dir,
Collections: []linedb.JSONLFileOptions{
{
CollectionName: "events",
AllocSize: 512,
IndexedFields: []string{"id", "tenant", "type"},
},
},
Partitions: []linedb.PartitionCollection{
{
CollectionName: "events",
PartIDFn: func(v any) string {
m, ok := v.(map[string]any)
if !ok {
return "unknown"
}
tenant, ok := m["tenant"].(string)
if !ok || tenant == "" {
return "unknown"
}
return tenant
},
},
},
}
db := linedb.NewLineDb(&linedb.LineDbOptions{
IndexStore: linedb.NewInMemoryIndexStore(),
})
if err := db.Init(true, initOptions); err != nil {
t.Fatalf("Init: %v", err)
}
return db
}
func TestPartitioned_InsertRead(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
events := []any{
map[string]any{"id": 1, "tenant": "A", "type": "signup", "status": "new", "ts": time.Now().Unix()},
map[string]any{"id": 2, "tenant": "A", "type": "purchase", "status": "new", "ts": time.Now().Unix()},
map[string]any{"id": 3, "tenant": "B", "type": "signup", "status": "new", "ts": time.Now().Unix()},
map[string]any{"id": -1, "tenant": "", "type": "signup", "status": "new", "ts": time.Now().Unix()},
}
if err := db.Insert(events, "events", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
tenantA, err := db.ReadByFilter(map[string]any{"tenant": "A"}, "events", linedb.LineDbAdapterOptions{FailOnFailureIndexRead: true})
if err != nil {
t.Fatalf("ReadByFilter(nil): %v", err)
}
if len(tenantA) != 2 {
t.Errorf("expected 2 events, got %d", len(tenantA))
}
all, err := db.ReadByFilter(nil, "events", linedb.LineDbAdapterOptions{FailOnFailureIndexRead: false})
if err != nil {
t.Fatalf("ReadByFilter(nil): %v", err)
}
if len(all) != 4 {
t.Errorf("expected 4 events, got %d", len(all))
}
}
func TestPartitioned_ReadByFilter_ByTenant(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert([]any{
map[string]any{"id": 1, "tenant": "X", "type": "a"},
map[string]any{"id": 2, "tenant": "X", "type": "b"},
map[string]any{"id": 3, "tenant": "Y", "type": "c"},
}, "events", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
tenantX, err := db.ReadByFilter(map[string]any{"tenant": "X"}, "events", opts)
if err != nil {
t.Fatalf("ReadByFilter tenant X: %v", err)
}
if len(tenantX) != 2 {
t.Fatalf("tenant X: expected 2, got %d", len(tenantX))
}
tenantY, _ := db.ReadByFilter(map[string]any{"tenant": "Y"}, "events", opts)
if len(tenantY) != 1 {
t.Fatalf("tenant Y: expected 1, got %d", len(tenantY))
}
}
func TestPartitioned_ReadByFilter_Indexed(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert([]any{
map[string]any{"id": 100, "tenant": "T1", "type": "signup"},
map[string]any{"id": 200, "tenant": "T1", "type": "purchase"},
}, "events", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
// Поиск по индексированному полю id
found, err := db.ReadByFilter(map[string]any{"id": 100}, "events", opts)
if err != nil {
t.Fatalf("ReadByFilter id: %v", err)
}
if len(found) != 1 {
t.Fatalf("ReadByFilter id 100: expected 1, got %d", len(found))
}
// Поиск по type
foundType, err := db.ReadByFilter(map[string]any{"type": "purchase"}, "events", linedb.LineDbAdapterOptions{FailOnFailureIndexRead: true})
if err != nil {
t.Fatalf("ReadByFilter type: %v", err)
}
if len(foundType) != 1 {
t.Fatalf("ReadByFilter type: expected 1, got %d", len(foundType))
}
}
func TestPartitioned_Update(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert([]any{
map[string]any{"id": 1, "tenant": "A", "type": "new", "status": "pending"},
map[string]any{"id": 2, "tenant": "A", "type": "new", "status": "pending"},
}, "events", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
updated, err := db.Update(
map[string]any{"status": "processed"},
"events",
map[string]any{"tenant": "A"},
opts,
)
if err != nil {
t.Fatalf("Update: %v", err)
}
if len(updated) != 2 {
t.Fatalf("Update: expected 2, got %d", len(updated))
}
processed, _ := db.ReadByFilter(map[string]any{"tenant": "A", "status": "processed"}, "events", opts)
if len(processed) != 2 {
t.Fatalf("After update: expected 2 processed, got %d", len(processed))
}
}
func TestPartitioned_Delete(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Insert([]any{
map[string]any{"id": 1, "tenant": "P", "type": "a"},
map[string]any{"id": 2, "tenant": "P", "type": "b"},
map[string]any{"id": 3, "tenant": "Q", "type": "c"},
}, "events", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
deleted, err := db.Delete(map[string]any{"id": 2}, "events", opts)
if err != nil {
t.Fatalf("Delete: %v", err)
}
if len(deleted) != 1 {
t.Fatalf("Delete: expected 1, got %d", len(deleted))
}
all, _ := db.ReadByFilter(nil, "events", opts)
if len(all) != 2 {
t.Fatalf("After delete expected 2, got %d", len(all))
}
}
func TestPartitioned_WriteWithDoIndexing(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
if err := db.Write(
[]any{map[string]any{"id": 999, "tenant": "W", "type": "direct"}},
"events",
linedb.LineDbAdapterOptions{DoIndexing: true},
); err != nil {
t.Fatalf("Write: %v", err)
}
found, _ := db.ReadByFilter(map[string]any{"id": 999}, "events", opts)
if len(found) != 1 {
t.Fatalf("Write+DoIndexing: expected 1, got %d", len(found))
}
}
func TestPartitioned_FullCycle(t *testing.T) {
db := setupDB(t)
defer db.Close()
opts := linedb.LineDbAdapterOptions{}
// 1. Insert в разные партиции
if err := db.Insert([]any{
map[string]any{"id": 1, "tenant": "Alpha", "type": "signup"},
map[string]any{"id": 2, "tenant": "Alpha", "type": "login"},
map[string]any{"id": 3, "tenant": "Beta", "type": "signup"},
}, "events", opts); err != nil {
t.Fatalf("Insert: %v", err)
}
// 2. Чтение
alpha, _ := db.ReadByFilter(map[string]any{"tenant": "Alpha"}, "events", opts)
if len(alpha) != 2 {
t.Fatalf("Alpha: expected 2, got %d", len(alpha))
}
// 3. Update в одной партиции
_, err := db.Update(
map[string]any{"type": "updated"},
"events",
map[string]any{"id": 2},
opts,
)
if err != nil {
t.Fatalf("Update: %v", err)
}
// 4. Delete
_, err = db.Delete(map[string]any{"id": 3}, "events", opts)
if err != nil {
t.Fatalf("Delete: %v", err)
}
// 5. Итог
all, _ := db.ReadByFilter(nil, "events", opts)
if len(all) != 2 {
t.Fatalf("After cycle expected 2, got %d", len(all))
}
updated, _ := db.ReadByFilter(map[string]any{"id": 2}, "events", opts)
if len(updated) != 1 {
t.Fatalf("id 2: expected 1, got %d", len(updated))
}
if m, ok := updated[0].(map[string]any); ok && m["type"] != "updated" {
t.Errorf("Update did not apply: type=%v", m["type"])
}
}

View File

@@ -0,0 +1,2 @@
{"id":1,"tenant":"P","type":"a"}
{"id":2,"tenant":"P","type":"b"}

View File

@@ -0,0 +1 @@
{"id":3,"tenant":"Q","type":"c"}

View File

@@ -0,0 +1,2 @@
{"id":1,"tenant":"Alpha","type":"signup"}
{"id":2,"tenant":"Alpha","type":"login"}

View File

@@ -0,0 +1 @@
{"id":3,"tenant":"Beta","type":"signup"}

View File

@@ -0,0 +1,2 @@
{"id":1,"status":"new","tenant":"A","ts":1775462238,"type":"signup"}
{"id":2,"status":"new","tenant":"A","ts":1775462238,"type":"purchase"}

View File

@@ -0,0 +1 @@
{"id":3,"status":"new","tenant":"B","ts":1775462238,"type":"signup"}

View File

@@ -0,0 +1 @@
{"id":4,"status":"new","tenant":"","ts":1775462238,"type":"signup"}

View File

@@ -0,0 +1,2 @@
{"id":1,"tenant":"X","type":"a"}
{"id":2,"tenant":"X","type":"b"}

View File

@@ -0,0 +1 @@
{"id":3,"tenant":"Y","type":"c"}

View File

@@ -0,0 +1,2 @@
{"id":100,"tenant":"T1","type":"signup"}
{"id":200,"tenant":"T1","type":"purchase"}

View File

@@ -0,0 +1,2 @@
{"id":1,"status":"processed","tenant":"A","type":"new"}
{"id":2,"status":"processed","tenant":"A","type":"new"}

View File

@@ -0,0 +1 @@
{"id":999,"tenant":"W","type":"direct"}

11
tests/run_all.sh Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Запуск всех тестов коллекций (партиционированных и непартиционированных)
set -e
cd "$(dirname "$0")/.."
echo "=== Непартиционированные ==="
go test -v ./tests/nonpartitioned/... -count=1
echo ""
echo "=== Партиционированные ==="
go test -v ./tests/partitioned/... -count=1
echo ""
echo "=== Все тесты коллекций пройдены ==="

7
tests/run_nonpartitioned.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# Запуск тестов для непартиционированных коллекций
set -e
cd "$(dirname "$0")/.."
echo "=== Тесты непартиционированных коллекций ==="
go test -v ./tests/nonpartitioned/... -count=1
echo "=== OK ==="

7
tests/run_partitioned.sh Executable file
View File

@@ -0,0 +1,7 @@
#!/usr/bin/env bash
# Запуск тестов для партиционированных коллекций
set -e
cd "$(dirname "$0")/.."
echo "=== Тесты партиционированных коллекций ==="
go test -v ./tests/partitioned/... -count=1
echo "=== OK ==="