-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathdb.go
51 lines (42 loc) · 1.34 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (c) 2018 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wallet
import (
"io"
"decred.org/dcrwallet/v5/errors"
"decred.org/dcrwallet/v5/wallet/walletdb"
)
// DB represents an ACID database for a wallet.
type DB interface {
io.Closer
// internal returns the internal interface, allowing DB to be used as an
// opaque type in other packages without revealing the walletdb.DB
// interface.
internal() walletdb.DB
}
type opaqueDB struct {
walletdb.DB
}
func (db opaqueDB) internal() walletdb.DB { return db.DB }
// OpenDB opens a database with some specific driver implementation. Args
// specify the arguments to open the database and may differ based on driver.
func OpenDB(driver string, args ...any) (DB, error) {
const op errors.Op = "wallet.OpenDB"
db, err := walletdb.Open(driver, args...)
if err != nil {
return nil, errors.E(op, err)
}
return opaqueDB{db}, nil
}
// CreateDB creates a new database with some specific driver implementation.
// Args specify the arguments to open the database and may differ based on
// driver.
func CreateDB(driver string, args ...any) (DB, error) {
const op errors.Op = "wallet.CreateDB"
db, err := walletdb.Create(driver, args...)
if err != nil {
return nil, errors.E(op, err)
}
return opaqueDB{db}, nil
}