removed some comments / dead code

This commit is contained in:
Brad Rydzewski 2015-04-30 20:40:59 -07:00
parent 3a4772cbe6
commit 4e61464225
6 changed files with 22 additions and 162 deletions

View file

@ -4,8 +4,6 @@ import (
"errors"
"github.com/boltdb/bolt"
"github.com/drone/drone/datastore"
)
var (
@ -57,13 +55,6 @@ func New(path string) (*DB, error) {
return nil
})
// REMOVE BELOW
var ds datastore.Datastore
if ds == nil {
ds = &DB{db}
}
// REMOVE ABOVE
return &DB{db}, nil
}

View file

@ -1,7 +1,6 @@
package bolt
import (
//"bytes"
"encoding/binary"
"strconv"
"time"
@ -114,41 +113,6 @@ func (db *DB) SetBuild(repo string, build *common.Build) error {
})
}
/*
// Status returns the status for the given repository
// and build number.
func (db *DB) Status(repo string, build int, status string) (*common.Status, error) {
status_ := &common.Status{}
key := []byte(repo + "/" + strconv.Itoa(build) + "/" + status)
err := db.Update(func(t *bolt.Tx) error {
return update(t, bucketBuildStatus, key, status)
})
return status_, err
}
// StatusList returned a list of all build statues for
// the given repository and build number.
func (db *DB) StatusList(repo string, build int) ([]*common.Status, error) {
// TODO (bradrydzewski) explore efficiency of cursor vs index
statuses := []*common.Status{}
err := db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(bucketBuildStatus).Cursor()
prefix := []byte(repo + "/" + strconv.Itoa(build) + "/")
for k, v := c.Seek(prefix); bytes.HasPrefix(k, prefix); k, v = c.Next() {
status := &common.Status{}
if err := decode(v, status); err != nil {
return err
}
statuses = append(statuses, status)
}
return nil
})
return statuses, err
}
*/
// SetStatus inserts a new build status for the
// named repository and build number. If the status already
// exists an error is returned.

View file

@ -102,10 +102,6 @@ func (db *DB) SetRepoNotExists(user *common.User, repo *common.Repo) error {
if err != nil {
return err
}
// err = push(t, bucketRepoUsers, repokey, userkey)
// if err != nil {
// return err
// }
return insert(t, bucketRepo, repokey, repo)
})
}
@ -141,41 +137,15 @@ func (db *DB) DelRepo(repo *common.Repo) error {
}
t.Bucket(bucketRepoKeys).Delete(key)
t.Bucket(bucketRepoParams).Delete(key)
// should we just ignore these error conditions? or should
// we go ahead with the transaction and assume we can
// cleanup the leftovers through some other maintenance process?
err = db.deleteTracesOfRepo(t, key)
t.Bucket(bucketBuildSeq).Delete(key)
deleteWithPrefix(t, bucketBuild, append(key, '/'))
deleteWithPrefix(t, bucketBuildLogs, append(key, '/'))
deleteWithPrefix(t, bucketBuildStatus, append(key, '/'))
return err
})
}
// deleteTracesOfRepo cleans up build leftovers when a repo is removed
func (db *DB) deleteTracesOfRepo(t *bolt.Tx, repoKey []byte) error {
// bucketBuildSeq uses the repoKey directly
err := t.Bucket(bucketBuildSeq).Delete(repoKey)
if err != nil {
// only error here is if our Tx is read-only
return err
}
// the other buckets use repoKey with '/buildNumber', at least.
// validating that an additiona '/' is there ensures that we don't
// match 'github.com/drone/droney' when we're cleaning up after
// 'github.com/drone/drone'.
prefix := append(repoKey, '/')
buckets := [][]byte{bucketBuildStatus, bucketBuildLogs, bucketBuild}
for _, b := range buckets {
err = deleteWithPrefix(t, b, prefix)
if err != nil {
break
}
}
return err
}
// Subscribed returns true if the user is subscribed
// to the named repository.
//

View file

@ -63,10 +63,7 @@ func TestRepo(t *testing.T) {
db.SetBuild(testRepo, &common.Build{State: "success"})
db.SetBuild(testRepo, &common.Build{State: "success"})
db.SetBuild(testRepo, &common.Build{State: "pending"})
db.SetBuildStatus(testRepo, 1, &common.Status{Context: "success"})
db.SetBuildStatus(testRepo, 2, &common.Status{Context: "success"})
db.SetBuildStatus(testRepo, 3, &common.Status{Context: "pending"})
db.SetLogs(testRepo, 1, 1, []byte("foo"))
// first a little sanity to validate our test conditions
_, err = db.BuildLast(testRepo)
@ -82,7 +79,7 @@ func TestRepo(t *testing.T) {
g.Assert(err).Equal(ErrKeyNotFound)
})
g.It("Should get RepoList", func() {
g.It("Should get Repo list", func() {
db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo})
db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo2})
@ -91,13 +88,13 @@ func TestRepo(t *testing.T) {
g.Assert(len(repos)).Equal(2)
})
g.It("Should set RepoParams", func() {
g.It("Should set Repo parameters", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetRepoParams(testRepo, map[string]string{"A": "Alpha"})
g.Assert(err).Equal(nil)
})
g.It("Should get RepoParams", func() {
g.It("Should get Repo parameters", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetRepoParams(testRepo, map[string]string{"A": "Alpha", "B": "Beta"})
params, err := db.RepoParams(testRepo)
@ -117,14 +114,14 @@ func TestRepo(t *testing.T) {
g.Assert(err_).Equal(ErrKeyExists)
})
g.It("Should set RepoKeypair", func() {
g.It("Should set Repo keypair", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetRepoKeypair(testRepo, &common.Keypair{Private: "A", Public: "Alpha"})
g.Assert(err).Equal(nil)
})
g.It("Should get RepoKeypair", func() {
g.It("Should get Repo keypair", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetRepoKeypair(testRepo, &common.Keypair{Private: "A", Public: "Alpha"})
@ -134,13 +131,13 @@ func TestRepo(t *testing.T) {
g.Assert(keypair.Private).Equal("A")
})
g.It("Should set Subscriber", func() {
g.It("Should set subscriber", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetSubscriber(testUser, testRepo)
g.Assert(err).Equal(nil)
})
g.It("Should get Subscribed", func() {
g.It("Should get subscribed", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
err := db.SetSubscriber(testUser, testRepo)
subscribed, err := db.Subscribed(testUser, testRepo)
@ -148,7 +145,7 @@ func TestRepo(t *testing.T) {
g.Assert(subscribed).Equal(true)
})
g.It("Should del Subscriber", func() {
g.It("Should del subscriber", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
db.SetSubscriber(testUser, testRepo)
err := db.DelSubscriber(testUser, testRepo)
@ -161,55 +158,3 @@ func TestRepo(t *testing.T) {
})
}
func TestRepoDel(t *testing.T) {
g := Goblin(t)
g.Describe("Delete repo", func() {
var db *DB // temporary database
user := &common.User{Login: "freya"}
repoUri := string("github.com/octopod/hq")
// create a new database before each unit
// test and destroy afterwards.
g.BeforeEach(func() {
file, err := ioutil.TempFile(os.TempDir(), "drone-bolt")
if err != nil {
panic(err)
}
db = Must(file.Name())
})
g.AfterEach(func() {
os.Remove(db.Path())
})
g.It("should cleanup", func() {
repo := &common.Repo{FullName: repoUri}
err := db.SetRepoNotExists(user, repo)
g.Assert(err).Equal(nil)
db.SetBuild(repoUri, &common.Build{State: "success"})
db.SetBuild(repoUri, &common.Build{State: "success"})
db.SetBuild(repoUri, &common.Build{State: "pending"})
db.SetBuildStatus(repoUri, 1, &common.Status{Context: "success"})
db.SetBuildStatus(repoUri, 2, &common.Status{Context: "success"})
db.SetBuildStatus(repoUri, 3, &common.Status{Context: "pending"})
// first a little sanity to validate our test conditions
_, err = db.BuildLast(repoUri)
g.Assert(err).Equal(nil)
// now run our specific test suite
// 1. ensure that we can delete the repo
err = db.DelRepo(repo)
g.Assert(err).Equal(nil)
// 2. ensure that deleting the repo cleans up other references
_, err = db.Build(repoUri, 1)
g.Assert(err).Equal(ErrKeyNotFound)
})
})
}

View file

@ -11,11 +11,11 @@ import (
func TestTask(t *testing.T) {
g := Goblin(t)
g.Describe("Tasks", func() {
//testUser := "octocat"
testRepo := "github.com/octopod/hq"
testRepo := "octopod/hq"
testBuild := 1
testTask := 0
//testTask2 := 1
testLogInfo := []byte("Log Info for SetLogs()")
var db *DB // Temp database
@ -30,22 +30,16 @@ func TestTask(t *testing.T) {
g.It("Should set Logs", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
//db.SetTask(testRepo, testBuild, &common.Task{Number: testTask})
//db.SetTask(testRepo, testBuild, &common.Task{Number: testTask2})
//
err := db.SetLogs(testRepo, testBuild, testTask, testLogInfo)
g.Assert(err).Equal(nil)
})
g.It("Should LogReader", func() {
g.It("Should get logs", func() {
db.SetRepo(&common.Repo{FullName: testRepo})
//db.SetTask(testRepo, testBuild, &common.Task{Number: testTask})
//db.SetTask(testRepo, testBuild, &common.Task{Number: testTask2})
db.SetLogs(testRepo, testBuild, testTask, testLogInfo)
//
buf, err_ := db.LogReader(testRepo, testBuild, testTask)
g.Assert(err_).Equal(nil)
logInfo, err_ := ioutil.ReadAll(buf)
buf, err := db.LogReader(testRepo, testBuild, testTask)
g.Assert(err).Equal(nil)
logInfo, err := ioutil.ReadAll(buf)
g.Assert(logInfo).Equal(testLogInfo)
})
})

View file

@ -71,9 +71,6 @@ func (db *DB) SetUserNotExists(user *common.User) error {
// DelUser deletes the user.
func (db *DB) DelUser(user *common.User) error {
key := []byte(user.Login)
// TODO(bradrydzewski) delete user subscriptions
// TODO(bradrydzewski) delete user tokens
return db.Update(func(t *bolt.Tx) error {
err := delete(t, bucketUserTokens, key)
if err != nil {
@ -83,9 +80,8 @@ func (db *DB) DelUser(user *common.User) error {
if err != nil {
return err
}
// deletePrefix(t, bucketTokens, prefix)
// or
// deleteKeys(t, bucketTokens, keys)
// IDEA: deleteKeys(t, bucketTokens, keys)
deleteWithPrefix(t, bucketTokens, append(key, '/'))
return delete(t, bucketUser, key)
})
}