store.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package store
  2. import (
  3. "encoding/json"
  4. "jjl-tools/tinodeService/store/types"
  5. )
  6. // Unique ID generator
  7. var uGen types.UidGenerator
  8. type configType struct {
  9. // 16-byte key for XTEA. Used to initialize types.UidGenerator.
  10. UidKey []byte `json:"uid_key"`
  11. // Maximum number of results to return from adapter.
  12. MaxResults int `json:"max_results"`
  13. // Configurations for individual adapters.
  14. Adapters map[string]json.RawMessage `json:"adapters"`
  15. }
  16. // GetUid generates a unique ID suitable for use as a primary key.
  17. func GetUid() types.Uid {
  18. return uGen.Get()
  19. }
  20. // GetUidString generate unique ID as string
  21. func GetUidString() string {
  22. return uGen.GetStr()
  23. }
  24. // DecodeUid takes an XTEA encrypted Uid and decrypts it into an int64.
  25. // This is needed for sql compatibility. Tte original int64 values
  26. // are generated by snowflake which ensures that the top bit is unset.
  27. func DecodeUid(uid types.Uid) int64 {
  28. if uid.IsZero() {
  29. return 0
  30. }
  31. return uGen.DecodeUid(uid)
  32. }
  33. // EncodeUid applies XTEA encryption to an int64 value. It's the inverse of DecodeUid.
  34. func EncodeUid(id int64) types.Uid {
  35. if id == 0 {
  36. return types.ZeroUid
  37. }
  38. return uGen.EncodeInt64(id)
  39. }
  40. // UsersObjMapper is a users struct to hold methods for persistence mapping for the User object.
  41. type UsersObjMapper struct{}
  42. // Users is the ancor for storing/retrieving User objects
  43. var Users UsersObjMapper