finish index feature
This commit is contained in:
@@ -223,19 +223,19 @@ func TestIndexEncodedCollectionCache(t *testing.T) {
|
||||
if !foundInCache {
|
||||
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")
|
||||
if len(idxSnapshot) == 0 {
|
||||
t.Error("Expected index snapshot to have entries")
|
||||
}
|
||||
if emailIdx, ok := idxSnapshot["users:email"].(map[string][]int); ok {
|
||||
if lines, ok := emailIdx["bob@secret.com"]; !ok || len(lines) != 1 {
|
||||
t.Errorf("Expected index users:email bob@secret.com to have 1 line, got %v", emailIdx["bob@secret.com"])
|
||||
if emailIdx, ok := idxSnapshot["users:email"].(map[string][]linedb.IndexPosition); ok {
|
||||
if positions, ok := emailIdx["bob@secret.com"]; !ok || len(positions) != 1 {
|
||||
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 lines, ok := nameIdx["bob_updated"]; !ok || len(lines) != 1 {
|
||||
t.Errorf("Expected index users:name bob_updated to have 1 line, got %v", nameIdx["bob_updated"])
|
||||
if nameIdx, ok := idxSnapshot["users:name"].(map[string][]linedb.IndexPosition); ok {
|
||||
if positions, ok := nameIdx["bob_updated"]; !ok || len(positions) != 1 {
|
||||
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 не должны быть в индексе (или пустые срезы)
|
||||
idxSnapshot2 := db.GetIndexSnapshotForTest("give_me_cache")
|
||||
if emailIdx, ok := idxSnapshot2["users:email"].(map[string][]int); ok {
|
||||
if lines, has := emailIdx["bob@secret.com"]; has && len(lines) > 0 {
|
||||
t.Errorf("After delete, index users:email bob@secret.com should be empty, got %v", lines)
|
||||
if emailIdx, ok := idxSnapshot2["users:email"].(map[string][]linedb.IndexPosition); ok {
|
||||
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", positions)
|
||||
}
|
||||
}
|
||||
if nameIdx, ok := idxSnapshot2["users:name"].(map[string][]int); ok {
|
||||
if lines, has := nameIdx["bob_updated"]; has && len(lines) > 0 {
|
||||
t.Errorf("After delete, index users:name bob_updated should be empty, got %v", lines)
|
||||
if nameIdx, ok := idxSnapshot2["users:name"].(map[string][]linedb.IndexPosition); ok {
|
||||
if positions, has := nameIdx["bob_updated"]; has && len(positions) > 0 {
|
||||
t.Errorf("After delete, index users:name bob_updated should be empty, got %v", positions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
218
tests/nonpartitioned/collection_test.go
Normal file
218
tests/nonpartitioned/collection_test.go
Normal 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"])
|
||||
}
|
||||
}
|
||||
@@ -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"}
|
||||
@@ -0,0 +1 @@
|
||||
{"email":"x@test.com","id":1}
|
||||
88
tests/nonpartitioned/index_fail_test.go
Normal file
88
tests/nonpartitioned/index_fail_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
274
tests/partitioned/collection_test.go
Normal file
274
tests/partitioned/collection_test.go
Normal 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"])
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"id":1,"tenant":"P","type":"a"}
|
||||
{"id":2,"tenant":"P","type":"b"}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":3,"tenant":"Q","type":"c"}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"id":1,"tenant":"Alpha","type":"signup"}
|
||||
{"id":2,"tenant":"Alpha","type":"login"}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":3,"tenant":"Beta","type":"signup"}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"id":1,"status":"new","tenant":"A","ts":1775462238,"type":"signup"}
|
||||
{"id":2,"status":"new","tenant":"A","ts":1775462238,"type":"purchase"}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":3,"status":"new","tenant":"B","ts":1775462238,"type":"signup"}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":4,"status":"new","tenant":"","ts":1775462238,"type":"signup"}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"id":1,"tenant":"X","type":"a"}
|
||||
{"id":2,"tenant":"X","type":"b"}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":3,"tenant":"Y","type":"c"}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"id":100,"tenant":"T1","type":"signup"}
|
||||
{"id":200,"tenant":"T1","type":"purchase"}
|
||||
@@ -0,0 +1,2 @@
|
||||
{"id":1,"status":"processed","tenant":"A","type":"new"}
|
||||
{"id":2,"status":"processed","tenant":"A","type":"new"}
|
||||
@@ -0,0 +1 @@
|
||||
{"id":999,"tenant":"W","type":"direct"}
|
||||
11
tests/run_all.sh
Executable file
11
tests/run_all.sh
Executable 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
7
tests/run_nonpartitioned.sh
Executable 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
7
tests/run_partitioned.sh
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
# Запуск тестов для партиционированных коллекций
|
||||
set -e
|
||||
cd "$(dirname "$0")/.."
|
||||
echo "=== Тесты партиционированных коллекций ==="
|
||||
go test -v ./tests/partitioned/... -count=1
|
||||
echo "=== OK ==="
|
||||
Reference in New Issue
Block a user