mirror of
https://github.com/fluencelabs/fluent-pad
synced 2025-04-25 00:42:14 +00:00
history in-memory
This commit is contained in:
parent
b26cd026b9
commit
13c957fc90
174
services/history-inmemory/Cargo.lock
generated
174
services/history-inmemory/Cargo.lock
generated
@ -19,89 +19,46 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
||||
|
||||
[[package]]
|
||||
name = "fce-sqlite-connector"
|
||||
version = "0.1.3"
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8165090ee52453a5c14bd42212bfc6516860c4aaa2315fd568c0198b46b52901"
|
||||
dependencies = [
|
||||
"fluence 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "fluence"
|
||||
version = "0.2.9"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1099731bb64286758969fb8dd6f903d1e4a7398d8e63bdec1be6387576f4d8ab"
|
||||
checksum = "e65d9ffa281a0e9328fa6eb2468b8235a197013b9b5032aa9c2d2c0d54f99442"
|
||||
dependencies = [
|
||||
"fluence-sdk-macro 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"fluence-sdk-main 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence"
|
||||
version = "0.2.9"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
|
||||
dependencies = [
|
||||
"fluence-sdk-macro 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
|
||||
"fluence-sdk-main 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
|
||||
"fluence-sdk-macro",
|
||||
"fluence-sdk-main",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-macro"
|
||||
version = "0.2.9"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ca5ffdf0ccf817b1e4e8438f6da7e8fa024679c706a69bde7aa8cad8b43e90ee"
|
||||
checksum = "4e5402cf99199b025bd41f52b4e77c58f6b76a8c34ae33a2c3612f024af28d32"
|
||||
dependencies = [
|
||||
"fluence-sdk-wit 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-macro"
|
||||
version = "0.2.9"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
|
||||
dependencies = [
|
||||
"fluence-sdk-wit 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
|
||||
"fluence-sdk-wit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-main"
|
||||
version = "0.2.9"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4f81c3778c18d372fec6d96049f25e29fc4ff7ba4ab65ef4c2285f971e8670a"
|
||||
checksum = "584cc07494112ad39d04fc8c0f6fb687f6c03c4908c7739c46c79ae112df083d"
|
||||
dependencies = [
|
||||
"fluence-sdk-macro 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-main"
|
||||
version = "0.2.9"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
|
||||
dependencies = [
|
||||
"fluence-sdk-macro 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
|
||||
"fluence-sdk-macro",
|
||||
"log",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-wit"
|
||||
version = "0.2.9"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f9c68c4d07e821e1be23b01c278acdae4e825d03c46879f453426ea3160b3e25"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"syn",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-wit"
|
||||
version = "0.2.9"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#fd9672636e8d7a91275e5e0b8b88a34494336e5a"
|
||||
checksum = "91c6e8b4da4bb732a744fd625de39e5f9faaf41d3919c8391a11d8528dad22e5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -117,7 +74,7 @@ version = "0.1.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cfg-if 0.1.10",
|
||||
"libc",
|
||||
"wasi",
|
||||
]
|
||||
@ -128,10 +85,19 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"boolinator",
|
||||
"fce-sqlite-connector",
|
||||
"fluence 0.2.9 (git+https://github.com/fluencelabs/rust-sdk)",
|
||||
"fluence",
|
||||
"log",
|
||||
"once_cell",
|
||||
"parking_lot",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -146,13 +112,22 @@ version = "0.2.80"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dd96ffd135b2fd7b973ac026d28085defbe8983df057ced3eb4f2130b0831312"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"cfg-if 0.1.10",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -161,6 +136,31 @@ version = "1.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "260e51e7efe62b592207e9e13a68e43692a7a279171d6ba57abd208bf23645ad"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
|
||||
dependencies = [
|
||||
"instant",
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ccb628cad4f84851442432c60ad8e1f607e29752d0bf072cbd0baf28aa34272"
|
||||
dependencies = [
|
||||
"cfg-if 1.0.0",
|
||||
"instant",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.10"
|
||||
@ -226,6 +226,12 @@ dependencies = [
|
||||
"rand_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.1.57"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.5"
|
||||
@ -233,19 +239,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.117"
|
||||
name = "scopeguard"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a"
|
||||
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.117"
|
||||
version = "1.0.118"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e"
|
||||
checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -263,6 +275,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a55ca5f3b68e41c979bf8c46a6f1da892ca4db8f94023ce0bd32407573b1ac0"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.48"
|
||||
@ -294,3 +312,25 @@ name = "wasi"
|
||||
version = "0.9.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
@ -9,10 +9,10 @@ name = "history"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
|
||||
fce-sqlite-connector = "=0.1.3"
|
||||
fluence = { version = "0.2.16", features = ["logger"] }
|
||||
|
||||
anyhow = "1.0.31"
|
||||
boolinator = "2.4.0"
|
||||
log = "0.4.8"
|
||||
once_cell = "1.4.1"
|
||||
parking_lot = "0.11.1"
|
||||
|
@ -16,23 +16,16 @@
|
||||
|
||||
use crate::message::Message;
|
||||
use crate::service_api::AddServiceResult;
|
||||
use crate::service_api::CountServiceResult;
|
||||
use crate::service_api::GetMessagesServiceResult;
|
||||
use crate::Result;
|
||||
|
||||
use fce_sqlite_connector::Error as SqliteConnectorError;
|
||||
use fce_sqlite_connector::Value;
|
||||
|
||||
use std::convert::From;
|
||||
use std::error::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum HistoryError {
|
||||
SqliteConnectorError(SqliteConnectorError),
|
||||
CorruptedMessage(Vec<Value>),
|
||||
InternalError(String),
|
||||
InvalidArgument(String),
|
||||
UnexpectedValueType(Value, &'static str),
|
||||
}
|
||||
|
||||
impl Error for HistoryError {}
|
||||
@ -40,29 +33,12 @@ impl Error for HistoryError {}
|
||||
impl std::fmt::Display for HistoryError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
||||
match self {
|
||||
Self::SqliteConnectorError(err) => writeln!(f, "{:?}", err),
|
||||
Self::CorruptedMessage(values) => writeln!(
|
||||
f,
|
||||
"message can't be constructed from returned values: {:?}",
|
||||
values
|
||||
),
|
||||
Self::InternalError(err_msg) => writeln!(f, "{}", err_msg),
|
||||
Self::InvalidArgument(err_msg) => writeln!(f, "{}", err_msg),
|
||||
Self::UnexpectedValueType(value, expected_type) => writeln!(
|
||||
f,
|
||||
"expected type {}, but value {:?} received",
|
||||
expected_type, value
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<SqliteConnectorError> for HistoryError {
|
||||
fn from(err: SqliteConnectorError) -> Self {
|
||||
HistoryError::SqliteConnectorError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::convert::Infallible> for HistoryError {
|
||||
fn from(_: std::convert::Infallible) -> Self {
|
||||
unreachable!()
|
||||
@ -71,21 +47,18 @@ impl From<std::convert::Infallible> for HistoryError {
|
||||
|
||||
fn to_error_core(err: &HistoryError) -> i32 {
|
||||
match err {
|
||||
HistoryError::SqliteConnectorError(_) => 0,
|
||||
HistoryError::CorruptedMessage(_) => 1,
|
||||
HistoryError::InternalError(_) => 2,
|
||||
HistoryError::InvalidArgument(_) => 3,
|
||||
HistoryError::UnexpectedValueType(..) => 4,
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Result<i64>> for AddServiceResult {
|
||||
fn from(result: Result<i64>) -> Self {
|
||||
impl From<Result<u64>> for AddServiceResult {
|
||||
fn from(result: Result<u64>) -> Self {
|
||||
match result {
|
||||
Ok(msg_id) => Self {
|
||||
ret_code: crate::service_api::SUCCESS_CODE,
|
||||
err_msg: String::new(),
|
||||
msg_id: msg_id as u64,
|
||||
msg_id: msg_id,
|
||||
},
|
||||
Err(err) => Self {
|
||||
ret_code: to_error_core(&err),
|
||||
@ -112,20 +85,3 @@ impl From<Result<Vec<Message>>> for GetMessagesServiceResult {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Result<i64>> for CountServiceResult {
|
||||
fn from(result: Result<i64>) -> Self {
|
||||
match result {
|
||||
Ok(messages_count) => Self {
|
||||
ret_code: crate::service_api::SUCCESS_CODE,
|
||||
err_msg: String::new(),
|
||||
messages_count: messages_count as u64,
|
||||
},
|
||||
Err(err) => Self {
|
||||
ret_code: to_error_core(&err),
|
||||
err_msg: format!("{}", err),
|
||||
messages_count: 0,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,13 +19,13 @@ mod message;
|
||||
mod service_api;
|
||||
mod storage_api;
|
||||
|
||||
use fluence::WasmLogger;
|
||||
use fluence::WasmLoggerBuilder;
|
||||
use storage_api::init;
|
||||
|
||||
pub(crate) type Result<T> = std::result::Result<T, errors::HistoryError>;
|
||||
|
||||
pub fn main() {
|
||||
WasmLogger::new()
|
||||
WasmLoggerBuilder::new()
|
||||
.with_log_level(log::Level::Info)
|
||||
.build()
|
||||
.unwrap();
|
||||
|
@ -19,10 +19,6 @@ use fluence::fce;
|
||||
#[fce]
|
||||
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
|
||||
pub struct Message {
|
||||
pub id: i64,
|
||||
pub author: String,
|
||||
pub id: u64,
|
||||
pub body: String,
|
||||
pub reply_to: i64,
|
||||
}
|
||||
|
||||
pub(crate) const MESSAGE_FIELDS_COUNT: usize = 4;
|
||||
|
@ -29,8 +29,8 @@ pub struct AddServiceResult {
|
||||
}
|
||||
|
||||
#[fce]
|
||||
fn add(author: String, msg: String, reply_to: i64) -> AddServiceResult {
|
||||
add_message(msg, author, reply_to).into()
|
||||
fn add(msg: String) -> AddServiceResult {
|
||||
add_message(msg).into()
|
||||
}
|
||||
|
||||
#[fce]
|
||||
@ -49,20 +49,3 @@ fn get_all() -> GetMessagesServiceResult {
|
||||
fn get_last(last: u64) -> GetMessagesServiceResult {
|
||||
get_messages_with_limit(last).into()
|
||||
}
|
||||
|
||||
#[fce]
|
||||
fn get_by_reply_to(reply_to: u64) -> GetMessagesServiceResult {
|
||||
get_messages_by_reply_to(reply_to).into()
|
||||
}
|
||||
|
||||
#[fce]
|
||||
pub struct CountServiceResult {
|
||||
pub ret_code: i32,
|
||||
pub err_msg: String,
|
||||
pub messages_count: u64,
|
||||
}
|
||||
|
||||
#[fce]
|
||||
fn count_by_reply_to(reply_to: u64) -> CountServiceResult {
|
||||
count_messages_by_reply_to(reply_to).into()
|
||||
}
|
||||
|
@ -20,105 +20,46 @@ use crate::message::Message;
|
||||
use crate::Result;
|
||||
use utils::*;
|
||||
|
||||
use fce_sqlite_connector::Connection;
|
||||
use fce_sqlite_connector::Value;
|
||||
use fce_sqlite_connector::Value::Integer as VInteger;
|
||||
use fce_sqlite_connector::Value::String as VString;
|
||||
use once_cell::sync::{Lazy, OnceCell};
|
||||
use parking_lot::Mutex;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
static SQLITE: Lazy<Connection> = Lazy::new(|| Connection::open(":memory:").unwrap());
|
||||
static INSTANCE: OnceCell<Mutex<HashMap<u64, Message>>> = OnceCell::new();
|
||||
|
||||
pub fn init() -> Result<()> {
|
||||
let init_sql = "CREATE TABLE IF NOT EXISTS history(\
|
||||
msg_id INTEGER PRIMARY KEY,\
|
||||
msg TEXT NOT NULL,\
|
||||
author TEXT NOT NULL,\
|
||||
reply_to INTEGER\
|
||||
);";
|
||||
|
||||
SQLITE.execute(init_sql).map_err(Into::into)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_message(msg: String, author: String, reply_to: i64) -> Result<i64> {
|
||||
use crate::errors::HistoryError::InternalError;
|
||||
fn get_data() -> &'static Mutex<HashMap<u64, Message>> {
|
||||
INSTANCE.get_or_init(|| {
|
||||
<_>::default()
|
||||
})
|
||||
}
|
||||
|
||||
let add_msg_sql = "INSERT INTO history (msg, author, reply_to) VALUES (?, ?, ?)";
|
||||
let mut cursor = SQLITE.prepare(add_msg_sql)?.cursor();
|
||||
cursor.bind(&[VString(msg), VString(author), VInteger(reply_to)])?;
|
||||
cursor.next()?;
|
||||
pub fn add_message(msg: String) -> Result<u64> {
|
||||
let mut data = get_data().lock();
|
||||
|
||||
let last_rowid_sql = "SELECT last_insert_rowid()";
|
||||
let mut cursor = SQLITE.prepare(last_rowid_sql)?.cursor();
|
||||
let raw_id = cursor
|
||||
.next()?
|
||||
.ok_or_else(|| InternalError(String::from("last_insert_rowid didn't return any value")))?
|
||||
.first()
|
||||
.unwrap();
|
||||
let id = usize_to_u64(data.len())?;
|
||||
|
||||
data.insert(id, Message { id, body: msg });
|
||||
|
||||
return Ok(id)
|
||||
|
||||
value_to_integer(raw_id)
|
||||
}
|
||||
|
||||
pub fn get_messages_with_limit(limit: u64) -> Result<Vec<Message>> {
|
||||
let get_messages_with_limit_sql = "SELECT * FROM history ORDER BY msg_id DESC LIMIT ?";
|
||||
let limit = u64_to_i64(limit)?;
|
||||
let data = get_data().lock();
|
||||
let limit = u64_to_usize(limit)?;
|
||||
|
||||
get_messages(get_messages_with_limit_sql, &[VInteger(limit)])
|
||||
}
|
||||
let mut msgs: Vec<Message> = data.values().map(|user| user.clone()).collect();
|
||||
|
||||
pub fn get_messages_by_reply_to(reply_to: u64) -> Result<Vec<Message>> {
|
||||
let get_messages_by_reply_to_sql = "SELECT * FROM history WHERE reply_to = ?";
|
||||
let reply_to = u64_to_i64(reply_to)?;
|
||||
let last = &msgs[msgs.len() - limit..];
|
||||
|
||||
get_messages(get_messages_by_reply_to_sql, &[VInteger(reply_to)])
|
||||
Ok(last.to_vec())
|
||||
}
|
||||
|
||||
pub fn get_all_messages() -> Result<Vec<Message>> {
|
||||
let get_all_messages_sql = "SELECT * FROM history";
|
||||
let data = get_data().lock();
|
||||
|
||||
get_messages(get_all_messages_sql, &[])
|
||||
}
|
||||
|
||||
pub fn count_messages_by_reply_to(reply_to: u64) -> Result<i64> {
|
||||
use crate::errors::HistoryError::InternalError;
|
||||
|
||||
let get_messages_count_by_reply_to_sql = "SELECT COUNT(*) FROM history WHERE reply_to = ?";
|
||||
let mut cursor = SQLITE.prepare(get_messages_count_by_reply_to_sql)?.cursor();
|
||||
|
||||
let reply_to = u64_to_i64(reply_to)?;
|
||||
cursor.bind(&[VInteger(reply_to)])?;
|
||||
|
||||
let messages_count = cursor
|
||||
.next()?
|
||||
.ok_or_else(|| InternalError(String::from("count didn't return any value")))?
|
||||
.first()
|
||||
.unwrap();
|
||||
|
||||
value_to_integer(messages_count)
|
||||
}
|
||||
|
||||
fn get_messages(sql: &str, bind_values: &[Value]) -> Result<Vec<Message>> {
|
||||
use crate::errors::HistoryError::CorruptedMessage;
|
||||
use crate::message::MESSAGE_FIELDS_COUNT;
|
||||
|
||||
let mut get_msgs_cursor = SQLITE.prepare(sql)?.cursor();
|
||||
get_msgs_cursor.bind(bind_values)?;
|
||||
|
||||
let mut messages = Vec::new();
|
||||
while let Some(raw_message) = get_msgs_cursor.next()? {
|
||||
if raw_message.len() != MESSAGE_FIELDS_COUNT {
|
||||
return Err(CorruptedMessage(raw_message.into()));
|
||||
}
|
||||
|
||||
let message = Message {
|
||||
id: value_to_integer(&raw_message[0])?,
|
||||
author: value_to_string(&raw_message[2])?,
|
||||
body: value_to_string(&raw_message[1])?,
|
||||
reply_to: value_to_integer(&raw_message[3])?,
|
||||
};
|
||||
|
||||
messages.push(message);
|
||||
}
|
||||
|
||||
Ok(messages)
|
||||
Ok(data.values().map(|msg| msg.clone()).collect())
|
||||
}
|
||||
|
@ -16,26 +16,6 @@
|
||||
|
||||
use crate::Result;
|
||||
|
||||
use fce_sqlite_connector::Value;
|
||||
|
||||
pub(super) fn value_to_string(value: &Value) -> Result<String> {
|
||||
use crate::errors::HistoryError::UnexpectedValueType;
|
||||
|
||||
value
|
||||
.as_string()
|
||||
.ok_or_else(|| UnexpectedValueType(value.clone(), "string"))
|
||||
.map(Into::into)
|
||||
}
|
||||
|
||||
pub(super) fn value_to_integer(value: &Value) -> Result<i64> {
|
||||
use crate::errors::HistoryError::UnexpectedValueType;
|
||||
|
||||
value
|
||||
.as_integer()
|
||||
.ok_or_else(|| UnexpectedValueType(value.clone(), "integer"))
|
||||
.map(Into::into)
|
||||
}
|
||||
|
||||
pub(super) fn u64_to_i64(value: u64) -> Result<i64> {
|
||||
use crate::errors::HistoryError::InvalidArgument;
|
||||
use std::convert::TryFrom;
|
||||
@ -43,3 +23,19 @@ pub(super) fn u64_to_i64(value: u64) -> Result<i64> {
|
||||
i64::try_from(value)
|
||||
.map_err(|_| InvalidArgument(format!("limit should be less than {}", i64::max_value())))
|
||||
}
|
||||
|
||||
pub(super) fn u64_to_usize(value: u64) -> Result<usize> {
|
||||
use crate::errors::HistoryError::InvalidArgument;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
usize::try_from(value)
|
||||
.map_err(|_| InvalidArgument(format!("limit should be less than {}", i64::max_value())))
|
||||
}
|
||||
|
||||
pub(super) fn usize_to_u64(value: usize) -> Result<u64> {
|
||||
use crate::errors::HistoryError::InvalidArgument;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
u64::try_from(value)
|
||||
.map_err(|_| InvalidArgument(format!("limit should be less than {}", i64::max_value())))
|
||||
}
|
||||
|
20
services/user-list-inmemory/Cargo.lock
generated
20
services/user-list-inmemory/Cargo.lock
generated
@ -26,8 +26,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "fluence"
|
||||
version = "0.2.13"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#4979c1e1dbdcdeb7ebe1a14b41794d4a971f7bcf"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e65d9ffa281a0e9328fa6eb2468b8235a197013b9b5032aa9c2d2c0d54f99442"
|
||||
dependencies = [
|
||||
"fluence-sdk-macro",
|
||||
"fluence-sdk-main",
|
||||
@ -35,16 +36,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-macro"
|
||||
version = "0.2.13"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#4979c1e1dbdcdeb7ebe1a14b41794d4a971f7bcf"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e5402cf99199b025bd41f52b4e77c58f6b76a8c34ae33a2c3612f024af28d32"
|
||||
dependencies = [
|
||||
"fluence-sdk-wit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-main"
|
||||
version = "0.2.13"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#4979c1e1dbdcdeb7ebe1a14b41794d4a971f7bcf"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "584cc07494112ad39d04fc8c0f6fb687f6c03c4908c7739c46c79ae112df083d"
|
||||
dependencies = [
|
||||
"fluence-sdk-macro",
|
||||
"log",
|
||||
@ -53,8 +56,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "fluence-sdk-wit"
|
||||
version = "0.2.13"
|
||||
source = "git+https://github.com/fluencelabs/rust-sdk#4979c1e1dbdcdeb7ebe1a14b41794d4a971f7bcf"
|
||||
version = "0.2.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91c6e8b4da4bb732a744fd625de39e5f9faaf41d3919c8391a11d8528dad22e5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -9,7 +9,7 @@ name = "user-list"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
fluence = { git = "https://github.com/fluencelabs/rust-sdk", features = ["logger"] }
|
||||
fluence = { version = "0.2.16", features = ["logger"] }
|
||||
|
||||
anyhow = "1.0.31"
|
||||
boolinator = "2.4.0"
|
||||
|
Loading…
x
Reference in New Issue
Block a user