github.com/lino-network/lino@v0.6.11/x/post/model/storage.go (about)

     1  package model
     2  
     3  import (
     4  	wire "github.com/cosmos/cosmos-sdk/codec"
     5  	sdk "github.com/cosmos/cosmos-sdk/types"
     6  
     7  	linotypes "github.com/lino-network/lino/types"
     8  	"github.com/lino-network/lino/utils"
     9  	"github.com/lino-network/lino/x/post/types"
    10  )
    11  
    12  var (
    13  	PostSubStore              = []byte{0x00} // SubStore for all post info
    14  	ConsumptionWindowSubStore = []byte{0x01} // SubStore for consumption window.
    15  )
    16  
    17  func GetAuthorPrefix(author linotypes.AccountKey) []byte {
    18  	return append(PostSubStore, author...)
    19  }
    20  
    21  // GetPostInfoKey - "post info substore" + "permlink"
    22  func GetPostInfoKey(permlink linotypes.Permlink) []byte {
    23  	return append(PostSubStore, permlink...)
    24  }
    25  
    26  // GetConsumptionWindowKey - "consumption window substore"
    27  func GetConsumptionWindowKey() []byte {
    28  	return ConsumptionWindowSubStore
    29  }
    30  
    31  // PostStorage - post storage
    32  type PostStorage struct {
    33  	key sdk.StoreKey
    34  	cdc *wire.Codec
    35  }
    36  
    37  // NewPostStorage - returns a new PostStorage that
    38  // uses codec to (binary) encode and decode concrete Post
    39  func NewPostStorage(key sdk.StoreKey) PostStorage {
    40  	cdc := wire.New()
    41  	wire.RegisterCrypto(cdc)
    42  
    43  	return PostStorage{
    44  		key: key,
    45  		cdc: cdc,
    46  	}
    47  }
    48  
    49  // DoesPostExist - check if a post exists in KVStore or not
    50  func (ps PostStorage) HasPost(ctx sdk.Context, permlink linotypes.Permlink) bool {
    51  	store := ctx.KVStore(ps.key)
    52  	return store.Has(GetPostInfoKey(permlink))
    53  }
    54  
    55  // GetPostInfo - get post info from KVStore
    56  func (ps PostStorage) GetPost(ctx sdk.Context, permlink linotypes.Permlink) (*Post, sdk.Error) {
    57  	store := ctx.KVStore(ps.key)
    58  	key := GetPostInfoKey(permlink)
    59  	infoByte := store.Get(key)
    60  	if infoByte == nil {
    61  		return nil, types.ErrPostNotFound(permlink)
    62  	}
    63  	postInfo := new(Post)
    64  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(infoByte, postInfo)
    65  	return postInfo, nil
    66  }
    67  
    68  // SetPostInfo - set post info to KVStore
    69  func (ps PostStorage) SetPost(ctx sdk.Context, postInfo *Post) {
    70  	store := ctx.KVStore(ps.key)
    71  	infoByte := ps.cdc.MustMarshalBinaryLengthPrefixed(*postInfo)
    72  	store.Set(GetPostInfoKey(linotypes.GetPermlink(postInfo.Author, postInfo.PostID)), infoByte)
    73  }
    74  
    75  // Post cannot be deleted in the store. you can mark it as deleted.
    76  // // SetPostInfo - set post info to KVStore
    77  // func (ps PostStorage) DeletePost(ctx sdk.Context, permlink linotypes.Permlink) {
    78  // 	store := ctx.KVStore(ps.key)
    79  // 	store.Delete(GetPostInfoKey(permlink))
    80  // }
    81  
    82  func (ps PostStorage) GetConsumptionWindow(ctx sdk.Context) linotypes.MiniDollar {
    83  	store := ctx.KVStore(ps.key)
    84  	bz := store.Get(GetConsumptionWindowKey())
    85  	if bz == nil {
    86  		return linotypes.NewMiniDollar(0)
    87  	}
    88  	consumption := linotypes.NewMiniDollar(0)
    89  	ps.cdc.MustUnmarshalBinaryLengthPrefixed(bz, &consumption)
    90  	return consumption
    91  }
    92  
    93  func (ps PostStorage) SetConsumptionWindow(ctx sdk.Context, consumption linotypes.MiniDollar) {
    94  	store := ctx.KVStore(ps.key)
    95  	bz := ps.cdc.MustMarshalBinaryLengthPrefixed(consumption)
    96  	store.Set(GetConsumptionWindowKey(), bz)
    97  }
    98  
    99  func (ps PostStorage) PartialStoreMap(ctx sdk.Context) utils.StoreMap {
   100  	store := ctx.KVStore(ps.key)
   101  	stores := []utils.SubStore{
   102  		{
   103  			Store:      store,
   104  			Prefix:     PostSubStore,
   105  			ValCreator: func() interface{} { return new(Post) },
   106  			Decoder:    ps.cdc.MustUnmarshalBinaryLengthPrefixed,
   107  		},
   108  	}
   109  	return utils.NewStoreMap(stores)
   110  }