Skip to content

Commit

Permalink
reorganize traits to allow proper wasm compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Feb 11, 2022
1 parent 92d5f6d commit 9a100fa
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 78 deletions.
12 changes: 6 additions & 6 deletions nix/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
"homepage": "",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "46821ea01c8f54d2a20f5a503809abfc605269d7",
"sha256": "093daxh4yib44ddq2xgbnvk0kxgk754sfagqiplgd5rh7zmb1bpb",
"rev": "8ca77a63599ed951d6a2d244c1d62092776a3fe1",
"sha256": "11yc4lnp24x7mkn495impsx6rcmmc5m4ym44yjqihvpdz9yb0w9d",
"type": "tarball",
"url": "https://github.com/NixOS/nixpkgs/archive/46821ea01c8f54d2a20f5a503809abfc605269d7.tar.gz",
"url": "https://github.com/NixOS/nixpkgs/archive/8ca77a63599ed951d6a2d244c1d62092776a3fe1.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
},
"rust-overlay": {
Expand All @@ -29,10 +29,10 @@
"homepage": null,
"owner": "oxalica",
"repo": "rust-overlay",
"rev": "27fb59f3f4c687d599ec63a6c328e8432cd61101",
"sha256": "0mxqj0zrm4qyrjdhgyx243x2wknrp3032x91a6nff2zbfnc64wn2",
"rev": "9fb49daf1bbe1d91e6c837706c481f9ebb3d8097",
"sha256": "1h8v9346kw70glmsg58dz3fa260iy38p9kdf73nxphnnf6dy2yd4",
"type": "tarball",
"url": "https://github.com/oxalica/rust-overlay/archive/27fb59f3f4c687d599ec63a6c328e8432cd61101.tar.gz",
"url": "https://github.com/oxalica/rust-overlay/archive/9fb49daf1bbe1d91e6c837706c481f9ebb3d8097.tar.gz",
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
}
}
21 changes: 15 additions & 6 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ in
with pkgs;

pkgs.mkShell {
nativeBuildInputs = [
clang_13
];

buildInputs = [
rustPlatform.rust.cargo
openssl
pkg-config

git
stdenv.cc.cc.lib
(python38.withPackages(ps: with ps; [ virtualenv tox setuptools ]))
(python39.withPackages(ps: with ps; [ virtualenv setuptools ]))
(python37.withPackages(ps: with ps; [ virtualenv setuptools ]))
Expand All @@ -25,11 +31,14 @@ in
cargo-watch
cargo-limit
cargo-udeps

llvmPackages_13.libclang
llvmPackages_13.libcxxClang
];

shellHook = ''
# workaround for https://github.com/NixOS/nixpkgs/blob/48dfc9fa97d762bce28cc8372a2dd3805d14c633/doc/languages-frameworks/python.section.md#python-setuppy-bdist_wheel-cannot-create-whl
export SOURCE_DATE_EPOCH=315532800 # 1980
export LD_LIBRARY_PATH="${stdenv.cc.cc.lib}/lib64:$LD_LIBRARY_PATH";
'';
BINDGEN_EXTRA_CLANG_ARGS = "-isystem ${llvmPackages_13.libclang.lib}/lib/clang/${lib.getVersion clang}/include";
LIBCLANG_PATH = "${llvmPackages_13.libclang.lib}/lib";

# workaround for https://github.com/NixOS/nixpkgs/blob/48dfc9fa97d762bce28cc8372a2dd3805d14c633/doc/languages-frameworks/python.section.md#python-setuppy-bdist_wheel-cannot-create-whl
SOURCE_DATE_EPOCH = 315532800; # 1980
}
3 changes: 3 additions & 0 deletions src/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ features = ["console", "File"]

[target.'cfg(all(target_arch = "wasm32", target_vendor="unknown"))'.dev-dependencies]
wasm-bindgen-test = "0.3.0"

### These crates don't compile on wasm
[target.'cfg(not(all(target_arch = "wasm32", target_vendor="unknown")))'.dependencies]
13 changes: 0 additions & 13 deletions src/core/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,9 @@ use getset::{CopyGetters, Getters, Setters};
use typed_builder::TypedBuilder;

