aboutsummaryrefslogtreecommitdiff
path: root/src/db/db.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/db/db.rs')
-rw-r--r--src/db/db.rs17
1 files changed, 5 insertions, 12 deletions
diff --git a/src/db/db.rs b/src/db/db.rs
index 590f3ea..30ea1da 100644
--- a/src/db/db.rs
+++ b/src/db/db.rs
@@ -4,7 +4,6 @@ use crate::{
util::serialization,
};
use anyhow::Result;
-use rand::{rngs::ThreadRng, Rng};
use rusqlite::{params, Connection};
use rusqlite_migration::{Migrations, M};
@@ -29,18 +28,17 @@ pub fn synchronize(conn: &Connection, entries: Vec<Entry>) -> Result<()> {
let now = time::now()?;
let state = serde_json::to_string(&space_repetition::init())?;
- let mut rng = rand::thread_rng();
for entry in entries {
let concat_1 = serialization::words_to_line(&entry.part_1);
let concat_2 = serialization::words_to_line(&entry.part_2);
for w in entry.part_1.iter() {
- insert(&conn, &mut rng, now, &w, &concat_2, &state)?;
+ insert(&conn, now, &w, &concat_2, &state)?;
}
for w in entry.part_2.iter() {
- insert(&conn, &mut rng, now, &w, &concat_1, &state)?;
+ insert(&conn, now, &w, &concat_1, &state)?;
}
}
@@ -51,23 +49,18 @@ pub fn synchronize(conn: &Connection, entries: Vec<Entry>) -> Result<()> {
fn insert(
conn: &Connection,
- rng: &mut ThreadRng,
now: u64,
question: &String,
responses: &String,
state: &String,
) -> Result<()> {
- // Subtract a random amount of time so that cards are not initially given in the same order as
- // in the deck
- let ready_sub: u64 = rng.gen_range(0..100);
-
conn.execute(
"
INSERT INTO cards (question, responses, state, created, deck_read, ready)
VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT (question, responses) DO UPDATE SET deck_read = ?, deleted = null
",
- params![question, responses, state, now, now, now - ready_sub, now],
+ params![question, responses, state, now, now, now, now],
)?;
Ok(())
@@ -82,14 +75,14 @@ fn delete_read_before(conn: &Connection, t: u64) -> Result<()> {
Ok(())
}
-pub fn next_ready(conn: &Connection) -> Option<Card> {
+pub fn pick_random_ready(conn: &Connection) -> Option<Card> {
let mut stmt = conn
.prepare(
"
SELECT question, responses, state, ready
FROM cards
WHERE deleted IS NULL
- ORDER BY ready
+ ORDER BY RANDOM()
LIMIT 1
",
)