This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
/
db.go
168 lines (142 loc) · 3.82 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2015 The rkt Authors
//
// 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.
package store
import (
"database/sql"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"github.com/coreos/rkt/pkg/lock"
"github.com/hashicorp/errwrap"
_ "github.com/cznic/ql/driver"
)
const (
DbFilename = "ql.db"
)
// dbLock is used to guarantee both thread-safety and process-safety
// for db access.
type dbLock struct {
// This lock is to make that access to the ql db file being blocking
// since ql use an internal locking that will not block and return an
// error when a lock is already held.
fl *lock.FileLock
// This lock is to avoid concurrent access from multiple goroutines.
sync.Mutex
}
func newDBLock(dirPath string) (*dbLock, error) {
l, err := lock.NewLock(dirPath, lock.Dir)
if err != nil {
return nil, err
}
return &dbLock{fl: l}, nil
}
func (dl *dbLock) lock() error {
dl.Lock()
if err := dl.fl.ExclusiveLock(); err != nil {
dl.Unlock()
return err
}
return nil
}
func (dl *dbLock) unlock() {
if err := dl.fl.Unlock(); err != nil {
// TODO(sgotti) what'll happen when df.fl.Unlock fails? From
// man 2 flock looks like it'll happen only when the underlying
// fd has been closed (in this case the lock has been released
// when the fd has been closed, assuming no dup fd due to
// forking etc...).
// If there're other cases where it fails without unlocking,
// there's no simple way to handle them.
// Possible solutions:
// * panic (done here)
// * try to close the lock (and related fd), panic if close
// fails and create a new lock.
//
// Passing a specific error to the caller and let it recover
// creating a new store instance is tricky because we
// don't know the lock state and cannot be sure on how to clean
// this store instance
panic(fmt.Errorf("failed to unlock the db flock: %v", err))
}
dl.Unlock()
}
type DB struct {
dbdir string
dl *dbLock
sqldb *sql.DB
}
func NewDB(dbdir string) (*DB, error) {
if err := os.MkdirAll(dbdir, defaultPathPerm); err != nil {
return nil, err
}
dl, err := newDBLock(dbdir)
if err != nil {
return nil, err
}
return &DB{dbdir: dbdir, dl: dl}, nil
}
func (db *DB) Open() error {
if err := db.dl.lock(); err != nil {
return err
}
sqldb, err := sql.Open("ql", filepath.Join(db.dbdir, DbFilename))
if err != nil {
db.dl.unlock()
return err
}
db.sqldb = sqldb
return nil
}
func (db *DB) Close() error {
if db.sqldb == nil {
panic("cas db, Close called without an open sqldb")
}
if err := db.sqldb.Close(); err != nil {
return errwrap.Wrap(errors.New("cas db close failed"), err)
}
db.sqldb = nil
// Don't close the flock as it will be reused.
db.dl.unlock()
return nil
}
func (db *DB) Begin() (*sql.Tx, error) {
return db.sqldb.Begin()
}
type txfunc func(*sql.Tx) error
// Do Opens the db, executes DoTx and then Closes the DB
func (db *DB) Do(fns ...txfunc) error {
err := db.Open()
if err != nil {
return err
}
defer db.Close()
return db.DoTx(fns...)
}
// DoTx executes the provided txfuncs inside a unique transaction.
// If one of the functions returns an error the whole transaction is rolled back.
func (db *DB) DoTx(fns ...txfunc) error {
tx, err := db.Begin()
if err != nil {
return err
}
for _, fn := range fns {
if err := fn(tx); err != nil {
tx.Rollback()
return err
}
}
return tx.Commit()
}