You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lib.rs 2.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. mod yitgen;
  2. use yitgen::gen::YitIdHelper;
  3. use yitgen::contract::*;
  4. #[macro_use]
  5. extern crate lazy_static;
  6. extern crate redis;
  7. extern crate libc;
  8. use redis::Commands;
  9. use libc::{c_char, uint32_t};
  10. use std::ffi::{CStr, CString};
  11. use std::str;
  12. lazy_static! {
  13. //static ref TestValue: Vec<i32> = vec!(0);
  14. // static ref MAP: HashMap<u32, String> = HashMap::new();
  15. }
  16. // #[export_name = "SetIdGenerator"]
  17. #[no_mangle]
  18. pub extern "C" fn SetIdGenerator(options: IdGeneratorOptions) {
  19. YitIdHelper::SetIdGenerator(options);
  20. }
  21. #[no_mangle]
  22. pub extern "C" fn SetWorkerId(workerId: u32) {
  23. YitIdHelper::SetWorkerId(workerId);
  24. }
  25. #[no_mangle]
  26. pub extern "C" fn NextId() -> i64 {
  27. YitIdHelper::NextId()
  28. }
  29. static mut TestValue: i32 = 0;
  30. #[no_mangle]
  31. pub extern "C" fn Test() -> i32 {
  32. unsafe {
  33. TestValue += 1;
  34. return TestValue;
  35. }
  36. }
  37. #[no_mangle]
  38. pub extern "C" fn GetWorkerId(ip: *const c_char, port: i32) -> redis::RedisResult<isize> {
  39. // let c_str = unsafe {
  40. // assert!(!ip.is_null());
  41. // CStr::from_ptr(ip)
  42. // };
  43. //
  44. // let r_str = c_str.to_str();
  45. // connect to redis
  46. // let client = redis::Client::open(format!("redis://{}:{}/", String::from(r_str).to_string(), port))?;
  47. let client = redis::Client::open(format!("redis://localhost:{}/", port))?;
  48. let mut con = client.get_connection()?;
  49. // throw away the result, just make sure it does not fail
  50. unsafe {
  51. let _: () = con.set("my_key111", TestValue.clone())?;
  52. }
  53. con.get("my_key")
  54. // read back the key and return it. Because the return value
  55. // from the function is a result for integer this will automatically
  56. // convert into one.
  57. //
  58. // match simple_redis::create(&format!("redis://{}:{}/", ip, port)) {
  59. // Ok(mut client) => {
  60. // println!("Created Redis Client");
  61. //
  62. // let valueString = TestValue.to_string();
  63. // let valueString2 = (*TestValue).to_string();
  64. //
  65. // match client.set("my_key", valueString) {
  66. // Err(error) => println!("Unable to set value in Redis: {}", error),
  67. // _ => println!("Value set in Redis")
  68. // };
  69. //
  70. // match client.set("my_key2", valueString2) {
  71. // Err(error) => println!("Unable to set value in Redis: {}", error),
  72. // _ => println!("Value set in Redis")
  73. // };
  74. //
  75. // match client.quit() {
  76. // Err(error) => println!("Error: {}", error),
  77. // _ => println!("Connection Closed.")
  78. // }
  79. // }
  80. // Err(error) => println!("Unable to create Redis client: {}", error)
  81. // }
  82. //return 1;
  83. }

雪花算法中非常好用的数字ID生成器