|
1 | 1 | /*
|
2 |
| -Copyright IBM Corp. 2016 All Rights Reserved. |
| 2 | +Copyright IBM Corp. All Rights Reserved. |
3 | 3 |
|
4 |
| -Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
| -you may not use this file except in compliance with the License. |
6 |
| -You may obtain a copy of the License at |
7 |
| -
|
8 |
| - http://www.apache.org/licenses/LICENSE-2.0 |
9 |
| -
|
10 |
| -Unless required by applicable law or agreed to in writing, software |
11 |
| -distributed under the License is distributed on an "AS IS" BASIS, |
12 |
| -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
| -See the License for the specific language governing permissions and |
14 |
| -limitations under the License. |
| 4 | +SPDX-License-Identifier: Apache-2.0 |
15 | 5 | */
|
16 | 6 |
|
17 | 7 | package historydb
|
18 | 8 |
|
19 | 9 | import (
|
| 10 | + "bytes" |
20 | 11 | "testing"
|
21 | 12 |
|
| 13 | + "github.com/hyperledger/fabric/common/ledger/util" |
22 | 14 | "github.com/stretchr/testify/assert"
|
23 | 15 | )
|
24 | 16 |
|
25 | 17 | var strKeySep = string(CompositeKeySep)
|
26 | 18 |
|
27 |
| -func TestConstructCompositeKey(t *testing.T) { |
28 |
| - compositeKey := ConstructCompositeHistoryKey("ns1", "key1", 1, 1) |
29 |
| - assert.NotNil(t, compositeKey) |
30 |
| - //historyleveldb_test.go tests the actual output |
31 |
| -} |
| 19 | +func TestCompositeKeyConstruction(t *testing.T) { |
| 20 | + type keyComponents struct { |
| 21 | + ns, key string |
| 22 | + blkNum, tranNum uint64 |
| 23 | + } |
32 | 24 |
|
33 |
| -func TestConstructPartialCompositeKey(t *testing.T) { |
34 |
| - compositeStartKey := ConstructPartialCompositeHistoryKey("ns1", "key1", false) |
35 |
| - compositeEndKey := ConstructPartialCompositeHistoryKey("ns1", "key1", true) |
| 25 | + testData := []*keyComponents{ |
| 26 | + {"ns1", "key1", 1, 0}, |
| 27 | + {"ns1", "key1\x00", 1, 5}, |
| 28 | + {"ns1", "key1\x00\x00", 100, 100}, |
| 29 | + {"ns1", "key\x00\x001", 100, 100}, |
| 30 | + {"ns1", "\x00key\x00\x001", 100, 100}, |
| 31 | + } |
36 | 32 |
|
37 |
| - assert.Equal(t, []byte("ns1"+strKeySep+"key1"+strKeySep), compositeStartKey) |
38 |
| - assert.Equal(t, []byte("ns1"+strKeySep+"key1"+strKeySep+string([]byte{0xff})), compositeEndKey) |
| 33 | + for _, testDatum := range testData { |
| 34 | + key := ConstructCompositeHistoryKey(testDatum.ns, testDatum.key, testDatum.blkNum, testDatum.tranNum) |
| 35 | + startKey := ConstructPartialCompositeHistoryKey(testDatum.ns, testDatum.key, false) |
| 36 | + endKey := ConstructPartialCompositeHistoryKey(testDatum.ns, testDatum.key, true) |
| 37 | + assert.Equal(t, bytes.Compare(startKey, key), -1) //startKey should be smaller than key |
| 38 | + assert.Equal(t, bytes.Compare(endKey, key), 1) //endKey should be greater than key |
| 39 | + } |
| 40 | + |
| 41 | + for i, testDatum := range testData { |
| 42 | + for j, another := range testData { |
| 43 | + if i == j { |
| 44 | + continue |
| 45 | + } |
| 46 | + startKey := ConstructPartialCompositeHistoryKey(testDatum.ns, testDatum.key, false) |
| 47 | + endKey := ConstructPartialCompositeHistoryKey(testDatum.ns, testDatum.key, true) |
| 48 | + |
| 49 | + anotherKey := ConstructCompositeHistoryKey(another.ns, another.key, another.blkNum, another.tranNum) |
| 50 | + assert.False(t, bytes.Compare(anotherKey, startKey) == 1 && bytes.Compare(anotherKey, endKey) == -1) //any key should not fall in the range of start/end key range query for any other key |
| 51 | + } |
| 52 | + } |
39 | 53 | }
|
40 | 54 |
|
41 | 55 | func TestSplitCompositeKey(t *testing.T) {
|
42 |
| - compositeFullKey := []byte("ns1" + strKeySep + "key1" + strKeySep + "extra bytes to split") |
| 56 | + compositeFullKey := ConstructCompositeHistoryKey("ns1", "key1", 20, 200) |
43 | 57 | compositePartialKey := ConstructPartialCompositeHistoryKey("ns1", "key1", false)
|
44 |
| - |
45 | 58 | _, extraBytes := SplitCompositeHistoryKey(compositeFullKey, compositePartialKey)
|
| 59 | + blkNum, bytesConsumed, err := util.DecodeOrderPreservingVarUint64(extraBytes) |
| 60 | + assert.NoError(t, err) |
| 61 | + txNum, _, err := util.DecodeOrderPreservingVarUint64(extraBytes[bytesConsumed:]) |
| 62 | + assert.NoError(t, err) |
46 | 63 | // second position should hold the extra bytes that were split off
|
47 |
| - assert.Equal(t, []byte("extra bytes to split"), extraBytes) |
| 64 | + assert.Equal(t, blkNum, uint64(20)) |
| 65 | + assert.Equal(t, txNum, uint64(200)) |
48 | 66 | }
|
0 commit comments