snowid.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. *
  3. * MIT License
  4. *
  5. * Copyright (c) 2022 beyonddream
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #ifndef __SNOWID_H__
  23. #define __SNOWID_H__
  24. #include <stdbool.h>
  25. #include <stdint.h>
  26. #include <stdio.h>
  27. typedef struct snow_config {
  28. char *interface; /* network interface name */
  29. char *timestamp_path; /* file location to save current time periodically */
  30. uint64_t allowable_downtime; /* time since snowid is called last - default 0 */
  31. } snow_config_t;
  32. /* public type that represents a snowid at any given time */
  33. struct snow_id {
  34. uint64_t timestamp;
  35. uint64_t worker_id;
  36. uint16_t sequence_id;
  37. };
  38. typedef struct snow_id snow_id_t;
  39. typedef unsigned char snow_id_binary_t[16];
  40. /**
  41. * Generates unique 128-bit id from current timestamp,worker_id,sequence_id.
  42. * sequence_id is incremented as many times as the function is called within the
  43. * same timestamp.
  44. *
  45. * @warning Not multi-thread safe
  46. * @param snowid - If able to generate an id, set the value of id to `snow_id`.
  47. * @return bool - true if successfully able to generate an id, false if not.
  48. */
  49. bool snow_get_id(snow_id_t *snowid);
  50. /**
  51. * Dump snow state and config to stdout for debugging.
  52. *
  53. * @warning Not multi-thread safe
  54. * @param stream - any file descriptor (stdout if NULL)
  55. * @return void
  56. */
  57. void snow_dump(FILE *stream);
  58. /**
  59. * Initializes the snowid engine with the config. It has to be called only once before
  60. * calling any other API functions.
  61. *
  62. * @warning Not multi-thread safe
  63. * @param config - The snowid configuration
  64. * @return void
  65. */
  66. void snow_init(snow_config_t *config);
  67. /**
  68. * Deinitializes the snowid engine. It has to be called only once at the end
  69. * and no other API functions should be called after it except snow_init().
  70. *
  71. * @warning Not multi-thread safe
  72. * @param void
  73. * @return void
  74. */
  75. void snow_shutdown(void);
  76. /**
  77. * Generate a new snow_id as binary - unsigned char[16] representing 128 bits.
  78. * Represented as:
  79. * <unix timestamp (64 bits) + MAC address (48 bits) + sequence number (16 bits)>
  80. *
  81. * @param snowid_as_bin - set to snowid as binary
  82. * @return bool - true if success, else false.
  83. */
  84. bool snow_get_id_as_binary(snow_id_binary_t snowid_as_bin);
  85. /**
  86. * Convert an already generated snow_id as binary (represented as byte array).
  87. *
  88. * @param snowid_as_bin - set the converted snowid as binary
  89. * @param snowid - pass in already generated snowid
  90. * @return bool - true if success, else false.
  91. */
  92. bool snow_id_convert(snow_id_binary_t snowid_as_bin, const snow_id_t *snowid);
  93. #endif /* __SNOWID_H__ */