Skip to content

Commit

Permalink
[FAB-7600] Clean up CouchDB index review comments
Browse files Browse the repository at this point in the history
Change-Id: I6684440cacb139ec70c54bd6d285d769f60d1b8d
Signed-off-by: Chris Elder <chris.elder@us.ibm.com>
Signed-off-by: Ry Jones <ry@walledcity.org>
  • Loading branch information
Chris Elder committed May 2, 2018
1 parent d9e9f5a commit 98c8c58
Show file tree
Hide file tree
Showing 7 changed files with 978 additions and 1,115 deletions.
2 changes: 1 addition & 1 deletion core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func finitPeer(lis net.Listener, chainIDs ...string) {
requestTimeout := viper.GetDuration("ledger.state.couchDBConfig.requestTimeout")

couchInstance, _ := couchdb.CreateCouchInstance(connectURL, username, password, maxRetries, maxRetriesOnStartup, requestTimeout)
db := couchdb.CouchDatabase{CouchInstance: *couchInstance, DBName: chainID}
db := couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: chainID}
//drop the test database
db.DropDatabase()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func newVersionedDB(couchInstance *couchdb.CouchInstance, dbName string) (*Versi
// CreateCouchDatabase creates a CouchDB database object, as well as the underlying database if it does not exist
chainName := dbName
dbName = couchdb.ConstructMetadataDBName(dbName)
metadataDB, err := couchdb.CreateCouchDatabase(*couchInstance, dbName)

metadataDB, err := couchdb.CreateCouchDatabase(couchInstance, dbName)
if err != nil {
return nil, err
}
Expand All @@ -113,7 +114,7 @@ func (vdb *VersionedDB) getNamespaceDBHandle(namespace string) (*couchdb.CouchDa
db = vdb.namespaceDBs[namespace]
if db == nil {
var err error
db, err = couchdb.CreateCouchDatabase(*vdb.couchInstance, namespaceDBName)
db, err = couchdb.CreateCouchDatabase(vdb.couchInstance, namespaceDBName)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*
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.
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package statecouchdb
Expand Down Expand Up @@ -53,7 +42,7 @@ func CleanupDB(dbName string) {
couchDBDef := couchdb.GetCouchDBDefinition()
couchInstance, _ := couchdb.CreateCouchInstance(couchDBDef.URL, couchDBDef.Username, couchDBDef.Password,
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
db := couchdb.CouchDatabase{CouchInstance: *couchInstance, DBName: strings.ToLower(dbName)}
db := couchdb.CouchDatabase{CouchInstance: couchInstance, DBName: strings.ToLower(dbName)}
//drop the test database
db.DropDatabase()
}
63 changes: 26 additions & 37 deletions core/ledger/util/couchdb/couchdb.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
/*
Copyright IBM Corp. 2016, 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.
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package couchdb
Expand Down Expand Up @@ -144,7 +133,7 @@ type CouchInstance struct {

//CouchDatabase represents a database within a CouchDB instance
type CouchDatabase struct {
CouchInstance CouchInstance //connection configuration
CouchInstance *CouchInstance //connection configuration
DBName string
IndexWarmCounter int
}
Expand Down Expand Up @@ -211,20 +200,6 @@ type Base64Attachment struct {
AttachmentData string `json:"data"`
}

//ListIndexResponse contains the definition for listing couchdb indexes
type ListIndexResponse struct {
TotalRows int `json:"total_rows"`
Indexes []IndexDefinition `json:"indexes"`
}

//IndexDefinition contains the definition for a couchdb index
type IndexDefinition struct {
DesignDocument string `json:"ddoc"`
Name string `json:"name"`
Type string `json:"type"`
Definition json.RawMessage `json:"def"`
}

//IndexResult contains the definition for a couchdb index
type IndexResult struct {
DesignDocument string `json:"designdoc"`
Expand Down Expand Up @@ -410,7 +385,7 @@ func (couchInstance *CouchInstance) VerifyCouchConfig() (*ConnectionInfo, *DBRet
//Verifying the existence of the system database accomplishes two steps
//1. Ensures the system databases are created
//2. Verifies the username password provided in the CouchDB config are valid for system admin
err = CreateSystemDatabasesIfNotExist(*couchInstance)
err = CreateSystemDatabasesIfNotExist(couchInstance)
if err != nil {
logger.Errorf("Unable to connect to CouchDB, error: %s Check the admin username and password.\n", err.Error())
return nil, nil, fmt.Errorf("Unable to connect to CouchDB, error: %s Check the admin username and password.\n", err.Error())
Expand Down Expand Up @@ -1082,9 +1057,23 @@ func (dbclient *CouchDatabase) QueryDocuments(query string) (*[]QueryResult, err
}

// ListIndex method lists the defined indexes for a database
func (dbclient *CouchDatabase) ListIndex() (*[]IndexResult, error) {
func (dbclient *CouchDatabase) ListIndex() ([]*IndexResult, error) {

logger.Debugf("Entering ListIndex()")
//IndexDefinition contains the definition for a couchdb index
type indexDefinition struct {
DesignDocument string `json:"ddoc"`
Name string `json:"name"`
Type string `json:"type"`
Definition json.RawMessage `json:"def"`
}

//ListIndexResponse contains the definition for listing couchdb indexes
type listIndexResponse struct {
TotalRows int `json:"total_rows"`
Indexes []indexDefinition `json:"indexes"`
}

logger.Debug("Entering ListIndex()")

indexURL, err := url.Parse(dbclient.CouchInstance.conf.URL)
if err != nil {
Expand All @@ -1109,14 +1098,14 @@ func (dbclient *CouchDatabase) ListIndex() (*[]IndexResult, error) {
return nil, err
}

var jsonResponse = &ListIndexResponse{}
var jsonResponse = &listIndexResponse{}

err2 := json.Unmarshal(jsonResponseRaw, &jsonResponse)
err2 := json.Unmarshal(jsonResponseRaw, jsonResponse)
if err2 != nil {
return nil, err2
}

var results []IndexResult
var results []*IndexResult

for _, row := range jsonResponse.Indexes {

Expand All @@ -1129,14 +1118,14 @@ func (dbclient *CouchDatabase) ListIndex() (*[]IndexResult, error) {

//Add the index definition to the results
var addIndexResult = &IndexResult{DesignDocument: designDoc, Name: row.Name, Definition: fmt.Sprintf("%s", row.Definition)}
results = append(results, *addIndexResult)
results = append(results, addIndexResult)
}

}

logger.Debugf("Exiting ListIndex()")

return &results, nil
return results, nil

}

Expand Down Expand Up @@ -1281,7 +1270,7 @@ func (dbclient *CouchDatabase) WarmIndexAllIndexes() error {
}

//For each index definition, execute an index refresh
for _, elem := range *listResult {
for _, elem := range listResult {

err := dbclient.WarmIndex(elem.DesignDocument, elem.Name)
if err != nil {
Expand Down
Loading

0 comments on commit 98c8c58

Please sign in to comment.