Skip to content

Commit af6e1a6

Browse files
committed
FAB-1140 Move couchDB code to Util
Move the couchDB common code to util directory for use by both the state and history database functionality. Change-Id: Ia6ce37a6ae576fdc9faf46666b4ff1a0269bc1f5 Signed-off-by: Mari Wade <[email protected]>
1 parent ed2d579 commit af6e1a6

File tree

10 files changed

+289
-31
lines changed

10 files changed

+289
-31
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
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.
15+
*/
16+
17+
package history
18+
19+
import (
20+
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
21+
"github.com/hyperledger/fabric/protos/common"
22+
logging "github.com/op/go-logging"
23+
)
24+
25+
var logger = logging.MustGetLogger("txhistorymgmt")
26+
27+
// CouchDBHistMgr a simple implementation of interface `histmgmt.TxHistMgr'.
28+
// TODO This implementation does not currently use a lock but may need one to insure query's are consistent
29+
type CouchDBHistMgr struct {
30+
couchDB *couchdb.CouchDBConnectionDef // COUCHDB new properties for CouchDB
31+
}
32+
33+
// NewCouchDBHistMgr constructs a new `CouchDBTxHistMgr`
34+
func NewCouchDBHistMgr(couchDBConnectURL string, dbName string, id string, pw string) *CouchDBHistMgr {
35+
36+
//TODO locking has not been implemented but may need some sort of locking to insure queries are valid data.
37+
38+
couchDB, err := couchdb.CreateCouchDBConnectionAndDB(couchDBConnectURL, dbName, id, pw)
39+
if err != nil {
40+
logger.Errorf("Error during NewCouchDBHistMgr(): %s\n", err.Error())
41+
return nil
42+
}
43+
44+
// db and stateIndexCF will not be used for CouchDB. TODO to cleanup
45+
return &CouchDBHistMgr{couchDB: couchDB}
46+
}
47+
48+
// Commit implements method in interface `txhistorymgmt.TxMgr`
49+
func (histmgr *CouchDBHistMgr) Commit(block *common.Block) error {
50+
logger.Debugf("===HISTORYDB=== Entering CouchDBTxHistMgr.Commit()")
51+
return nil
52+
}
+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
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.
15+
*/
16+
17+
package history
18+
19+
import (
20+
"fmt"
21+
"os"
22+
"testing"
23+
24+
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
25+
"github.com/hyperledger/fabric/core/ledger/testutil"
26+
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
27+
)
28+
29+
//Complex setup to test the use of couch in ledger
30+
type testEnvCouch struct {
31+
couchDBPath string
32+
couchDBAddress string
33+
couchDatabaseName string
34+
couchUsername string
35+
couchPassword string
36+
}
37+
38+
func newTestEnvCouch(t testing.TB, dbPath string, dbName string) *testEnvCouch {
39+
40+
couchDBDef := ledgerconfig.GetCouchDBDefinition()
41+
os.RemoveAll(dbPath)
42+
43+
return &testEnvCouch{
44+
couchDBPath: dbPath,
45+
couchDBAddress: couchDBDef.URL,
46+
couchDatabaseName: dbName,
47+
couchUsername: couchDBDef.Username,
48+
couchPassword: couchDBDef.Password,
49+
}
50+
}
51+
52+
func (env *testEnvCouch) cleanup() {
53+
os.RemoveAll(env.couchDBPath)
54+
//create a new connection
55+
couchDB, _ := couchdb.CreateConnectionDefinition(env.couchDBAddress, env.couchDatabaseName, env.couchUsername, env.couchPassword)
56+
//drop the test database if it already existed
57+
couchDB.DropDatabase()
58+
}
59+
60+
// couchdb_test.go tests couchdb functions already. This test just tests that a CouchDB history database is auto-created
61+
// upon creating a new history transaction manager
62+
func TestHistoryDatabaseAutoCreate(t *testing.T) {
63+
64+
//call a helper method to load the core.yaml
65+
testutil.SetupCoreYAMLConfig("./../../../peer")
66+
67+
//Only run the tests if CouchDB is explitily enabled in the code,
68+
//otherwise CouchDB may not be installed and all the tests would fail
69+
//TODO replace this with external config property rather than config within the code
70+
if ledgerconfig.IsCouchDBEnabled() == true {
71+
72+
env := newTestEnvCouch(t, "/tmp/tests/ledger/history", "history-test")
73+
env.cleanup() //cleanup at the beginning to ensure the database doesn't exist already
74+
defer env.cleanup() //and cleanup at the end
75+
76+
histMgr := NewCouchDBHistMgr(
77+
env.couchDBAddress, //couchDB Address
78+
env.couchDatabaseName, //couchDB db name
79+
env.couchUsername, //enter couchDB id
80+
env.couchPassword) //enter couchDB pw
81+
82+
//NewCouchDBhistMgr should have automatically created the database, let's make sure it has been created
83+
//Retrieve the info for the new database and make sure the name matches
84+
dbResp, _, errdb := histMgr.couchDB.GetDatabaseInfo()
85+
testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve database information"))
86+
testutil.AssertEquals(t, dbResp.DbName, env.couchDatabaseName)
87+
88+
//Call NewCouchDBhistMgr again, this time the database will already exist from last time
89+
histMgr2 := NewCouchDBHistMgr(
90+
env.couchDBAddress, //couchDB Address
91+
env.couchDatabaseName, //couchDB db name
92+
env.couchUsername, //enter couchDB id
93+
env.couchPassword) //enter couchDB pw
94+
95+
//Retrieve the info for the database again, and make sure the name still matches
96+
dbResp2, _, errdb2 := histMgr2.couchDB.GetDatabaseInfo()
97+
testutil.AssertNoError(t, errdb2, fmt.Sprintf("Error when trying to retrieve database information"))
98+
testutil.AssertEquals(t, dbResp2.DbName, env.couchDatabaseName)
99+
100+
}
101+
102+
}

core/ledger/history/histmgmt.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
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.
15+
*/
16+
17+
package history
18+
19+
import "github.com/hyperledger/fabric/protos/common"
20+
21+
// TxHistMgr - an interface that a transaction history manager should implement
22+
type TxHistMgr interface {
23+
Commit(block *common.Block) error
24+
}

core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb_txmgmt_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import (
2121
"os"
2222
"testing"
2323

24-
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb"
2524
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
2625
"github.com/hyperledger/fabric/core/ledger/testutil"
26+
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
2727
)
2828

2929
type testEnv struct {
@@ -97,7 +97,7 @@ func TestDatabaseAutoCreate(t *testing.T) {
9797
env.couchPassword) //enter couchDB pw
9898

9999
//Retrieve the info for the database again, and make sure the name still matches
100-
dbResp2, _, errdb2 := txMgr.couchDB.GetDatabaseInfo()
100+
dbResp2, _, errdb2 := txMgr2.couchDB.GetDatabaseInfo()
101101
testutil.AssertNoError(t, errdb2, fmt.Sprintf("Error when trying to retrieve database information"))
102102
testutil.AssertEquals(t, dbResp2.DbName, env.couchDatabaseName)
103103

core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb_txmgr.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"github.com/golang/protobuf/proto"
2323
"github.com/hyperledger/fabric/core/ledger"
2424
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt"
25-
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb"
25+
"github.com/hyperledger/fabric/core/ledger/util/couchdb"
2626
"github.com/hyperledger/fabric/core/ledger/util/db"
2727
"github.com/op/go-logging"
2828

@@ -74,6 +74,7 @@ type CouchDBTxMgr struct {
7474
}
7575

7676
// CouchConnection provides connection info for CouchDB
77+
//TODO not currently used
7778
type CouchConnection struct {
7879
host string
7980
port int
@@ -89,18 +90,10 @@ func NewCouchDBTxMgr(conf *Conf, couchDBConnectURL string, dbName string, id str
8990
db := db.CreateDB(&db.Conf{DBPath: conf.DBPath})
9091
db.Open()
9192

92-
couchDB, err := couchdb.CreateConnectionDefinition(couchDBConnectURL,
93-
dbName,
94-
id,
95-
pw)
93+
couchDB, err := couchdb.CreateCouchDBConnectionAndDB(couchDBConnectURL, dbName, id, pw)
9694
if err != nil {
97-
logger.Errorf("===COUCHDB=== Error during CreateConnectionDefinition(): %s\n", err.Error())
98-
}
99-
100-
// Create CouchDB database upon ledger startup, if it doesn't already exist
101-
_, err = couchDB.CreateDatabaseIfNotExist()
102-
if err != nil {
103-
logger.Errorf("===COUCHDB=== Error during CreateDatabaseIfNotExist(): %s\n", err.Error())
95+
logger.Errorf("Error during NewCouchDBTxMgr(): %s\n", err.Error())
96+
return nil
10497
}
10598

10699
// db and stateIndexCF will not be used for CouchDB. TODO to cleanup

core/ledger/ledgerconfig/ledger_config_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ import (
2525

2626
func TestIsCouchDBEnabledDefault(t *testing.T) {
2727
setUpCoreYAMLConfig()
28+
// During a build the default values should be false.
29+
30+
// If the ledger test are run with CouchDb enabled, need to provide a mechanism
31+
// To let this test run but still test default values.
32+
if IsCouchDBEnabled() == true {
33+
testutil.ResetConfigToDefaultValues()
34+
defer viper.Set("ledger.state.stateDatabase", "CouchDB")
35+
}
2836
defaultValue := IsCouchDBEnabled()
2937
testutil.AssertEquals(t, defaultValue, false) //test default config is false
3038
}

core/ledger/kvledger/txmgmt/couchdbtxmgmt/couchdb/couchdb_test.go core/ledger/util/couchdb/couchdb_test.go

+15-17
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ import (
2525
"github.com/hyperledger/fabric/core/ledger/testutil"
2626
)
2727

28+
//Basic setup to test couch
29+
var connectURL = "localhost:5984"
30+
var badConnectURL = "localhost:5990"
31+
var database = "testdb1"
32+
var username = ""
33+
var password = ""
34+
35+
func cleanup() {
36+
//create a new connection
37+
db, _ := CreateConnectionDefinition(connectURL, database, username, password)
38+
//drop the test database
39+
db.DropDatabase()
40+
}
41+
2842
type Asset struct {
2943
ID string `json:"_id"`
3044
Rev string `json:"_rev"`
@@ -34,18 +48,12 @@ type Asset struct {
3448
Owner string `json:"owner"`
3549
}
3650

37-
var connectURL = "localhost:5984"
38-
var badConnectURL = "localhost:5990"
39-
var database = "testdb1"
40-
var username = ""
41-
var password = ""
42-
4351
var assetJSON = []byte(`{"asset_name":"marble1","color":"blue","size":"35","owner":"jerry"}`)
4452

4553
func TestDBConnectionDef(t *testing.T) {
4654

4755
//call a helper method to load the core.yaml
48-
testutil.SetupCoreYAMLConfig("./../../../../../../peer")
56+
testutil.SetupCoreYAMLConfig("./../../../../peer")
4957

5058
//create a new connection
5159
_, err := CreateConnectionDefinition(connectURL, "database", "", "")
@@ -317,13 +325,3 @@ func TestDBTestDropDatabaseBadConnection(t *testing.T) {
317325
}
318326

319327
}
320-
321-
func cleanup() {
322-
323-
//create a new connection
324-
db, _ := CreateConnectionDefinition(connectURL, database, username, password)
325-
326-
//drop the test database
327-
db.DropDatabase()
328-
329-
}
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
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.
15+
*/
16+
17+
package couchdb
18+
19+
//CreateCouchDBConnectionAndDB creates a CouchDB Connection and the database if it does not exist
20+
func CreateCouchDBConnectionAndDB(couchDBConnectURL string, dbName string, id string, pw string) (*CouchDBConnectionDef, error) {
21+
couchDB, err := CreateConnectionDefinition(couchDBConnectURL,
22+
dbName,
23+
id,
24+
pw)
25+
if err != nil {
26+
logger.Errorf("Error during CouchDB CreateConnectionDefinition() to dbName: %s error: %s\n", dbName, err.Error())
27+
return nil, err
28+
}
29+
30+
// Create CouchDB database upon ledger startup, if it doesn't already exist
31+
_, err = couchDB.CreateDatabaseIfNotExist()
32+
if err != nil {
33+
logger.Errorf("Error during CouchDB CreateDatabaseIfNotExist() to dbName: %s error: %s\n", dbName, err.Error())
34+
return nil, err
35+
}
36+
37+
//return the couch db connection
38+
return couchDB, nil
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright IBM Corp. 2016 All Rights Reserved.
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.
15+
*/
16+
17+
package couchdb
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
24+
"github.com/hyperledger/fabric/core/ledger/testutil"
25+
)
26+
27+
//Unit test of couch db util functionality
28+
func TestCreateCouchDBConnectionAndDB(t *testing.T) {
29+
30+
//call a helper method to load the core.yaml
31+
testutil.SetupCoreYAMLConfig("./../../../../peer")
32+
33+
if ledgerconfig.IsCouchDBEnabled() == true {
34+
35+
cleanup()
36+
defer cleanup()
37+
//create a new connection
38+
_, err := CreateCouchDBConnectionAndDB(connectURL, database, "", "")
39+
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchDBConnectionAndDB"))
40+
}
41+
42+
}

0 commit comments

Comments
 (0)