You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.0 KiB

  1. package arangodb
  2. import (
  3. "crypto/md5"
  4. "encoding/binary"
  5. "encoding/hex"
  6. "io"
  7. )
  8. //convert a string into arango-key safe hex bytes hash
  9. func hashString(dir string) string {
  10. h := md5.New()
  11. io.WriteString(h, dir)
  12. b := h.Sum(nil)
  13. return hex.EncodeToString(b)
  14. }
  15. // convert slice of bytes into slice of uint64
  16. // the first uint64 indicates the length in bytes
  17. func bytesToArray(bs []byte) []uint64 {
  18. out := make([]uint64, 0, 2+len(bs)/8)
  19. out = append(out, uint64(len(bs)))
  20. for len(bs)%8 != 0 {
  21. bs = append(bs, 0)
  22. }
  23. for i := 0; i < len(bs); i = i + 8 {
  24. out = append(out, binary.BigEndian.Uint64(bs[i:]))
  25. }
  26. return out
  27. }
  28. // convert from slice of uint64 back to bytes
  29. // if input length is 0 or 1, will return nil
  30. func arrayToBytes(xs []uint64) []byte {
  31. if len(xs) < 2 {
  32. return nil
  33. }
  34. first := xs[0]
  35. out := make([]byte, len(xs)*8) // i think this can actually be len(xs)*8-8, but i dont think an extra 8 bytes hurts...
  36. for i := 1; i < len(xs); i = i + 1 {
  37. binary.BigEndian.PutUint64(out[((i-1)*8):], xs[i])
  38. }
  39. return out[:first]
  40. }