#string-conversion #uuid #snowflake #id2str

UUID_extend

Extension of uuid generation based on snowflake algorithm and implementation of ID to string conversion

5 releases

0.1.7 Sep 27, 2023
0.1.5 Aug 22, 2023
0.1.0 May 11, 2023

#498 in Algorithms

46 downloads per month

BSD-3-Clause

13KB
120 lines

简介

生成全局UUID.扩展的雪花算法。可自定义存储u32数字,使用decode可解码出自定义数字

注意:使用大端存储

适用类型

  1. 作为UUID生成器,自定义数字可使用 机房+服务器等作为编码

  2. 作为ID转换器,自定义数字为数据库ID

INT返回

若返回int型 UUID::uuid(u32):会返回高位u64 和低位u32 俩个数字.

高位存储时间戳和随机数,低位存储加密的自定义数字.

PHP版本 new UUID()->uuid(int)返回的是两个pack大端编码的字节数组

String 返回

返回String类型,会将高低位合并,然后使用 NO_PAD 和 URL安全的base64编码

参考 雪花算法

Rust Bench

Encode: encode      time:   [min:355.38 ns avg:358.24 ns max:361.84 ns]

Decode: decode      time:   [min:214.15 ns avg:217.19 ns max:220.71 ns]

Example

Encode Decode String

     for i in 0..10 {
         let encode = UUID::uuid_string(i);
         let decode = UUID::decode(encode.clone());
         // println!("{}", encode,);
         assert_eq!(decode, i, "测试失败 with decode:{} and i:{}", decode, i);
     }

Encode Decode Int

 for i in 0..100 {
     let encode = UUID::uuid(i);
     let decode = UUID::decode_int(encode);
     // println!("{}", encode);
     assert_eq!(decode, i, "测试失败 with decode:{} and i:{}", decode, i);
 }

decode_full

let (custom,secs,mils,rand) = UUID::decode_int(encode);

PHP 版本 。源码:php/lib.php

查看https://github.com/xyanyue/UUID_extend

 $uuid = new UUID();
 for ($i = 0; $i < 100; $i++) {
     $encode = $uuid->uuid_string($i);
     $de = $uuid->decode($encode);
     echo $i, "-", $encode, "-", $de, "\n";
 }

样例

自定义数字:23424 - uuid:ZFyqoU5IAgUCBRXI - 解码:23424
自定义数字:23425 - uuid:ZFyqoQPtAgUCBVhs - 解码:23425
自定义数字:23426 - uuid:ZFyqoSmEAgUCBXIG - 解码:23426
自定义数字:23427 - uuid:ZFyqoaNaAgUCBfjZ - 解码:23427
自定义数字:23428 - uuid:ZFyqoSkXAgUCBXKT - 解码:23428
自定义数字:23429 - uuid:ZFyqoZE7AgUCBcq- - 解码:23429
自定义数字:23430 - uuid:ZFyqoQvBAgUCBVBH - 解码:23430
自定义数字:23431 - uuid:ZFyqoWegAgYCBjwn - 解码:23431
自定义数字:23432 - uuid:ZFyqobRPAgYCBu_H - 解码:23432
自定义数字:23433 - uuid:ZFyqofOBAgYCBqgI - 解码:23433
自定义数字:23434 - uuid:ZFyqof2IAgYCBqYC - 解码:23434
自定义数字:23435 - uuid:ZFyqoVEfAgYCBgqU - 解码:23435

Dependencies

~530KB