-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <mariwade@us.ibm.com>
- Loading branch information
Showing
10 changed files
with
289 additions
and
31 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,52 @@ | ||
/* | ||
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 history | ||
|
||
import ( | ||
"github.com/hyperledger/fabric/core/ledger/util/couchdb" | ||
"github.com/hyperledger/fabric/protos/common" | ||
logging "github.com/op/go-logging" | ||
) | ||
|
||
var logger = logging.MustGetLogger("txhistorymgmt") | ||
|
||
// CouchDBHistMgr a simple implementation of interface `histmgmt.TxHistMgr'. | ||
// TODO This implementation does not currently use a lock but may need one to insure query's are consistent | ||
type CouchDBHistMgr struct { | ||
couchDB *couchdb.CouchDBConnectionDef // COUCHDB new properties for CouchDB | ||
} | ||
|
||
// NewCouchDBHistMgr constructs a new `CouchDBTxHistMgr` | ||
func NewCouchDBHistMgr(couchDBConnectURL string, dbName string, id string, pw string) *CouchDBHistMgr { | ||
|
||
//TODO locking has not been implemented but may need some sort of locking to insure queries are valid data. | ||
|
||
couchDB, err := couchdb.CreateCouchDBConnectionAndDB(couchDBConnectURL, dbName, id, pw) | ||
if err != nil { | ||
logger.Errorf("Error during NewCouchDBHistMgr(): %s\n", err.Error()) | ||
return nil | ||
} | ||
|
||
// db and stateIndexCF will not be used for CouchDB. TODO to cleanup | ||
return &CouchDBHistMgr{couchDB: couchDB} | ||
} | ||
|
||
// Commit implements method in interface `txhistorymgmt.TxMgr` | ||
func (histmgr *CouchDBHistMgr) Commit(block *common.Block) error { | ||
logger.Debugf("===HISTORYDB=== Entering CouchDBTxHistMgr.Commit()") | ||
return nil | ||
} |
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,102 @@ | ||
/* | ||
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 history | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/core/ledger/ledgerconfig" | ||
"github.com/hyperledger/fabric/core/ledger/testutil" | ||
"github.com/hyperledger/fabric/core/ledger/util/couchdb" | ||
) | ||
|
||
//Complex setup to test the use of couch in ledger | ||
type testEnvCouch struct { | ||
couchDBPath string | ||
couchDBAddress string | ||
couchDatabaseName string | ||
couchUsername string | ||
couchPassword string | ||
} | ||
|
||
func newTestEnvCouch(t testing.TB, dbPath string, dbName string) *testEnvCouch { | ||
|
||
couchDBDef := ledgerconfig.GetCouchDBDefinition() | ||
os.RemoveAll(dbPath) | ||
|
||
return &testEnvCouch{ | ||
couchDBPath: dbPath, | ||
couchDBAddress: couchDBDef.URL, | ||
couchDatabaseName: dbName, | ||
couchUsername: couchDBDef.Username, | ||
couchPassword: couchDBDef.Password, | ||
} | ||
} | ||
|
||
func (env *testEnvCouch) cleanup() { | ||
os.RemoveAll(env.couchDBPath) | ||
//create a new connection | ||
couchDB, _ := couchdb.CreateConnectionDefinition(env.couchDBAddress, env.couchDatabaseName, env.couchUsername, env.couchPassword) | ||
//drop the test database if it already existed | ||
couchDB.DropDatabase() | ||
} | ||
|
||
// couchdb_test.go tests couchdb functions already. This test just tests that a CouchDB history database is auto-created | ||
// upon creating a new history transaction manager | ||
func TestHistoryDatabaseAutoCreate(t *testing.T) { | ||
|
||
//call a helper method to load the core.yaml | ||
testutil.SetupCoreYAMLConfig("./../../../peer") | ||
|
||
//Only run the tests if CouchDB is explitily enabled in the code, | ||
//otherwise CouchDB may not be installed and all the tests would fail | ||
//TODO replace this with external config property rather than config within the code | ||
if ledgerconfig.IsCouchDBEnabled() == true { | ||
|
||
env := newTestEnvCouch(t, "/tmp/tests/ledger/history", "history-test") | ||
env.cleanup() //cleanup at the beginning to ensure the database doesn't exist already | ||
defer env.cleanup() //and cleanup at the end | ||
|
||
histMgr := NewCouchDBHistMgr( | ||
env.couchDBAddress, //couchDB Address | ||
env.couchDatabaseName, //couchDB db name | ||
env.couchUsername, //enter couchDB id | ||
env.couchPassword) //enter couchDB pw | ||
|
||
//NewCouchDBhistMgr should have automatically created the database, let's make sure it has been created | ||
//Retrieve the info for the new database and make sure the name matches | ||
dbResp, _, errdb := histMgr.couchDB.GetDatabaseInfo() | ||
testutil.AssertNoError(t, errdb, fmt.Sprintf("Error when trying to retrieve database information")) | ||
testutil.AssertEquals(t, dbResp.DbName, env.couchDatabaseName) | ||
|
||
//Call NewCouchDBhistMgr again, this time the database will already exist from last time | ||
histMgr2 := NewCouchDBHistMgr( | ||
env.couchDBAddress, //couchDB Address | ||
env.couchDatabaseName, //couchDB db name | ||
env.couchUsername, //enter couchDB id | ||
env.couchPassword) //enter couchDB pw | ||
|
||
//Retrieve the info for the database again, and make sure the name still matches | ||
dbResp2, _, errdb2 := histMgr2.couchDB.GetDatabaseInfo() | ||
testutil.AssertNoError(t, errdb2, fmt.Sprintf("Error when trying to retrieve database information")) | ||
testutil.AssertEquals(t, dbResp2.DbName, env.couchDatabaseName) | ||
|
||
} | ||
|
||
} |
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,24 @@ | ||
/* | ||
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 history | ||
|
||
import "github.com/hyperledger/fabric/protos/common" | ||
|
||
// TxHistMgr - an interface that a transaction history manager should implement | ||
type TxHistMgr interface { | ||
Commit(block *common.Block) error | ||
} |
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
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
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
File renamed without changes.
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
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,39 @@ | ||
/* | ||
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 couchdb | ||
|
||
//CreateCouchDBConnectionAndDB creates a CouchDB Connection and the database if it does not exist | ||
func CreateCouchDBConnectionAndDB(couchDBConnectURL string, dbName string, id string, pw string) (*CouchDBConnectionDef, error) { | ||
couchDB, err := CreateConnectionDefinition(couchDBConnectURL, | ||
dbName, | ||
id, | ||
pw) | ||
if err != nil { | ||
logger.Errorf("Error during CouchDB CreateConnectionDefinition() to dbName: %s error: %s\n", dbName, err.Error()) | ||
return nil, err | ||
} | ||
|
||
// Create CouchDB database upon ledger startup, if it doesn't already exist | ||
_, err = couchDB.CreateDatabaseIfNotExist() | ||
if err != nil { | ||
logger.Errorf("Error during CouchDB CreateDatabaseIfNotExist() to dbName: %s error: %s\n", dbName, err.Error()) | ||
return nil, err | ||
} | ||
|
||
//return the couch db connection | ||
return couchDB, nil | ||
} |
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,42 @@ | ||
/* | ||
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 couchdb | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hyperledger/fabric/core/ledger/ledgerconfig" | ||
"github.com/hyperledger/fabric/core/ledger/testutil" | ||
) | ||
|
||
//Unit test of couch db util functionality | ||
func TestCreateCouchDBConnectionAndDB(t *testing.T) { | ||
|
||
//call a helper method to load the core.yaml | ||
testutil.SetupCoreYAMLConfig("./../../../../peer") | ||
|
||
if ledgerconfig.IsCouchDBEnabled() == true { | ||
|
||
cleanup() | ||
defer cleanup() | ||
//create a new connection | ||
_, err := CreateCouchDBConnectionAndDB(connectURL, database, "", "") | ||
testutil.AssertNoError(t, err, fmt.Sprintf("Error when trying to CreateCouchDBConnectionAndDB")) | ||
} | ||
|
||
} |