hash.pyx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from libc.stdint cimport uint64_t
  2. from libc.string cimport memcpy
  3. from lakesuperior.model.base cimport Buffer
  4. memcpy(&term_hash_seed32, TERM_HASH_SEED, HLEN_32)
  5. memcpy(&term_hash_seed64_1, TERM_HASH_SEED, HLEN_64)
  6. memcpy(&term_hash_seed64_2, TERM_HASH_SEED + HLEN_64, HLEN_64)
  7. # We only need a few basic functions from spookyhash. No need for a pxd file.
  8. cdef extern from 'spookyhash_api.h':
  9. uint32_t spookyhash_32(const void *input, size_t input_size, uint32_t seed)
  10. uint64_t spookyhash_64(const void *input, size_t input_size, uint64_t seed)
  11. void spookyhash_128(
  12. const void *input, size_t input_size, uint64_t *hash_1,
  13. uint64_t *hash_2)
  14. cdef inline int hash32(const Buffer *message, Hash32 *hash) except -1:
  15. """
  16. Get a 32-bit (unsigned int) hash value of a byte string.
  17. """
  18. cdef uint32_t seed = term_hash_seed64_1
  19. hash[0] = spookyhash_32(message[0].addr, message[0].sz, seed)
  20. cdef inline int hash64(const Buffer *message, Hash64 *hash) except -1:
  21. """
  22. Get a 64-bit (unsigned long) hash value of a byte string.
  23. """
  24. cdef uint64_t seed = term_hash_seed32
  25. hash[0] = spookyhash_64(message[0].addr, message[0].sz, seed)
  26. cdef inline int hash128(const Buffer *message, Hash128 *hash) except -1:
  27. """
  28. Get the hash value of a byte string with a defined size.
  29. The hashing algorithm is `SpookyHash
  30. <http://burtleburtle.net/bob/hash/spooky.html>`_ which produces 128-bit
  31. (16-byte) digests.
  32. Note that this returns a char array while the smaller functions return
  33. numeric types (uint, ulong).
  34. The initial seeds are determined in the application configuration.
  35. :rtype: Hash128
  36. """
  37. cdef:
  38. DoubleHash64 seed = [term_hash_seed64_1, term_hash_seed64_2]
  39. Hash128 digest
  40. spookyhash_128(message[0].addr, message[0].sz, seed, seed + 1)
  41. # This casts the 2 contiguous uint64_t's into a char[16] pointer.
  42. hash[0] = <Hash128>seed