Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,6 @@ func buildRepositories(cfg config.Config) (repository.PaperRepository, repositor
return nil, nil, nil, err
}
return database.Papers, database.Chunks, database.Users, nil

case "json":
repo, err := repository.NewJSONRepository(cfg.DBPath)
if err != nil {
return nil, nil, nil, err
}
return repo, repository.NewJSONChunkRepository(repo), nil, nil

case "memory":
return repository.NewMemoryPaperRepository(), repository.NewMemoryChunkRepository(), nil, nil

default:
return nil, nil, nil, fmt.Errorf("unsupported CERULEAN_DB_DRIVER=%q; supported: mysql, json, memory", cfg.DBDriver)
}
Expand Down
54 changes: 54 additions & 0 deletions internal/dao/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dao
import (
"context"
"fmt"
"strings"

"github.com/CeruleanFlow/cerulean/internal/domain"
"github.com/CeruleanFlow/cerulean/internal/entity"
Expand Down Expand Up @@ -135,3 +136,56 @@ func fromJSONMap(values datatypes.JSONMap) map[string]string {

return out
}

func (d *ChunkDAO) ReplaceByPaperID(ctx context.Context, paperID string, chunks []domain.Chunk) error {
if strings.TrimSpace(paperID) == "" {
return fmt.Errorf("invalid paper id")
}

return d.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.Where("paper_id = ?", paperID).Delete(&entity.Chunk{}).Error; err != nil {
return fmt.Errorf("delete old chunks: %w", err)
}
if len(chunks) == 0 {
return nil
}

entities := make([]entity.Chunk, 0, len(chunks))

for _, chunk := range chunks {
if chunk.PaperID != paperID {
return fmt.Errorf("invalid paper id")
}
if chunk.PaperID == "" {
chunk.PaperID = paperID
}

entities = append(entities, domainChunkToEntity(chunk))
}

if err := tx.Create(&entities).Error; err != nil {
return fmt.Errorf("insert new chunks: %w", err)
}
return nil
})
}

func domainChunkToEntity(chunk domain.Chunk) entity.Chunk {
metadata := map[string]any{}
for k, v := range chunk.Metadata {
metadata[k] = v
}

return entity.Chunk{
ID: chunk.ID,
PaperID: chunk.PaperID,
PageNo: chunk.PageNo,
ChunkIndex: chunk.Index,
Text: chunk.Text,
ObjectKey: chunk.ObjectKey,
VectorID: chunk.VectorID,
Metadata: metadata,
CreatedAt: chunk.CreatedAt,
UpdatedAt: chunk.UpdatedAt,
}
}
15 changes: 4 additions & 11 deletions internal/ingest/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,15 @@ func (s *Service) runPDFTextIngest(ctx context.Context, job task.Task, paper dom
return fmt.Errorf("pdf text parser produced no chunks")
}

// delete old chunks in mysql
if err := s.chunks.DeleteByPaperID(ctx, paper.ID); err != nil {
return fmt.Errorf("delete old chunks from mysql: %w", err)
if err := s.chunks.ReplaceByPaperID(ctx, paper.ID, chunks); err != nil {
return fmt.Errorf("replace chunks in mysql: %w", err)
}
// delete old chunk index in es

if s.search != nil {
if err := s.search.DeleteByPaperID(ctx, paper.ID); err != nil {
return fmt.Errorf("delete old chunks from elasticsearch: %w", err)
}
}
// save new chunks in mysql
if err := s.chunks.UpsertMany(ctx, chunks); err != nil {
return fmt.Errorf("save chunks to mysql: %w", err)
}
// save new chunks index in es
if s.search != nil {

if err := s.search.IndexChunks(ctx, chunks); err != nil {
return fmt.Errorf("index chunks to elasticsearch: %w", err)
}
Expand Down
7 changes: 0 additions & 7 deletions internal/repository/chunk_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import (
"github.com/CeruleanFlow/cerulean/internal/domain"
)

type ChunkRepository interface {
UpsertMany(ctx context.Context, chunks []domain.Chunk) error
List(ctx context.Context, filters map[string]string) ([]domain.Chunk, error)
ListByPaperID(ctx context.Context, paperID string) ([]domain.Chunk, error)
DeleteByPaperID(ctx context.Context, paperID string) error
}

type MemoryChunkRepository struct {
mu sync.RWMutex
chunks map[string]domain.Chunk
Expand Down
15 changes: 15 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package repository

import (
"context"

"github.com/CeruleanFlow/cerulean/internal/domain"
)

type ChunkRepository interface {
UpsertMany(ctx context.Context, chunks []domain.Chunk) error
List(ctx context.Context, filters map[string]string) ([]domain.Chunk, error)
ListByPaperID(ctx context.Context, paperID string) ([]domain.Chunk, error)
DeleteByPaperID(ctx context.Context, paperID string) error
ReplaceByPaperID(ctx context.Context, paperID string, chunks []domain.Chunk) error
}
Loading