-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
334 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use sea_orm::{entity::prelude::*, InsertResult, Set}; | ||
use serde::Serialize; | ||
|
||
use super::{indexed_document, vec_documents}; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Eq)] | ||
#[sea_orm(table_name = "vec_to_indexed")] | ||
pub struct Model { | ||
#[sea_orm(primary_key)] | ||
pub id: i64, | ||
pub indexed_id: i64, | ||
/// When this was first added to the crawl queue. | ||
pub created_at: DateTimeUtc, | ||
/// When this task was last updated. | ||
pub updated_at: DateTimeUtc, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter)] | ||
pub enum Relation { | ||
IndexedId, | ||
} | ||
|
||
impl RelationTrait for Relation { | ||
fn def(&self) -> RelationDef { | ||
match self { | ||
Self::IndexedId => Entity::belongs_to(super::indexed_document::Entity) | ||
.from(Column::IndexedId) | ||
.to(super::indexed_document::Column::Id) | ||
.into(), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl ActiveModelBehavior for ActiveModel { | ||
fn new() -> Self { | ||
Self { | ||
created_at: Set(chrono::Utc::now()), | ||
updated_at: Set(chrono::Utc::now()), | ||
..ActiveModelTrait::default() | ||
} | ||
} | ||
|
||
// Triggered before insert / update | ||
async fn before_save<C>(mut self, _db: &C, _insert: bool) -> Result<Self, DbErr> | ||
where | ||
C: ConnectionTrait, | ||
{ | ||
Ok(self) | ||
} | ||
} | ||
|
||
pub async fn insert_embedding_mapping( | ||
db: &DatabaseConnection, | ||
indexed_id: i64, | ||
) -> Result<InsertResult<ActiveModel>, DbErr> { | ||
let mut active_model = ActiveModel::new(); | ||
active_model.indexed_id = Set(indexed_id); | ||
|
||
Entity::insert(active_model).exec(db).await | ||
} | ||
|
||
pub async fn delete_all_for_document( | ||
db: &DatabaseConnection, | ||
indexed_id: i64, | ||
) -> Result<(), DbErr> { | ||
let documents = Entity::find() | ||
.filter(Column::IndexedId.eq(indexed_id)) | ||
.all(db) | ||
.await?; | ||
|
||
if !documents.is_empty() { | ||
let ids = documents.iter().map(|val| val.id).collect::<Vec<i64>>(); | ||
let _ = vec_documents::delete_embedding_by_ids(db, &ids).await?; | ||
|
||
let _ = Entity::delete_many() | ||
.filter(Column::Id.is_in(ids)) | ||
.exec(db) | ||
.await; | ||
Ok(()) | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub async fn delete_all_by_urls(db: &DatabaseConnection, urls: &[String]) -> Result<(), DbErr> { | ||
let documents = indexed_document::Entity::find() | ||
.filter(indexed_document::Column::Url.is_in(urls)) | ||
.all(db) | ||
.await?; | ||
|
||
for doc in documents { | ||
delete_all_for_document(db, doc.id).await?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
crates/migrations/src/m20241115_000001_embedding_to_indexed_document.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[derive(Iden)] | ||
enum VecToIndexed { | ||
#[iden = "vec_to_indexed"] | ||
Table, | ||
Id, | ||
IndexedId, | ||
CreatedAt, | ||
UpdatedAt, | ||
} | ||
|
||
#[derive(Iden)] | ||
enum IndexedDocument { | ||
#[iden = "indexed_document"] | ||
Table, | ||
Id, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.create_table( | ||
Table::create() | ||
.table(VecToIndexed::Table) | ||
.if_not_exists() | ||
.foreign_key( | ||
ForeignKey::create() | ||
.name("fk-vec_to_indexed-indexed_document") | ||
.from(VecToIndexed::Table, VecToIndexed::IndexedId) | ||
.to(IndexedDocument::Table, IndexedDocument::Id) | ||
.on_delete(ForeignKeyAction::Cascade) | ||
.on_update(ForeignKeyAction::NoAction), | ||
) | ||
.col( | ||
ColumnDef::new(VecToIndexed::Id) | ||
.big_integer() | ||
.not_null() | ||
.auto_increment() | ||
.primary_key(), | ||
) | ||
.col( | ||
ColumnDef::new(VecToIndexed::IndexedId) | ||
.big_integer() | ||
.not_null(), | ||
) | ||
.col( | ||
ColumnDef::new(VecToIndexed::CreatedAt) | ||
.timestamp_with_time_zone() | ||
.not_null(), | ||
) | ||
.col( | ||
ColumnDef::new(VecToIndexed::UpdatedAt) | ||
.timestamp_with_time_zone() | ||
.not_null(), | ||
) | ||
.to_owned(), | ||
) | ||
.await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn down(&self, _manager: &SchemaManager) -> Result<(), DbErr> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.