Skip to content

Commit

Permalink
db: no need to entirely lock DB unless needed
Browse files Browse the repository at this point in the history
  • Loading branch information
odeke-em committed Aug 11, 2015
1 parent d9cb5c1 commit 9050e2b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 46 deletions.
26 changes: 0 additions & 26 deletions cmd/drive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,6 @@ func (cmd *indexCmd) Run(args []string) {
byId := *cmd.byId
byMatches := *cmd.matches
sources, context, path := preprocessArgsByToggle(args, byMatches || byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

options := &drive.Options{
Sources: sources,
Expand Down Expand Up @@ -497,8 +495,6 @@ func (cmd *pullCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *pullCmd) Run(args []string) {
sources, context, path := preprocessArgsByToggle(args, (*cmd.byId || *cmd.matches))
exitWithError(context.OpenDB())
defer context.CloseDB()

excludes := drive.NonEmptyTrimmedStrings(strings.Split(*cmd.excludeOps, ",")...)
excludeCrudMask := drive.CrudAtoi(excludes...)
Expand Down Expand Up @@ -593,8 +589,6 @@ func (cmd *pushCmd) Run(args []string) {
cmd.pushMounted(args)
} else {
sources, context, path := preprocessArgs(args)
exitWithError(context.OpenDB())
defer context.CloseDB()

options := cmd.createPushOptions()
options.Path = path
Expand Down Expand Up @@ -627,8 +621,6 @@ func (cmd *touchCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *touchCmd) Run(args []string) {
sources, context, path := preprocessArgsByToggle(args, *cmd.matches || *cmd.byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

opts := drive.Options{
Hidden: *cmd.hidden,
Expand Down Expand Up @@ -706,8 +698,6 @@ func (cmd *pushCmd) pushMounted(args []string) {

rest = drive.NonEmptyStrings(rest...)
context, path := discoverContext(contextArgs)
exitWithError(context.OpenDB())
defer context.CloseDB()

contextAbsPath, err := filepath.Abs(path)
exitWithError(err)
Expand Down Expand Up @@ -787,8 +777,6 @@ func (cmd *diffCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *diffCmd) Run(args []string) {
sources, context, path := preprocessArgs(args)
exitWithError(context.OpenDB())
defer context.CloseDB()

exitWithError(drive.New(context, &drive.Options{
Recursive: true,
Expand Down Expand Up @@ -866,8 +854,6 @@ func (cmd *deleteCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *deleteCmd) Run(args []string) {
sources, context, path := preprocessArgsByToggle(args, *cmd.matches || *cmd.byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

opts := drive.Options{
Path: path,
Expand Down Expand Up @@ -899,8 +885,6 @@ func (cmd *trashCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *trashCmd) Run(args []string) {
sources, context, path := preprocessArgsByToggle(args, *cmd.matches || *cmd.byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

opts := drive.Options{
Path: path,
Expand Down Expand Up @@ -928,8 +912,6 @@ func (cmd *newCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *newCmd) Run(args []string) {
sources, context, path := preprocessArgs(args)
exitWithError(context.OpenDB())
defer context.CloseDB()

opts := drive.Options{
Path: path,
Expand Down Expand Up @@ -975,8 +957,6 @@ func (cmd *copyCmd) Run(args []string) {
dest := args[end]

sources, context, path := preprocessArgsByToggle(args, *cmd.byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

// Unshift by the end path
sources = sources[:len(sources)-1]
Expand Down Expand Up @@ -1011,8 +991,6 @@ func (cmd *untrashCmd) Flags(fs *flag.FlagSet) *flag.FlagSet {

func (cmd *untrashCmd) Run(args []string) {
sources, context, path := preprocessArgsByToggle(args, *cmd.byId || *cmd.matches)
exitWithError(context.OpenDB())
defer context.CloseDB()

opts := drive.Options{
Path: path,
Expand Down Expand Up @@ -1091,8 +1069,6 @@ func (cmd *moveCmd) Run(args []string) {
exitWithError(fmt.Errorf("move: expecting a path or more"))
}
sources, context, path := preprocessArgsByToggle(args, *cmd.byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

// Unshift by the end path
sources = sources[:len(sources)-1]
Expand Down Expand Up @@ -1130,8 +1106,6 @@ func (cmd *renameCmd) Run(args []string) {
}
rest, last := args[:argc-1], args[argc-1]
sources, context, path := preprocessArgsByToggle(rest, *cmd.byId)
exitWithError(context.OpenDB())
defer context.CloseDB()

sources = append(sources, last)
exitWithError(drive.New(context, &drive.Options{
Expand Down
76 changes: 56 additions & 20 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (

ErrNoDriveContext = errors.New("no drive context found; run `drive init` or go into one of the directories (sub directories) that you performed `drive init`")
ErrDerefNilIndex = errors.New("cannot dereference a nil index")
ErrDerefNilDB = errors.New("cannot dereference a nil db")
ErrEmptyFileIdForIndex = errors.New("fileId for index must be non-empty")
ErrNoSuchDbKey = errors.New("no such db key exists")
ErrNoSuchDbBucket = errors.New("no such bucket exists")
Expand All @@ -52,7 +53,6 @@ type Context struct {
ClientSecret string `json:"client_secret"`
RefreshToken string `json:"refresh_token"`
AbsPath string `json:"-"`
DB *bolt.DB `json:"-"`
}

type Index struct {
Expand Down Expand Up @@ -111,9 +111,16 @@ func (c *Context) DeserializeIndex(key string) (*Index, error) {
return nil, creationErr
}

db, err := c.OpenDB()
if err != nil {
return nil, err
}

defer db.Close()

var data []byte

err := c.DB.View(func(tx *bolt.Tx) error {
err = db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(byteify(IndicesKey))
if bucket == nil {
return ErrNoSuchDbBucket
Expand Down Expand Up @@ -143,12 +150,19 @@ func (c *Context) ListKeys(dir, bucketName string) (chan string, error) {
return keysChan, creationErr
}

db, err := c.OpenDB()
if err != nil {
close(keysChan)
return keysChan, err
}

go func() {
defer func() {
db.Close()
close(keysChan)
}()

c.DB.View(func(tx *bolt.Tx) error {
db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(byteify(bucketName))
if bucket == nil {
return ErrNoSuchDbBucket
Expand All @@ -172,7 +186,13 @@ func (c *Context) PopIndicesKey(key string) error {
}

func (c *Context) popDbKey(bucketName, key string) error {
return c.DB.Update(func(tx *bolt.Tx) error {
db, err := c.OpenDB()
if err != nil {
return err
}
defer db.Close()

return db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(byteify(IndicesKey))
if err != nil {
return err
Expand All @@ -193,7 +213,13 @@ func (c *Context) RemoveIndex(index *Index, p string) error {
return ErrEmptyFileIdForIndex
}

return c.DB.Update(func(tx *bolt.Tx) error {
db, err := c.OpenDB()
if err != nil {
return err
}
defer db.Close()

return db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(byteify(IndicesKey))
if err != nil {
return err
Expand All @@ -206,7 +232,13 @@ func (c *Context) RemoveIndex(index *Index, p string) error {
}

func (c *Context) CreateIndicesBucket() error {
return c.DB.Update(func(tx *bolt.Tx) error {
db, err := c.OpenDB()
if err != nil {
return err
}
defer db.Close()

return db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(byteify(IndicesKey))
if err != nil {
return err
Expand All @@ -220,11 +252,19 @@ func (c *Context) CreateIndicesBucket() error {

func (c *Context) SerializeIndex(index *Index) (err error) {
var data []byte
var db *bolt.DB

if data, err = json.Marshal(index); err != nil {
return
}

return c.DB.Update(func(tx *bolt.Tx) error {
db, err = c.OpenDB()
if err != nil {
return err
}
defer db.Close()

return db.Update(func(tx *bolt.Tx) error {
bucket, err := tx.CreateBucketIfNotExists(byteify(IndicesKey))
if err != nil {
return err
Expand Down Expand Up @@ -268,23 +308,19 @@ func (c *Context) DeInitialize(prompter func(...interface{}) bool, returnOnAnyEr
return nil
}

func (c *Context) OpenDB() error {
func (c *Context) OpenDB() (db *bolt.DB, err error) {
dbPath := DbSuffixedPath(c.AbsPathOf(""))
db, err := bolt.Open(dbPath, O_RWForAll, nil)
if err != nil {
return err
}
db, err = bolt.Open(dbPath, O_RWForAll, nil)

c.DB = db
return nil
}
if err != nil {
return db, err
}

func (c *Context) CloseDB() error {
if c.DB == nil {
return errors.New("nil dereference of db")
}
if db == nil {
return db, ErrDerefNilDB
}

return c.DB.Close()
return db, nil
}

// Discovers the gd directory, if no gd directory or credentials
Expand Down

0 comments on commit 9050e2b

Please sign in to comment.