From a0d49c4ad9a562c2cc36e5d82e9c97d5fa5a6ef0 Mon Sep 17 00:00:00 2001 From: Andre Pinto Date: Fri, 3 Jun 2016 17:11:02 +0100 Subject: [PATCH] add db adapter --- configuration/config.go | 6 +- domain/database_adpater.go | 48 +++++++++ glide.lock | 28 +++-- glide.yaml | 3 + middlewares/couchbase/couchbase.go | 141 ++++++++++++++++++++++++++ middlewares/couchbase/nickel_query.go | 42 ++++++++ 6 files changed, 260 insertions(+), 8 deletions(-) create mode 100644 domain/database_adpater.go create mode 100644 middlewares/couchbase/couchbase.go create mode 100644 middlewares/couchbase/nickel_query.go diff --git a/configuration/config.go b/configuration/config.go index 761d69b..2eda0ec 100644 --- a/configuration/config.go +++ b/configuration/config.go @@ -17,6 +17,7 @@ type PConfig struct{ Mongodb Mongodb Flags Flags EventSourcing EventSourcing + SupportModule SupportModule } type Server struct { @@ -60,7 +61,6 @@ type Flags struct { S3Region string RollBarToken string RemoteLogs bool - Infos string } type EventSourcing struct{ @@ -68,6 +68,10 @@ type EventSourcing struct{ 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() diff --git a/domain/database_adpater.go b/domain/database_adpater.go new file mode 100644 index 0000000..c8c7776 --- /dev/null +++ b/domain/database_adpater.go @@ -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 +} \ No newline at end of file diff --git a/glide.lock b/glide.lock index 6b7f666..0d330db 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: 368fc86b1d82e2c4429dc20a5afce4d1fdc968a79077d8c5f1580fa4b9bcb8a9 -updated: 2016-05-30T15:33:14.8640987+01:00 +hash: daced0d34a386e4f78abc672e41397f8c2bb45eeb5a9ebd4808e822b2400166b +updated: 2016-06-03T11:03:10.6295781+01:00 imports: - name: github.com/armon/consul-api version: dcfedd50ed5334f96adee43fc88518a4f095e15c @@ -10,13 +10,19 @@ imports: subpackages: - github.com\BurntSushi\toml - name: github.com/codegangsta/negroni - version: fb7b7c045dfb05dc81a5c3688c568550b5bd6e36 + version: feacfc52d357c844f524c794947493483ed881b3 subpackages: - github.com\codegangsta\negroni - name: github.com/coreos/go-etcd version: 003851be7bb0694fe3cc457a49529a19388ee7cf subpackages: - github.com\coreos\go-etcd\etcd +- name: github.com/couchbase/gocb + version: af0565dd4f6167a98a5a6f180bea333426bd04a9 + subpackages: + - github.com\couchbase\gocb + - github.com\couchbase\gocb\gocbcore + - github.com\couchbase\gocb\jsonx - name: github.com/fsnotify/fsnotify version: 30411dbcefb7a1da7e84f75530ad3abe4011b4f8 subpackages: @@ -74,12 +80,20 @@ imports: version: 3eb4713e2655cd1be805795be31b3f85b74b05fd subpackages: - github.com\stvp\rollbar +- name: github.com/Tlantic/mrs-integration-api-gateway + version: b986a30c929932fedab25a2ba09b6bac2d0c6d61 + subpackages: + - github.com\Tlantic\mrs-integration-api-gateway\domain - name: github.com/Tlantic/mrs-integration-utils version: c23ff162d970e37503784c00b2a92d7711e0501a subpackages: - github.com\Tlantic\mrs-integration-utils\configuration +- name: github.com/twinj/uuid + version: 5a4b9dcb2a5e9eaba079cd853d275582fc764505 + subpackages: + - github.com\twinj\uuid - name: github.com/ugorji/go - version: a396ed22fc049df733440d90efe17475e3929ccb + version: b94837a2404ab90efe9289e77a70694c355739cb subpackages: - github.com\ugorji\go\codec - name: github.com/unrolled/render @@ -105,11 +119,11 @@ imports: - golang.org\x\crypto\cast5 - golang.org\x\crypto\openpgp\elgamal - name: golang.org/x/net - version: 30db96677b74e24b967e23f911eb3364fc61a011 + version: c4c3ea71919de159c9e246d7be66deb7f0a39a58 subpackages: - golang.org\x\net\netutil - name: golang.org/x/sys - version: d4feaf1a7e61e1d9e79e6c4e76c6349e9cab0a03 + version: 076b546753157f758b316e59bcb51e6807c04057 subpackages: - golang.org\x\sys\unix - name: gopkg.in/mgo.v2 @@ -120,7 +134,7 @@ imports: - gopkg.in\mgo.v2\internal\sasl - gopkg.in\mgo.v2\internal\scram - name: gopkg.in/olivere/elastic.v2 - version: d255a5d003ec35be2bf62361588fdb75eca8a701 + version: 7ca93ba4173415890f2748cfd921cca8b3ebd3aa subpackages: - gopkg.in\olivere\elastic.v2 - gopkg.in\olivere\elastic.v2\backoff diff --git a/glide.yaml b/glide.yaml index fb55e67..7216761 100644 --- a/glide.yaml +++ b/glide.yaml @@ -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 + diff --git a/middlewares/couchbase/couchbase.go b/middlewares/couchbase/couchbase.go new file mode 100644 index 0000000..d7c9f54 --- /dev/null +++ b/middlewares/couchbase/couchbase.go @@ -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() +} \ No newline at end of file diff --git a/middlewares/couchbase/nickel_query.go b/middlewares/couchbase/nickel_query.go new file mode 100644 index 0000000..7047213 --- /dev/null +++ b/middlewares/couchbase/nickel_query.go @@ -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 +}