From 5cb6c37765e039068fe6780ae7ae11bcfab35744 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Tue, 6 Dec 2022 20:32:34 +0100 Subject: [PATCH] RUST: Add a way to add things in the queue from the database connexion + fix the default insert function --- .../liblektor-rs/src/database/connexion.rs | 41 ++++++++++++++++++- src/rust/liblektor-rs/src/database/mod.rs | 7 +++- src/rust/liblektor-rs/src/database/queue.rs | 8 +--- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/rust/liblektor-rs/src/database/connexion.rs b/src/rust/liblektor-rs/src/database/connexion.rs index e3619b59..0443ade4 100644 --- a/src/rust/liblektor-rs/src/database/connexion.rs +++ b/src/rust/liblektor-rs/src/database/connexion.rs @@ -1,5 +1,7 @@ -use super::*; - +use super::{ + queue::{LktDatabasePriority, LktDatabaseQueueRangeIter}, + *, +}; use crate::{database::models::*, kurisu_api::v1 as api_v1}; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; @@ -161,4 +163,39 @@ impl LktDatabaseConnection { }; Ok((id, kara, lang, kara_makers, tags)) } + + /// Peek the next kara to play + pub fn peek_next_kara_in_queue(&self) -> Option<u64> { + self.queue.peek_next() + } + + /// Pop the next kara to play + pub fn pop_next_kara_in_queue(&mut self) -> Option<u64> { + self.queue.pop_next() + } + + /// Get the content of the queue from a range. + pub fn get_queue_content(&self, range: Range<usize>) -> LktDatabaseQueueRangeIter { + self.queue.range(range) + } + + /// Add a kara at the end of the queue. + pub fn enqueue_kara(&mut self, local_id: u64) { + self.queue.enqueue_kara(local_id) + } + + /// Add a kara at the end of a priority in the queue. + pub fn enqueue_kara_with_priority(&mut self, local_id: u64, priority: LktDatabasePriority) { + self.queue.enqueue_kara_with_priority(local_id, priority) + } + + /// Add a kara at the top of the queue. + pub fn insert_kara(&mut self, local_id: u64) { + self.queue.insert_kara(local_id) + } + + /// Add a kara at the top of a priority in the queue. + pub fn insert_kara_with_priority(&mut self, local_id: u64, priority: LktDatabasePriority) { + self.queue.insert_kara_with_priority(local_id, priority) + } } diff --git a/src/rust/liblektor-rs/src/database/mod.rs b/src/rust/liblektor-rs/src/database/mod.rs index 159cbedd..53608479 100644 --- a/src/rust/liblektor-rs/src/database/mod.rs +++ b/src/rust/liblektor-rs/src/database/mod.rs @@ -10,7 +10,12 @@ pub mod unsafe_interface; pub(self) use diesel::prelude::*; pub(self) use error::*; pub(self) use log::*; -pub(self) use std::path::Path; +pub(self) use std::{ + collections::VecDeque, + ops::{Index, Range}, + path::Path, + slice::SliceIndex, +}; /// All the information needed to add a kara recieved from a repo! pub type NewKaraRequest<'a> = ( diff --git a/src/rust/liblektor-rs/src/database/queue.rs b/src/rust/liblektor-rs/src/database/queue.rs index 10e40541..8be40e49 100644 --- a/src/rust/liblektor-rs/src/database/queue.rs +++ b/src/rust/liblektor-rs/src/database/queue.rs @@ -1,8 +1,4 @@ -use std::{ - collections::VecDeque, - ops::{Index, Range}, - slice::SliceIndex, -}; +use super::*; /// The number of priority levels in the queues. The higher the number, the /// higher the priority. @@ -99,7 +95,7 @@ impl LktDatabaseQueue { } pub fn insert_kara(&mut self, local_id: u64) { - self.levels[0].push_front(local_id); + self.levels[LKT_DATABASE_QUEUES_COUNT - 1].push_front(local_id); } pub fn insert_kara_with_priority(&mut self, local_id: u64, priority: LktDatabasePriority) { -- GitLab