search.go 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file will just have the search functions.
  2. // Note that the deprecated versions are still kept around, just in case I need to figure out again
  3. // how iterators work...
  4. // (gwyneth 20211102)
  5. package main
  6. import (
  7. "encoding/json"
  8. "strings"
  9. "time"
  10. "github.com/dgraph-io/badger/v3"
  11. "github.com/syndtr/goleveldb/leveldb"
  12. "github.com/tidwall/buntdb"
  13. )
  14. // searchKVname searches the KV database for an avatar name.
  15. // Returns NullUUID if the key wasn't found.
  16. func searchKVname(avatarName string) (uuid string, grid string) {
  17. _, tempUUID, tempGrid := searchKV(strings.TrimSpace(avatarName))
  18. return tempUUID, tempGrid
  19. }
  20. // searchKVUUID searches the KV database for an avatar UUID.
  21. // Returns empty string if the avatar name wasn't found.
  22. func searchKVUUID(avatarKey string) (name string, grid string) {
  23. tempAvatarName, _, tempGrid := searchKV(strings.TrimSpace(avatarKey))
  24. return tempAvatarName, tempGrid
  25. }
  26. // Universal search, since we put everything in the KV database, we can basically search for anything.
  27. // *Way* more efficient! (gwyneth 20211031)
  28. // Returns the unmarshaled record from the KV store, if found;
  29. // otherwise, avatar name will be the empty string and avatar key will be NullUUID.
  30. func searchKV(searchItem string) (name string, uuid string, grid string) {
  31. // return value.
  32. var val = avatarUUID{"", NullUUID, ""}
  33. var err error // to deal with scope issues.
  34. time_start := time.Now() // start chroometer to time this transaction.
  35. switch goslConfig.database {
  36. case "badger":
  37. kv, err = badger.Open(Opt)
  38. checkErrPanic(err)
  39. defer kv.Close()
  40. err = kv.View(func(txn *badger.Txn) error {
  41. item, err := txn.Get([]byte(searchItem))
  42. if err != nil {
  43. return err
  44. }
  45. data, err := item.ValueCopy(nil)
  46. if err != nil {
  47. log.Errorf("error %q while getting data from %v\n", err, item)
  48. return err
  49. }
  50. if err = json.Unmarshal(data, &val); err != nil {
  51. log.Errorf("error while unmarshalling reply to search item: %q (%v)\n", searchItem, err)
  52. return err
  53. }
  54. return nil
  55. })
  56. if err != nil {
  57. log.Errorf("error while getting or unmarshalling reply to search item: %q (%v)\n", searchItem, err)
  58. }
  59. case "buntdb":
  60. db, err := buntdb.Open(goslConfig.dbNamePath)
  61. checkErrPanic(err)
  62. defer db.Close()
  63. var data string
  64. err = db.View(func(tx *buntdb.Tx) error {
  65. data, err = tx.Get(searchItem)
  66. return err
  67. })
  68. err = json.Unmarshal([]byte(data), &val)
  69. if err != nil {
  70. log.Errorf("error while unmarshalling reply to search item: %q (%v)\n", searchItem, err)
  71. }
  72. case "leveldb":
  73. db, errdb := leveldb.OpenFile(goslConfig.dbNamePath, nil)
  74. checkErrPanic(errdb)
  75. defer db.Close()
  76. var data []byte // for scoping reasons.
  77. data, err = db.Get([]byte(searchItem), nil)
  78. if err != nil {
  79. log.Errorf("error while unmarshalling reply to search item: %q (%v)\n", searchItem, err)
  80. } else {
  81. if err = json.Unmarshal(data, &val); err != nil {
  82. log.Errorf("error while unmarshalling reply to search item: %q (%v)\n", searchItem, err)
  83. }
  84. }
  85. }
  86. log.Debugf("time to lookup %q: %v\n", searchItem, time.Since(time_start))
  87. if err != nil {
  88. checkErr(err)
  89. return "", NullUUID, ""
  90. } // else:
  91. return val.AvatarName, val.UUID, val.Grid
  92. }