9 changed files with 78 additions and 78 deletions
@ -1,6 +1,76 @@
|
||||
mod dict_importer; |
||||
mod infrastructure; |
||||
|
||||
use futures::stream::BoxStream; |
||||
|
||||
use crate::common::errors::RepositoryError; |
||||
|
||||
pub use self::dict_importer::DictImporter; |
||||
pub use self::infrastructure::json_file_dict_source::JsonFileDictSource; |
||||
pub use self::infrastructure::sqlite_dict_repository::SqliteDictRepository; |
||||
|
||||
use std::collections::HashMap; |
||||
|
||||
pub type DictEntryId = u64; |
||||
|
||||
#[derive(Debug, Clone, PartialEq)] |
||||
pub struct DictEntry { |
||||
pub id: Option<DictEntryId>, |
||||
pub text: String, |
||||
pub metadata: HashMap<String, String>, |
||||
} |
||||
|
||||
impl DictEntry { |
||||
pub fn new(id: Option<DictEntryId>, text: String) -> Self { |
||||
DictEntry { |
||||
id, |
||||
text, |
||||
metadata: HashMap::new(), |
||||
} |
||||
} |
||||
} |
||||
|
||||
#[derive(Debug, Clone)] |
||||
pub struct Dict { |
||||
pub name: String, |
||||
pub entries: HashMap<DictEntryId, DictEntry>, |
||||
} |
||||
|
||||
impl Dict { |
||||
pub fn new(name: String) -> Self { |
||||
Dict { |
||||
name, |
||||
entries: HashMap::new(), |
||||
} |
||||
} |
||||
|
||||
pub fn add_entry(&mut self, entry: DictEntry) { |
||||
self.entries.insert(entry.id.unwrap(), entry); |
||||
} |
||||
} |
||||
|
||||
#[async_trait::async_trait] |
||||
pub trait DictRepository: Send + Sync { |
||||
fn use_dict(&mut self, name: &str); |
||||
async fn create_dict(&self) -> Result<(), RepositoryError>; |
||||
|
||||
/// "Upsert" logic:
|
||||
/// - If entry exists (by text), update metadata.
|
||||
/// - If not, insert new.
|
||||
/// - IDs are handled by the Database.
|
||||
async fn save_entries(&self, entries: &[DictEntry]) -> Result<(), RepositoryError>; |
||||
|
||||
/// Fetch a page of entries.
|
||||
async fn fetch_many(&self, limit: usize, offset: usize) -> Result<Dict, RepositoryError>; |
||||
|
||||
/// Returns a cold stream that fetches strings in chunks.
|
||||
/// The stream yields `Result<Vec<String>, RepositoryError>`.
|
||||
async fn stream_batches( |
||||
&self, |
||||
batch_size: usize, |
||||
) -> Result<BoxStream<'_, Result<Vec<String>, RepositoryError>>, RepositoryError>; |
||||
} |
||||
|
||||
pub trait DictSource { |
||||
fn next_entry(&mut self) -> Option<Result<DictEntry, anyhow::Error>>; |
||||
} |
||||
|
||||
Loading…
Reference in new issue