Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BoltDB support added #15

Merged
merged 4 commits into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ Simple DB CRUD operations service. Supports some golang Key-Value pair file base

- [x] ~~Upload existing DB~~
- [x] ~~View Key-Value pairs~~
- [x] Add new Key-Value pair
- [x] Remove Key-Value pair
- [x] Update Key-Value pair
- [x] Download updated file
- [ ] View Buckets in boltDB
- [ ] Add / remove bucket
- [x] ~~Add new Key-Value pair~~
- [x] ~~Remove Key-Value pair~~
- [x] ~~Update Key-Value pair~~
- [x] ~~Download updated file~~
- [x] View Buckets in boltDB
- [x] Add / remove bucket
- [ ] Move/Copy Key-Value pair under a bucket to another bucket

## Usage
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v.0.0.3
v.0.1.1
59 changes: 56 additions & 3 deletions database/boltdb_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ import (
"github.com/boltdb/bolt"
)

type buckets interface {
AddBucket(string)
SetBucket(string)
ListBuckets() ([]string, error)
DeleteBucket(string) error
}

// BoltDB - BoltDB struct
type BoltDB struct {
*bolt.DB
bucketName []byte
buckets
}

// BoltBucket godoc - BoltDB Bucket struct
type BoltBucket struct {
*bolt.DB
// Conn godoc - Returns the underlying database connection
func (db *BoltDB) Conn() interface{} {
return db
}

// openBolt godoc - Creates a new BoltDB instance
Expand Down Expand Up @@ -101,3 +109,48 @@ func (db *BoltDB) List(args ...interface{}) (data []KeyValuePair, err error) {
})
return
}

// AddBucket godoc - Adds a new bucket to the database
func (db *BoltDB) AddBucket(bucketName string) error {

// add the bucket to the boltdb file
return db.Update(func(t *bolt.Tx) error {
_, err := t.CreateBucketIfNotExists([]byte(bucketName))
return err
})
}

// SetBucket godoc - Sets the current bucket
func (db *BoltDB) SetBucket(bucketName string) {
db.bucketName = []byte(bucketName)
}

// SetBucket godoc - Sets the current bucket
func (db *BoltDB) GetDefBucket() string {
return string(db.bucketName)
}

// ListBuckets godoc - Lists all buckets in the database
func (db *BoltDB) ListBuckets() (data []string, err error) {

// open the db in view mode
err = db.View(func(tx *bolt.Tx) error {

// iterate on each buckets from the db
err = tx.ForEach(func(name []byte, _ *bolt.Bucket) error {
data = append(data, string(name))
return err
})
return err
})
return
}

// DeleteBucket godoc - Deletes a bucket from the database
func (db *BoltDB) DeleteBucket(bucketName string) error {

// delete the bucket from the boltdb file
return db.Update(func(tx *bolt.Tx) error {
return tx.DeleteBucket([]byte(bucketName))
})
}
5 changes: 5 additions & 0 deletions database/buntdb_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ type BuntDB struct {
*buntdb.DB
}

// Conn godoc - Returns the underlying database connection
func (db *BuntDB) Conn() interface{} {
return db
}

