github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/teams/storage/std.go (about)

     1  package storage
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/keybase1"
     8  )
     9  
    10  // Storage stores TeamData's on memory and disk. Threadsafe. For the standard loader,
    11  // which does a complete but slower load. Compare to FTLStorage which stores the
    12  // Fast-loaded team state.
    13  type Storage struct {
    14  	*storageGeneric
    15  }
    16  
    17  // Increment to invalidate the disk cache.
    18  const diskStorageVersion = 12
    19  const memCacheLRUSize = 200
    20  
    21  type DiskStorageItem struct {
    22  	Version int                `codec:"V"`
    23  	State   *keybase1.TeamData `codec:"S"`
    24  }
    25  
    26  var _ teamDataGeneric = (*keybase1.TeamData)(nil)
    27  var _ diskItemGeneric = (*DiskStorageItem)(nil)
    28  
    29  func (d *DiskStorageItem) version() int {
    30  	return d.Version
    31  }
    32  func (d *DiskStorageItem) value() teamDataGeneric {
    33  	return d.State
    34  }
    35  func (d *DiskStorageItem) setVersion(i int) {
    36  	d.Version = i
    37  }
    38  func (d *DiskStorageItem) setValue(v teamDataGeneric) error {
    39  	typed, ok := v.(*keybase1.TeamData)
    40  	if !ok {
    41  		return fmt.Errorf("teams.Storage#Put: Bad object for setValue; got type %T", v)
    42  	}
    43  	d.State = typed
    44  	return nil
    45  }
    46  
    47  func NewStorage(g *libkb.GlobalContext) *Storage {
    48  	s := newStorageGeneric(g, memCacheLRUSize, diskStorageVersion, libkb.DBSlowTeamsAlias, libkb.EncryptionReasonTeamsLocalStorage, "slow", func() diskItemGeneric { return &DiskStorageItem{} })
    49  	return &Storage{s}
    50  }
    51  
    52  func (s *Storage) Put(mctx libkb.MetaContext, state *keybase1.TeamData) {
    53  	s.storageGeneric.put(mctx, state)
    54  }
    55  
    56  // Get can return nil and no error.
    57  func (s *Storage) Get(mctx libkb.MetaContext, teamID keybase1.TeamID, public bool) (data *keybase1.TeamData, 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.TeamData)
    63  	if !ok {
    64  		mctx.Debug("teams.Storage#Get cast error: %T is wrong type", vp)
    65  		return nil, false, false
    66  	}
    67  
    68  	if ret.Frozen {
    69  		mctx.Debug("returning frozen team data")
    70  	}
    71  	if ret.Tombstoned {
    72  		mctx.Debug("returning tombstoned team data")
    73  	}
    74  	return ret, ret.Frozen, ret.Tombstoned
    75  }