Allow to close database in go bindings (#8793)
* Allow to close database in go bindings
This commit is contained in:
parent
891331caed
commit
f40b8da3e9
|
@ -42,6 +42,8 @@ import (
|
|||
// usually created and committed automatically by the (Database).Transact
|
||||
// method.
|
||||
type Database struct {
|
||||
// String reference to the cluster file.
|
||||
clusterFile string
|
||||
*database
|
||||
}
|
||||
|
||||
|
@ -56,6 +58,16 @@ type DatabaseOptions struct {
|
|||
d *database
|
||||
}
|
||||
|
||||
// Close will close the Database and clean up all resources.
|
||||
// You have to ensure that you're not resuing this database.
|
||||
func (d *Database) Close() {
|
||||
// Remove database object from the cached databases
|
||||
delete(openDatabases, d.clusterFile)
|
||||
|
||||
// Destroy the database
|
||||
d.destroy()
|
||||
}
|
||||
|
||||
func (opt DatabaseOptions) setOpt(code int, param []byte) error {
|
||||
return setOpt(func(p *C.uint8_t, pl C.int) C.fdb_error_t {
|
||||
return C.fdb_database_set_option(opt.d.ptr, C.FDBDatabaseOption(code), p, pl)
|
||||
|
@ -63,6 +75,10 @@ func (opt DatabaseOptions) setOpt(code int, param []byte) error {
|
|||
}
|
||||
|
||||
func (d *database) destroy() {
|
||||
if d.ptr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
C.fdb_database_destroy(d.ptr)
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,7 @@ import (
|
|||
// Would put this in futures.go but for the documented issue with
|
||||
// exports and functions in preamble
|
||||
// (https://code.google.com/p/go-wiki/wiki/cgo#Global_functions)
|
||||
//
|
||||
//export unlockMutex
|
||||
func unlockMutex(p unsafe.Pointer) {
|
||||
m := (*sync.Mutex)(p)
|
||||
|
@ -337,7 +338,7 @@ func createDatabase(clusterFile string) (Database, error) {
|
|||
db := &database{outdb}
|
||||
runtime.SetFinalizer(db, (*database).destroy)
|
||||
|
||||
return Database{db}, nil
|
||||
return Database{clusterFile, db}, nil
|
||||
}
|
||||
|
||||
// Deprecated: Use OpenDatabase instead.
|
||||
|
|
|
@ -48,7 +48,10 @@ func ExampleOpenDefault() {
|
|||
return
|
||||
}
|
||||
|
||||
_ = db
|
||||
// Close the database after usage
|
||||
defer db.Close()
|
||||
|
||||
// Do work here
|
||||
|
||||
// Output:
|
||||
}
|
||||
|
@ -313,3 +316,30 @@ func ExamplePrintable() {
|
|||
fmt.Println(fdb.Printable([]byte{0, 1, 2, 'a', 'b', 'c', '1', '2', '3', '!', '?', 255}))
|
||||
// Output: \x00\x01\x02abc123!?\xff
|
||||
}
|
||||
|
||||
func TestDatabaseCloseRemovesResources(t *testing.T) {
|
||||
err := fdb.APIVersion(API_VERSION)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to set API version: %v\n", err)
|
||||
}
|
||||
|
||||
// OpenDefault opens the database described by the platform-specific default
|
||||
// cluster file
|
||||
db, err := fdb.OpenDefault()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to set API version: %v\n", err)
|
||||
}
|
||||
|
||||
// Close the database after usage
|
||||
db.Close()
|
||||
|
||||
// Open the same database again, if the database is still in the cache we would return the same object, if not we create a new object with a new pointer
|
||||
newDB, err := fdb.OpenDefault()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to set API version: %v\n", err)
|
||||
}
|
||||
|
||||
if db == newDB {
|
||||
t.Fatalf("Expected a different database object, got: %v and %v\n", db, newDB)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue