github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/vecmt/store_vectors.go (about)

     1  package vecmt
     2  
     3  import (
     4  	"github.com/unicornultrafoundation/go-helios/hash"
     5  	"github.com/unicornultrafoundation/go-helios/u2udb"
     6  )
     7  
     8  func (vi *Index) getBytes(table u2udb.Store, id hash.Event) []byte {
     9  	key := id.Bytes()
    10  	b, err := table.Get(key)
    11  	if err != nil {
    12  		vi.crit(err)
    13  	}
    14  	return b
    15  }
    16  
    17  func (vi *Index) setBytes(table u2udb.Store, id hash.Event, b []byte) {
    18  	key := id.Bytes()
    19  	err := table.Put(key, b)
    20  	if err != nil {
    21  		vi.crit(err)
    22  	}
    23  }
    24  
    25  // GetHighestBeforeTime reads the vector from DB
    26  func (vi *Index) GetHighestBeforeTime(id hash.Event) *HighestBeforeTime {
    27  	if bVal, okGet := vi.cache.HighestBeforeTime.Get(id); okGet {
    28  		return bVal.(*HighestBeforeTime)
    29  	}
    30  
    31  	b := HighestBeforeTime(vi.getBytes(vi.table.HighestBeforeTime, id))
    32  	if b == nil {
    33  		return nil
    34  	}
    35  	vi.cache.HighestBeforeTime.Add(id, &b, uint(len(b)))
    36  	return &b
    37  }
    38  
    39  // GetHighestBefore reads the vector from DB
    40  func (vi *Index) GetHighestBefore(id hash.Event) *HighestBefore {
    41  	return &HighestBefore{
    42  		VSeq:  vi.Base.GetHighestBefore(id),
    43  		VTime: vi.GetHighestBeforeTime(id),
    44  	}
    45  }
    46  
    47  // SetHighestBeforeTime stores the vector into DB
    48  func (vi *Index) SetHighestBeforeTime(id hash.Event, vec *HighestBeforeTime) {
    49  	vi.setBytes(vi.table.HighestBeforeTime, id, *vec)
    50  
    51  	vi.cache.HighestBeforeTime.Add(id, vec, uint(len(*vec)))
    52  }
    53  
    54  // SetHighestBefore stores the vectors into DB
    55  func (vi *Index) SetHighestBefore(id hash.Event, vec *HighestBefore) {
    56  	vi.Base.SetHighestBefore(id, vec.VSeq)
    57  	vi.SetHighestBeforeTime(id, vec.VTime)
    58  }