From be29f7ecef1dabbccf7022c97e573667fddf0e2d Mon Sep 17 00:00:00 2001 From: disksing Date: Mon, 28 Jun 2021 10:36:18 +0800 Subject: [PATCH] move unionstore package to internal (#179) Signed-off-by: disksing --- integration_tests/scan_test.go | 3 +- .../unionstore}/main_test.go | 0 {unionstore => internal/unionstore}/memdb.go | 0 .../unionstore}/memdb_arena.go | 0 .../unionstore}/memdb_bench_test.go | 0 .../unionstore}/memdb_iterator.go | 0 .../unionstore}/memdb_norace_test.go | 0 .../unionstore}/memdb_snapshot.go | 0 .../unionstore}/memdb_test.go | 0 {unionstore => internal/unionstore}/mock.go | 0 .../unionstore}/union_iter.go | 0 .../unionstore}/union_store.go | 0 .../unionstore}/union_store_test.go | 0 tikv/2pc.go | 6 +-- tikv/batch_getter.go | 3 +- tikv/snapshot.go | 5 +- tikv/test_probe.go | 2 +- tikv/txn.go | 8 +-- tikv/unionstore_export.go | 51 +++++++++++++++++++ unionstore/unionstore.go | 35 +++++++++++++ 20 files changed, 98 insertions(+), 15 deletions(-) rename {unionstore => internal/unionstore}/main_test.go (100%) rename {unionstore => internal/unionstore}/memdb.go (100%) rename {unionstore => internal/unionstore}/memdb_arena.go (100%) rename {unionstore => internal/unionstore}/memdb_bench_test.go (100%) rename {unionstore => internal/unionstore}/memdb_iterator.go (100%) rename {unionstore => internal/unionstore}/memdb_norace_test.go (100%) rename {unionstore => internal/unionstore}/memdb_snapshot.go (100%) rename {unionstore => internal/unionstore}/memdb_test.go (100%) rename {unionstore => internal/unionstore}/mock.go (100%) rename {unionstore => internal/unionstore}/union_iter.go (100%) rename {unionstore => internal/unionstore}/union_store.go (100%) rename {unionstore => internal/unionstore}/union_store_test.go (100%) create mode 100644 tikv/unionstore_export.go create mode 100644 unionstore/unionstore.go diff --git a/integration_tests/scan_test.go b/integration_tests/scan_test.go index 2c92a471..7d300643 100644 --- a/integration_tests/scan_test.go +++ b/integration_tests/scan_test.go @@ -42,7 +42,6 @@ import ( "github.com/tikv/client-go/v2/kv" "github.com/tikv/client-go/v2/logutil" "github.com/tikv/client-go/v2/tikv" - "github.com/tikv/client-go/v2/unionstore" "github.com/tikv/client-go/v2/util" "go.uber.org/zap" ) @@ -104,7 +103,7 @@ func (s *testScanSuite) makeValue(i int) []byte { } func (s *testScanSuite) TestScan() { - check := func(scan unionstore.Iterator, rowNum int, keyOnly bool) { + check := func(scan tikv.Iterator, rowNum int, keyOnly bool) { for i := 0; i < rowNum; i++ { k := scan.Key() expectedKey := s.makeKey(i) diff --git a/unionstore/main_test.go b/internal/unionstore/main_test.go similarity index 100% rename from unionstore/main_test.go rename to internal/unionstore/main_test.go diff --git a/unionstore/memdb.go b/internal/unionstore/memdb.go similarity index 100% rename from unionstore/memdb.go rename to internal/unionstore/memdb.go diff --git a/unionstore/memdb_arena.go b/internal/unionstore/memdb_arena.go similarity index 100% rename from unionstore/memdb_arena.go rename to internal/unionstore/memdb_arena.go diff --git a/unionstore/memdb_bench_test.go b/internal/unionstore/memdb_bench_test.go similarity index 100% rename from unionstore/memdb_bench_test.go rename to internal/unionstore/memdb_bench_test.go diff --git a/unionstore/memdb_iterator.go b/internal/unionstore/memdb_iterator.go similarity index 100% rename from unionstore/memdb_iterator.go rename to internal/unionstore/memdb_iterator.go diff --git a/unionstore/memdb_norace_test.go b/internal/unionstore/memdb_norace_test.go similarity index 100% rename from unionstore/memdb_norace_test.go rename to internal/unionstore/memdb_norace_test.go diff --git a/unionstore/memdb_snapshot.go b/internal/unionstore/memdb_snapshot.go similarity index 100% rename from unionstore/memdb_snapshot.go rename to internal/unionstore/memdb_snapshot.go diff --git a/unionstore/memdb_test.go b/internal/unionstore/memdb_test.go similarity index 100% rename from unionstore/memdb_test.go rename to internal/unionstore/memdb_test.go diff --git a/unionstore/mock.go b/internal/unionstore/mock.go similarity index 100% rename from unionstore/mock.go rename to internal/unionstore/mock.go diff --git a/unionstore/union_iter.go b/internal/unionstore/union_iter.go similarity index 100% rename from unionstore/union_iter.go rename to internal/unionstore/union_iter.go diff --git a/unionstore/union_store.go b/internal/unionstore/union_store.go similarity index 100% rename from unionstore/union_store.go rename to internal/unionstore/union_store.go diff --git a/unionstore/union_store_test.go b/internal/unionstore/union_store_test.go similarity index 100% rename from unionstore/union_store_test.go rename to internal/unionstore/union_store_test.go diff --git a/tikv/2pc.go b/tikv/2pc.go index 8d0707c6..4e53b0ba 100644 --- a/tikv/2pc.go +++ b/tikv/2pc.go @@ -52,13 +52,13 @@ import ( "github.com/tikv/client-go/v2/config" tikverr "github.com/tikv/client-go/v2/error" "github.com/tikv/client-go/v2/internal/locate" + "github.com/tikv/client-go/v2/internal/unionstore" "github.com/tikv/client-go/v2/kv" "github.com/tikv/client-go/v2/logutil" "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/oracle" "github.com/tikv/client-go/v2/retry" "github.com/tikv/client-go/v2/tikvrpc" - "github.com/tikv/client-go/v2/unionstore" "github.com/tikv/client-go/v2/util" zap "go.uber.org/zap" ) @@ -136,11 +136,11 @@ type twoPhaseCommitter struct { } type memBufferMutations struct { - storage *unionstore.MemDB + storage *MemDB handles []unionstore.MemKeyHandle } -func newMemBufferMutations(sizeHint int, storage *unionstore.MemDB) *memBufferMutations { +func newMemBufferMutations(sizeHint int, storage *MemDB) *memBufferMutations { return &memBufferMutations{ handles: make([]unionstore.MemKeyHandle, 0, sizeHint), storage: storage, diff --git a/tikv/batch_getter.go b/tikv/batch_getter.go index 47b70754..dcd60956 100644 --- a/tikv/batch_getter.go +++ b/tikv/batch_getter.go @@ -37,13 +37,12 @@ import ( "github.com/pingcap/errors" tikverr "github.com/tikv/client-go/v2/error" - "github.com/tikv/client-go/v2/unionstore" ) // BatchBufferGetter is the interface for BatchGet. type BatchBufferGetter interface { Len() int - unionstore.Getter + Getter } // BatchGetter is the interface for BatchGet. diff --git a/tikv/snapshot.go b/tikv/snapshot.go index ff61713d..440eb70b 100644 --- a/tikv/snapshot.go +++ b/tikv/snapshot.go @@ -54,7 +54,6 @@ import ( "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/retry" "github.com/tikv/client-go/v2/tikvrpc" - "github.com/tikv/client-go/v2/unionstore" "github.com/tikv/client-go/v2/util" "go.uber.org/zap" ) @@ -616,13 +615,13 @@ func (s *KVSnapshot) mergeExecDetail(detail *kvrpcpb.ExecDetailsV2) { } // Iter return a list of key-value pair after `k`. -func (s *KVSnapshot) Iter(k []byte, upperBound []byte) (unionstore.Iterator, error) { +func (s *KVSnapshot) Iter(k []byte, upperBound []byte) (Iterator, error) { scanner, err := newScanner(s, k, upperBound, scanBatchSize, false) return scanner, errors.Trace(err) } // IterReverse creates a reversed Iterator positioned on the first entry which key is less than k. -func (s *KVSnapshot) IterReverse(k []byte) (unionstore.Iterator, error) { +func (s *KVSnapshot) IterReverse(k []byte) (Iterator, error) { scanner, err := newScanner(s, nil, k, scanBatchSize, true) return scanner, errors.Trace(err) } diff --git a/tikv/test_probe.go b/tikv/test_probe.go index 0798da8f..f27573ec 100644 --- a/tikv/test_probe.go +++ b/tikv/test_probe.go @@ -42,9 +42,9 @@ import ( "github.com/pingcap/kvproto/pkg/kvrpcpb" "github.com/pingcap/kvproto/pkg/metapb" "github.com/tikv/client-go/v2/internal/locate" + "github.com/tikv/client-go/v2/internal/unionstore" "github.com/tikv/client-go/v2/retry" "github.com/tikv/client-go/v2/tikvrpc" - "github.com/tikv/client-go/v2/unionstore" pd "github.com/tikv/pd/client" ) diff --git a/tikv/txn.go b/tikv/txn.go index 2bb4a78a..3b3466c9 100644 --- a/tikv/txn.go +++ b/tikv/txn.go @@ -49,12 +49,12 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/kvproto/pkg/kvrpcpb" tikverr "github.com/tikv/client-go/v2/error" + "github.com/tikv/client-go/v2/internal/unionstore" tikv "github.com/tikv/client-go/v2/kv" "github.com/tikv/client-go/v2/logutil" "github.com/tikv/client-go/v2/metrics" "github.com/tikv/client-go/v2/oracle" "github.com/tikv/client-go/v2/retry" - "github.com/tikv/client-go/v2/unionstore" "github.com/tikv/client-go/v2/util" "go.uber.org/zap" ) @@ -217,12 +217,12 @@ func (txn *KVTxn) String() string { // If such entry is not found, it returns an invalid Iterator with no error. // It yields only keys that < upperBound. If upperBound is nil, it means the upperBound is unbounded. // The Iterator must be Closed after use. -func (txn *KVTxn) Iter(k []byte, upperBound []byte) (unionstore.Iterator, error) { +func (txn *KVTxn) Iter(k []byte, upperBound []byte) (Iterator, error) { return txn.us.Iter(k, upperBound) } // IterReverse creates a reversed Iterator positioned on the first entry which key is less than k. -func (txn *KVTxn) IterReverse(k []byte) (unionstore.Iterator, error) { +func (txn *KVTxn) IterReverse(k []byte) (Iterator, error) { return txn.us.IterReverse(k) } @@ -763,7 +763,7 @@ func (txn *KVTxn) GetUnionStore() *unionstore.KVUnionStore { } // GetMemBuffer return the MemBuffer binding to this transaction. -func (txn *KVTxn) GetMemBuffer() *unionstore.MemDB { +func (txn *KVTxn) GetMemBuffer() *MemDB { return txn.us.GetMemBuffer() } diff --git a/tikv/unionstore_export.go b/tikv/unionstore_export.go new file mode 100644 index 00000000..8a838047 --- /dev/null +++ b/tikv/unionstore_export.go @@ -0,0 +1,51 @@ +// Copyright 2021 TiKV Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +// NOTE: The code in this file is based on code from the +// TiDB project, licensed under the Apache License v 2.0 +// +// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/txn.go +// + +// Copyright 2016 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package tikv + +import "github.com/tikv/client-go/v2/internal/unionstore" + +// Getter is the interface for the Get method. +type Getter = unionstore.Getter + +// Iterator is the interface for a iterator on KV store. +type Iterator = unionstore.Iterator + +// MemDB is rollbackable Red-Black Tree optimized for transaction states buffer use scenario. +// You can think MemDB is a combination of two separate tree map, one for key => value and another for key => keyFlags. +// +// The value map is rollbackable, that means you can use the `Staging`, `Release` and `Cleanup` API to safely modify KVs. +// +// The flags map is not rollbackable. There are two types of flag, persistent and non-persistent. +// When discarding a newly added KV in `Cleanup`, the non-persistent flags will be cleared. +// If there are persistent flags associated with key, we will keep this key in node without value. +type MemDB = unionstore.MemDB diff --git a/unionstore/unionstore.go b/unionstore/unionstore.go new file mode 100644 index 00000000..93223680 --- /dev/null +++ b/unionstore/unionstore.go @@ -0,0 +1,35 @@ +// Copyright 2021 TiKV Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package unionstore + +import "github.com/tikv/client-go/v2/internal/unionstore" + +// Re-export internal/unionstore for compatibility. +// TODO: Remove it. + +// Getter is the interface for the Get method. +type Getter = unionstore.Getter + +// Iterator is the interface for a iterator on KV store. +type Iterator = unionstore.Iterator + +// MemDB is rollbackable Red-Black Tree optimized for transaction states buffer use scenario. +// You can think MemDB is a combination of two separate tree map, one for key => value and another for key => keyFlags. +// +// The value map is rollbackable, that means you can use the `Staging`, `Release` and `Cleanup` API to safely modify KVs. +// +// The flags map is not rollbackable. There are two types of flag, persistent and non-persistent. +// When discarding a newly added KV in `Cleanup`, the non-persistent flags will be cleared. +// If there are persistent flags associated with key, we will keep this key in node without value. +type MemDB = unionstore.MemDB