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

     1  package storage
     2  
     3  import (
     4  	"encoding/hex"
     5  
     6  	"github.com/keybase/client/go/gregor"
     7  	"github.com/keybase/client/go/libkb"
     8  )
     9  
    10  type localStorageRecord struct {
    11  	Dismissals [][]byte `json:"d"`
    12  	Outbox     [][]byte `json:"o"`
    13  }
    14  
    15  type LocalDb struct {
    16  	libkb.Contextified
    17  }
    18  
    19  func NewLocalDB(g *libkb.GlobalContext) *LocalDb {
    20  	return &LocalDb{
    21  		Contextified: libkb.NewContextified(g),
    22  	}
    23  }
    24  
    25  func dbKey(u gregor.UID) libkb.DbKey {
    26  	return libkb.DbKey{Typ: libkb.DBGregor, Key: hex.EncodeToString(u.Bytes())}
    27  }
    28  
    29  func dbKeyLocal(u gregor.UID) libkb.DbKey {
    30  	return libkb.DbKey{Typ: libkb.DBGregor, Key: "_ld" + hex.EncodeToString(u.Bytes())}
    31  }
    32  
    33  func (db *LocalDb) Store(u gregor.UID, state []byte, outbox [][]byte, localDismissals [][]byte) error {
    34  	if err := db.G().LocalDb.PutRaw(dbKey(u), state); err != nil {
    35  		return err
    36  	}
    37  	ldr := localStorageRecord{
    38  		Dismissals: localDismissals,
    39  		Outbox:     outbox,
    40  	}
    41  	return db.G().LocalDb.PutObj(dbKeyLocal(u), nil, ldr)
    42  }
    43  
    44  func (db *LocalDb) Load(u gregor.UID) (state []byte, outbox [][]byte, localDismissals [][]byte, err error) {
    45  	if state, _, err = db.G().LocalDb.GetRaw(dbKey(u)); err != nil {
    46  		return state, outbox, localDismissals, err
    47  	}
    48  	var ldr localStorageRecord
    49  	if _, err = db.G().LocalDb.GetInto(&ldr, dbKeyLocal(u)); err != nil {
    50  		return state, outbox, localDismissals, err
    51  	}
    52  	return state, ldr.Outbox, ldr.Dismissals, nil
    53  }