learner 0.9.0

A simple library for learning stuff
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Database instruction implementation for adding papers and documents.
//!
//! This module provides functionality for adding papers and their associated documents
//! to the database. It supports several addition patterns:
//!
//! - Adding paper metadata only
//! - Adding complete papers with documents
//! - Batch addition of documents for existing papers
//!
//! The implementation emphasizes:
//! - Atomic transactions for data consistency
//! - Efficient batch processing
//! - Concurrent document downloads
//! - Duplicate handling
//!
//! # Examples
//!
//! ```no_run
//! use learner::{
//!   database::{Add, Database, Query},
//!   prelude::*,
//!   resource::Paper,
//!   Learner,
//! };
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let mut db = Database::open(Database::default_path()).await?;
//!
//! // Add just paper metadata
//! # let learner = Learner::builder().build().await?;
//! # let paper = learner.retriever.get_paper("2301.07041").await?;
//! Add::paper(&paper).execute(&mut db).await?;
//!
//! // Add paper with document
//! Add::complete(&paper).execute(&mut db).await?;
//!
//! // Add documents for papers matching a query
//! let query = Query::by_author("Alice Researcher");
//! Add::documents(query).execute(&mut db).await?;
//! # Ok(())
//! # }
//! ```

use std::collections::HashSet;

use futures::future::try_join_all;

use super::*;

// TODO (autoparallel): Would be good to have `Papers` and `Documents` and `Completes` instead,
// possibly, and just have a simple API for single paper calls that just dumps into the 3 variants.
/// Represents different types of additions to the database.
///
/// This enum defines the supported addition operations, each handling a different
/// aspect of paper and document management:
///
/// - Metadata-only additions
/// - Complete paper additions (metadata + document)
/// - Batch document additions for existing papers
#[derive(Debug)]
pub enum Addition<'a> {
  /// Add just the paper metadata without associated documents
  Paper(&'a Paper),
  /// Add both paper metadata and download its associated document
  Complete(&'a Paper),
  /// Add documents for papers matching a specified query
  Documents(Query<'a>),
}

/// Database instruction for adding papers and documents.
///
/// This struct implements the [`DatabaseInstruction`] trait to provide
/// paper and document addition functionality. It handles:
///
/// - Paper metadata insertion
/// - Author information management
/// - Document downloading and storage
/// - Batch processing for multiple papers
///
/// Operations are performed atomically using database transactions to
/// ensure consistency.
pub struct Add<'a> {
  /// The type of addition operation to perform
  addition: Addition<'a>,
}

impl<'a> Add<'a> {
  /// Creates an instruction to add paper metadata only.
  ///
  /// This method creates an addition that will store the paper's metadata
  /// in the database without downloading or storing its associated document.
  ///
  /// # Arguments
  ///
  /// * `paper` - Reference to the paper to add
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use learner::database::Add;
  /// # use learner::resource::Paper;
  /// # use learner::Learner;
  /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
  /// # let learner = Learner::builder().build().await?;
  /// # let retriever = learner.retriever;
  /// let paper = retriever.get_paper("2301.07041").await?;
  /// let instruction = Add::paper(&paper);
  /// # Ok(())
  /// # }
  /// ```
  pub fn paper(paper: &'a Paper) -> Self { Self { addition: Addition::Paper(paper) } }

  /// Creates an instruction to add a complete paper with its document.
  ///
  /// This method creates an addition that will:
  /// 1. Store the paper's metadata
  /// 2. Download the paper's document
  /// 3. Store the document in the configured storage location
  ///
  /// # Arguments
  ///
  /// * `paper` - Reference to the paper to add
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use learner::database::Add;
  /// # use learner::resource::Paper;
  /// # use learner::Learner;
  /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
  /// # let learner = Learner::builder().build().await?;
  /// # let retriever = learner.retriever;
  /// let paper = retriever.get_paper("2301.07041").await?;
  /// let instruction = Add::complete(&paper);
  /// # Ok(())
  /// # }
  /// ```
  pub fn complete(paper: &'a Paper) -> Self { Self { addition: Addition::Complete(paper) } }

  /// Creates an instruction to add documents for papers matching a query.
  ///
  /// This method supports batch document addition by:
  /// 1. Finding papers matching the query
  /// 2. Filtering out papers that already have documents
  /// 3. Concurrently downloading missing documents
  /// 4. Storing documents in the configured location
  ///
  /// # Arguments
  ///
  /// * `query` - Query to identify papers needing documents
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use learner::database::{Add, Query};
  /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
  /// // Add documents for all papers by an author
  /// let query = Query::by_author("Alice Researcher");
  /// let instruction = Add::documents(query);
  ///
  /// // Or add documents for papers matching a search
  /// let query = Query::text("quantum computing");
  /// let instruction = Add::documents(query);
  /// # Ok(())
  /// # }
  /// ```
  pub fn documents(query: Query<'a>) -> Self { Self { addition: Addition::Documents(query) } }

  /// Converts a paper-only addition to a complete addition.
  ///
  /// This method allows for fluent conversion of a paper metadata addition
  /// to include document download and storage.
  ///
  /// # Examples
  ///
  /// ```no_run
  /// # use learner::database::Add;
  /// # use learner::Learner;
  /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
  /// # let learner = Learner::builder().build().await?;
  /// # let retriever = learner.retriever;
  /// let paper = retriever.get_paper("2301.07041").await?;
  /// let instruction = Add::paper(&paper).with_document();
  /// # Ok(())
  /// # }
  /// ```
  pub fn with_document(self) -> Self {
    match self.addition {
      Addition::Paper(paper) => Self { addition: Addition::Complete(paper) },
      _ => self,
    }
  }

  /// Builds the SQL for inserting paper metadata.
  fn build_paper_sql(paper: &Paper) -> (String, Vec<Option<String>>) {
    (
      "INSERT INTO papers (
            title, abstract_text, publication_date,
            source, source_identifier, pdf_url, doi
        ) VALUES (?, ?, ?, ?, ?, ?, ?)"
        .to_string(),
      vec![
        Some(paper.title.clone()),
        Some(paper.abstract_text.clone()),
        Some(paper.publication_date.to_rfc3339()),
        Some(paper.source.to_string()),
        Some(paper.source_identifier.clone()),
        paper.pdf_url.clone(),
        paper.doi.clone(),
      ],
    )
  }

  /// Builds the SQL for inserting author information.
  fn build_author_sql(author: &Author, paper: &Paper) -> (String, Vec<Option<String>>) {
    (
      "INSERT INTO authors (paper_id, name, affiliation, email)
         SELECT id, ?, ?, ?
         FROM papers
         WHERE source = ? AND source_identifier = ?"
        .to_string(),
      vec![
        Some(author.name.clone()),
        author.affiliation.clone(),
        author.email.clone(),
        Some(paper.source.to_string()),
        Some(paper.source_identifier.clone()),
      ],
    )
  }

  /// Builds the SQL for recording document storage information.
  fn build_document_sql(
    paper: &Paper,
    storage_path: &Path,
    filename: &Path,
  ) -> (String, Vec<Option<String>>) {
    (
      "INSERT INTO files (paper_id, path, filename, download_status)
         SELECT p.id, ?, ?, 'Success'
         FROM papers p
         WHERE p.source = ? AND p.source_identifier = ?"
        .to_string(),
      vec![
        Some(storage_path.to_string_lossy().to_string()),
        Some(filename.to_string_lossy().to_string()),
        Some(paper.source.to_string()),
        Some(paper.source_identifier.clone()),
      ],
    )
  }

  /// Builds the SQL for checking existing document records.
  fn build_existing_docs_sql(papers: &[&Paper]) -> (String, Vec<Option<String>>) {
    let mut params = Vec::new();
    let mut param_placeholders = Vec::new();

    for paper in papers {
      params.push(Some(paper.source.to_string()));
      params.push(Some(paper.source_identifier.clone()));
      param_placeholders.push("(? = p.source AND ? = p.source_identifier)");
    }

    (
      format!(
        "SELECT p.source, p.source_identifier
             FROM files f
             JOIN papers p ON p.id = f.paper_id
             WHERE f.download_status = 'Success'
             AND ({})",
        param_placeholders.join(" OR ")
      ),
      params,
    )
  }
}

#[async_trait]
impl DatabaseInstruction for Add<'_> {
  type Output = Vec<Paper>;

  async fn execute(&self, db: &mut Database) -> Result<Self::Output> {
    match &self.addition {
      Addition::Paper(paper) => {
        // Check for existing paper
        if Query::by_source(&paper.source, &paper.source_identifier)
          .execute(db)
          .await?
          .into_iter()
          .next()
          .is_some()
        {
          return Err(LearnerError::DatabaseDuplicatePaper(paper.title.clone()));
        }

        let (paper_sql, paper_params) = Self::build_paper_sql(paper);
        let author_statements: Vec<_> =
          paper.authors.iter().map(|author| Self::build_author_sql(author, paper)).collect();

        db.conn
          .call(move |conn| {
            let tx = conn.transaction()?;
            tx.execute(&paper_sql, params_from_iter(paper_params))?;

            for (author_sql, author_params) in author_statements {
              tx.execute(&author_sql, params_from_iter(author_params))?;
            }

            tx.commit()?;
            Ok(())
          })
          .await?;

        Ok(vec![(*paper).clone()])
      },

      Addition::Complete(paper) => {
        // Add paper first
        if let Err(LearnerError::DatabaseDuplicatePaper(_)) = Add::paper(paper).execute(db).await {
          warn!(
            "Tried to add complete paper when paper existed in database already, attempting to \
             add only the document!"
          )
        };

        // Add document
        let storage_path = db.get_storage_path().await?;
        let filename = paper.download_pdf(&storage_path).await?;

        let (doc_sql, doc_params) = Self::build_document_sql(paper, &storage_path, &filename);

        db.conn
          .call(move |conn| {
            let tx = conn.transaction()?;
            tx.execute(&doc_sql, params_from_iter(doc_params))?;
            tx.commit()?;
            Ok(())
          })
          .await?;

        Ok(vec![(*paper).clone()])
      },

      Addition::Documents(query) => {
        let papers = query.execute(db).await?;
        if papers.is_empty() {
          return Ok(Vec::new());
        }

        let storage_path = db.get_storage_path().await?;
        let mut added = Vec::new();

        // Process papers in batches
        for chunk in papers.chunks(10) {
          // Check which papers already have documents
          let paper_refs: Vec<_> = chunk.iter().collect();
          let (check_sql, check_params) = Self::build_existing_docs_sql(&paper_refs);

          let existing_docs: HashSet<(String, String)> = db
            .conn
            .call(move |conn| {
              let mut docs = HashSet::new();
              let mut stmt = conn.prepare_cached(&check_sql)?;
              let mut rows = stmt.query(params_from_iter(check_params))?;

              while let Some(row) = rows.next()? {
                docs.insert((row.get::<_, String>(0)?, row.get::<_, String>(1)?));
              }
              Ok(docs)
            })
            .await?;

          // Create future for each paper that needs downloading
          let download_futures: Vec<_> = chunk
            .iter()
            .filter(|paper| {
              let key = (paper.source.to_string(), paper.source_identifier.clone());
              !existing_docs.contains(&key)
            })
            .map(|paper| {
              let paper = paper.clone();
              let storage_path = storage_path.clone();
              async move { paper.download_pdf(&storage_path).await.map(|f| (paper, f)) }
            })
            .collect();

          if download_futures.is_empty() {
            continue;
          }

          // Download PDFs concurrently and collect results
          let results = try_join_all(download_futures).await?;

          // Prepare batch insert for successful downloads
          let mut insert_sqls = Vec::new();
          let mut insert_params = Vec::new();

          for (paper, filename) in results {
            let (sql, params) = Self::build_document_sql(&paper, &storage_path, &filename);
            insert_sqls.push(sql);
            insert_params.extend(params);
            added.push(paper);
          }

          if !insert_sqls.is_empty() {
            // Execute batch insert
            db.conn
              .call(move |conn| {
                let tx = conn.transaction()?;
                for (sql, params) in insert_sqls.iter().zip(insert_params.chunks(4)) {
                  tx.execute(sql, params_from_iter(params))?;
                }
                tx.commit()?;
                Ok(())
              })
              .await?;
          }
        }

        Ok(added)
      },
    }
  }
}