-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Tlantic/master
add db adapter
- Loading branch information
Showing
6 changed files
with
260 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |