func (s *store) delete(key []byte) { mainrev := s.currentRev.main + 1 tx := s.b.BatchTx() tx.Lock() defer tx.Unlock() ibytes := newRevBytes() revToBytes(revision{main: mainrev, sub: s.currentRev.sub}, ibytes) event := storagepb.Event{ Type: storagepb.DELETE, Kv: &storagepb.KeyValue{ Key: key, }, } d, err := event.Marshal() if err != nil { log.Fatalf("storage: cannot marshal event: %v", err) } tx.UnsafePut(keyBucketName, ibytes, d) err = s.kvindex.Tombstone(key, revision{main: mainrev, sub: s.currentRev.sub}) if err != nil { log.Fatalf("storage: cannot tombstone an existing key (%s): %v", string(key), err) } s.currentRev.sub += 1 }
func (s *store) put(key, value []byte) { rev := s.currentRev.main + 1 c := rev // if the key exists before, use its previous created _, created, ver, err := s.kvindex.Get(key, rev) if err == nil { c = created.main } ibytes := newRevBytes() revToBytes(revision{main: rev, sub: s.currentRev.sub}, ibytes) ver = ver + 1 event := storagepb.Event{ Type: storagepb.PUT, Kv: &storagepb.KeyValue{ Key: key, Value: value, CreateRevision: c, ModRevision: rev, Version: ver, }, } d, err := event.Marshal() if err != nil { log.Fatalf("storage: cannot marshal event: %v", err) } s.tx.UnsafePut(keyBucketName, ibytes, d) s.kvindex.Put(key, revision{main: rev, sub: s.currentRev.sub}) s.currentRev.sub += 1 }
func (s *store) put(key, value []byte, rev int64) { c := rev // if the key exists before, use its previous created _, created, err := s.kvindex.Get(key, rev) if err == nil { c = created.main } ibytes := newRevBytes() revToBytes(reversion{main: rev, sub: s.currentRev.sub}, ibytes) event := storagepb.Event{ Type: storagepb.PUT, Kv: storagepb.KeyValue{ Key: key, Value: value, CreateIndex: c, ModIndex: rev, }, } d, err := event.Marshal() if err != nil { log.Fatalf("storage: cannot marshal event: %v", err) } tx := s.b.BatchTx() tx.Lock() defer tx.Unlock() tx.UnsafePut(keyBucketName, ibytes, d) s.kvindex.Put(key, reversion{main: rev, sub: s.currentRev.sub}) s.currentRev.sub += 1 }
func TestStoreRestore(t *testing.T) { s, b, index := newFakeStore() putev := storagepb.Event{ Type: storagepb.PUT, Kv: &storagepb.KeyValue{ Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 3, ModRevision: 3, Version: 1, }, } putevb, err := putev.Marshal() if err != nil { t.Fatal(err) } delev := storagepb.Event{ Type: storagepb.DELETE, Kv: &storagepb.KeyValue{ Key: []byte("foo"), }, } delevb, err := delev.Marshal() if err != nil { t.Fatal(err) } b.tx.rangeRespc <- rangeResp{[][]byte{finishedCompactKeyName}, [][]byte{newTestBytes(revision{2, 0})}} b.tx.rangeRespc <- rangeResp{[][]byte{newTestBytes(revision{3, 0}), newTestBytes(revision{4, 0})}, [][]byte{putevb, delevb}} b.tx.rangeRespc <- rangeResp{[][]byte{scheduledCompactKeyName}, [][]byte{newTestBytes(revision{2, 0})}} s.Restore() if s.compactMainRev != 2 { t.Errorf("compact rev = %d, want 4", s.compactMainRev) } wrev := revision{4, 0} if !reflect.DeepEqual(s.currentRev, wrev) { t.Errorf("current rev = %v, want %v", s.currentRev, wrev) } wact := []testutil.Action{ {"range", []interface{}{metaBucketName, finishedCompactKeyName, []byte(nil), int64(0)}}, {"range", []interface{}{keyBucketName, newTestBytes(revision{}), newTestBytes(revision{math.MaxInt64, math.MaxInt64}), int64(0)}}, {"range", []interface{}{metaBucketName, scheduledCompactKeyName, []byte(nil), int64(0)}}, } if g := b.tx.Action(); !reflect.DeepEqual(g, wact) { t.Errorf("tx actions = %+v, want %+v", g, wact) } wact = []testutil.Action{ {"restore", []interface{}{[]byte("foo"), revision{3, 0}, revision{3, 0}, int64(1)}}, {"tombstone", []interface{}{[]byte("foo"), revision{4, 0}}}, } if g := index.Action(); !reflect.DeepEqual(g, wact) { t.Errorf("index action = %+v, want %+v", g, wact) } }
func (s *store) delete(key []byte, mainrev int64) bool { grev := mainrev if s.currentRev.sub > 0 { grev += 1 } rev, _, _, err := s.kvindex.Get(key, grev) if err != nil { // key not exist return false } tx := s.b.BatchTx() tx.Lock() defer tx.Unlock() revbytes := newRevBytes() revToBytes(rev, revbytes) _, vs := tx.UnsafeRange(keyBucketName, revbytes, nil, 0) if len(vs) != 1 { log.Fatalf("storage: delete cannot find rev (%d,%d)", rev.main, rev.sub) } e := &storagepb.Event{} if err := e.Unmarshal(vs[0]); err != nil { log.Fatalf("storage: cannot unmarshal event: %v", err) } if e.Type == storagepb.DELETE { return false } ibytes := newRevBytes() revToBytes(reversion{main: mainrev, sub: s.currentRev.sub}, ibytes) event := storagepb.Event{ Type: storagepb.DELETE, Kv: &storagepb.KeyValue{ Key: key, }, } d, err := event.Marshal() if err != nil { log.Fatalf("storage: cannot marshal event: %v", err) } tx.UnsafePut(keyBucketName, ibytes, d) err = s.kvindex.Tombstone(key, reversion{main: mainrev, sub: s.currentRev.sub}) if err != nil { log.Fatalf("storage: cannot tombstone an existing key (%s): %v", string(key), err) } s.currentRev.sub += 1 return true }
func (s *store) put(key, value []byte, rev int64) { ibytes := newRevBytes() revToBytes(reversion{main: rev, sub: s.currentRev.sub}, ibytes) event := storagepb.Event{ Type: storagepb.PUT, Kv: storagepb.KeyValue{ Key: key, Value: value, }, } d, err := event.Marshal() if err != nil { log.Fatalf("storage: cannot marshal event: %v", err) } tx := s.b.BatchTx() tx.Lock() defer tx.Unlock() tx.UnsafePut(keyBucketName, ibytes, d) s.kvindex.Put(key, reversion{main: rev, sub: s.currentRev.sub}) s.currentRev.sub += 1 }
func TestStoreRangeEvents(t *testing.T) { ev := storagepb.Event{ Type: storagepb.PUT, Kv: &storagepb.KeyValue{ Key: []byte("foo"), Value: []byte("bar"), CreateRevision: 1, ModRevision: 2, Version: 1, }, } evb, err := ev.Marshal() if err != nil { t.Fatal(err) } currev := revision{2, 0} tests := []struct { idxr indexRangeEventsResp r rangeResp }{ { indexRangeEventsResp{[]revision{{2, 0}}}, rangeResp{[][]byte{newTestBytes(revision{2, 0})}, [][]byte{evb}}, }, { indexRangeEventsResp{[]revision{{2, 0}, {3, 0}}}, rangeResp{[][]byte{newTestBytes(revision{2, 0})}, [][]byte{evb}}, }, } for i, tt := range tests { s, b, index := newFakeStore() s.currentRev = currev index.indexRangeEventsRespc <- tt.idxr b.tx.rangeRespc <- tt.r evs, _, err := s.RangeEvents([]byte("foo"), []byte("goo"), 1, 1, 4) if err != nil { t.Errorf("#%d: err = %v, want nil", i, err) } if w := []storagepb.Event{ev}; !reflect.DeepEqual(evs, w) { t.Errorf("#%d: evs = %+v, want %+v", i, evs, w) } wact := []testutil.Action{ {"rangeEvents", []interface{}{[]byte("foo"), []byte("goo"), int64(1)}}, } if g := index.Action(); !reflect.DeepEqual(g, wact) { t.Errorf("#%d: index action = %+v, want %+v", i, g, wact) } wact = []testutil.Action{ {"range", []interface{}{keyBucketName, newTestBytes(tt.idxr.revs[0]), []byte(nil), int64(0)}}, } if g := b.tx.Action(); !reflect.DeepEqual(g, wact) { t.Errorf("#%d: tx action = %+v, want %+v", i, g, wact) } if s.currentRev != currev { t.Errorf("#%d: current rev = %+v, want %+v", i, s.currentRev, currev) } } }