github.com/status-im/status-go@v1.1.0/db/history_store.go (about)

     1  package db
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/syndtr/goleveldb/leveldb/errors"
     7  
     8  	"github.com/status-im/status-go/eth-node/types"
     9  )
    10  
    11  // NewHistoryStore returns HistoryStore instance.
    12  func NewHistoryStore(storage Storage) HistoryStore {
    13  	return HistoryStore{
    14  		topicDB:   NewDBNamespace(storage, TopicHistoryBucket),
    15  		requestDB: NewDBNamespace(storage, HistoryRequestBucket),
    16  	}
    17  }
    18  
    19  // HistoryStore provides utility methods for quering history and requests store.
    20  type HistoryStore struct {
    21  	topicDB   DB
    22  	requestDB DB
    23  }
    24  
    25  // GetHistory creates history instance and loads history from database.
    26  // Returns instance populated with topic and duration if history is not found in database.
    27  func (h HistoryStore) GetHistory(topic types.TopicType, duration time.Duration) (TopicHistory, error) {
    28  	thist := h.NewHistory(topic, duration)
    29  	err := thist.Load()
    30  	if err != nil && err != errors.ErrNotFound {
    31  		return TopicHistory{}, err
    32  	}
    33  	return thist, nil
    34  }
    35  
    36  // NewRequest returns instance of the HistoryRequest.
    37  func (h HistoryStore) NewRequest() HistoryRequest {
    38  	return HistoryRequest{requestDB: h.requestDB, topicDB: h.topicDB}
    39  }
    40  
    41  // NewHistory creates TopicHistory object with required values.
    42  func (h HistoryStore) NewHistory(topic types.TopicType, duration time.Duration) TopicHistory {
    43  	return TopicHistory{db: h.topicDB, Duration: duration, Topic: topic}
    44  }
    45  
    46  // GetRequest loads HistoryRequest from database.
    47  func (h HistoryStore) GetRequest(id types.Hash) (HistoryRequest, error) {
    48  	req := HistoryRequest{requestDB: h.requestDB, topicDB: h.topicDB, ID: id}
    49  	err := req.Load()
    50  	if err != nil {
    51  		return HistoryRequest{}, err
    52  	}
    53  	return req, nil
    54  }
    55  
    56  // GetAllRequests loads all not-finished history requests from database.
    57  func (h HistoryStore) GetAllRequests() ([]HistoryRequest, error) {
    58  	rst := []HistoryRequest{}
    59  	iter := h.requestDB.NewIterator(h.requestDB.Range(nil, nil))
    60  	for iter.Next() {
    61  		req := HistoryRequest{
    62  			requestDB: h.requestDB,
    63  			topicDB:   h.topicDB,
    64  		}
    65  		err := req.RawUnmarshall(iter.Value())
    66  		if err != nil {
    67  			return nil, err
    68  		}
    69  		rst = append(rst, req)
    70  	}
    71  	return rst, nil
    72  }
    73  
    74  // GetHistoriesByTopic returns all histories with a given topic.
    75  // This is needed when we will have multiple range per single topic.
    76  // TODO explain
    77  func (h HistoryStore) GetHistoriesByTopic(topic types.TopicType) ([]TopicHistory, error) {
    78  	rst := []TopicHistory{}
    79  	iter := h.topicDB.NewIterator(h.topicDB.Range(topic[:], nil))
    80  	for iter.Next() {
    81  		key := TopicHistoryKey{}
    82  		copy(key[:], iter.Key())
    83  		th, err := LoadTopicHistoryFromKey(h.topicDB, key)
    84  		if err != nil {
    85  			return nil, err
    86  		}
    87  		rst = append(rst, th)
    88  	}
    89  	return rst, nil
    90  }