|
|
|
@@ -88,6 +88,8 @@ func (db *LineDb) Init(force bool, initOptions *LineDbInitOptions) error {
|
|
|
|
if dbFolder == "" {
|
|
|
|
if dbFolder == "" {
|
|
|
|
dbFolder = "linedb"
|
|
|
|
dbFolder = "linedb"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Нормализуем и сохраняем фактический путь, чтобы партиции создавались в той же папке
|
|
|
|
|
|
|
|
db.initOptions.DBFolder = dbFolder
|
|
|
|
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dbFolder, 0755); err != nil {
|
|
|
|
if err := os.MkdirAll(dbFolder, 0755); err != nil {
|
|
|
|
return fmt.Errorf("failed to create database folder: %w", err)
|
|
|
|
return fmt.Errorf("failed to create database folder: %w", err)
|
|
|
|
@@ -229,7 +231,13 @@ func (db *LineDb) seedLastIDPartitioned(dbFolder, baseName string) error {
|
|
|
|
if existing, ok := db.adapters[partName]; ok {
|
|
|
|
if existing, ok := db.adapters[partName]; ok {
|
|
|
|
adapter = existing
|
|
|
|
adapter = existing
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
adapter = NewJSONLFile(path, "", JSONLFileOptions{CollectionName: partName})
|
|
|
|
// Партиции должны наследовать опции базовой коллекции (allocSize/encode/crypto/marshal/etc.)
|
|
|
|
|
|
|
|
opts := JSONLFileOptions{CollectionName: partName}
|
|
|
|
|
|
|
|
if baseOpts := db.getCollectionOptions(baseName); baseOpts != nil {
|
|
|
|
|
|
|
|
opts = *baseOpts
|
|
|
|
|
|
|
|
opts.CollectionName = partName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter = NewJSONLFile(path, opts.EncryptKeyForLineDb, opts)
|
|
|
|
if err := adapter.Init(false, LineDbAdapterOptions{}); err != nil {
|
|
|
|
if err := adapter.Init(false, LineDbAdapterOptions{}); err != nil {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -392,12 +400,7 @@ func (db *LineDb) Insert(data any, collectionName string, options LineDbAdapterO
|
|
|
|
return fmt.Errorf("failed to write data: %w", err)
|
|
|
|
return fmt.Errorf("failed to write data: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Обновляем кэш
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
if db.cacheExternal != nil {
|
|
|
|
|
|
|
|
for _, item := range resultDataArray {
|
|
|
|
|
|
|
|
db.cacheExternal.UpdateCacheAfterInsert(item, collectionName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -530,10 +533,7 @@ func (db *LineDb) Update(data any, collectionName string, filter any, options Li
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if used {
|
|
|
|
if used {
|
|
|
|
if db.cacheExternal != nil {
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
ids := extractIDsFromRecords(result)
|
|
|
|
|
|
|
|
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -552,10 +552,7 @@ func (db *LineDb) Update(data any, collectionName string, filter any, options Li
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if db.cacheExternal != nil {
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
ids := extractIDsFromRecords(result)
|
|
|
|
|
|
|
|
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -596,10 +593,7 @@ func (db *LineDb) Delete(data any, collectionName string, options LineDbAdapterO
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if used {
|
|
|
|
if used {
|
|
|
|
if db.cacheExternal != nil {
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
ids := extractIDsFromRecords(result)
|
|
|
|
|
|
|
|
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -618,10 +612,7 @@ func (db *LineDb) Delete(data any, collectionName string, options LineDbAdapterO
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if db.cacheExternal != nil {
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
ids := extractIDsFromRecords(result)
|
|
|
|
|
|
|
|
db.cacheExternal.ClearEntriesContainingIDs(ids)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -672,7 +663,7 @@ func (db *LineDb) ReadByFilter(filter any, collectionName string, options LineDb
|
|
|
|
if err != nil || (options.FailOnFailureIndexRead && !hit) {
|
|
|
|
if err != nil || (options.FailOnFailureIndexRead && !hit) {
|
|
|
|
return nil, fmt.Errorf("index read failed: %w", err)
|
|
|
|
return nil, fmt.Errorf("index read failed: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hit && err == nil {
|
|
|
|
if hit {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -703,7 +694,7 @@ func (db *LineDb) ReadByFilter(filter any, collectionName string, options LineDb
|
|
|
|
if err != nil || (options.FailOnFailureIndexRead && !hit) {
|
|
|
|
if err != nil || (options.FailOnFailureIndexRead && !hit) {
|
|
|
|
return nil, fmt.Errorf("index read failed: %w", err)
|
|
|
|
return nil, fmt.Errorf("index read failed: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hit && err == nil {
|
|
|
|
if hit {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -758,15 +749,21 @@ func (db *LineDb) ClearCache(collectionName string, options LineDbAdapterOptions
|
|
|
|
if collectionName == "" {
|
|
|
|
if collectionName == "" {
|
|
|
|
db.cacheExternal.Clear()
|
|
|
|
db.cacheExternal.Clear()
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// Очищаем только записи для конкретной коллекции
|
|
|
|
db.cacheExternal.ClearCollection(collectionName)
|
|
|
|
// Это упрощенная реализация
|
|
|
|
|
|
|
|
db.cacheExternal.Clear()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// clearCacheForCollection сбрасывает кэш запросов по логической коллекции (включая ключи партиций base_*).
|
|
|
|
|
|
|
|
func (db *LineDb) clearCacheForCollection(collectionName string) {
|
|
|
|
|
|
|
|
if db.cacheExternal == nil || collectionName == "" {
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
db.cacheExternal.ClearCollection(collectionName)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Close закрывает базу данных
|
|
|
|
// Close закрывает базу данных
|
|
|
|
func (db *LineDb) Close() {
|
|
|
|
func (db *LineDb) Close() {
|
|
|
|
db.mutex.Lock()
|
|
|
|
db.mutex.Lock()
|
|
|
|
@@ -1159,7 +1156,7 @@ func (db *LineDb) getCollectionOptions(collectionName string) *JSONLFileOptions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// isValueEmpty проверяет, считается ли значение "пустым" (nil или "" для string) — для обратной совместимости
|
|
|
|
// isValueEmpty проверяет, считается ли значение "пустым" (nil или "" для string) — для обратной совместимости
|
|
|
|
func (db *LineDb) isValueEmpty(v any) bool {
|
|
|
|
func (db *LineDb) IsValueEmpty(v any) bool {
|
|
|
|
if v == nil {
|
|
|
|
if v == nil {
|
|
|
|
return true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -1397,7 +1394,13 @@ func (db *LineDb) getPartitionAdapter(data any, collectionName string) (*JSONLFi
|
|
|
|
if !exists {
|
|
|
|
if !exists {
|
|
|
|
// Создаем новый адаптер для партиции
|
|
|
|
// Создаем новый адаптер для партиции
|
|
|
|
filename := filepath.Join(db.initOptions.DBFolder, partitionName+".jsonl")
|
|
|
|
filename := filepath.Join(db.initOptions.DBFolder, partitionName+".jsonl")
|
|
|
|
adapter = NewJSONLFile(filename, "", JSONLFileOptions{CollectionName: partitionName})
|
|
|
|
// Партиции должны наследовать опции базовой коллекции (allocSize/encode/crypto/marshal/etc.)
|
|
|
|
|
|
|
|
opts := JSONLFileOptions{CollectionName: partitionName}
|
|
|
|
|
|
|
|
if baseOpts := db.getCollectionOptions(collectionName); baseOpts != nil {
|
|
|
|
|
|
|
|
opts = *baseOpts
|
|
|
|
|
|
|
|
opts.CollectionName = partitionName
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
adapter = NewJSONLFile(filename, opts.EncryptKeyForLineDb, opts)
|
|
|
|
|
|
|
|
|
|
|
|
if err := adapter.Init(false, LineDbAdapterOptions{}); err != nil {
|
|
|
|
if err := adapter.Init(false, LineDbAdapterOptions{}); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to init partition adapter: %w", err)
|
|
|
|
return nil, fmt.Errorf("failed to init partition adapter: %w", err)
|
|
|
|
@@ -1561,24 +1564,6 @@ func (db *LineDb) compareIDs(a, b any) bool {
|
|
|
|
return a == b
|
|
|
|
return a == b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// extractIDsFromRecords извлекает id из списка записей (map[string]any).
|
|
|
|
|
|
|
|
func extractIDsFromRecords(records []any) []any {
|
|
|
|
|
|
|
|
if len(records) == 0 {
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := make([]any, 0, len(records))
|
|
|
|
|
|
|
|
for _, rec := range records {
|
|
|
|
|
|
|
|
m, ok := rec.(map[string]any)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if id, exists := m["id"]; exists && id != nil {
|
|
|
|
|
|
|
|
ids = append(ids, id)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ids
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (db *LineDb) generateCacheKey(filter any, collectionName string) string {
|
|
|
|
func (db *LineDb) generateCacheKey(filter any, collectionName string) string {
|
|
|
|
// Упрощенная реализация генерации ключа кэша
|
|
|
|
// Упрощенная реализация генерации ключа кэша
|
|
|
|
return fmt.Sprintf("%s:%v", collectionName, filter)
|
|
|
|
return fmt.Sprintf("%s:%v", collectionName, filter)
|
|
|
|
@@ -1677,6 +1662,7 @@ func (db *LineDb) updatePartitioned(data any, collectionName string, filter any,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
return allResults, nil
|
|
|
|
return allResults, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -1710,6 +1696,7 @@ func (db *LineDb) deletePartitioned(data any, collectionName string, options Lin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.clearCacheForCollection(collectionName)
|
|
|
|
return allResults, nil
|
|
|
|
return allResults, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|