github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/store/db/db.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package db
    16  
    17  import (
    18  	"database/sql"
    19  	"os"
    20  	"path/filepath"
    21  
    22  	"github.com/rkt/rkt/pkg/lock"
    23  
    24  	_ "github.com/cznic/ql/driver"
    25  )
    26  
    27  const (
    28  	defaultPathPerm = os.FileMode(0770 | os.ModeSetgid)
    29  
    30  	DbFilename = "ql.db"
    31  )
    32  
    33  type DB struct {
    34  	dbdir string
    35  }
    36  
    37  func NewDB(dbdir string) (*DB, error) {
    38  	if err := os.MkdirAll(dbdir, defaultPathPerm); err != nil {
    39  		return nil, err
    40  	}
    41  	return &DB{dbdir: dbdir}, nil
    42  }
    43  
    44  type txfunc func(*sql.Tx) error
    45  
    46  // Do Opens the db, executes DoTx and then Closes the DB
    47  // To support a multiprocess and multigoroutine model on a single file access
    48  // database like ql there's the need to exlusively lock, open, close, unlock the
    49  // db for every transaction. For this reason every db transaction should be
    50  // fast to not block other processes/goroutines.
    51  func (db *DB) Do(fns ...txfunc) error {
    52  	l, err := lock.ExclusiveLock(db.dbdir, lock.Dir)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	defer l.Close()
    57  
    58  	sqldb, err := sql.Open("ql", filepath.Join(db.dbdir, DbFilename))
    59  	if err != nil {
    60  		return err
    61  	}
    62  	defer sqldb.Close()
    63  
    64  	tx, err := sqldb.Begin()
    65  	if err != nil {
    66  		return err
    67  	}
    68  	for _, fn := range fns {
    69  		if err := fn(tx); err != nil {
    70  			tx.Rollback()
    71  			return err
    72  		}
    73  	}
    74  	return tx.Commit()
    75  }