UUID
ã¡ãã£ã¨è¦ã¦ãã®ã§ã¾ã¨ãã
- Universally Unique Identifier - Wikipedia
- RFC 4122 (rfc4122) - A Universally Unique IDentifier (UUID) URN Namespace
UUID ã¨ã¯ãUniversally Unique Identifier ã®ç¥ã§ãåæ£ã·ã¹ãã ä¸ã§ã©ãããçµ±å¶ãåããã¨ããä¸æã«ç¹å®å¯è½ãªèå¥åãå
¨é¨ã§ 16 bytes (128 bits) ã®æ°åã§è¡¨ãããABNF ã¯ä»¥ä¸ã®ã¨ããã
UUID = time-low "-" time-mid "-" time-high-and-version "-" clock-seq-and-reserved clock-seq-low "-" node time-low = 4hexOctet time-mid = 2hexOctet time-high-and-version = 2hexOctet clock-seq-and-reserved = hexOctet clock-seq-low = hexOctet node = 6hexOctet hexOctet = hexDigit hexDigit hexDigit = "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" / "a" / "b" / "c" / "d" / "e" / "f" / "A" / "B" / "C" / "D" / "E" / "F"
RFC 4122 ã®ãªãã§ã¯ãUUID ã®çæã«é¢ãã¦ããã¼ã¸ã§ã³çªå·1ã3ã4ã5 ã§èå¥ããã4ã¤ã®ã¢ã«ã´ãªãºã ãè¦å®ã
- Version1
- MACã¢ãã¬ã¹ã¨ã¿ã¤ã ã¹ã¿ã³ããå«ã¾ããã®ã§ãã³ã³ãã¥ã¼ã¿ã®èå¥ãçææå»ã®ç¹å®ãå¯è½ããã種ã®ã»ãã¥ãªãã£ã«æ³¨æãã¹ãã¢ããªã±ã¼ã·ã§ã³ã§ã¯é©ããªãã
- Version3
- ä¸ããããåå空éããã¨ã«ä½æãããã·ã¥æ¹å¼ã« MD5 ã使ç¨ã
- Version4
- ã©ã³ãã ãªæ°ããçæã
- Version5
- Version3 ã¨ä¼¼ã¦ããããããã·ã¥æ¹å¼ã« SHA-1 ã使ç¨ããã®åå®å ¨ã
ã§ãRuby ã§ã®å®è£
ãã¿ã¦ã¿ãã
wikipedia:en:uuid ã«ããã¨ã以ä¸ã®3ã¤ã
- Ruby-UUID: ææ°ãã¼ã¸ã§ã³ã¯ 2007.12.30 ã« CodeRepos ã«ç§»è¡ãã 0.1
- UUID: ææ°ãã¼ã¸ã§ã³ã¯ 2008.8.27 å ¬éã® 2.0.1
- UUIDTools: ææ°ãã¼ã¸ã§ã³ã¯ 2008.12.4 å ¬éã® 1.0.7
Rubyforge ã§æ¢ã㨠uuid4r ã guid ã¨ããè¦ã¤ããã
- Guid: Guid is a Ruby library for portable GUID/UUID generation. At the moment it can be used on Windows (except first release of Win95 and older) and on Unix.
- UUID4R: This is a wrapper of OSSP uuid C library, and can generate and parse UUID.
ã¡ã³ããã³ã¹ç¶æ³ã¨ããã¿ãã¨ãUUID 㨠UUIDTools ãã¢ã¯ãã£ãã£ã½ãã
ããã«ã½ã¼ã¹ãã¿ã¦ã¿ãã
uuidtools-1.0.7 ã¯ãå®ç´ã«ãã¼ã¸ã§ã³1ã3ã4ã5 ãå®è£
ãã¦ããã
使ãæ¹ã¯ããããããããªæãã§ãto_s ãã㨠16 bytes ã®æååãå¾ãããã
UUID.timestamp_create => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8> UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com") => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29> UUID.random_create => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159> UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com") => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
ä¸æ¹ã® uuid-2.0.1 ã¯ããã¼ã¸ã§ã³1 ã®ã¿ã®å®è£
ãããæå³ãç·åã
uuid = UUID.new 10.times do p uuid.generate end
ã¡ãªã¿ã«ãåã®ãã¼ã¸ã§ã³ã§ãããuuid-1.0.4 ã®ä½¿ãæ¹ã¯å¾®å¦ã«éã£ã¦ããã :(
10.times do p UUID.new end
æ©è½ã®å·®ã¯æ´ç¶ãªã®ã§ããã³ããã¼ã¯ãã¨ã£ã¦ã¿ããæ¯è¼å¯¾è±¡ã¯ããããããã¼ã¸ã§ã³1 ã®ã¢ã«ã´ãªãºã ã§ãUUID ã1ä¸åçæãã¦ã¿ãã
% gem list --local uuid *** LOCAL GEMS *** uuid (2.0.1, 1.0.4) uuidtools (1.0.7) % ruby186 uuid_bench.rb uuidtools-1.0.7 10000 benchmark target: uuidtools-1.0.7 user system total real UUID.timestamp_create.to_s: 0.720000 0.010000 0.730000 ( 0.775910) % ruby186 uuid_bench.rb uuid-1.0.4 10000 benchmark target: uuid-1.0.4 user system total real UUID.new: 0.150000 0.000000 0.150000 ( 0.156139) % ruby186 uuid_bench.rb uuid-2.0.1 10000 benchmark target: uuid-2.0.1 user system total real @uuid.generate: 0.140000 0.010000 0.150000 ( 0.150422) UUID.generate: 0.160000 0.000000 0.160000 ( 0.164215) UUID.new.generate: 1.030000 1.260000 2.290000 ( 13.347787)
API 㯠(æ©è½ãå°ãªãå) ããããããã®ã§ãã»ãã¥ãªãã£ã®åé¡ããªããã°ãuuid-x.x.x ã§ååãã?
# æå¾ã® UUID.new.generate ã¯ãµã¤ããããªãã¨æããã©ãä¸å¿ãã£ã¦ã¿ãã :-p
以ä¸ããã³ããã¼ã¯ã«ä½¿ç¨ããããã°ã©ã ã
require 'benchmark' require 'rubygems' puts "Usage: ruby uuid_bench.rb target_uuid repeat_count" if ARGV.size < 2 name, version = ARGV[0].split("-") count = ARGV[1].to_i # switch version gem "#{name}", "= #{version}" require "#{name}" puts "benchmark target: #{name}-#{version}" def iterate(count, &block) count.times do yield block end end # main Benchmark.bm(30) do |x| case name when "uuid" uuid = UUID.new if uuid.kind_of? String # uuid-1.x.x x.report("UUID.new:") {iterate(count) {UUID.new}} elsif uuid.kind_of? UUID # uuid-2.x.x x.report("@uuid.generate:") {iterate(count) {uuid.generate}} x.report("UUID.generate:") {iterate(count) {UUID.generate}} x.report('UUID.new.generate:') {iterate(count) {UUID.new.generate}} end when "uuidtools" x.report('UUID.timestamp_create.to_s:') {iterate(count) {UUID.timestamp_create.to_s}} end end