improve database memory usage
This commit is contained in:
46
Cargo.lock
generated
46
Cargo.lock
generated
@@ -22,6 +22,7 @@ name = "archive"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"byteyarn",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
@@ -53,12 +54,36 @@ version = "2.9.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "buf-trait"
|
||||||
|
version = "0.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "21eaafc770e8c073d6c3facafe7617e774305d4954aa6351b9c452eb37ee17b4"
|
||||||
|
dependencies = [
|
||||||
|
"zerocopy",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteorder"
|
||||||
|
version = "1.5.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bytes"
|
name = "bytes"
|
||||||
version = "1.10.1"
|
version = "1.10.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
|
checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "byteyarn"
|
||||||
|
version = "0.5.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b93e51d26468a15ea59f8525e0c13dc405db43e644a0b1e6d44346c72cf4cf7b"
|
||||||
|
dependencies = [
|
||||||
|
"buf-trait",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.1"
|
version = "1.0.1"
|
||||||
@@ -368,3 +393,24 @@ name = "windows_x86_64_msvc"
|
|||||||
version = "0.52.6"
|
version = "0.52.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
|
||||||
|
dependencies = [
|
||||||
|
"byteorder",
|
||||||
|
"zerocopy-derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "zerocopy-derive"
|
||||||
|
version = "0.7.35"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|||||||
@@ -5,5 +5,6 @@ edition = "2024"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bytes = "1.10.1"
|
bytes = "1.10.1"
|
||||||
|
byteyarn = "0.5.1"
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tokio = { version = "1.45.1", features = ["full"] }
|
tokio = { version = "1.45.1", features = ["full"] }
|
||||||
|
|||||||
@@ -11,12 +11,7 @@ use get::Get;
|
|||||||
use has::Has;
|
use has::Has;
|
||||||
use set::Set;
|
use set::Set;
|
||||||
|
|
||||||
use crate::{
|
use crate::{Result, connection::Connection, database::Database, errors::AppError};
|
||||||
Result,
|
|
||||||
connection::Connection,
|
|
||||||
database::{Database, Value},
|
|
||||||
errors::AppError,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
|
|||||||
@@ -1,28 +1,31 @@
|
|||||||
use std::{collections::HashMap, sync::Arc};
|
use std::{collections::BTreeMap, sync::Arc};
|
||||||
|
|
||||||
use bytes::{BufMut, Bytes, BytesMut};
|
use bytes::{BufMut, Bytes, BytesMut};
|
||||||
|
use byteyarn::Yarn;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::Result;
|
use crate::Result;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
entries: Arc<Mutex<HashMap<String, Value>>>,
|
entries: Arc<Mutex<BTreeMap<Yarn, Value>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Value {
|
pub struct Value {
|
||||||
data: Bytes,
|
data: Box<[u8]>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
pub fn new(data: Bytes) -> Self {
|
pub fn new(data: Bytes) -> Self {
|
||||||
Self { data }
|
Self {
|
||||||
|
data: data.into_iter().collect(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_string(data: String) -> Self {
|
pub fn from_string(data: String) -> Self {
|
||||||
Self {
|
Self {
|
||||||
data: Bytes::from(data),
|
data: data.as_bytes().into(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +42,7 @@ impl Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get(&self, key: &str) -> Option<Bytes> {
|
pub async fn get(&self, key: &str) -> Option<Box<[u8]>> {
|
||||||
let entries = self.entries.lock().await;
|
let entries = self.entries.lock().await;
|
||||||
|
|
||||||
entries.get(key).map(|v| v.data.clone())
|
entries.get(key).map(|v| v.data.clone())
|
||||||
@@ -48,12 +51,12 @@ impl Database {
|
|||||||
pub async fn set(&self, key: String, value: Value) -> Result<()> {
|
pub async fn set(&self, key: String, value: Value) -> Result<()> {
|
||||||
let mut entries = self.entries.lock().await;
|
let mut entries = self.entries.lock().await;
|
||||||
|
|
||||||
entries.insert(key, value);
|
entries.insert(key.into(), value);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn delete(&self, key: &str) -> Option<Bytes> {
|
pub async fn delete(&self, key: &str) -> Option<Box<[u8]>> {
|
||||||
let mut entries = self.entries.lock().await;
|
let mut entries = self.entries.lock().await;
|
||||||
|
|
||||||
entries.remove(key).map(|v| v.data)
|
entries.remove(key).map(|v| v.data)
|
||||||
|
|||||||
18
src/main.rs
18
src/main.rs
@@ -1,4 +1,3 @@
|
|||||||
use bytes::Bytes;
|
|
||||||
use client::Client;
|
use client::Client;
|
||||||
use database::Value;
|
use database::Value;
|
||||||
use errors::AppError;
|
use errors::AppError;
|
||||||
@@ -18,7 +17,7 @@ pub type Result<T> = std::result::Result<T, AppError>;
|
|||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
let mut server = Server::new("127.0.0.1:6171").await?;
|
let mut server = Server::new("127.0.0.1:6171").await?;
|
||||||
|
|
||||||
tokio::spawn(client("client-1".into()));
|
// tokio::spawn(client());
|
||||||
|
|
||||||
server.run().await?;
|
server.run().await?;
|
||||||
|
|
||||||
@@ -26,16 +25,15 @@ async fn main() -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test stuff
|
// Test stuff
|
||||||
async fn client(v: String) -> Result<()> {
|
async fn client() -> Result<()> {
|
||||||
let mut client = Client::new("127.0.0.1:6171").await?;
|
let mut client = Client::new("127.0.0.1:6171").await?;
|
||||||
|
|
||||||
client
|
for i in 0..1_000_000 {
|
||||||
.set(&v, Value::from_string(format!("{v}'s value")))
|
let key = format!("key-{i}");
|
||||||
.await?;
|
client.set(&key, Value::from_string(i.to_string())).await?;
|
||||||
assert_eq!(
|
}
|
||||||
client.get(&v).await.unwrap().unwrap(),
|
|
||||||
Bytes::from(format!("{v}'s value"))
|
println!("Finished writing keys");
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user