Generates Twitter-like Snowflake ids in Java - GitHub

文章推薦指數: 80 %
投票人數:10人

Generates Twitter-like Snowflake ids. In short, this is an id scheme to generate unique 64 bit ids which are roughly sortable across ... Skiptocontent {{message}} phxql / snowflake-id Public Notifications Fork 1 Star 13 GeneratesTwitter-likeSnowflakeidsinJava License LGPL-3.0license 13 stars 1 fork Star Notifications Code Issues 0 Pullrequests 0 Actions Projects 0 Wiki Security Insights More Code Issues Pullrequests Actions Projects Wiki Security Insights Thiscommitdoesnotbelongtoanybranchonthisrepository,andmaybelongtoaforkoutsideoftherepository. main Branches Tags Couldnotloadbranches Nothingtoshow {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default 1 branch 1 tag Code Latestcommit   Gitstats 21 commits Files Permalink Failedtoloadlatestcommitinformation. Type Name Latestcommitmessage Committime .mvn/wrapper     src     .editorconfig     .gitignore     CHANGELOG.md     LICENSE     README.md     mvnw     mvnw.cmd     pom.xml     Viewcode SnowflakeId Howtouse Maven Simpleexample Moreconfigurationoptions Calculatemaximumtimestamps,generators,sequenceidsandwraparounddates Defaultsettings Changelog? License README.md SnowflakeId GeneratesTwitter-likeSnowflakeids. Inshort,thisisanidschemetogenerateunique64bitidswhichareroughlysortableacrossmultiplesystemswithout acentralinstance.Seethisblogpostformoredetails. ThisprojectwasheavilyinspiredbyIdGenforC#,there'sagreatHowitworksinthereadme,too. ThealgorithmisimplementedinplainJavawithoutanydependencies.AllyouneedisatleastJava11. Suchgeneratedidinbinarylookslikethis(thisis4425020822061056indecimal): 0000000000001111101110001000100001110010001110010000000000000000 |||| ||Timestamp(16880114830)||Sequence(0) || |Signbit(always0)|Generatorid(1) Thestructureusedforthisis45bitsforthetimestamp,2forthegeneratorandtheremaining16forthesequence.Thisstructurecanbechangedeasily,seebelowforthecode. Theidsreallyuseonly63bitsofthe64availabletocircumventproblemswithunsignedlongs.Thegeneratedvaluesarealwayspositive. Idsfromthesamegeneratoraremonotonicallyincreasing. Howtouse Maven de.mkammerer.snowflake-id snowflake-id 0.0.1 Simpleexample //generatorIdmustbeuniqueoverallyourinstances! intgeneratorId=0; //usedefaultgeneratorsettings SnowflakeIdGeneratorgenerator=SnowflakeIdGenerator.createDefault(generatorId); //generate10ids for(inti=0;i<10;i++){ longid=generator.next(); System.out.println(id); } Moreconfigurationoptions //Useacustomepoch TimeSourcetimeSource=newMonotonicTimeSource(Instant.parse("2020-04-01T00:00:00Z")); //Use45bitsforthetimestamp,2bitsforthegeneratorand16bitsforthesequence Structurestructure=newStructure(45,2,16); //Ifthesequencenumberoverflows,throwanexception Optionsoptions=newOptions(Options.SequenceOverflowStrategy.THROW_EXCEPTION); //generatorIdmustbeuniqueoverallyourinstances! longgeneratorId=1; SnowflakeIdGeneratorgenerator=SnowflakeIdGenerator.createCustom(generatorId,timeSource,structure,options); //generate10ids for(inti=0;i<10;i++){ longid=generator.next(); System.out.println(id); } Calculatemaximumtimestamps,generators,sequenceidsandwraparounddates YoucanquerytheStructureclasstofindoutthemaximumnumbersoftimestamps,generators,sequenceidsandwraparounddates: TimeSourcetimeSource=newMonotonicTimeSource(Instant.parse("2020-04-01T00:00:00Z")); Structurestructure=newStructure(45,2,16); System.out.println("Maxgenerators:"+structure.maxGenerators()); System.out.println("Maxsequencespermspergenerator:"+structure.maxSequenceIds()); System.out.println("Maxsequencespermstotal:"+structure.maxSequenceIds()*structure.maxGenerators()); System.out.println("Wraparoundduration:"+structure.calculateWraparoundDuration(timeSource)); System.out.println("Wraparounddate:"+structure.calculateWraparoundDate(timeSource)); Thisprints: Maxgenerators:4 Maxsequencespermspergenerator:65536 Maxsequencespermstotal:262144 Wraparoundduration:PT9773436H41M28.832S Wraparounddate:3135-03-14T12:41:28.832Z Defaultsettings ThedefaultsettingsfortheStructureare41bitsforthetimestamp,10forthegeneratoridand12forthesequence. Thiswillleadtothefollowingproperties: Maxgenerators:1024 Maxsequencespermspergenerator:4096 Maxsequencespermstotal:4194304 Wraparoundduration:PT610839H47M35.552S Wraparounddate:2089-09-06T15:47:35.552Z Changelog? Seehere. License LGPLv3 About GeneratesTwitter-likeSnowflakeidsinJava Resources Readme License LGPL-3.0license Stars 13 stars Watchers 2 watching Forks 1 fork Releases 1 tags Packages0 Nopackagespublished Usedby5 Languages Java 100.0% Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.



請為這篇文章評分?