mirror of
https://github.com/marcominerva/SqlDatabaseVectorSearch.git
synced 2026-06-20 12:23:10 +00:00
Better transaction on document save
This commit is contained in:
@@ -24,7 +24,7 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
if (documentId.HasValue)
|
if (documentId.HasValue)
|
||||||
{
|
{
|
||||||
// If the user is importing a document that already exists, delete the previous one.
|
// If the user is importing a document that already exists, delete the previous one.
|
||||||
await DeleteDocumentAsync(documentId.Value);
|
await DeleteDocumentAsync(documentId.Value, saveChanges: false);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -58,11 +58,22 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
return documents;
|
return documents;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task DeleteDocumentAsync(Guid documentId)
|
public async Task DeleteDocumentAsync(Guid documentId, bool saveChanges = true)
|
||||||
|
{
|
||||||
|
var document = await dbContext.Documents.Include(d => d.DocumentChunks).FirstOrDefaultAsync(d => d.Id == documentId);
|
||||||
|
if (document is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
dbContext.DocumentChunks.RemoveRange(document.DocumentChunks);
|
||||||
|
dbContext.Documents.Remove(document);
|
||||||
|
|
||||||
|
if (saveChanges)
|
||||||
{
|
{
|
||||||
await DeleteDocumentInternalAsync(documentId);
|
|
||||||
await dbContext.SaveChangesAsync();
|
await dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<Response?> AskQuestionAsync(Question question, bool reformulate = true)
|
public async Task<Response?> AskQuestionAsync(Question question, bool reformulate = true)
|
||||||
{
|
{
|
||||||
@@ -85,7 +96,7 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
{
|
{
|
||||||
var content = new StringBuilder();
|
var content = new StringBuilder();
|
||||||
|
|
||||||
// Reads the content of the PDF document using PdfPig.
|
// Read the content of the PDF document.
|
||||||
using var pdfDocument = PdfDocument.Open(stream);
|
using var pdfDocument = PdfDocument.Open(stream);
|
||||||
|
|
||||||
foreach (var page in pdfDocument.GetPages().Where(x => x is not null))
|
foreach (var page in pdfDocument.GetPages().Where(x => x is not null))
|
||||||
@@ -96,16 +107,4 @@ public class VectorSearchService(ApplicationDbContext dbContext, ITextEmbeddingG
|
|||||||
|
|
||||||
return Task.FromResult(content.ToString());
|
return Task.FromResult(content.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task DeleteDocumentInternalAsync(Guid documentId)
|
|
||||||
{
|
|
||||||
var document = await dbContext.Documents.Include(d => d.DocumentChunks).FirstOrDefaultAsync(d => d.Id == documentId);
|
|
||||||
if (document is null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
dbContext.DocumentChunks.RemoveRange(document.DocumentChunks);
|
|
||||||
dbContext.Documents.Remove(document);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user