Skip to content

Commit

Permalink
Rework BoltDB util functions to use bolt.Tx instead of DB along with …
Browse files Browse the repository at this point in the history
…updates to existing API that leverage these to use Bolt's transaction wrapping functions.
  • Loading branch information
benschumacher committed Apr 10, 2015
1 parent 779a6b4 commit 46114f1
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 101 deletions.
93 changes: 47 additions & 46 deletions datastore/bolt/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import (
func (db *DB) GetBuild(repo string, build int) (*common.Build, error) {
build_ := &common.Build{}
key := []byte(repo + "/" + strconv.Itoa(build))
err := get(db, bucketBuild, key, build_)

err := db.View(func(t *bolt.Tx) error {
return get(t, bucketBuild, key, build_)
})

return build_, err
}

Expand All @@ -41,7 +45,11 @@ func (db *DB) GetBuildLast(repo string) (*common.Build, error) {
func (db *DB) GetBuildStatus(repo string, build int, status string) (*common.Status, error) {
status_ := &common.Status{}
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + status)
err := update(db, bucketBuildStatus, key, status)

err := db.Update(func(t *bolt.Tx) error {
return update(t, bucketBuildStatus, key, status)
})

return status_, err
}

Expand All @@ -68,57 +76,47 @@ func (db *DB) GetBuildStatusList(repo string, build int) ([]*common.Status, erro

// InsertBuild inserts a new build for the named repository
func (db *DB) InsertBuild(repo string, build *common.Build) error {
var seqno int

t, err := db.Begin(true)
if err != nil {
return err
}
key := []byte(repo)
raw := t.Bucket(bucketBuildSeq).Get(key)
if raw != nil {
// convert our raw to an integer value
seqno = int(binary.LittleEndian.Uint32(raw))
}

// increment the seqno, if no record was found, this starts us at 1
seqno += 1

// convert our new seqno back to raw value
raw = make([]byte, 4) // TODO: replace magic number 4 (uint32)
binary.LittleEndian.PutUint32(raw, uint32(seqno))
err = t.Bucket(bucketBuildSeq).Put(key, raw)
if err != nil {
t.Rollback()
return err
}

// fill out build structure
build.Number = seqno
build.Created = time.Now().UTC().Unix()

key = []byte(repo + "/" + strconv.Itoa(build.Number))
raw, err = encode(build)
if err != nil {
t.Rollback()
return err
}

err = t.Bucket(bucketBuild).Put(key, raw)
if err != nil {
t.Rollback()
return err
}

return t.Commit()

return db.Update(func (t *bolt.Tx) error {
raw, err := raw(t, bucketBuildSeq, key)

var next_seq uint32
switch err {
case ErrKeyNotFound:
next_seq = 1
case nil:
next_seq = 1 + binary.LittleEndian.Uint32(raw)
default:
return err
}

// covert our seqno to raw value
raw = make([]byte, 4) // TODO(benschumacher) replace magic number 4 (uint32)
binary.LittleEndian.PutUint32(raw, next_seq)
err = t.Bucket(bucketBuildSeq).Put(key, raw)
if err != nil {
return err
}

// fill out the build structure
build.Number = int(next_seq)
build.Created = time.Now().UTC().Unix()

key = []byte(repo + "/" + strconv.Itoa(build.Number))
return insert(t, bucketBuild, key, build)
})
}

// InsertBuildStatus inserts a new build status for the
// named repository and build number. If the status already
// exists an error is returned.
func (db *DB) InsertBuildStatus(repo string, build int, status *common.Status) error {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + status.Context)
return update(db, bucketBuildStatus, key, status)

return db.Update(func(t *bolt.Tx) error {
return update(t, bucketBuildStatus, key, status)
})
}

// UpdateBuild updates an existing build for the named
Expand All @@ -127,5 +125,8 @@ func (db *DB) InsertBuildStatus(repo string, build int, status *common.Status) e
func (db *DB) UpdateBuild(repo string, build *common.Build) error {
key := []byte(repo + "/" + strconv.Itoa(build.Number))
build.Updated = time.Now().UTC().Unix()
return update(db, bucketBuild, key, build)

return db.Update(func(t *bolt.Tx) error {
return update(t, bucketBuild, key, build)
})
}
45 changes: 36 additions & 9 deletions datastore/bolt/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"time"

"github.com/drone/drone/common"
"github.com/boltdb/bolt"
)

