oids.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. var bytes = cast[cstring](cast[pointer](cast[int](addr(result.time)) + 4))
  40. var i = 0
  41. while i < 12:
  42. bytes[i] = chr((hexbyte(str[2 * i]) shl 4) or hexbyte(str[2 * i + 1]))
  43. inc(i)
  44. proc `$`*(oid: Oid): string =
  45. ## Converts an OID to a string.
  46. const hex = "0123456789abcdef"
  47. result.setLen 24
  48. var o = oid
  49. var bytes = cast[cstring](cast[pointer](cast[int](addr(o)) + 4))
  50. var i = 0
  51. while i < 12:
  52. let b = bytes[i].ord
  53. result[2 * i] = hex[(b and 0xF0) shr 4]
  54. result[2 * i + 1] = hex[b and 0xF]
  55. inc(i)
  56. let
  57. t = getTime().toUnix
  58. var
  59. seed = initRand(t)
  60. incr: int = seed.rand(int.high)
  61. let fuzz = cast[int32](seed.rand(high(int)))
  62. template genOid(result: var Oid, incr: var int, fuzz: int32) =
  63. var time = getTime().toUnix
  64. var i = cast[int32](atomicInc(incr))
  65. bigEndian64(addr result.time, addr(time))
  66. result.fuzz = fuzz
  67. bigEndian32(addr result.count, addr(i))
  68. proc genOid*(): Oid =
  69. ## Generates a new OID.
  70. runnableExamples:
  71. doAssert ($genOid()).len == 24
  72. runnableExamples("-r:off"):
  73. echo $genOid() # for example, "5fc7f546ddbbc84800006aaf"
  74. genOid(result, incr, fuzz)
  75. proc generatedTime*(oid: Oid): Time =
  76. ## Returns the generated timestamp of the OID.
  77. var tmp: int64
  78. var dummy = oid.time
  79. bigEndian64(addr(tmp), addr(dummy))
  80. result = fromUnix(tmp)