1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
//! Arthas is an in-memory structure database. //! //! # Usage //! //! 1. Add dependencies to `Cargo.toml`. //! //! ```html //! [dependencies] //! arthas = "^0.3" //! arthas_derive = "^0.1" //! serde_derive = "^0.9" //! ``` //! //! 2. In your `main.rs` or `lib.rs`: //! //! ```html //! extern crate arthas; //! #[macro_use] //! extern crate arthas_derive; //! #[macro_use] //! extern crate serde_derive; //! ``` //! //! 3. Add "#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Arthas)]" attribute to your struct. //! //! ```html //! #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Arthas)] //! pub struct Article { //! pub _id: String, // If you want to use id. add a field named `_id`. //! pub title: String, //! pub content: String, //! pub views: usize, //! } //! ``` //! //! //! # CRUD Examples //! All struct can use the static method `session()`. `session()` will return a [`Query`](struct.Query.html). //! //! ``` //! extern crate arthas; //! #[macro_use] //! extern crate arthas_derive; //! #[macro_use] //! extern crate serde_derive; //! //! use arthas::prelude::*; //! //! #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Arthas)] //! pub struct Article { //! pub _id: String, //! pub title: String, //! pub content: String, //! pub views: usize, //! } //! //! impl Article { //! pub fn new<T: Into<String>>(title: T) -> Article { //! Article { title: title.into(), ..Default::default() } //! } //! } //! //! fn main() { //! // Disable persistence for the tests. //! arthas::config::persistence(false); //! //! // Insert //! let id = Article::session().insert(Article::new("Hello world!")).unwrap(); //! //! // Update //! Article::session().id(&id).update(|article| article.views = 10).unwrap(); //! //! // Find //! let items = Article::session().find().unwrap(); //! assert!(items.len() > 0); //! //! // Find One //! let item = Article::session().id(&id).find_one().unwrap(); //! assert!(item.is_some()); //! assert_eq!(item.unwrap().title, "Hello world!"); //! //! // Remove //! Article::session().id(&id).remove().unwrap(); //! } //! ``` //! //! # Load Persistence //! Arthas will not automatically load persistence from disk, you have to load persistence yourself. //! //! ```html //! arthas::load::<Article>(); // Load `Article`'s persistence. //! ``` //! //! # Update Structure //! Sometimes you want to update your structure. Like renaming or removing fields. Arthas will automatically remove and add fields, but you have to tell Arthas if you want to **rename** fields. //! //! ```html //! #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize, Arthas)] //! #[arthas(rename = "content = body, views = visit")] // Use `#[arthas]` attribute. //! pub struct Article { //! pub _id: String, //! pub title: String, //! pub body: String, // This is the field renamed from `content` //! pub visit: usize, // This is the field renamed from `views` //! } //! ``` //! #![deny(missing_docs)] #[cfg(test)] extern crate env_logger; #[macro_use] extern crate quick_error; #[macro_use] extern crate serde_derive; #[macro_use] extern crate lazy_static; #[macro_use] extern crate log; extern crate serde_json; extern crate memmap; extern crate bincode; extern crate serde; extern crate glob; extern crate num_cpus; extern crate scoped_pool; extern crate quickersort; extern crate objectid; extern crate thread_id; extern crate chrono; extern crate vec_map; #[macro_use] mod macros; #[doc(hidden)] pub mod encoder; mod memory; mod persistence; mod store; mod utils; mod loader; mod query; mod error; mod tree; mod item; #[doc(hidden)] pub mod traits; pub mod prelude; pub use query::Query; pub use error::Error; pub use item::Id; #[doc(hidden)] pub mod types; pub mod config; /// Load persistence. pub fn load<T: traits::Structure>() { loader::load::<T>(); } /// Convert variable to `serde_json::Value`. pub fn to_value<S: serde::Serialize>(value: S) -> serde_json::Value { serde_json::to_value(value).unwrap() } const DATA_EXTENSION: &'static str = "ar"; const PERSISTENCE_EXTENSION: &'static str = "arx"; const LOG_EXTENSION: &'static str = "arl"; const SAVING_EXTENSION: &'static str = "saving"; const DATA_DIR: &'static str = "arthas.ar"; const BINENCODE: bool = true;