Last active
November 17, 2021 19:51
-
-
Save chertov/ffaf35ceadbf6a90bb1c86f5a5b7abf9 to your computer and use it in GitHub Desktop.
rust schema codegen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use tarantool::index::{ IndexType, IndexOptions, IndexPart, IndexFieldType, IteratorType }; | |
use tarantool::space::{ Space, SpaceCreateOptions, SpaceFieldFormat, SpaceFieldType }; | |
use tarantool::tuple::{ AsTuple, Tuple }; | |
pub const SPACE_NAME: &str = "pubsub"; | |
const INDEX__ID: &str = "id"; | |
const INDEX__TO: &str = "to"; | |
const INDEX__CRATED_AT: &str = "crated_at"; | |
const FIELD__ID: &str = "id"; | |
const FIELD__TO: &str = "to"; | |
const FIELD__MSG: &str = "msg"; | |
const FIELD__CRATED_AT: &str = "crated_at"; | |
pub const FIELD__ID__ID: u32 = 0; | |
pub const FIELD__TO__ID: u32 = 1; | |
pub const FIELD__MSG__ID: u32 = 2; | |
pub const FIELD__CRATED_AT__ID: u32 = 3; | |
pub fn space() -> Result<tarantool::space::Space, anyhow::Error> { SPACE.read().space() } | |
pub fn id_index() -> Result<tarantool::index::Index, anyhow::Error> { space()?.index(INDEX__ID).ok_or(anyhow!("Can't find space '{}' index '{}'", SPACE_NAME, INDEX__ID)) } | |
pub fn to_index() -> Result<tarantool::index::Index, anyhow::Error> { space()?.index(INDEX__TO).ok_or(anyhow!("Can't find space '{}' index '{}'", SPACE_NAME, INDEX__TO)) } | |
pub fn crated_at_index() -> Result<tarantool::index::Index, anyhow::Error> { space()?.index(INDEX__CRATED_AT).ok_or(anyhow!("Can't find space '{}' index '{}'", SPACE_NAME, INDEX__CRATED_AT)) } | |
pub fn create() -> Result<(), anyhow::Error> { SPACE.read().create() } | |
pub fn verify() -> Result<(), anyhow::Error> { SPACE.read().verify() } | |
pub fn drop() -> Result<(), anyhow::Error> { tarantool_schema::Space::drop(&mut SPACE.read()) } | |
pub fn truncate() -> Result<(), anyhow::Error> { SPACE.read().truncate() } | |
static SPACE: once_cell::sync::Lazy<parking_lot::RwLock<tarantool_schema::Space>> = once_cell::sync::Lazy::new(|| { | |
let mut format = vec![]; | |
format.push(tarantool_schema::Field { | |
name: FIELD__ID.to_string(), | |
field_type: SpaceFieldType::Unsigned, | |
is_nullable: Some(false), | |
}); | |
format.push(tarantool_schema::Field { | |
name: FIELD__TO.to_string(), | |
field_type: SpaceFieldType::String, | |
is_nullable: Some(false), | |
}); | |
format.push(tarantool_schema::Field { | |
name: FIELD__MSG.to_string(), | |
field_type: SpaceFieldType::String, | |
is_nullable: Some(false), | |
}); | |
format.push(tarantool_schema::Field { | |
name: FIELD__CRATED_AT.to_string(), | |
field_type: SpaceFieldType::Unsigned, | |
is_nullable: Some(false), | |
}); | |
let mut indexes = vec![]; | |
indexes.push(tarantool_schema::Index { | |
name: INDEX__ID.to_string(), | |
unique: true, | |
index_type: IndexType::Tree, | |
parts: { | |
let mut parts = vec![]; | |
parts.push(tarantool_schema::IndexPart{ | |
path: FIELD__ID.to_string(), | |
is_nullable: Some(false), | |
index_field_type: IndexFieldType::Unsigned, | |
part: IndexPart{ | |
field_index: 1, | |
field_type: IndexFieldType::Unsigned, | |
collation: None, | |
is_nullable: Some(false), | |
path: None, | |
}, | |
}); | |
parts | |
}, | |
}); | |
indexes.push(tarantool_schema::Index { | |
name: INDEX__TO.to_string(), | |
unique: false, | |
index_type: IndexType::Tree, | |
parts: { | |
let mut parts = vec![]; | |
parts.push(tarantool_schema::IndexPart{ | |
path: FIELD__TO.to_string(), | |
is_nullable: Some(false), | |
index_field_type: IndexFieldType::String, | |
part: IndexPart{ | |
field_index: 2, | |
field_type: IndexFieldType::String, | |
collation: None, | |
is_nullable: Some(false), | |
path: None, | |
}, | |
}); | |
parts | |
}, | |
}); | |
indexes.push(tarantool_schema::Index { | |
name: INDEX__CRATED_AT.to_string(), | |
unique: false, | |
index_type: IndexType::Tree, | |
parts: { | |
let mut parts = vec![]; | |
parts.push(tarantool_schema::IndexPart{ | |
path: FIELD__CRATED_AT.to_string(), | |
is_nullable: Some(false), | |
index_field_type: IndexFieldType::Unsigned, | |
part: IndexPart{ | |
field_index: 4, | |
field_type: IndexFieldType::Unsigned, | |
collation: None, | |
is_nullable: Some(false), | |
path: None, | |
}, | |
}); | |
parts | |
}, | |
}); | |
let space = tarantool_schema::Space { | |
name: SPACE_NAME.to_string(), | |
engine: tarantool::space::SpaceEngineType::Memtx, | |
is_local: false, | |
temporary: false, | |
format, | |
indexes, | |
}; | |
parking_lot::RwLock::new(space) | |
}); | |
#[derive(Debug, Clone, Serialize, Deserialize)] | |
pub struct Row { | |
id: u64, | |
to: String, | |
msg: String, | |
crated_at: u64, | |
} | |
impl tarantool::tuple::AsTuple for Row {} | |
pub fn init_data() -> Result<(), anyhow::Error> { | |
Ok(()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
spaces: | |
pubsub: | |
engine: 'memtx' | |
is_local: false | |
temporary: false | |
format: [ | |
{ name: 'id', is_nullable: false, type: 'unsigned' }, | |
{ name: 'to', is_nullable: false, type: 'string' }, | |
{ name: 'msg', is_nullable: false, type: 'string' }, | |
{ name: 'crated_at', is_nullable: false, type: 'unsigned' }, | |
] | |
indexes: | |
id: { type: 'TREE', unique: true, parts: [ { field: 'id', is_nullable: false, type: 'unsigned' } ] } | |
to: { type: 'TREE', unique: false, parts: [ { field: 'to', is_nullable: false, type: 'string' } ] } | |
crated_at: { type: 'TREE', unique: false, parts: [ { field: 'crated_at', is_nullable: false, type: 'unsigned' } ] } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment