1
0
mirror of synced 2026-02-24 22:07:56 +08:00

auto commit

This commit is contained in:
yitter
2021-04-01 02:38:11 +08:00
parent 43417f41cc
commit 0566c98e3e
48 changed files with 1294 additions and 109 deletions

View File

@@ -3,6 +3,23 @@ mod yitgen;
use yitgen::gen::YitIdHelper;
use yitgen::contract::*;
#[macro_use]
extern crate lazy_static;
extern crate redis;
extern crate libc;
use redis::Commands;
use libc::{c_char, uint32_t};
use std::ffi::{CStr, CString};
use std::str;
lazy_static! {
//static ref TestValue: Vec<i32> = vec!(0);
// static ref MAP: HashMap<u32, String> = HashMap::new();
}
// #[export_name = "SetIdGenerator"]
#[no_mangle]
pub extern "C" fn SetIdGenerator(options: IdGeneratorOptions) {
@@ -19,3 +36,64 @@ pub extern "C" fn NextId() -> i64 {
YitIdHelper::NextId()
}
static mut TestValue: i32 = 0;
#[no_mangle]
pub extern "C" fn Test() -> i32 {
unsafe {
TestValue += 1;
return TestValue;
}
}
#[no_mangle]
pub extern "C" fn GetWorkerId(ip: *const c_char, port: i32) -> redis::RedisResult<isize> {
// let c_str = unsafe {
// assert!(!ip.is_null());
// CStr::from_ptr(ip)
// };
//
// let r_str = c_str.to_str();
// connect to redis
// let client = redis::Client::open(format!("redis://{}:{}/", String::from(r_str).to_string(), port))?;
let client = redis::Client::open(format!("redis://localhost:{}/", port))?;
let mut con = client.get_connection()?;
// throw away the result, just make sure it does not fail
unsafe {
let _: () = con.set("my_key111", TestValue.clone())?;
}
con.get("my_key")
// read back the key and return it. Because the return value
// from the function is a result for integer this will automatically
// convert into one.
//
// match simple_redis::create(&format!("redis://{}:{}/", ip, port)) {
// Ok(mut client) => {
// println!("Created Redis Client");
//
// let valueString = TestValue.to_string();
// let valueString2 = (*TestValue).to_string();
//
// match client.set("my_key", valueString) {
// Err(error) => println!("Unable to set value in Redis: {}", error),
// _ => println!("Value set in Redis")
// };
//
// match client.set("my_key2", valueString2) {
// Err(error) => println!("Unable to set value in Redis: {}", error),
// _ => println!("Value set in Redis")
// };
//
// match client.quit() {
// Err(error) => println!("Error: {}", error),
// _ => println!("Connection Closed.")
// }
// }
// Err(error) => println!("Unable to create Redis client: {}", error)
// }
//return 1;
}

View File

@@ -6,6 +6,7 @@ use std::thread;
use chrono::Utc;
use std::time::Duration;
fn main() {
println!("Hello, world! Rust");
@@ -22,6 +23,8 @@ fn main() {
//... 可以继续设置其它 options 参数
YitIdHelper::SetIdGenerator(options);
set_redis();
// 以下开始测试生成数据默认5W单线程可以修改 multiThread=true 启用多线程。
loop {
let mut i = 0;
@@ -51,3 +54,24 @@ fn main() {
}
}
fn set_redis() {
// match simple_redis::create("redis://127.0.0.1:6379/") {
// Ok(mut client) => {
// println!("Created Redis Client");
//
// match client.set("my_key", "my_value") {
// Err(error) => println!("Unable to set value in Redis: {}", error),
// _ => println!("Value set in Redis")
// };
//
// match client.quit() {
// Err(error) => println!("Error: {}", error),
// _ => println!("Connection Closed.")
// }
// },
// Err(error) => println!("Unable to create Redis client: {}", error)
// }
}

View File

@@ -6,7 +6,7 @@ use super::super::contract::*;
use std::{thread};
use chrono::Utc;
use std::thread::sleep;
use lazy_static::lazy_static;
// use lazy_static::lazy_static;
pub struct SnowWorkerM1 {
///基础时间

View File

@@ -12,7 +12,7 @@ use std::sync::Mutex;
use std::sync::Arc;
use std::borrow::BorrowMut;
static mut instance2: Option<Arc<Mutex<SnowWorkerM1>>> = None;
// static mut instance2: Option<Arc<Mutex<SnowWorkerM1>>> = None;
pub struct DefaultIdGenerator {
pub Worker: SnowWorkerM1,