query.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package feed
  17. import (
  18. "fmt"
  19. "strconv"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
  22. )
  23. // Query is used to specify constraints when performing an update lookup
  24. // TimeLimit indicates an upper bound for the search. Set to 0 for "now"
  25. type Query struct {
  26. Feed
  27. Hint lookup.Epoch
  28. TimeLimit uint64
  29. }
  30. // FromValues deserializes this instance from a string key-value store
  31. // useful to parse query strings
  32. func (q *Query) FromValues(values Values) error {
  33. time, _ := strconv.ParseUint(values.Get("time"), 10, 64)
  34. q.TimeLimit = time
  35. level, _ := strconv.ParseUint(values.Get("hint.level"), 10, 32)
  36. q.Hint.Level = uint8(level)
  37. q.Hint.Time, _ = strconv.ParseUint(values.Get("hint.time"), 10, 64)
  38. if q.Feed.User == (common.Address{}) {
  39. return q.Feed.FromValues(values)
  40. }
  41. return nil
  42. }
  43. // AppendValues serializes this structure into the provided string key-value store
  44. // useful to build query strings
  45. func (q *Query) AppendValues(values Values) {
  46. if q.TimeLimit != 0 {
  47. values.Set("time", fmt.Sprintf("%d", q.TimeLimit))
  48. }
  49. if q.Hint.Level != 0 {
  50. values.Set("hint.level", fmt.Sprintf("%d", q.Hint.Level))
  51. }
  52. if q.Hint.Time != 0 {
  53. values.Set("hint.time", fmt.Sprintf("%d", q.Hint.Time))
  54. }
  55. q.Feed.AppendValues(values)
  56. }
  57. // NewQuery constructs an Query structure to find updates on or before `time`
  58. // if time == 0, the latest update will be looked up
  59. func NewQuery(feed *Feed, time uint64, hint lookup.Epoch) *Query {
  60. return &Query{
  61. TimeLimit: time,
  62. Feed: *feed,
  63. Hint: hint,
  64. }
  65. }
  66. // NewQueryLatest generates lookup parameters that look for the latest update to a feed
  67. func NewQueryLatest(feed *Feed, hint lookup.Epoch) *Query {
  68. return NewQuery(feed, 0, hint)
  69. }