Skip to content

Commit

Permalink
feat: adding content extraction and timing the functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ghanithan committed Oct 22, 2024
1 parent f6cd15f commit 5b747a7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 18 deletions.
44 changes: 40 additions & 4 deletions database/cb.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@ import (

"github.com/couchbase/gocb/v2"
"github.com/ghanithan/goBilling/config"
"github.com/ghanithan/goBilling/instrumentation"
)

// Module for couchbase db wrapper

type couchbaseDatabase struct {
Db *gocb.Cluster
Db *gocb.Cluster
Bucket *gocb.Bucket
logger instrumentation.GoLogger
}

var (
once sync.Once
dbInstance couchbaseDatabase
)

func InitCouchbaseDb(config *config.Config) couchbaseDatabase {
func InitCouchbaseDb(config *config.Config, logger instrumentation.GoLogger) couchbaseDatabase {
defer logger.TimeTheFunction(time.Now(), "InitCouchbaseDb")
once.Do(func() {
options := gocb.ClusterOptions{
Authenticator: gocb.PasswordAuthenticator{
Expand Down Expand Up @@ -49,18 +53,50 @@ func InitCouchbaseDb(config *config.Config) couchbaseDatabase {
log.Fatal(err)
}

bucket := cluster.Bucket(config.Db.Reponame)
err = bucket.WaitUntilReady(5*time.Second, nil)
if err != nil {
log.Fatal(err)
}

dbInstance = couchbaseDatabase{
Db: cluster,
Db: cluster,
Bucket: bucket,
logger: logger,
}
})
return dbInstance
}

func (cb *couchbaseDatabase) GetDb() *gocb.Cluster {
func (cb *couchbaseDatabase) GetDbInstance() *gocb.Cluster {
return cb.Db
}

func (cb *couchbaseDatabase) GetDb() *gocb.Bucket {
return cb.Bucket
}

func (cb *couchbaseDatabase) Close() {
closeOpt := gocb.ClusterCloseOptions{}
cb.Db.Close(&closeOpt)
}

func (cb *couchbaseDatabase) FetchById(collection string, id string) (*gocb.GetResult, error) {
getResult, err := cb.Bucket.Collection(collection).Get(id, &gocb.GetOptions{})
if err != nil {
cb.logger.Error("Issue in fetching from collection: %q", err)
return nil, err
}

return getResult, nil
}

func ExtractContent[T any](getResult *gocb.GetResult, extractedOutput *T, logger instrumentation.GoLogger) error {
err := getResult.Content(getResult)
if err != nil {
logger.Error("Issue in unmarsheling: %q", err)
return err
}
return nil

}
55 changes: 43 additions & 12 deletions database/cb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ package database
import (
"fmt"
"testing"
"time"

"github.com/couchbase/gocb/v2"
"github.com/ghanithan/goBilling/config"
"github.com/ghanithan/goBilling/instrumentation"
)

type userType struct {
Id string `json:custid`
Name string `json: name`
Address struct {
Street string `json: street`
City string `json: city`
Zipcode string `json: zipcode`
} `json: address`
}

func TestInitCouchbaseDb(t *testing.T) {
logger := instrumentation.InitInstruments()
defer logger.TimeTheFunction(time.Now(), "TestInitCouchbaseDb")

if testing.Short() {
t.Skip("skipping test in short mode.")
}
Expand All @@ -17,13 +32,13 @@ func TestInitCouchbaseDb(t *testing.T) {
t.Fatalf("Error in reading config: %s", err)
}

db := InitCouchbaseDb(config)
db := InitCouchbaseDb(config, logger)

cluster := db.GetDb()
cluster := db.GetDbInstance()

// col := bucket.Scope("_default").Collection("customers")

// // Get the document back
// Get the document back
// getResult, err := col.Get("custid:C1", nil)
// if err != nil {
// t.Errorf("Error fetching: %s", err)
Expand Down Expand Up @@ -52,15 +67,31 @@ func TestInitCouchbaseDb(t *testing.T) {
fmt.Printf("interface result: %v\n", intfc)
}

// type UserType struct {
// Id string `json:custid`
// Name string `json: name`
// Address struct {
// Street string `json: street`
// City string `json: city`
// Zipcode string `json: zipcode`
// } `json: address`
// }
getResult, _ := db.GetDb().Collection("customers").Get("C1", &gocb.GetOptions{})

user := &userType{}

err = getResult.Content(&user)
if err != nil {
t.Errorf("Issue in unmarsheling: %q", err)

}

fmt.Println("output: ", user)

user1 := &userType{}

getResult, err = db.FetchById("customers", "C1")

if err != nil {
t.Errorf("Error in FetchById: %s", err)
}

if err := ExtractContent(getResult, user1, logger); err != nil {
t.Errorf("Error in extracting content: %s", err)

}
t.Errorf("output: %s", user1)

// user := &UserType{}
// err = getResult.Content(&user)
Expand Down
2 changes: 1 addition & 1 deletion database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ type DbService interface {
}

type DbStub interface {
FetchById(...string) any
FetchById(string, string) (any, error)
}
2 changes: 1 addition & 1 deletion instrumentation/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (l *GoLogger) Error(msg string, args ...any) {
l.logger.Error(msg, args...)
}

func TimeTheFunction(start time.Time, functionName string) {
func (l *GoLogger) TimeTheFunction(start time.Time, functionName string) {
elapsed := time.Since(start)
log.Printf("%s took %dms to complete", functionName, elapsed.Nanoseconds()/1000)
}

0 comments on commit 5b747a7

Please sign in to comment.