// GetRepo gets the repository by name.
func (db *DB) GetRepo(repo string) (*common.Repo, error) {
repo_ := &common.Repo{}
key := []byte(repo)
err := get(db, bucketRepo, key, repo_)

err := db.View(func (t *bolt.Tx) error {
return get(t, bucketRepo, key, repo_)
})

return repo_, err
}

Expand All @@ -19,7 +24,11 @@ func (db *DB) GetRepo(repo string) (*common.Repo, error) {
func (db *DB) GetRepoParams(repo string) (map[string]string, error) {
params := map[string]string{}
key := []byte(repo)
err := get(db, bucketRepoParams, key, &params)

err := db.View(func (t *bolt.Tx) error {
return get(t, bucketRepoParams, key, &params)
})

return params, err
}

Expand All @@ -28,7 +37,11 @@ func (db *DB) GetRepoParams(repo string) (map[string]string, error) {
func (db *DB) GetRepoKeys(repo string) (*common.Keypair, error) {
keypair := &common.Keypair{}
key := []byte(repo)
err := get(db, bucketRepoKeys, key, keypair)

err := db.View(func (t *bolt.Tx) error {
return get(t, bucketRepoKeys, key, keypair)
})

return keypair, err
}

Expand All @@ -37,7 +50,10 @@ func (db *DB) GetRepoKeys(repo string) (*common.Keypair, error) {
func (db *DB) UpdateRepo(repo *common.Repo) error {
key := []byte(repo.FullName)
repo.Updated = time.Now().UTC().Unix()
return update(db, bucketRepo, key, repo)

return db.Update(func (t *bolt.Tx) error {
return update(t, bucketRepo, key, repo)
})
}

// InsertRepo inserts a repository in the datastore and
Expand All @@ -46,27 +62,38 @@ func (db *DB) InsertRepo(user *common.User, repo *common.Repo) error {
key := []byte(repo.FullName)
repo.Created = time.Now().UTC().Unix()
repo.Updated = time.Now().UTC().Unix()
// TODO(bradrydzewski) add repo to user index
// TODO(bradrydzewski) add user to repo index
return insert(db, bucketRepo, key, repo)

return db.Update(func (t *bolt.Tx) error {
// TODO(bradrydzewski) add repo to user index
// TODO(bradrydzewski) add user to repo index
return insert(t, bucketRepo, key, repo)
})
}

// UpsertRepoParams inserts or updates the private
// environment parameters for the named repository.
func (db *DB) UpsertRepoParams(repo string, params map[string]string) error {
key := []byte(repo)
return update(db, bucketRepoParams, key, params)

return db.Update(func (t *bolt.Tx) error {
return update(t, bucketRepoParams, key, params)
})
}

// UpsertRepoKeys inserts or updates the private and
// public keypair for the named repository.
func (db *DB) UpsertRepoKeys(repo string, keypair *common.Keypair) error {
key := []byte(repo)
return update(db, bucketRepoKeys, key, keypair)

return db.Update(func (t *bolt.Tx) error {
return update(t, bucketRepoKeys, key, keypair)
})
}

// DeleteRepo deletes the repository.
func (db *DB) DeleteRepo(repo *common.Repo) error {
//TODO(benschumacher) rework this to use BoltDB's txn wrapper

t, err := db.Begin(true)
if err != nil {
return err
Expand Down
18 changes: 15 additions & 3 deletions datastore/bolt/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,32 @@ import (
"strconv"

"github.com/drone/drone/common"
"github.com/boltdb/bolt"
)

// GetTask gets the task at index N for the named
// repository and build number.
func (db *DB) GetTask(repo string, build int, task int) (*common.Task, error) {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
task_ := &common.Task{}
err := get(db, bucketBuildTasks, key, task_)
err := db.View(func (t *bolt.Tx) error {
return get(t, bucketBuildTasks, key, task_)
})
return task_, err
}

// GetTaskLogs gets the task logs at index N for
// the named repository and build number.
func (db *DB) GetTaskLogs(repo string, build int, task int) ([]byte, error) {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task))
log, err := raw(db, bucketBuildLogs, key)

var log []byte
err := db.View(func (t *bolt.Tx) error {
var err error
log, err = raw(t, bucketBuildLogs, key)
return err
})

return log, err
}

Expand Down Expand Up @@ -62,7 +72,9 @@ func (db *DB) GetTaskList(repo string, build int) ([]*common.Task, error) {
// repository and build number.
func (db *DB) UpsertTask(repo string, build int, task *common.Task) error {
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + strconv.Itoa(task.Number))
return update(db, bucketBuildTasks, key, task)
return db.Update(func (t *bolt.Tx) error {
return update(t, bucketBuildTasks, key, task)
})
}

// UpsertTaskLogs inserts or updates a task logs for the
Expand Down
15 changes: 12 additions & 3 deletions datastore/bolt/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ package bolt

import (
"github.com/drone/drone/common"
"github.com/boltdb/bolt"
)

// GetToken gets a token by sha value.
func (db *DB) GetToken(sha string) (*common.Token, error) {
token := &common.Token{}
key := []byte(sha)
err := get(db, bucketTokens, key, token)

err := db.View(func (t *bolt.Tx) error {
return get(t, bucketTokens, key, token)
})

return token, err
}

// InsertToken inserts a new user token in the datastore.
// If the token already exists and error is returned.
func (db *DB) InsertToken(token *common.Token) error {
key := []byte(token.Sha)
return insert(db, bucketTokens, key, token)
return db.Update(func (t *bolt.Tx) error {
return insert(t, bucketTokens, key, token)
})
// TODO(bradrydzewski) add token to users_token index
}

// DeleteUser deletes the token.
func (db *DB) DeleteToken(token *common.Token) error {
key := []byte(token.Sha)
return delete(db, bucketUser, key)
return db.Update(func (t *bolt.Tx) error {
return delete(t, bucketUser, key)
})
}
22 changes: 18 additions & 4 deletions datastore/bolt/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import (
"time"

"github.com/drone/drone/common"
"github.com/boltdb/bolt"
)

// GetUser gets a user by user login.
func (db *DB) GetUser(login string) (*common.User, error) {
user := &common.User{}
key := []byte(login)
err := get(db, bucketUser, key, user)

err := db.View(func (t *bolt.Tx) error {
return get(t, bucketUser, key, user)
})

return user, err
}

Expand Down Expand Up @@ -120,7 +125,10 @@ func (db *DB) GetUserList() ([]*common.User, error) {
func (db *DB) UpdateUser(user *common.User) error {
key := []byte(user.Login)
user.Updated = time.Now().UTC().Unix()
return update(db, bucketUser, key, user)

return db.Update(func (t *bolt.Tx) error {
return update(t, bucketUser, key, user)
})
}

// InsertUser inserts a new user into the datastore. If
Expand All @@ -129,13 +137,19 @@ func (db *DB) InsertUser(user *common.User) error {
key := []byte(user.Login)
user.Created = time.Now().UTC().Unix()
user.Updated = time.Now().UTC().Unix()
return insert(db, bucketUser, key, user)

return db.Update(func (t *bolt.Tx) error {
return insert(t, bucketUser, key, user)
})
}

// DeleteUser deletes the user.
func (db *DB) DeleteUser(user *common.User) error {
key := []byte(user.Login)
// TODO(bradrydzewski) delete user subscriptions
// TODO(bradrydzewski) delete user tokens
return delete(db, bucketUser, key)

return db.Update(func (t *bolt.Tx) error {
return delete(t, bucketUser, key)
})
}
Loading

0 comments on commit 46114f1

Please sign in to comment.