-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7eb2a52
commit f2faeb6
Showing
4 changed files
with
90 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package storage | ||
|
||
import ( | ||
b "encoding/binary" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"math" | ||
) | ||
|
||
type RankStorage struct { | ||
cis CidIndexStorage | ||
key *sdk.KVStoreKey | ||
} | ||
|
||
func NewRankStorage(cis CidIndexStorage, key *sdk.KVStoreKey) RankStorage { | ||
return RankStorage{ | ||
cis: cis, | ||
key: key, | ||
} | ||
} | ||
|
||
// CIDs index is array of all added CIDs, sorted asc by first link time. | ||
// - for given link, CIDs added in order [CID1, CID2] (if they both new to chain) | ||
// This method performs lookup of CIDs, returns index value, or create and put in index new value if not exists. | ||
func (rs RankStorage) StoreFullRank(ctx sdk.Context, ranks []float64) { | ||
|
||
store := ctx.KVStore(rs.key) | ||
|
||
var indexAsBytes [8]byte | ||
var rankAsBytes [8]byte | ||
for i, rank := range ranks { | ||
b.LittleEndian.PutUint64(indexAsBytes[:], uint64(i)) | ||
b.LittleEndian.PutUint64(rankAsBytes[:], math.Float64bits(rank)) | ||
store.Set(indexAsBytes[:], rankAsBytes[:]) | ||
} | ||
} | ||
|
||
// returns all added cids | ||
func (rs RankStorage) GetFullRank(ctx sdk.Context) []float64 { | ||
|
||
cidsCount := rs.cis.GetCidsCount(ctx) | ||
ranks := make([]float64, cidsCount) | ||
|
||
store := ctx.KVStore(rs.key) | ||
iterator := store.Iterator(nil, nil) | ||
|
||
for iterator.Valid() { | ||
ranks[b.LittleEndian.Uint64(iterator.Key())] = math.Float64frombits(b.LittleEndian.Uint64(iterator.Value())) | ||
iterator.Next() | ||
} | ||
iterator.Close() | ||
return ranks | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters