github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/teams/storage/ftl.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/keybase/client/go/libkb"
     6  	"github.com/keybase/client/go/protocol/keybase1"
     7  )
     8  
     9  type FTLStorageUpgrader func(mctx libkb.MetaContext, state *keybase1.FastTeamData) (changed bool, err error)
    10  
    11  // FTLStorage stores FTL state to disk or memory.
    12  type FTLStorage struct {
    13  	*storageGeneric
    14  	upgrader FTLStorageUpgrader
    15  }
    16  
    17  // Increment to invalidate the disk cache.
    18  const ftlDiskStorageVersion = 10
    19  const ftlMemCacheLRUSize = 200
    20  
    21  type ftlDiskStorageItem struct {
    22  	Version int                    `codec:"V"`
    23  	State   *keybase1.FastTeamData `codec:"S"`
    24  }
    25  
    26  var _ teamDataGeneric = (*keybase1.FastTeamData)(nil)
    27  var _ diskItemGeneric = (*ftlDiskStorageItem)(nil)
    28  
    29  func (d *ftlDiskStorageItem) version() int {
    30  	return d.Version
    31  }
    32  func (d *ftlDiskStorageItem) value() teamDataGeneric {
    33  	return d.State
    34  }
    35  func (d *ftlDiskStorageItem) setVersion(i int) {
    36  	d.Version = i
    37  }
    38  func (d *ftlDiskStorageItem) setValue(v teamDataGeneric) error {
    39  	typed, ok := v.(*keybase1.FastTeamData)
    40  	if !ok {
    41  		return fmt.Errorf("teams/storage.FTL#Put: Bad object for setValue; got type %T", v)
    42  	}
    43  	d.State = typed
    44  	return nil
    45  }
    46  
    47  func NewFTLStorage(g *libkb.GlobalContext, upgrader FTLStorageUpgrader) *FTLStorage {
    48  	s := newStorageGeneric(g, ftlMemCacheLRUSize, ftlDiskStorageVersion, libkb.DBFTLStorage, libkb.EncryptionReasonTeamsFTLLocalStorage, "ftl", func() diskItemGeneric { return &ftlDiskStorageItem{} })
    49  	return &FTLStorage{storageGeneric: s, upgrader: upgrader}
    50  }
    51  
    52  func (s *FTLStorage) Put(mctx libkb.MetaContext, state *keybase1.FastTeamData) {
    53  	s.storageGeneric.put(mctx, state)
    54  }
    55  
    56  // Can return nil.
    57  func (s *FTLStorage) Get(mctx libkb.MetaContext, teamID keybase1.TeamID, public bool) (data *keybase1.FastTeamData, frozen bool, tombstoned bool) {
    58  	vp := s.storageGeneric.get(mctx, teamID, public)
    59  	if vp == nil {
    60  		return nil, false, false
    61  	}
    62  	ret, ok := vp.(*keybase1.FastTeamData)
    63  	if !ok {
    64  		mctx.Debug("teams/storage.FTL#Get cast error: %T is wrong type", vp)
    65  		return nil, false, false
    66  	}
    67  
    68  	changed, err := s.upgrader(mctx, ret)
    69  	if err != nil {
    70  		mctx.Debug("error in upgrade of stored object: %s", err)
    71  		return nil, false, false
    72  	}
    73  
    74  	if changed {
    75  		// Put the upgraded object directly into the store.
    76  		s.Put(mctx, ret)
    77  	}
    78  
    79  	if ret.Frozen {
    80  		mctx.Debug("returning frozen fast team data")
    81  	}
    82  	if ret.Tombstoned {
    83  		mctx.Debug("returning tombstoned fast team data")
    84  	}
    85  	return ret, ret.Frozen, ret.Tombstoned
    86  }