func (app *App) OpenKV(tx *db.Tx, storage *db.VolumeStorage) (kv.KV, error) { var kvstores []kv.KV c := storage.Cursor() for item := c.First(); item != nil; item = c.Next() { backend, err := item.Backend() if err != nil { return nil, err } s, err := app.openStorage(backend) if err != nil { return nil, err } sharingKeyName, err := item.SharingKeyName() if err != nil { return nil, err } sharingKey, err := tx.SharingKeys().Get(sharingKeyName) if err != nil { return nil, fmt.Errorf("getting sharing key %q: %v", sharingKeyName, err) } var secret [32]byte sharingKey.Secret(&secret) s = untrusted.New(s, &secret) kvstores = append(kvstores, s) } return kvmulti.New(kvstores...), nil }
func TestPut(t *testing.T) { a := &kvmock.InMemory{} b := &kvmock.InMemory{} multi := kvmulti.New(a, b) if err := multi.Put([]byte("k1"), []byte("v1")); err != nil { t.Fatal(err) } if !reflect.DeepEqual(a.Data, map[string]string{"k1": "v1"}) { t.Errorf("bad data in a: %v", a.Data) } if !reflect.DeepEqual(b.Data, map[string]string{"k1": "v1"}) { t.Errorf("bad data in b: %v", a.Data) } }
func TestGetFallback(t *testing.T) { a := &kvmock.InMemory{} b := &kvmock.InMemory{} multi := kvmulti.New(a, b) if err := b.Put([]byte("k1"), []byte("v1")); err != nil { t.Fatal(err) } v, err := multi.Get([]byte("k1")) if err != nil { t.Error(err) } if g, e := string(v), "v1"; g != e { t.Errorf("bad value: %q != %q", g, e) } }
// Open key-value stores as allowed for this peer. Uses the opener // function for the actual open action. // // If the peer is not allowed to use any storage, returns // ErrNoStorageForPeer. // // Returned KV is valid after the transaction. Strings passed to the // opener function are valid after the transaction. func (p *PeerStorage) Open(opener func(string) (kv.KV, error)) (kv.KV, error) { var kvstores []kv.KV c := p.b.Cursor() for k, _ := c.First(); k != nil; k, _ = c.Next() { backend := string(k) // later value may include quota style restrictions s, err := opener(backend) if err != nil { // TODO once kv.KV has Close, close all in kvstores return nil, err } kvstores = append(kvstores, s) } if len(kvstores) == 0 { return nil, ErrNoStorageForPeer } return kvmulti.New(kvstores...), nil }
func (app *App) openKV(conf *wire.KV) (kv.KV, error) { var kvstores []kv.KV if conf.Local != nil { kvpath := filepath.Join(app.DataDir, "chunks") var s kv.KV var err error s, err = kvfiles.Open(kvpath) if err != nil { return nil, err } if conf.Local.Secret != nil { var secret [32]byte copy(secret[:], conf.Local.Secret) s = untrusted.New(s, &secret) } kvstores = append(kvstores, s) } for _, ext := range conf.External { var s kv.KV var err error s, err = kvfiles.Open(ext.Path) if err != nil { return nil, err } if ext.Secret != nil { var secret [32]byte copy(secret[:], ext.Secret) s = untrusted.New(s, &secret) } kvstores = append(kvstores, s) } if len(conf.XXX_unrecognized) > 0 { return nil, fmt.Errorf("unknown storage: %v", conf) } return kvmulti.New(kvstores...), nil }