Skip to content

Commit

Permalink
Merge pull request #1 from Tlantic/master
Browse files Browse the repository at this point in the history
add db adapter
  • Loading branch information
andrepinto committed Jun 3, 2016
2 parents 475a258 + a0d49c4 commit ab57aa3
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 8 deletions.
6 changes: 5 additions & 1 deletion configuration/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type PConfig struct{
Mongodb Mongodb
Flags Flags
EventSourcing EventSourcing
SupportModule SupportModule
}

type Server struct {
Expand Down Expand Up @@ -60,14 +61,17 @@ type Flags struct {
S3Region string
RollBarToken string
RemoteLogs bool
Infos string
}

type EventSourcing struct{
Exchange string
Prefix string
}

type SupportModule struct {
BucketName string
}

func InitConfig(path string, prefix string, url string, contentType string){
cf := configuration.GetConfiguration(path, prefix, url, contentType)
cf.Load()
Expand Down
48 changes: 48 additions & 0 deletions domain/database_adpater.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package domain


type DbAdapter interface {
GetName() string
Create(DbObject) error
Read(string) (error, []*DbObject)
ReadOne(string) (error, *DbObject)
ReadOneWithType(string, interface{}) (error, *DbObject)
Update(*DbObject) error
UpdateOne(*DbObject) error
Destroy(*DbObject) error
DestroyOne(string) error
}


type DbObject struct {
Key string
Data interface{}
Expiry uint32
}

func NewDbObject(key string) *DbObject {
return &DbObject{
Key: key,
}
}

func (c *DbObject) GetKey() string {
return c.Key
}

func (c *DbObject) SetKey(key string) {
c.Key = key
}

func (c *DbObject) SetData(data interface{}) {
c.Data = data
}

func (c *DbObject) GetData() interface{} {
return c.Data
}


func (c *DbObject) SetExpiry(time uint32) {
c.Expiry = time
}
28 changes: 21 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ import:
- package: gopkg.in/tylerb/graceful.v1
subpackages:
- gopkg.in\tylerb\graceful.v1
- package: github.com\couchbase\gocb
version: v1.0.8

141 changes: 141 additions & 0 deletions middlewares/couchbase/couchbase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package couchbase

import (
"github.com/couchbase/gocb"
"github.com/twinj/uuid"
"github.com/Tlantic/mrs-integration-api-gateway/domain"
)

type CouchbaseStore struct {
name string
host string
bucketName string
bucketUser string
bucketPassword string
bucket *gocb.Bucket
cluster *gocb.Cluster
}

func NewCouchbaseStore(host, bucketName, bucketUser, bucketPassword string) *CouchbaseStore {
return &CouchbaseStore{
name: "couchbase",
host: host,
bucketName: bucketName,
bucketUser: bucketUser,
bucketPassword: bucketPassword,
}
}

func (c *CouchbaseStore) ConnectBucket() error {
cluster, err := gocb.Connect(c.host)
if err != nil {
return err
}


b, err := cluster.OpenBucket(c.bucketName, c.bucketPassword)
if err != nil {
return err
}

c.bucket = b
c.cluster = cluster
return nil
}

func (c *CouchbaseStore) ShutdownBucket() {
c.bucket.Close()
}

func (c *CouchbaseStore) GetName() string {
return c.name
}

func (c *CouchbaseStore) SetName(name string) error {
c.name = name
return nil
}



func (c *CouchbaseStore) Create(obj domain.DbObject) error {

if obj.Key == "" {
obj.Key = uuid.NewV4().String()
}
_, err := c.bucket.Insert(obj.Key, obj.Data, obj.Expiry)
if err != nil {
return err
}

return nil
}

func (c *CouchbaseStore) ReadOneWithType(key string, data interface{}) (error, *domain.DbObject) {
//var data interface{}
_, err := c.bucket.Get(key, &data)
if err != nil {
return err, nil
}

obj := &domain.DbObject{
Key: key,
Data: data,
}

return nil, obj
}

func (c *CouchbaseStore) ReadOne(key string) (error, *domain.DbObject) {
var data interface{}
_, err := c.bucket.Get(key, &data)
if err != nil {
return err, nil
}


obj := &domain.DbObject{
Key: key,
Data: data,
}

return nil, obj
}

func (c *CouchbaseStore) UpdateOne(obj *domain.DbObject) error {
_, err := c.bucket.Replace(obj.Key, obj.Data, 0, obj.Expiry)
if err != nil {
return err
}

return nil
}


func (c *CouchbaseStore) Update(obj *domain.DbObject) error {
_, err := c.bucket.Replace(obj.Key, obj.Data, 0, obj.Expiry)
if err != nil {
return err
}

return nil
}

func (c *CouchbaseStore) DestroyOne(key string) error {
// We do not need to keep the ID that this returns.
_, err := c.bucket.Remove(key, 0)
if err != nil {
return err
}

return nil
}

func (c *CouchbaseStore) Destroy(data *domain.DbObject) error {
return nil
}

func (c *CouchbaseStore) Read(query string) (error, []*domain.DbObject) {
qyr := NewNickelQuery(query, c.bucket)
return qyr.Execute()
}
42 changes: 42 additions & 0 deletions middlewares/couchbase/nickel_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package couchbase

import (
"github.com/couchbase/gocb"
"github.com/Tlantic/mrs-integration-api-gateway/domain"
)

type NickelQuery struct {
query string
bucket *gocb.Bucket
}

func NewNickelQuery(query string, bucket *gocb.Bucket) *NickelQuery {
return &NickelQuery{
query: query,
bucket: bucket,
}
}

func (n *NickelQuery) Execute() (error, []*domain.DbObject) {
query := gocb.NewN1qlQuery(n.query)
rows, err := n.bucket.ExecuteN1qlQuery(query, nil)
if err != nil {
return err, nil
}

var document interface{}
var documents []*domain.DbObject
for rows.Next(&document) {
doc := &domain.DbObject{
Data: document,
}
documents = append(documents, doc)
}

err = rows.Close()
if err != nil {
return err, nil
}

return nil, documents
}

0 comments on commit ab57aa3

Please sign in to comment.