// openBunt godoc - Creates a new BuntDB instance
func openBunt(fileName string) (db *BuntDB, err error) {

Expand Down
9 changes: 1 addition & 8 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,14 @@ type KeyValuePair struct {

// DB interface for underlying database packages
type DB interface {
Conn() interface{}
Add(string, string, ...interface{}) error
CloseDB()
Get(key string) (string, error) // TODO: add list all keys
Delete(string) error
List(args ...interface{}) ([]KeyValuePair, error)
}

// Buckets interface for underlying bolt DB buckets
type Buckets interface {
Add()
Get()
List()
Delete()
}

// DBType for identifying underlying database packages
type DBType string

Expand Down
145 changes: 141 additions & 4 deletions server/bucket.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,157 @@
package server

import (
"encoding/json"
"log"
"net/url"

"github.com/ric-v/divulge-keyvalue-db-ui/database"
"github.com/valyala/fasthttp"
)

// addBucket is the handler for the POST /api/v1/db/bucket/dbKey/file endpoint.
// Adds a new bucket to the open DB file.
func addBucket(ctx *fasthttp.RequestCtx) {

var bucket string
// get the dbKey from header
dbSession, err := handleDBSession(ctx)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

switch dbSession.DBType {

case database.BOLT_DB:
// get the DB type from params
bucket = string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// add the bucket to the db
err := dbSession.DB.Conn().(*database.BoltDB).AddBucket(bucket)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

default:
log.Println("DB type not supported:", dbSession.DBType)
ctx.Error("DB type not supported: "+dbSession.DBType, fasthttp.StatusInternalServerError)
return
}

// return success message to UI
json.NewEncoder(ctx).Encode(generateResponse("Successfully added bucket: "+bucket, nil, &dbSession))
}

// listBucket godoc - loads the list of buckets in a boltdb file
func listBuckets(ctx *fasthttp.RequestCtx) {

// get the dbKey from params
dbKey := string(ctx.QueryArgs().Peek("dbKey"))
log.Println("dbKey:", dbKey)
type bucketList struct {
Buckets []string `json:"buckets"`
DefaultBucket string `json:"defaultBucket"`
}

var buckets bucketList

// get the dbKey from header
dbSession, err := handleDBSession(ctx)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

switch dbSession.DBType {

case database.BOLT_DB:
var err error
// get the list of buckets from the db
list, err := dbSession.DB.Conn().(*database.BoltDB).ListBuckets()
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}
buckets.Buckets = list
buckets.DefaultBucket = dbSession.DB.Conn().(*database.BoltDB).GetDefBucket()
}

// return success message to UI
json.NewEncoder(ctx).Encode(generateResponse("", buckets, &dbSession))
}

// setBucket is the handler for the POST /api/v1/db/bucket/dbKey/file endpoint.
func setBucket(ctx *fasthttp.RequestCtx) {

var bucket string
// get the dbKey from header
dbSession, err := handleDBSession(ctx)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

switch dbSession.DBType {

case database.BOLT_DB:
// get the DB type from params
bucket = string(ctx.UserValue("bucket").(string))
bucket, _ = url.QueryUnescape(bucket)
log.Println("bucket:", bucket)

// set the bucket in the db
dbSession.DB.Conn().(*database.BoltDB).SetBucket(bucket)

default:
log.Println("DB type not supported:", dbSession.DBType)
ctx.Error("DB type not supported: "+dbSession.DBType, fasthttp.StatusInternalServerError)
return
}

// return success message to UI
json.NewEncoder(ctx).Encode(generateResponse("Successfully applied default bucket: "+bucket, nil, &dbSession))
}

func getBucket(ctx *fasthttp.RequestCtx) {
// deleteBucket is the handler for the POST /api/v1/db/bucket/dbKey/file endpoint.
// Removes a bucket from the open DB file.
func deleteBucket(ctx *fasthttp.RequestCtx) {

var bucket string
// get the dbKey from header
dbSession, err := handleDBSession(ctx)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

switch dbSession.DBType {

case database.BOLT_DB:

// get the DB type from params
bucket = string(ctx.QueryArgs().Peek("bucket"))
log.Println("bucket:", bucket)

// delete the bucket from the db
err := dbSession.DB.Conn().(*database.BoltDB).DeleteBucket(bucket)
if err != nil {
log.Println(err)
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
return
}

default:
log.Println("DB type not supported:", dbSession.DBType)
ctx.Error("DB type not supported: "+dbSession.DBType, fasthttp.StatusInternalServerError)
return
}

// return success message to UI
json.NewEncoder(ctx).Encode(generateResponse("Successfully removed bucket: "+bucket, nil, &dbSession))
}
Loading