Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

World persistence and .MCA region files #95

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
188 changes: 188 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"pumpkin-plugin",
"pumpkin-protocol/",
"pumpkin-registry/",
"pumpkin-storage",
"pumpkin-world",
"pumpkin/",
]
Expand Down Expand Up @@ -38,6 +39,7 @@ rayon = "1.10.0"
parking_lot = "0.12.3"
crossbeam = "0.8.4"

speedy = "0.8.7"
uuid = { version = "1.10.0", features = ["serde", "v3", "v4"] }
derive_more = { version = "1.0.0", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
15 changes: 15 additions & 0 deletions pumpkin-storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pumpkin-storage"
version.workspace = true
edition.workspace = true

[dependencies]
heed = "0.20.5"
xxhash-rust = { version = "0.8.12", features = ["xxh3"] }
futures = "*"
speedy.workspace = true
parking_lot.workspace = true
rayon.workspace = true
crossbeam.workspace = true

pumpkin-world = { path = "../pumpkin-world"}
31 changes: 31 additions & 0 deletions pumpkin-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::fmt::Debug;

use pumpkin_world::chunk::ChunkData;

/// Chunk storage over LMDB
pub mod lmdb;

/// Chunk storage over .MCA folder
pub mod mca;

#[allow(async_fn_in_trait)]
/// A trait implementing the API for fetching and inserting chunks to storage
pub trait ChunkStorage: Sized + Send + Sync {

/// Error returned by the database
type Error: Debug; // In the future: + Into<PumpkinError>

type OpenOptions: Clone;

/// Open storage
async fn start(options: Self::OpenOptions) -> Result<Self, Self::Error>;

/// Close storage
async fn close(self);

/// Get chunk from storage
async fn get_chunk(&self, x: i32, z: i32, dimension: &'static str) -> Result<Option<ChunkData>, Self::Error>;

/// Insert chunk into storage
async fn insert_chunk(&self, x: i32, z: i32, dimension: &'static str, chunk: ChunkData) -> Result<(),Self::Error>;
}
25 changes: 25 additions & 0 deletions pumpkin-storage/src/lmdb/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::{borrow::Cow, marker::PhantomData};

use heed::{BytesDecode, BytesEncode};
use speedy::{LittleEndian, Readable, Writable};

pub struct Speedy<'t, T>(PhantomData<&'t T>);

impl<'t, T: Writable<LittleEndian>> BytesEncode<'t> for Speedy<'t, T> {
type EItem = T;

fn bytes_encode(item: &'t Self::EItem) -> Result<Cow<'t, [u8]>, heed::BoxedError> {

let bytes = item.write_to_vec()?;
Ok(Cow::Owned(bytes))
}
}

impl<'t, T: Readable<'t, LittleEndian>> BytesDecode<'t> for Speedy<'t, T> {
type DItem = T;

fn bytes_decode(bytes: &'t [u8]) -> Result<Self::DItem, heed::BoxedError> {

Ok(T::read_from_buffer(bytes)?)
}
}
Loading
Loading