oids.nim 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Nim OID support. An OID is a global ID that consists of a timestamp,
  10. ## a unique counter and a random value. This combination should suffice to
  11. ## produce a globally distributed unique ID.
  12. ##
  13. ## This implementation calls `initRand()` for the first call of
  14. ## `genOid`.
  15. import std/[hashes, times, endians, random]
  16. from std/private/decode_helpers import handleHexChar
  17. when defined(nimPreviewSlimSystem):
  18. import std/sysatomics
  19. type
  20. Oid* = object ## An OID.
  21. time: int64
  22. fuzz: int32
  23. count: int32
  24. proc `==`*(oid1: Oid, oid2: Oid): bool {.inline.} =
  25. ## Compares two OIDs for equality.
  26. result = (oid1.time == oid2.time) and (oid1.fuzz == oid2.fuzz) and
  27. (oid1.count == oid2.count)
  28. proc hash*(oid: Oid): Hash =
  29. ## Generates the hash of an OID for use in hashtables.
  30. var h: Hash = 0
  31. h = h !& hash(oid.time)
  32. h = h !& hash(oid.fuzz)
  33. h = h !& hash(oid.count)
  34. result = !$h
  35. proc hexbyte*(hex: char): int {.inline.} =
  36. result = handleHexChar(hex)
  37. proc parseOid*(str: cstring): Oid =
  38. ## Parses an OID.
  39. result = Oid()
  40. var bytes = cast[cstring](cast[pointer](cast[int](addr(result.time)) + 4))
  41. var i = 0
  42. while i < 12:
  43. bytes[i] = chr((hexbyte(str[2 * i]) shl 4) or hexbyte(str[2 * i + 1]))
  44. inc(i)
  45. proc `$`*(oid: Oid): string =
  46. ## Converts an OID to a string.
  47. const hex = "0123456789abcdef"
  48. result = ""
  49. result.setLen 24
  50. var o = oid
  51. var bytes = cast[cstring](cast[pointer](cast[int](addr(o)) + 4))
  52. var i = 0
  53. while i < 12:
  54. let b = bytes[i].ord
  55. result[2 * i] = hex[(b and 0xF0) shr 4]
  56. result[2 * i + 1] = hex[b and 0xF]
  57. inc(i)
  58. let
  59. t = getTime().toUnix
  60. var
  61. seed = initRand(t)
  62. incr: int = seed.rand(int.high)
  63. let fuzz = cast[int32](seed.rand(high(int)))
  64. template genOid(result: var Oid, incr: var int, fuzz: int32) =
  65. var time = getTime().toUnix
  66. var i = cast[int32](atomicInc(incr))
  67. bigEndian64(addr result.time, addr(time))
  68. result.fuzz = fuzz
  69. bigEndian32(addr result.count, addr(i))
  70. proc genOid*(): Oid =
  71. ## Generates a new OID.
  72. runnableExamples:
  73. doAssert ($genOid()).len == 24
  74. runnableExamples("-r:off"):
  75. echo $genOid() # for example, "5fc7f546ddbbc84800006aaf"
  76. result = Oid()
  77. genOid(result, incr, fuzz)
  78. proc generatedTime*(oid: Oid): Time =
  79. ## Returns the generated timestamp of the OID.
  80. var tmp: int64 = int64(0)
  81. var dummy = oid.time
  82. bigEndian64(addr(tmp), addr(dummy))
  83. result = fromUnix(tmp)