From f73eea3fb434a98011237172655c409a9a5a4209 Mon Sep 17 00:00:00 2001 From: senthil Date: Thu, 4 May 2017 15:03:26 +0530 Subject: [PATCH] [FAB-3654] Remove ledger/util/filterbitarray.go To create/configure transaction filter metadata in a block, ledger/util/txvalidationflags.go is used instead of ledger/util/filterbitarray.go. As filterbitarray.go is no longer used by any module, we remove the file in this CR. Change-Id: I9e517f3503a84d14774a752c638797fa292ca053 Signed-off-by: senthil --- core/ledger/util/filterbitarray.go | 153 ------------------------ core/ledger/util/filterbitarray_test.go | 140 ---------------------- 2 files changed, 293 deletions(-) delete mode 100644 core/ledger/util/filterbitarray.go delete mode 100644 core/ledger/util/filterbitarray_test.go diff --git a/core/ledger/util/filterbitarray.go b/core/ledger/util/filterbitarray.go deleted file mode 100644 index 62a92be14cc..00000000000 --- a/core/ledger/util/filterbitarray.go +++ /dev/null @@ -1,153 +0,0 @@ -/* -Copyright IBM Corp. 2016 All Rights Reserved. - -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, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package util - -// FilterBitArray is an array of bits based on byte unit, so 8 bits at each -// index. The array automatically increases if the set index is larger than the -// current capacity. The bit index starts at 0. -type FilterBitArray []byte - -const ( - byteMask byte = 0xFF - byteSize = 8 -) - -// NewFilterBitArray creates an array with the specified bit-size. This is an -// optimization to make array once for the expected capacity rather than -// using Set function to auto-increase the array. -func NewFilterBitArray(size uint) FilterBitArray { - ba := make(FilterBitArray, (size-1)/byteSize+1) - return ba -} - -// NewFilterBitArrayFromBytes reconstructs an array from given byte array. -func NewFilterBitArrayFromBytes(bytes []byte) FilterBitArray { - bitArray := FilterBitArray{} - bitArray.FromBytes(bytes) - return bitArray -} - -// Capacity returns the number of bits in the FilterBitArray. -func (ba *FilterBitArray) Capacity() uint { - return uint(len(*ba) * byteSize) -} - -// Set assigns 1 to the specified bit-index, which is starting from 0. -// Set automatically increases the array to accommodate the bit-index. -func (ba *FilterBitArray) Set(i uint) { - // Location of i in the array index is floor(i/byte_size) + 1. If it exceeds the - // current byte array, we'll make a new one large enough to include the - // specified bit-index - if i >= ba.Capacity() { - ba.expand(i/byteSize + 1) - } - (*ba)[i/byteSize] |= 1 << (i % byteSize) -} - -// SetRange assigns 1 to the bit-indexes specified by range [begin, end] -// Set automatically increases the array to accommodate the bit-index. -func (ba *FilterBitArray) SetRange(begin uint, end uint) { - // Location of i in the array index is floor(i/byte_size) + 1. If it exceeds the - // current byte array, we'll make a new one large enough to include the - // specified bit-index - startByteIndex := ba.byteIndex(begin) - endByteIndex := ba.byteIndex(end) - - if end >= ba.Capacity() { - ba.expand(endByteIndex + 1) - } - - firstByteMask := byteMask << (begin % byteSize) - lastByteMask := byteMask >> ((byteSize - end - 1) % byteSize) - - if startByteIndex == endByteIndex { - (*ba)[startByteIndex] |= (firstByteMask & lastByteMask) - } else { - (*ba)[startByteIndex] |= firstByteMask - for i := startByteIndex + 1; i < endByteIndex; i++ { - (*ba)[i] = byteMask - } - (*ba)[endByteIndex] |= lastByteMask - } -} - -// Unset assigns 0 the specified bit-index. If bit-index is larger than capacity, -// do nothing. -func (ba *FilterBitArray) Unset(i uint) { - if i < ba.Capacity() { - (*ba)[i/byteSize] &^= 1 << (i % byteSize) - } -} - -// UnsetRange assigns 0 to all bits in range [begin, end]. If bit-index is larger than capacity, -// do nothing. -func (ba *FilterBitArray) UnsetRange(begin uint, end uint) { - if begin > ba.Capacity() || begin == end { - return - } - - startByteIndex := ba.byteIndex(begin) - endByteIndex := ba.byteIndex(end) - - firstByteMask := byteMask << (begin % byteSize) - lastByteMask := byteMask >> ((byteSize - end - 1) % byteSize) - - if startByteIndex == endByteIndex { - (*ba)[startByteIndex] &= ^(firstByteMask & lastByteMask) - } else { - (*ba)[startByteIndex] &= ^firstByteMask - for i := startByteIndex + 1; i < endByteIndex; i++ { - (*ba)[i] = 0 - } - (*ba)[endByteIndex] &= ^lastByteMask - } -} - -// ValueAt returns the value at the specified bit-index. If bit-index is out -// of range, return 0. Note that the returned value is in byte, so it may be -// a power of 2 if not 0. -func (ba *FilterBitArray) ValueAt(i uint) byte { - if i < ba.Capacity() { - return (*ba)[i/byteSize] & (1 << (i % byteSize)) - } - return 0 -} - -// IsSet returns true if the specified bit-index is 1; false otherwise. -func (ba *FilterBitArray) IsSet(i uint) bool { - return (ba.ValueAt(i) != 0) -} - -// ToBytes returns the byte array for storage. -func (ba *FilterBitArray) ToBytes() []byte { - return *ba -} - -// FromBytes accepts a byte array. -func (ba *FilterBitArray) FromBytes(bytes []byte) { - *ba = bytes -} - -func (ba *FilterBitArray) expand(newSize uint) { - array := make([]byte, newSize) - copy(array, *ba) - *ba = array -} - -func (ba *FilterBitArray) byteIndex(i uint) uint { - return i / byteSize -} diff --git a/core/ledger/util/filterbitarray_test.go b/core/ledger/util/filterbitarray_test.go deleted file mode 100644 index ba701c498a1..00000000000 --- a/core/ledger/util/filterbitarray_test.go +++ /dev/null @@ -1,140 +0,0 @@ -/* -Copyright IBM Corp. 2016 All Rights Reserved. - -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, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -// To run this test by itself, enter `go test -run FilterBitArray` from this folder -package util - -import ( - "bytes" - "encoding/binary" - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestFilterBitArrayFixed(t *testing.T) { - ba := NewFilterBitArray(25) - // Note that capacity may be greater than 25 - t.Logf("FilterBitArray capacity: %d\n", ba.Capacity()) - var i uint - for i = 0; i < ba.Capacity(); i++ { - ba.Set(i) - } - // All bits must be set - for i = 0; i < ba.Capacity(); i++ { - if !ba.IsSet(i) { - t.FailNow() - } - } - for i = 0; i < ba.Capacity(); i++ { - ba.Unset(i) - } - // All bits must be unset - for i = 0; i < ba.Capacity(); i++ { - if ba.IsSet(i) { - t.FailNow() - } - } -} - -func TestFilterBitArraySparse(t *testing.T) { - ba := new(FilterBitArray) - // test byte boundary - ba.Unset(0) - ba.Set(8) - ba.Unset(9) - ba.Set(116) - if ba.IsSet(0) { - t.FailNow() - } - if !ba.IsSet(8) { - t.FailNow() - } - if ba.IsSet(9) { - t.FailNow() - } - if !ba.IsSet(116) { - t.FailNow() - } -} - -func TestFilterBitArrayIO(t *testing.T) { - ba := NewFilterBitArray(20) - var i uint - for i = 0; i < 20; i++ { - if i%2 == 0 { - ba.Set(i) - } else { - ba.Unset(i) - } - } - b := ba.ToBytes() - buf := new(bytes.Buffer) - if err := binary.Write(buf, binary.LittleEndian, b); err != nil { - t.Fatalf("binary.Write failed: %s", err) - } - if err := binary.Read(buf, binary.LittleEndian, b); err != nil { - t.Fatalf("binary.Read failed: %s", err) - } - ba.FromBytes(b) - for i = 0; i < 20; i++ { - if i%2 == 0 { - if !ba.IsSet(i) { - t.FailNow() - } - } else { - if ba.IsSet(i) { - t.FailNow() - } - } - } -} - -func TestFilterBitArrayRangeFuncs(t *testing.T) { - ba := NewFilterBitArray(12) - - // 1111 1110 0111 1111 ==> { 254, 127 } - ba.SetRange(1, 14) - assert.True(t, bytes.Equal(ba.ToBytes(), []byte{254, 127})) - - // 0111 1110 0111 1110 ==> { 126, 126 } - ba.UnsetRange(7, 8) - assert.True(t, bytes.Equal(ba.ToBytes(), []byte{126, 126})) - - if !ba.IsSet(11) { - t.FailNow() - } - - if !ba.IsSet(1) { - t.FailNow() - } - - // 1100 0000 0111 1110 0111 1110 ==> { 126, 126, 192 } - ba.SetRange(22, 23) - assert.Equal(t, ba.ToBytes(), []byte{126, 126, 192}) - - if ba.IsSet(15) { - t.FailNow() - } - - if ba.IsSet(20) { - t.FailNow() - } - - // 1100 0000 0111 1110 0000 1110 ==> { 198, 127, 192 } - ba.UnsetRange(4, 6) - assert.Equal(t, ba.ToBytes(), []byte{14, 126, 192}) -}