// Close the database. // // This flushes all write operations to the database. // // This is called automaticly on garbage collection. // Note that teminating the program does not call the garbage collector. func (db *Db) Close() { db.lock.Lock() defer db.lock.Unlock() if db.opened { // Collect all the keys before starting to close, because closing will change the hash keys := make([]uint64, 0, len(db.queries)) for key := range db.queries { keys = append(keys, key) } for _, key := range keys { db.queries[key].Close() } C.c_dbxml_free(db.db) db.opened = false } }
// Open a database. // // Call db.Close() to ensure all write operations to the database are finished, before terminating the program. func Open(filename string) (*Db, error) { db := &Db{ queries: make(map[uint64]*Query), } cs := C.CString(filename) defer C.free(unsafe.Pointer(cs)) db.db = C.c_dbxml_open(cs) if C.c_dbxml_error(db.db) != 0 { err := errors.New(C.GoString(C.c_dbxml_errstring(db.db))) C.c_dbxml_free(db.db) return db, err } db.opened = true runtime.SetFinalizer(db, (*Db).Close) return db, nil }