func (d *ddl) getSnapshotRows(t table.Table, version uint64, seekHandle int64) ([]int64, error) { ver := kv.Version{Ver: version} snap, err := d.store.GetSnapshot(ver) if err != nil { return nil, errors.Trace(err) } defer snap.MvccRelease() firstKey := t.RecordKey(seekHandle, nil) prefix := []byte(t.KeyPrefix()) it := snap.NewMvccIterator(kv.EncodeKey([]byte(firstKey)), ver) defer it.Close() handles := make([]int64, 0, maxBatchSize) for it.Valid() { key := kv.DecodeKey([]byte(it.Key())) if !bytes.HasPrefix(key, prefix) { break } var handle int64 handle, err = util.DecodeHandleFromRowKey(string(key)) if err != nil { return nil, errors.Trace(err) } rk := kv.EncodeKey(t.RecordKey(handle, nil)) handles = append(handles, handle) if len(handles) == maxBatchSize { seekHandle = handle + 1 break } err = kv.NextUntil(it, util.RowKeyPrefixFilter(rk)) if terror.ErrorEqual(err, kv.ErrNotExist) { break } else if err != nil { return nil, errors.Trace(err) } } return handles, nil }
func (txn *dbTxn) Inc(k []byte, step int64) (int64, error) { log.Debugf("Inc %s, step %d txn:%d", k, step, txn.tID) k = kv.EncodeKey(k) if err := txn.markOrigin(k); err != nil { return 0, err } val, err := txn.UnionStore.Get(k) if kv.IsErrNotFound(err) { err = txn.UnionStore.Set(k, []byte(strconv.FormatInt(step, 10))) if err != nil { return 0, err } return step, nil } if err != nil { return 0, err } intVal, err := strconv.ParseInt(string(val), 10, 0) if err != nil { return intVal, err } intVal += step err = txn.UnionStore.Set(k, []byte(strconv.FormatInt(intVal, 10))) if err != nil { return 0, err } return intVal, nil }
func (txn *dbTxn) LockKeys(keys ...kv.Key) error { for _, key := range keys { key = kv.EncodeKey(key) txn.markOrigin(key) } return nil }
func (txn *dbTxn) Inc(k kv.Key, step int64) (int64, error) { log.Debugf("Inc %q, step %d txn:%d", k, step, txn.tid) k = kv.EncodeKey(k) txn.markOrigin(k) val, err := txn.UnionStore.Get(k) if kv.IsErrNotFound(err) { err = txn.UnionStore.Set(k, []byte(strconv.FormatInt(step, 10))) if err != nil { return 0, errors.Trace(err) } return step, nil } if err != nil { return 0, errors.Trace(err) } intVal, err := strconv.ParseInt(string(val), 10, 0) if err != nil { return intVal, errors.Trace(err) } intVal += step err = txn.UnionStore.Set(k, []byte(strconv.FormatInt(intVal, 10))) if err != nil { return 0, errors.Trace(err) } txn.store.compactor.OnSet(k) return intVal, nil }
func (txn *dbTxn) BatchPrefetch(keys []kv.Key) error { encodedKeys := make([]kv.Key, len(keys)) for i, k := range keys { encodedKeys[i] = kv.EncodeKey(k) } _, err := txn.UnionStore.Snapshot.BatchGet(encodedKeys) return err }
func (txn *hbaseTxn) LockKeys(keys ...kv.Key) error { for _, key := range keys { key = kv.EncodeKey(key) if err := txn.txn.LockRow(txn.storeName, key); err != nil { return errors.Trace(err) } } return nil }
func (txn *hbaseTxn) Delete(k kv.Key) error { log.Debugf("delete %q txn:%d", k, txn.tid) k = kv.EncodeKey(k) err := txn.UnionStore.Delete(k) if err != nil { return errors.Trace(err) } return nil }
func (txn *hbaseTxn) Seek(k kv.Key) (kv.Iterator, error) { log.Debugf("seek %q txn:%d", k, txn.tid) k = kv.EncodeKey(k) iter, err := txn.UnionStore.Seek(k, txn) if err != nil { return nil, errors.Trace(err) } return kv.NewDecodeKeyIter(iter), nil }
func (txn *dbTxn) LockKeys(keys ...kv.Key) error { for _, key := range keys { key = kv.EncodeKey(key) if err := txn.markOrigin(key); err != nil { return errors.Trace(err) } } return nil }
func (txn *dbTxn) LockKeys(keys ...[]byte) error { for _, key := range keys { key = kv.EncodeKey(key) if err := txn.markOrigin(key); err != nil { return err } } return nil }
func (txn *dbTxn) Delete(k kv.Key) error { log.Debugf("delete key:%q, txn:%d", k, txn.tid) k = kv.EncodeKey(k) err := txn.UnionStore.Delete(k) if err != nil { return errors.Trace(err) } txn.store.compactor.OnDelete(k) return nil }
func (txn *dbTxn) Set(k []byte, data []byte) error { if len(data) == 0 { // Incase someone use it in the wrong way, we can figure it out immediately debug.PrintStack() return ErrCannotSetNilValue } log.Debugf("set key:%s, txn:%d", k, txn.tID) k = kv.EncodeKey(k) return txn.UnionStore.Set(k, data) }
func (txn *hbaseTxn) Set(k kv.Key, data []byte) error { if len(data) == 0 { // Incase someone use it in the wrong way, we can figure it out immediately debug.PrintStack() return errors.Trace(ErrCannotSetNilValue) } log.Debugf("set key:%q, txn:%d", k, txn.tid) k = kv.EncodeKey(k) return txn.UnionStore.Set(k, data) }
func (txn *hbaseTxn) Get(k kv.Key) ([]byte, error) { log.Debugf("get key:%q, txn:%d", k, txn.tid) k = kv.EncodeKey(k) val, err := txn.UnionStore.Get(k) if kv.IsErrNotFound(err) || len(val) == 0 { return nil, errors.Trace(kv.ErrNotExist) } if err != nil { return nil, errors.Trace(err) } return val, nil }
func (txn *dbTxn) GetInt64(k kv.Key) (int64, error) { k = kv.EncodeKey(k) val, err := txn.UnionStore.Get(k) if kv.IsErrNotFound(err) { return 0, nil } if err != nil { return 0, errors.Trace(err) } intVal, err := strconv.ParseInt(string(val), 10, 0) return intVal, errors.Trace(err) }
func (txn *dbTxn) Seek(k kv.Key) (kv.Iterator, error) { log.Debugf("seek key:%q, txn:%d", k, txn.tid) k = kv.EncodeKey(k) iter, err := txn.UnionStore.Seek(k, txn) if err != nil { return nil, errors.Trace(err) } if !iter.Valid() { return &kv.UnionIter{}, nil } return kv.NewDecodeKeyIter(iter), nil }
func (txn *dbTxn) Set(k kv.Key, data []byte) error { if len(data) == 0 { // Incase someone use it in the wrong way, we can figure it out immediately. debug.PrintStack() return errors.Trace(ErrCannotSetNilValue) } log.Debugf("set key:%q, txn:%d", k, txn.tid) k = kv.EncodeKey(k) err := txn.UnionStore.Set(k, data) if err != nil { return errors.Trace(err) } txn.store.compactor.OnSet(k) return nil }
func (s *testKVSuite) TestDBClose(c *C) { path := "memory:test" d := Driver{ goleveldb.MemoryDriver{}, } store, err := d.Open(path) c.Assert(err, IsNil) txn, err := store.Begin() c.Assert(err, IsNil) err = txn.Set([]byte("a"), []byte("b")) c.Assert(err, IsNil) err = txn.Commit() c.Assert(err, IsNil) ver, err := store.CurrentVersion() c.Assert(err, IsNil) c.Assert(kv.MaxVersion.Cmp(ver), Equals, 1) snap, err := store.GetSnapshot(kv.MaxVersion) c.Assert(err, IsNil) _, err = snap.MvccGet(kv.EncodeKey([]byte("a")), kv.MaxVersion) c.Assert(err, IsNil) txn, err = store.Begin() c.Assert(err, IsNil) err = store.Close() c.Assert(err, IsNil) _, err = store.Begin() c.Assert(err, NotNil) _, err = store.GetSnapshot(kv.MaxVersion) c.Assert(err, NotNil) err = txn.Set([]byte("a"), []byte("b")) c.Assert(err, IsNil) err = txn.Commit() c.Assert(err, NotNil) snap.MvccRelease() }
func (txn *dbTxn) Get(k []byte) ([]byte, error) { log.Debugf("get key:%s, txn:%d", k, txn.tID) k = kv.EncodeKey(k) val, err := txn.UnionStore.Get(k) if kv.IsErrNotFound(err) { return nil, kv.ErrNotExist } if err != nil { return nil, err } if len(val) == 0 { return nil, kv.ErrNotExist } return val, nil }
func (txn *dbTxn) Seek(k kv.Key, fnKeyCmp func(kv.Key) bool) (kv.Iterator, error) { log.Debugf("seek key:%q, txn:%d", k, txn.tID) k = kv.EncodeKey(k) iter, err := txn.UnionStore.Seek(k, txn) if err != nil { return nil, errors.Trace(err) } if !iter.Valid() { return &kv.UnionIter{}, nil } if fnKeyCmp != nil { if fnKeyCmp([]byte(iter.Key())[:1]) { return &kv.UnionIter{}, nil } } return iter, nil }
func (txn *dbTxn) Delete(k kv.Key) error { log.Debugf("delete key:%q, txn:%d", k, txn.tID) k = kv.EncodeKey(k) return txn.UnionStore.Delete(k) }
func (txn *dbTxn) RangePrefetch(start, end kv.Key, limit int) error { _, err := txn.UnionStore.Snapshot.RangeGet(kv.EncodeKey(start), kv.EncodeKey(end), limit) return err }
func encodeTestDataKey(i int) []byte { return kv.EncodeKey(encodeInt(i)) }
func (txn *dbTxn) Delete(k []byte) error { log.Debugf("delete %s txn:%d", k, txn.tID) k = kv.EncodeKey(k) return txn.UnionStore.Delete(k) }