From ca24daae51b704000ded2eed3d0432f2ab89d389 Mon Sep 17 00:00:00 2001 From: Paul Masurel Date: Thu, 11 Aug 2016 00:37:07 +0900 Subject: [PATCH] Added CLI wizard for index new --- Cargo.toml | 2 +- src/core/index.rs | 2 +- src/lib.rs | 1 + src/schema/field.rs | 2 +- src/schema/field_entry.rs | 1 + src/schema/mod.rs | 12 ++++++++++-- src/schema/text_options.rs | 30 ------------------------------ src/schema/u32_options.rs | 17 +++++++++++------ 8 files changed, 26 insertions(+), 41 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da50c0456..0e4c2bcb6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,9 +28,9 @@ uuid = "0.1" iron = "0.4" staticfile = "0.3.0" persistent="*" +ansi_term = "*" clap = "2" - [dev-dependencies] rand = "0.3.13" diff --git a/src/core/index.rs b/src/core/index.rs index a149c8837..4a155666b 100644 --- a/src/core/index.rs +++ b/src/core/index.rs @@ -197,7 +197,7 @@ impl Index { self.metas.write().unwrap().clone_from(&loaded_meta); Ok(()) } - + pub fn save_metas(&mut self,) -> Result<()> { let mut w = Vec::new(); { diff --git a/src/lib.rs b/src/lib.rs index da5f4dedb..869c872b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ extern crate num_cpus; extern crate combine; extern crate itertools; + #[cfg(test)] extern crate test; #[cfg(test)] extern crate rand; diff --git a/src/schema/field.rs b/src/schema/field.rs index e4f129120..c4abfbd3b 100644 --- a/src/schema/field.rs +++ b/src/schema/field.rs @@ -15,4 +15,4 @@ impl BinarySerializable for Field { u8::deserialize(reader).map(Field) } } - \ No newline at end of file + diff --git a/src/schema/field_entry.rs b/src/schema/field_entry.rs index 7f11a9d5d..15cc8c76d 100644 --- a/src/schema/field_entry.rs +++ b/src/schema/field_entry.rs @@ -1,6 +1,7 @@ use schema::TextOptions; use schema::U32Options; + #[derive(Clone, Debug, RustcDecodable, RustcEncodable)] pub enum FieldEntry { Text(String, TextOptions), diff --git a/src/schema/mod.rs b/src/schema/mod.rs index 6fc0f74ab..ff64e9446 100644 --- a/src/schema/mod.rs +++ b/src/schema/mod.rs @@ -20,13 +20,21 @@ pub use self::field_entry::FieldEntry; pub use self::field_value::FieldValue; pub use self::text_options::TextOptions; -pub use self::text_options::FAST; pub use self::text_options::TEXT; pub use self::text_options::STRING; pub use self::text_options::STORED; pub use self::text_options::TextIndexingOptions; pub use self::u32_options::U32Options; -pub use self::u32_options::FAST_U32; +pub use self::u32_options::FAST; + +use regex::Regex; + +pub fn is_valid_field_name(field_name: &str) -> bool { + lazy_static! { + static ref FIELD_NAME_PTN: Regex = Regex::new("[_a-zA-Z0-9]+").unwrap(); + } + FIELD_NAME_PTN.is_match(field_name) +} \ No newline at end of file diff --git a/src/schema/text_options.rs b/src/schema/text_options.rs index 67ce47d87..9989aeaa6 100644 --- a/src/schema/text_options.rs +++ b/src/schema/text_options.rs @@ -5,7 +5,6 @@ use std::ops::BitOr; pub struct TextOptions { indexing_options: TextIndexingOptions, stored: bool, - fast: bool, } impl TextOptions { @@ -18,20 +17,11 @@ impl TextOptions { self.stored } - pub fn is_fast(&self,) -> bool { - self.fast - } - pub fn set_stored(mut self,) -> TextOptions { self.stored = true; self } - pub fn set_fast(mut self,) -> TextOptions { - self.fast = true; - self - } - pub fn set_indexing_options(mut self, indexing_options: TextIndexingOptions) -> TextOptions { self.indexing_options = indexing_options; self @@ -39,7 +29,6 @@ impl TextOptions { pub fn new() -> TextOptions { TextOptions { - fast: false, indexing_options: TextIndexingOptions::Unindexed, stored: false, } @@ -115,7 +104,6 @@ impl BitOr for TextIndexingOptions { pub const STRING: TextOptions = TextOptions { indexing_options: TextIndexingOptions::Untokenized, stored: false, - fast: false, }; @@ -123,7 +111,6 @@ pub const STRING: TextOptions = TextOptions { pub const TEXT: TextOptions = TextOptions { indexing_options: TextIndexingOptions::TokenizedWithFreqAndPosition, stored: false, - fast: false, }; /// A stored fields of a document can be retrieved given its DocId. @@ -133,15 +120,6 @@ pub const TEXT: TextOptions = TextOptions { pub const STORED: TextOptions = TextOptions { indexing_options: TextIndexingOptions::Unindexed, stored: true, - fast: false, -}; - -/// Fast field are used for field you need to access many times during -/// collection. (e.g: for sort, aggregates). -pub const FAST: TextOptions = TextOptions { - indexing_options: TextIndexingOptions::Unindexed, - stored: false, - fast: true }; @@ -153,7 +131,6 @@ impl BitOr for TextOptions { let mut res = TextOptions::new(); res.indexing_options = self.indexing_options | other.indexing_options; res.stored = self.stored || other.stored; - res.fast = self.fast || other.fast; res } } @@ -168,16 +145,9 @@ mod tests { #[test] fn test_field_options() { - { - let field_options = STORED | FAST; - assert!(field_options.is_stored()); - assert!(field_options.is_fast()); - assert!(!field_options.get_indexing_options().is_tokenized()); - } { let field_options = STORED | TEXT; assert!(field_options.is_stored()); - assert!(!field_options.is_fast()); assert!(field_options.get_indexing_options().is_tokenized()); } { diff --git a/src/schema/u32_options.rs b/src/schema/u32_options.rs index ac2d3c9f2..a6987c1b7 100644 --- a/src/schema/u32_options.rs +++ b/src/schema/u32_options.rs @@ -22,16 +22,21 @@ impl U32Options { pub fn is_indexed(&self,) -> bool { self.indexed } + + pub fn is_fast(&self,) -> bool { + self.fast + } + + pub fn set_stored(mut self,) -> U32Options { + self.stored = true; + self + } pub fn set_indexed(mut self,) -> U32Options { self.indexed = true; self } - - pub fn is_fast(&self,) -> bool { - self.fast - } - + pub fn set_fast(mut self,) -> U32Options { self.fast = true; self @@ -42,7 +47,7 @@ impl U32Options { /// The field will be tokenized and indexed -pub const FAST_U32: U32Options = U32Options { +pub const FAST: U32Options = U32Options { indexed: false, stored: false, fast: true,