Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FAB-6553] Ledger bookkeeping provider
This CR introduces a bookkeeping provider. This is intented to be used in maintaining different aspects of ledger data management. The initial use would be to maintain the expiry schedule of pvt data items in the pvt statedb Change-Id: I21f5eef5034cc413c39fdf4bee798fefafbcdf27 Signed-off-by: manish <manish.sethi@gmail.com>
- Loading branch information
1 parent
3ac6358
commit b966981
Showing
4 changed files
with
139 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package bookkeeping | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper" | ||
"github.com/hyperledger/fabric/core/ledger/ledgerconfig" | ||
) | ||
|
||
// Category is an enum type for representing the bookkeeping of different type | ||
type Category int | ||
|
||
const ( | ||
// PvtdataExpiry repersents the bookkeeping related to expiry of pvtdata because of BTL policy | ||
PvtdataExpiry Category = iota | ||
) | ||
|
||
// Provider provides handle to different bookkeepers for the given ledger | ||
type Provider interface { | ||
// GetDBHandle returns a db handle that can be used for maintaining the bookkeeping of a given category | ||
GetDBHandle(ledgerID string, cat Category) *leveldbhelper.DBHandle | ||
// Close closes the BookkeeperProvider | ||
Close() | ||
} | ||
|
||
type provider struct { | ||
dbProvider *leveldbhelper.Provider | ||
} | ||
|
||
// NewProvider instantiates a new provider | ||
func NewProvider() Provider { | ||
dbProvider := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: getInternalBookkeeperPath()}) | ||
return &provider{dbProvider: dbProvider} | ||
} | ||
|
||
// GetDBHandle implements the function in the interface 'BookkeeperProvider' | ||
func (provider *provider) GetDBHandle(ledgerID string, cat Category) *leveldbhelper.DBHandle { | ||
return provider.dbProvider.GetDBHandle(fmt.Sprintf(ledgerID+"/%d", cat)) | ||
} | ||
|
||
// Close implements the function in the interface 'BookKeeperProvider' | ||
func (provider *provider) Close() { | ||
provider.dbProvider.Close() | ||
} | ||
|
||
func getInternalBookkeeperPath() string { | ||
return ledgerconfig.GetInternalBookkeeperPath() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
Copyright IBM Corp. All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package bookkeeping | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
viper.Set("peer.fileSystemPath", "/tmp/fabric/ledgertests/kvledger/bookkeeping") | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestProvider(t *testing.T) { | ||
testEnv := NewTestEnv(t) | ||
defer testEnv.Cleanup() | ||
p := testEnv.TestProvider | ||
db := p.GetDBHandle("TestLedger", PvtdataExpiry) | ||
assert.NoError(t, db.Put([]byte("key"), []byte("value"), true)) | ||
val, err := db.Get([]byte("key")) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte("value"), val) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
Copyright IBM Corp. 2017 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 bookkeeping | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
// TestEnv provides the bookkeeper provider env for testing | ||
type TestEnv struct { | ||
t testing.TB | ||
TestProvider Provider | ||
} | ||
|
||
// NewTestEnv construct a TestEnv for testing | ||
func NewTestEnv(t *testing.T) *TestEnv { | ||
removePath(t) | ||
provider := NewProvider() | ||
return &TestEnv{t, provider} | ||
} | ||
|
||
// Cleanup cleansup the store env after testing | ||
func (env *TestEnv) Cleanup() { | ||
env.TestProvider.Close() | ||
removePath(env.t) | ||
} | ||
|
||
func removePath(t testing.TB) { | ||
dbPath := getInternalBookkeeperPath() | ||
if err := os.RemoveAll(dbPath); err != nil { | ||
t.Fatalf("Err: %s", err) | ||
t.FailNow() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters