change module name 21
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@@ -137,12 +138,10 @@ func (db *LineDb) Init(force bool, initOptions *LineDbInitOptions) error {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if db.isCollectionPartitioned(opt.CollectionName) {
|
if db.isCollectionPartitioned(opt.CollectionName) {
|
||||||
// Партиции: обходим все существующие партиции (могут быть созданы ранее)
|
// Основной файл + все base_* (пустой partition id → основной файл)
|
||||||
baseName := opt.CollectionName
|
baseName := opt.CollectionName
|
||||||
for name, adapter := range db.adapters {
|
for _, adapter := range db.partitionStorageAdaptersOrdered(baseName) {
|
||||||
if !strings.HasPrefix(name, baseName+"_") {
|
name := adapter.GetCollectionName()
|
||||||
continue
|
|
||||||
}
|
|
||||||
records, lineIdx, err := adapter.ReadWithPhysicalLineIndexes(LineDbAdapterOptions{})
|
records, lineIdx, err := adapter.ReadWithPhysicalLineIndexes(LineDbAdapterOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -249,7 +248,9 @@ func (db *LineDb) seedLastIDPartitioned(dbFolder, baseName string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read читает все записи из коллекции
|
// Read читает все записи из коллекции.
|
||||||
|
// Для партиционированной логической коллекции (имя без суффикса _*) читает основной файл
|
||||||
|
// и все файлы партиций base_* подряд. Запрос по имени одной партиции (base_part) читает только её файл.
|
||||||
func (db *LineDb) Read(collectionName string, options LineDbAdapterOptions) ([]any, error) {
|
func (db *LineDb) Read(collectionName string, options LineDbAdapterOptions) ([]any, error) {
|
||||||
db.mutex.RLock()
|
db.mutex.RLock()
|
||||||
defer db.mutex.RUnlock()
|
defer db.mutex.RUnlock()
|
||||||
@@ -258,6 +259,23 @@ func (db *LineDb) Read(collectionName string, options LineDbAdapterOptions) ([]a
|
|||||||
collectionName = db.getFirstCollection()
|
collectionName = db.getFirstCollection()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
baseName := db.getBaseCollectionName(collectionName)
|
||||||
|
if db.isCollectionPartitioned(baseName) && collectionName == baseName {
|
||||||
|
adapters := db.partitionStorageAdaptersOrdered(baseName)
|
||||||
|
if len(adapters) == 0 {
|
||||||
|
return nil, fmt.Errorf("collection %s not found", collectionName)
|
||||||
|
}
|
||||||
|
var all []any
|
||||||
|
for _, a := range adapters {
|
||||||
|
recs, err := a.Read(options)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
all = append(all, recs...)
|
||||||
|
}
|
||||||
|
return all, nil
|
||||||
|
}
|
||||||
|
|
||||||
adapter, exists := db.adapters[collectionName]
|
adapter, exists := db.adapters[collectionName]
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, fmt.Errorf("collection %s not found", collectionName)
|
return nil, fmt.Errorf("collection %s not found", collectionName)
|
||||||
@@ -798,10 +816,8 @@ func (db *LineDb) indexRebuildTimerLoop(interval time.Duration) {
|
|||||||
}
|
}
|
||||||
if db.isCollectionPartitioned(opt.CollectionName) {
|
if db.isCollectionPartitioned(opt.CollectionName) {
|
||||||
baseName := opt.CollectionName
|
baseName := opt.CollectionName
|
||||||
for name, adapter := range db.adapters {
|
for _, adapter := range db.partitionStorageAdaptersOrdered(baseName) {
|
||||||
if !strings.HasPrefix(name, baseName+"_") {
|
name := adapter.GetCollectionName()
|
||||||
continue
|
|
||||||
}
|
|
||||||
dirty, err := adapter.HasBlankSlots(LineDbAdapterOptions{})
|
dirty, err := adapter.HasBlankSlots(LineDbAdapterOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
@@ -1336,17 +1352,28 @@ func (db *LineDb) isCollectionPartitioned(collectionName string) bool {
|
|||||||
return exists
|
return exists
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *LineDb) getPartitionFiles(collectionName string) ([]string, error) {
|
// partitionStorageAdaptersOrdered — для логического имени партиционированной коллекции baseName:
|
||||||
baseName := db.getBaseCollectionName(collectionName)
|
// сначала адаптер основного файла (baseName.jsonl), затем все baseName_* в лексикографическом порядке.
|
||||||
var files []string
|
// Пустой ключ партиции пишет в основной файл; эти записи учитываются здесь первым адаптером.
|
||||||
|
func (db *LineDb) partitionStorageAdaptersOrdered(baseName string) []*JSONLFile {
|
||||||
for name, filename := range db.collections {
|
var out []*JSONLFile
|
||||||
|
if a := db.adapters[baseName]; a != nil {
|
||||||
|
out = append(out, a)
|
||||||
|
}
|
||||||
|
var partNames []string
|
||||||
|
for name := range db.adapters {
|
||||||
|
if name == baseName {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if strings.HasPrefix(name, baseName+"_") {
|
if strings.HasPrefix(name, baseName+"_") {
|
||||||
files = append(files, filename)
|
partNames = append(partNames, name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sort.Strings(partNames)
|
||||||
return files, nil
|
for _, name := range partNames {
|
||||||
|
out = append(out, db.adapters[name])
|
||||||
|
}
|
||||||
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *LineDb) getPartitionAdapter(data any, collectionName string) (*JSONLFile, error) {
|
func (db *LineDb) getPartitionAdapter(data any, collectionName string) (*JSONLFile, error) {
|
||||||
@@ -1356,6 +1383,14 @@ func (db *LineDb) getPartitionAdapter(data any, collectionName string) (*JSONLFi
|
|||||||
}
|
}
|
||||||
|
|
||||||
partitionID := partitionFn(data)
|
partitionID := partitionFn(data)
|
||||||
|
if strings.TrimSpace(partitionID) == "" {
|
||||||
|
adapter, ok := db.adapters[collectionName]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("collection %s not found", collectionName)
|
||||||
|
}
|
||||||
|
return adapter, nil
|
||||||
|
}
|
||||||
|
|
||||||
partitionName := fmt.Sprintf("%s_%s", collectionName, partitionID)
|
partitionName := fmt.Sprintf("%s_%s", collectionName, partitionID)
|
||||||
|
|
||||||
adapter, exists := db.adapters[partitionName]
|
adapter, exists := db.adapters[partitionName]
|
||||||
@@ -1609,29 +1644,16 @@ 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)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
dataMap, err := db.toMap(data)
|
dataMap, err := db.toMap(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("invalid update data format: %w", err)
|
return nil, fmt.Errorf("invalid update data format: %w", err)
|
||||||
}
|
}
|
||||||
opts := db.getCollectionOptions(collectionName)
|
opts := db.getCollectionOptions(collectionName)
|
||||||
|
baseName := db.getBaseCollectionName(collectionName)
|
||||||
|
|
||||||
var allResults []any
|
var allResults []any
|
||||||
for _, filename := range partitionFiles {
|
for _, adapter := range db.partitionStorageAdaptersOrdered(baseName) {
|
||||||
var adapter *JSONLFile
|
partitionName := adapter.GetCollectionName()
|
||||||
var partitionName string
|
|
||||||
for name, adapterFile := range db.collections {
|
|
||||||
if adapterFile == filename {
|
|
||||||
adapter = db.adapters[name]
|
|
||||||
partitionName = name
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if adapter != nil {
|
|
||||||
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
|
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
|
||||||
upd, used, terr := db.tryIndexUpdate(adapter, filter, dataMap, collectionName, partitionName, opts.IndexedFields, options)
|
upd, used, terr := db.tryIndexUpdate(adapter, filter, dataMap, collectionName, partitionName, opts.IndexedFields, options)
|
||||||
if terr != nil {
|
if terr != nil {
|
||||||
@@ -1654,31 +1676,17 @@ func (db *LineDb) updatePartitioned(data any, collectionName string, filter any,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return allResults, nil
|
return allResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
opts := db.getCollectionOptions(collectionName)
|
opts := db.getCollectionOptions(collectionName)
|
||||||
|
baseName := db.getBaseCollectionName(collectionName)
|
||||||
|
|
||||||
var allResults []any
|
var allResults []any
|
||||||
for _, filename := range partitionFiles {
|
for _, adapter := range db.partitionStorageAdaptersOrdered(baseName) {
|
||||||
var adapter *JSONLFile
|
partitionName := adapter.GetCollectionName()
|
||||||
var partitionName string
|
|
||||||
for name, adapterFile := range db.collections {
|
|
||||||
if adapterFile == filename {
|
|
||||||
adapter = db.adapters[name]
|
|
||||||
partitionName = name
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if adapter != nil {
|
|
||||||
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
|
if db.indexStore != nil && opts != nil && len(opts.IndexedFields) > 0 {
|
||||||
del, used, derr := db.tryIndexDelete(adapter, data, collectionName, partitionName, opts.IndexedFields, options)
|
del, used, derr := db.tryIndexDelete(adapter, data, collectionName, partitionName, opts.IndexedFields, options)
|
||||||
if derr != nil {
|
if derr != nil {
|
||||||
@@ -1701,38 +1709,20 @@ func (db *LineDb) deletePartitioned(data any, collectionName string, options Lin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return allResults, nil
|
return allResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *LineDb) readByFilterPartitioned(filter any, collectionName string, options LineDbAdapterOptions) ([]any, error) {
|
func (db *LineDb) readByFilterPartitioned(filter any, collectionName string, options LineDbAdapterOptions) ([]any, error) {
|
||||||
// Получаем все партиции
|
baseName := db.getBaseCollectionName(collectionName)
|
||||||
partitionFiles, err := db.getPartitionFiles(collectionName)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var allResults []any
|
var allResults []any
|
||||||
for _, filename := range partitionFiles {
|
for _, adapter := range db.partitionStorageAdaptersOrdered(baseName) {
|
||||||
// Находим адаптер по имени файла
|
|
||||||
var adapter *JSONLFile
|
|
||||||
for name, adapterFile := range db.collections {
|
|
||||||
if adapterFile == filename {
|
|
||||||
adapter = db.adapters[name]
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if adapter != nil {
|
|
||||||
results, err := adapter.ReadByFilter(filter, options)
|
results, err := adapter.ReadByFilter(filter, options)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
allResults = append(allResults, results...)
|
allResults = append(allResults, results...)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return allResults, nil
|
return allResults, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user