github.com/codingfuture/orig-energi3@v0.8.4/swarm/storage/feed/query.go (about) 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 17 package feed 18 19 import ( 20 "fmt" 21 "strconv" 22 23 "github.com/ethereum/go-ethereum/common" 24 "github.com/ethereum/go-ethereum/swarm/storage/feed/lookup" 25 ) 26 27 // Query is used to specify constraints when performing an update lookup 28 // TimeLimit indicates an upper bound for the search. Set to 0 for "now" 29 type Query struct { 30 Feed 31 Hint lookup.Epoch 32 TimeLimit uint64 33 } 34 35 // FromValues deserializes this instance from a string key-value store 36 // useful to parse query strings 37 func (q *Query) FromValues(values Values) error { 38 time, _ := strconv.ParseUint(values.Get("time"), 10, 64) 39 q.TimeLimit = time 40 41 level, _ := strconv.ParseUint(values.Get("hint.level"), 10, 32) 42 q.Hint.Level = uint8(level) 43 q.Hint.Time, _ = strconv.ParseUint(values.Get("hint.time"), 10, 64) 44 if q.Feed.User == (common.Address{}) { 45 return q.Feed.FromValues(values) 46 } 47 return nil 48 } 49 50 // AppendValues serializes this structure into the provided string key-value store 51 // useful to build query strings 52 func (q *Query) AppendValues(values Values) { 53 if q.TimeLimit != 0 { 54 values.Set("time", fmt.Sprintf("%d", q.TimeLimit)) 55 } 56 if q.Hint.Level != 0 { 57 values.Set("hint.level", fmt.Sprintf("%d", q.Hint.Level)) 58 } 59 if q.Hint.Time != 0 { 60 values.Set("hint.time", fmt.Sprintf("%d", q.Hint.Time)) 61 } 62 q.Feed.AppendValues(values) 63 } 64 65 // NewQuery constructs an Query structure to find updates on or before `time` 66 // if time == 0, the latest update will be looked up 67 func NewQuery(feed *Feed, time uint64, hint lookup.Epoch) *Query { 68 return &Query{ 69 TimeLimit: time, 70 Feed: *feed, 71 Hint: hint, 72 } 73 } 74 75 // NewQueryLatest generates lookup parameters that look for the latest update to a feed 76 func NewQueryLatest(feed *Feed, hint lookup.Epoch) *Query { 77 return NewQuery(feed, 0, hint) 78 }