refactor(rag): update loadDocumentByID and loadFAQByID to return pointers and simplify error handling

This commit is contained in:
mlogclub
2026-04-19 11:56:03 +08:00
parent 700ec2a38b
commit 60956488e0
2 changed files with 17 additions and 17 deletions
+4 -4
View File
@@ -51,7 +51,7 @@ func (s *index) IndexDocumentByID(ctx context.Context, documentID int64) error {
if err != nil {
return err
}
return s.IndexDocument(ctx, document)
return s.IndexDocument(ctx, *document)
}
func (s *index) IndexDocument(ctx context.Context, document models.KnowledgeDocument) error {
@@ -72,7 +72,7 @@ func (s *index) IndexDocument(ctx context.Context, document models.KnowledgeDocu
if err != nil {
return fail(err)
}
vectors, chunkCount, err := s.runDocumentIndex(ctx, document, knowledgeBase)
vectors, chunkCount, err := s.runDocumentIndex(ctx, document, *knowledgeBase)
if err != nil {
return fail(err)
}
@@ -105,11 +105,11 @@ func (s *index) IndexFAQByID(ctx context.Context, faqID int64) error {
}
return err
}
knowledgeBase, err := s.loadFAQKnowledgeBase(faq)
knowledgeBase, err := s.loadFAQKnowledgeBase(*faq)
if err != nil {
return fail(err)
}
if err := s.runFAQIndex(ctx, faq, knowledgeBase); err != nil {
if err := s.runFAQIndex(ctx, *faq, *knowledgeBase); err != nil {
return fail(err)
}
if err := s.markFAQIndexIndexed(faq.ID); err != nil {
+13 -13
View File
@@ -10,37 +10,37 @@ import (
"github.com/mlogclub/simple/sqls"
)
func (s *index) loadDocumentByID(documentID int64) (models.KnowledgeDocument, error) {
func (s *index) loadDocumentByID(documentID int64) (*models.KnowledgeDocument, error) {
document := repositories.KnowledgeDocumentRepository.Get(sqls.DB(), documentID)
if document == nil {
return models.KnowledgeDocument{}, fmt.Errorf("document not found: %d", documentID)
return nil, fmt.Errorf("document not found: %d", documentID)
}
return *document, nil
return document, nil
}
func (s *index) loadFAQByID(faqID int64) (models.KnowledgeFAQ, error) {
func (s *index) loadFAQByID(faqID int64) (*models.KnowledgeFAQ, error) {
faq := repositories.KnowledgeFAQRepository.Get(sqls.DB(), faqID)
if faq == nil {
return models.KnowledgeFAQ{}, fmt.Errorf("faq not found: %d", faqID)
return nil, fmt.Errorf("faq not found: %d", faqID)
}
return *faq, nil
return faq, nil
}
func (s *index) loadDocumentKnowledgeBase(document models.KnowledgeDocument) (models.KnowledgeBase, error) {
func (s *index) loadDocumentKnowledgeBase(document models.KnowledgeDocument) (*models.KnowledgeBase, error) {
knowledgeBase := repositories.KnowledgeBaseRepository.Get(sqls.DB(), document.KnowledgeBaseID)
if knowledgeBase == nil {
return models.KnowledgeBase{}, fmt.Errorf("knowledge base not found: %d", document.KnowledgeBaseID)
return nil, fmt.Errorf("knowledge base not found: %d", document.KnowledgeBaseID)
}
return *knowledgeBase, nil
return knowledgeBase, nil
}
func (s *index) loadFAQKnowledgeBase(faq models.KnowledgeFAQ) (models.KnowledgeBase, error) {
func (s *index) loadFAQKnowledgeBase(faq models.KnowledgeFAQ) (*models.KnowledgeBase, error) {
knowledgeBase := repositories.KnowledgeBaseRepository.Get(sqls.DB(), faq.KnowledgeBaseID)
if knowledgeBase == nil {
return models.KnowledgeBase{}, fmt.Errorf("knowledge base not found: %d", faq.KnowledgeBaseID)
return nil, fmt.Errorf("knowledge base not found: %d", faq.KnowledgeBaseID)
}
if knowledgeBase.KnowledgeType != string(enums.KnowledgeBaseTypeFAQ) {
return models.KnowledgeBase{}, fmt.Errorf("knowledge base %d is not faq type", knowledgeBase.ID)
return nil, fmt.Errorf("knowledge base %d is not faq type", knowledgeBase.ID)
}
return *knowledgeBase, nil
return knowledgeBase, nil
}