use crate::encodings::HashFunctions;
use crate::index::MHBT;
use crate::signature::Signature;
use crate::sketch::minhash::{max_hash_for_scaled, KmerMinHashBTree};
use crate::sketch::Sketch;
use crate::Error;

pub fn prepare(index_path: &str) -> Result<(), Error> {
let mut index = MHBT::from_path(index_path)?;

// TODO equivalent to fill_internal in python
//unimplemented!();

index.save_file(index_path, None)?;

Ok(())
}

impl Signature {
pub fn from_params(params: &ComputeParameters) -> Signature {
Expand Down
10 changes: 8 additions & 2 deletions src/core/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ pub enum SourmashError {
HLLPrecisionBounds,

#[error(transparent)]
ReadDataError(#[from] crate::index::storage::ReadDataError),
ReadDataError(#[from] ReadDataError),

#[error(transparent)]
StorageError(#[from] crate::index::storage::StorageError),
StorageError(#[from] crate::storage::StorageError),

#[error(transparent)]
SerdeError(#[from] serde_json::error::Error),
Expand All @@ -65,6 +65,12 @@ pub enum SourmashError {
Panic(#[from] crate::ffi::utils::Panic),
}

#[derive(Debug, Error)]
pub enum ReadDataError {
#[error("Could not load data")]
LoadError,
}

#[repr(u32)]
pub enum SourmashErrorCode {
// no error
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/ffi/hyperloglog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;

use crate::index::sbt::Update;
use crate::prelude::*;
use crate::signature::SigsTrait;
use crate::sketch::hyperloglog::HyperLogLog;

Expand Down
2 changes: 1 addition & 1 deletion src/core/src/ffi/nodegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ffi::CStr;
use std::os::raw::c_char;
use std::slice;

use crate::index::sbt::Update;
use crate::prelude::*;
use crate::sketch::nodegraph::Nodegraph;

use crate::ffi::minhash::SourmashKmerMinHash;
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/index/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::rc::Rc;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::index::storage::{FSStorage, ReadData, Storage, StorageInfo, ToWriter};
use crate::index::{Comparable, DatasetInfo, Index, SigStore};
use crate::prelude::*;
use crate::storage::{FSStorage, Storage, StorageInfo};
use crate::Error;

#[derive(TypedBuilder)]
Expand Down
14 changes: 4 additions & 10 deletions src/core/src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ pub mod bigsi;
pub mod linear;
pub mod sbt;

pub mod storage;

pub mod search;

use std::ops::Deref;
Expand All @@ -19,12 +17,14 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::errors::ReadDataError;
use crate::index::sbt::{Node, SBT};
use crate::index::search::{search_minhashes, search_minhashes_containment};
use crate::index::storage::{ReadData, ReadDataError, Storage};
use crate::signature::{Signature, SigsTrait};
use crate::prelude::*;
use crate::signature::SigsTrait;
use crate::sketch::nodegraph::Nodegraph;
use crate::sketch::Sketch;
use crate::storage::Storage;
use crate::Error;

pub type MHBT = SBT<Node<Nodegraph>, Signature>;
Expand Down Expand Up @@ -103,12 +103,6 @@ pub trait Index<'a> {
*/
}

// TODO: split into two traits, Similarity and Containment?
pub trait Comparable<O> {
fn similarity(&self, other: &O) -> f64;
fn containment(&self, other: &O) -> f64;
}

impl<'a, N, L> Comparable<L> for &'a N
where
N: Comparable<L>,
Expand Down
11 changes: 5 additions & 6 deletions src/core/src/index/sbt/mhbt.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::collections::HashMap;
use std::io::Write;

use crate::index::sbt::{Factory, FromFactory, Node, Update, SBT};
use crate::index::storage::{ReadData, ReadDataError, ToWriter};
use crate::index::Comparable;
use crate::signature::{Signature, SigsTrait};
use crate::errors::ReadDataError;
use crate::index::sbt::{Factory, FromFactory, Node, SBT};
use crate::prelude::*;
use crate::signature::SigsTrait;
use crate::sketch::nodegraph::Nodegraph;
use crate::sketch::Sketch;
use crate::Error;
Expand Down Expand Up @@ -157,9 +157,8 @@ mod test {
use crate::index::linear::LinearIndex;
use crate::index::sbt::scaffold;
use crate::index::search::{search_minhashes, search_minhashes_containment};
use crate::index::storage::ReadData;
use crate::index::{Index, SigStore, MHBT};
use crate::signature::Signature;
use crate::prelude::*;

#[test]
fn save_sbt() {
Expand Down
12 changes: 2 additions & 10 deletions src/core/src/index/sbt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,11 @@ use once_cell::sync::OnceCell;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::index::storage::{FSStorage, ReadData, Storage, StorageInfo, ToWriter};
use crate::index::{Comparable, DatasetInfo, Index, SigStore};
use crate::signature::Signature;
use crate::prelude::*;
use crate::storage::{FSStorage, StorageInfo};
use crate::Error;

pub trait Update<O> {
fn update(&self, other: &mut O) -> Result<(), Error>;
}

pub trait FromFactory<N> {
fn factory(&self, name: &str) -> Result<N, Error>;
}

#[derive(TypedBuilder)]
pub struct SBT<N, L> {
#[builder(default = 2)]
Expand Down
6 changes: 4 additions & 2 deletions src/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
pub mod errors;
pub use errors::SourmashError as Error;

pub mod cmd;
pub mod prelude;

pub mod index;
pub mod cmd;

pub mod signature;
pub mod sketch;
pub mod storage;

pub mod encodings;

Expand All @@ -42,6 +43,7 @@ cfg_if! {
pub mod wasm;
} else {
pub mod ffi;
pub mod index;
}
}

Expand Down
31 changes: 31 additions & 0 deletions src/core/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use std::io::Write;

use crate::Error;

pub use crate::signature::Signature;
pub use crate::storage::Storage;

pub trait ToWriter {
fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>
where
W: Write;
}

pub trait Update<O> {
fn update(&self, other: &mut O) -> Result<(), Error>;
}

pub trait FromFactory<N> {
fn factory(&self, name: &str) -> Result<N, Error>;
}

/// Implemented by anything that wants to read specific data from a storage.
pub trait ReadData<D> {
fn data(&self) -> Result<&D, Error>;
}

// TODO: split into two traits, Similarity and Containment?
pub trait Comparable<O> {
fn similarity(&self, other: &O) -> f64;
fn containment(&self, other: &O) -> f64;
}
2 changes: 1 addition & 1 deletion src/core/src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use crate::encodings::{aa_to_dayhoff, aa_to_hp, revcomp, to_aa, HashFunctions, VALID};
use crate::index::storage::ToWriter;
use crate::prelude::*;
use crate::sketch::Sketch;
use crate::Error;
use crate::HashIntoType;
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/sketch/hyperloglog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use serde::{Deserialize, Serialize};

use crate::encodings::HashFunctions;
use crate::index::sbt::Update;
use crate::prelude::*;
use crate::signature::SigsTrait;
use crate::sketch::KmerMinHash;
use crate::Error;
Expand Down
1 change: 1 addition & 0 deletions src/core/src/sketch/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod hyperloglog;
pub mod minhash;

pub mod nodegraph;

use serde::{Deserialize, Serialize};
Expand Down
2 changes: 1 addition & 1 deletion src/core/src/sketch/nodegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::slice;
use byteorder::{BigEndian, ByteOrder, LittleEndian, ReadBytesExt, WriteBytesExt};
use fixedbitset::FixedBitSet;

use crate::index::sbt::Update;
use crate::prelude::*;
use crate::sketch::minhash::KmerMinHash;
use crate::Error;
use crate::HashIntoType;
Expand Down
17 changes: 0 additions & 17 deletions src/core/src/index/storage.rs → src/core/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,6 @@ pub enum StorageError {
EmptyPathError,
}

#[derive(Debug, Error)]
pub enum ReadDataError {
#[error("Could not load data")]
LoadError,
}

/// Implemented by anything that wants to read specific data from a storage.
pub trait ReadData<D> {
fn data(&self) -> Result<&D, Error>;
}

#[derive(Serialize, Deserialize)]
pub(crate) struct StorageInfo {
pub backend: String,
Expand Down Expand Up @@ -126,9 +115,3 @@ impl Storage for FSStorage {
}
}
}

pub trait ToWriter {
fn to_writer<W>(&self, writer: &mut W) -> Result<(), Error>
where
W: Write;
}

0 comments on commit 9a100fa

Please sign in to comment.