2019-04-30 06:46:12 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
whispertypes "github.com/status-im/status-protocol-go/transport/whisper/types"
|
|
|
|
statusproto "github.com/status-im/status-protocol-go/types"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewHistoryStore returns HistoryStore instance.
|
2019-05-06 06:33:19 +00:00
|
|
|
func NewHistoryStore(storage Storage) HistoryStore {
|
2019-04-30 06:46:12 +00:00
|
|
|
return HistoryStore{
|
2019-05-06 06:33:19 +00:00
|
|
|
topicDB: NewDBNamespace(storage, TopicHistoryBucket),
|
|
|
|
requestDB: NewDBNamespace(storage, HistoryRequestBucket),
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// HistoryStore provides utility methods for quering history and requests store.
|
|
|
|
type HistoryStore struct {
|
|
|
|
topicDB DB
|
|
|
|
requestDB DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHistory creates history instance and loads history from database.
|
|
|
|
// Returns instance populated with topic and duration if history is not found in database.
|
2019-10-09 14:22:53 +00:00
|
|
|
func (h HistoryStore) GetHistory(topic whispertypes.TopicType, duration time.Duration) (TopicHistory, error) {
|
2019-04-30 06:46:12 +00:00
|
|
|
thist := h.NewHistory(topic, duration)
|
|
|
|
err := thist.Load()
|
|
|
|
if err != nil && err != errors.ErrNotFound {
|
|
|
|
return TopicHistory{}, err
|
|
|
|
}
|
|
|
|
return thist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRequest returns instance of the HistoryRequest.
|
|
|
|
func (h HistoryStore) NewRequest() HistoryRequest {
|
|
|
|
return HistoryRequest{requestDB: h.requestDB, topicDB: h.topicDB}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHistory creates TopicHistory object with required values.
|
2019-10-09 14:22:53 +00:00
|
|
|
func (h HistoryStore) NewHistory(topic whispertypes.TopicType, duration time.Duration) TopicHistory {
|
2019-04-30 06:46:12 +00:00
|
|
|
return TopicHistory{db: h.topicDB, Duration: duration, Topic: topic}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetRequest loads HistoryRequest from database.
|
2019-10-09 14:22:53 +00:00
|
|
|
func (h HistoryStore) GetRequest(id statusproto.Hash) (HistoryRequest, error) {
|
2019-04-30 06:46:12 +00:00
|
|
|
req := HistoryRequest{requestDB: h.requestDB, topicDB: h.topicDB, ID: id}
|
|
|
|
err := req.Load()
|
|
|
|
if err != nil {
|
|
|
|
return HistoryRequest{}, err
|
|
|
|
}
|
|
|
|
return req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAllRequests loads all not-finished history requests from database.
|
|
|
|
func (h HistoryStore) GetAllRequests() ([]HistoryRequest, error) {
|
|
|
|
rst := []HistoryRequest{}
|
|
|
|
iter := h.requestDB.NewIterator(h.requestDB.Range(nil, nil))
|
|
|
|
for iter.Next() {
|
|
|
|
req := HistoryRequest{
|
|
|
|
requestDB: h.requestDB,
|
|
|
|
topicDB: h.topicDB,
|
|
|
|
}
|
|
|
|
err := req.RawUnmarshall(iter.Value())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, req)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetHistoriesByTopic returns all histories with a given topic.
|
|
|
|
// This is needed when we will have multiple range per single topic.
|
|
|
|
// TODO explain
|
2019-10-09 14:22:53 +00:00
|
|
|
func (h HistoryStore) GetHistoriesByTopic(topic whispertypes.TopicType) ([]TopicHistory, error) {
|
2019-04-30 06:46:12 +00:00
|
|
|
rst := []TopicHistory{}
|
|
|
|
iter := h.topicDB.NewIterator(h.topicDB.Range(topic[:], nil))
|
|
|
|
for iter.Next() {
|
|
|
|
key := TopicHistoryKey{}
|
|
|
|
copy(key[:], iter.Key())
|
|
|
|
th, err := LoadTopicHistoryFromKey(h.topicDB, key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rst = append(rst, th)
|
|
|
|
}
|
|
|
|
return rst, nil
|
|
|
|
}
|