Merge pull request #625 from ajbeamon/fix-golang-db-caching

fix: cache databases by cluster file and DB name in golang bindings.
This commit is contained in:
Alec Grieser 2018-07-30 09:54:45 -07:00 committed by GitHub
commit e7f1911d22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 4 deletions

View File

@ -192,12 +192,17 @@ var apiVersion int
var networkStarted bool
var networkMutex sync.Mutex
type DatabaseId struct {
clusterFile string
dbName string
}
var openClusters map[string]Cluster
var openDatabases map[string]Database
var openDatabases map[DatabaseId]Database
func init() {
openClusters = make(map[string]Cluster)
openDatabases = make(map[string]Database)
openDatabases = make(map[DatabaseId]Database)
}
func startNetwork() error {
@ -287,13 +292,13 @@ func Open(clusterFile string, dbName []byte) (Database, error) {
openClusters[clusterFile] = cluster
}
db, ok := openDatabases[string(dbName)]
db, ok := openDatabases[DatabaseId{clusterFile, string(dbName)}]
if !ok {
db, e = cluster.OpenDatabase(dbName)
if e != nil {
return Database{}, e
}
openDatabases[string(dbName)] = db
openDatabases[DatabaseId{clusterFile, string(dbName)}] = db
}
return db, nil