Skip to content

Commit

Permalink
Merge pull request #9 from FrosTiK-SD/debug
Browse files Browse the repository at this point in the history
Added support for debug
  • Loading branch information
dev-raj-1729 committed Feb 2, 2024
2 parents 8f64a11 + 486c26b commit 345adbc
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 28 deletions.
2 changes: 2 additions & 0 deletions constants/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ var DB_FIND = "DB_FIND"
var DB_FINDONE = "DB_FINDONE"
var DB_AGGREGATE = "DB_AGGREGATE"
var DB_AGGREGATEONE = "DB_AGGREGATEONE"

const MONGIK_DEBUG = "MONGIK_DEBUG"
16 changes: 6 additions & 10 deletions db/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ func Aggregate[Result any](mongikClient *mongik.Mongik, db string, collectionNam
if !noCache {
resultBytes := DBCacheFetch(mongikClient, key)
if err := json.Unmarshal(resultBytes, &result); err == nil {
fmt.Println("Retrieving DB call from the cache with cache key ", key)
return result, nil
}
}

CacheLog(mongikClient, fmt.Sprintf("Querying the DB\n KEY: %s", key))

// Query to DB
fmt.Println("Querying the DB")
cursor, err := mongikClient.MongoClient.Database(db).Collection(collectionName).Aggregate(context.Background(), pipeline, opts...)
if err != nil {
return nil, err
Expand All @@ -39,9 +39,7 @@ func Aggregate[Result any](mongikClient *mongik.Mongik, db string, collectionNam
lookupCollections := getLookupCollections(pipeline)

// Set to cache
if err := DBCacheSet(mongikClient, key, result, lookupCollections...); err == nil {
fmt.Println("Successfully set DB call in cache with key ", key)
}
DBCacheSet(mongikClient, key, result, lookupCollections...)

return result, nil
}
Expand All @@ -55,13 +53,13 @@ func AggregateOne[Result any](mongikClient *mongik.Mongik, db string, collection
if !noCache {
resultBytes := DBCacheFetch(mongikClient, key)
if err := json.Unmarshal(resultBytes, &result); err == nil {
fmt.Println("Retrieving DB call from the cache with cache key ", key)
return result, nil
}
}

CacheLog(mongikClient, fmt.Sprintf("Querying the DB\n KEY: %s", key))

// Query to DB
fmt.Println("Querying the DB")
cursor, err := mongikClient.MongoClient.Database(db).Collection(collectionName).Aggregate(context.Background(), pipeline, opts...)
if err != nil {
return result, err
Expand All @@ -79,9 +77,7 @@ func AggregateOne[Result any](mongikClient *mongik.Mongik, db string, collection
lookupCollections := getLookupCollections(pipeline)

// Set to cache
if err := DBCacheSet(mongikClient, key, result, lookupCollections...); err == nil {
fmt.Println("Successfully set DB call in cache with key ", key)
}
DBCacheSet(mongikClient, key, result, lookupCollections...)

return result, nil
}
7 changes: 5 additions & 2 deletions db/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ func DBCacheSet(mongikClient *mongik.Mongik, key string, value interface{}, look
} else if mongikClient.Config.Client == constants.REDIS {
err := mongikClient.RedisClient.Set(context.Background(), constants.KEY_STORE, keyStoreBytes, mongikClient.Config.TTL).Err()
if err != nil {

return err
}
return mongikClient.RedisClient.Set(context.Background(), key, valueBytes, mongikClient.Config.TTL).Err()
}

fmt.Println("Keystore set: ", keyStore)
CacheLog(mongikClient, fmt.Sprintf("Keystore set: %s", keyStore))

return nil
}
Expand Down Expand Up @@ -88,7 +89,7 @@ func DBCacheReset(mongikClient *mongik.Mongik, clusterName string) {
mongikClient.RedisClient.Del(context.Background(), key)
}
}

keyStore[clusterName] = []string{}

// Set the key store
Expand All @@ -115,9 +116,11 @@ func DBCacheFetch(mongikClient *mongik.Mongik, key string) []byte {
// Fetch from Cache
if mongikClient.Config.Client == constants.BIGCACHE {
resultBytes, _ := mongikClient.CacheClient.Get(key)
CacheLog(mongikClient, fmt.Sprintf("Retrieved data from the cache of the key: %s", key))
return resultBytes
} else if mongikClient.Config.Client == constants.REDIS {
resultBytes, _ := mongikClient.RedisClient.Get(context.Background(), key).Bytes()
CacheLog(mongikClient, fmt.Sprintf("Retrieved data from the cache of the key: %s", key))
return resultBytes
}
return nil
Expand Down
14 changes: 4 additions & 10 deletions db/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,18 @@ func FindOne[Result any](mongikClient *mongik.Mongik, db string, collectionName
if !noCache {
resultBytes := DBCacheFetch(mongikClient, key)
if err := json.Unmarshal(resultBytes, &result); err == nil {
fmt.Println("Retrieving DB call from the cache with cache key ", key)
return
}
}

// Query to DB
fmt.Println("Querying the DB")
mongikClient.MongoClient.Database(db).Collection(collectionName).FindOne(context.Background(), query, opts...).Decode(&resultInterface)

resultBody, _ := json.Marshal(resultInterface)
json.Unmarshal(resultBody, &result)

// Set to cache
if err := DBCacheSet(mongikClient, key, result); err == nil {
fmt.Println("Successfully set DB call in cache with key ", key)
}
DBCacheSet(mongikClient, key, result)
}

func Find[Result any](mongikClient *mongik.Mongik, db string, collectionName string, query bson.M, noCache bool, opts ...*options.FindOptions) ([]Result, error) {
Expand All @@ -45,13 +41,13 @@ func Find[Result any](mongikClient *mongik.Mongik, db string, collectionName str
if !noCache {
resultBytes := DBCacheFetch(mongikClient, key)
if err := json.Unmarshal(resultBytes, &result); err == nil {
fmt.Println("Retrieving DB call from the cache with cache key ", key)
return result, nil
}
}

CacheLog(mongikClient, fmt.Sprintf("Querying the DB\n KEY: %s", key))

// Query to DB
fmt.Println("Querying the DB")
cursor, err := mongikClient.MongoClient.Database(db).Collection(collectionName).Find(context.Background(), query, opts...)
if err != nil {
return nil, err
Expand All @@ -62,9 +58,7 @@ func Find[Result any](mongikClient *mongik.Mongik, db string, collectionName str
json.Unmarshal(resultBody, &result)

// Set to cache
if err := DBCacheSet(mongikClient, key, result); err == nil {
fmt.Println("Successfully set DB call in cache with key ", key)
}
DBCacheSet(mongikClient, key, result)

return result, nil
}
13 changes: 13 additions & 0 deletions db/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package mongik

import (
"fmt"

mongik "github.com/FrosTiK-SD/mongik/models"
)

func CacheLog(mongikClient *mongik.Mongik, log string) {
if mongikClient.Config.Debug == true {
fmt.Println(log)
}
}
2 changes: 0 additions & 2 deletions db/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package mongik

import (
"context"
"fmt"

mongik "github.com/FrosTiK-SD/mongik/models"
"go.mongodb.org/mongo-driver/bson"
Expand Down Expand Up @@ -30,7 +29,6 @@ func FindOneAndUpdate[Result any](mongikClient *mongik.Mongik, db string, collec
var result Result
var resultInterface map[string]interface{}

fmt.Println("Querying the DB")
mongikClient.MongoClient.Database(db).Collection(collectionName).FindOneAndUpdate(context.Background(), query, update, opts...).Decode(&resultInterface)

resultBody, _ := json.Marshal(resultInterface)
Expand Down
11 changes: 7 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ module github.com/FrosTiK-SD/mongik
go 1.21.1

require (
github.com/allegro/bigcache/v3 v3.1.0 // indirect
github.com/allegro/bigcache/v3 v3.1.0
github.com/json-iterator/go v1.1.12
github.com/redis/go-redis/v9 v9.4.0
go.mongodb.org/mongo-driver v1.13.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/redis/go-redis/v9 v9.4.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.13.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/text v0.7.0 // indirect
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand All @@ -20,10 +26,12 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk=
github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
Expand Down Expand Up @@ -67,4 +75,5 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func NewClient(mongoURL string, config *mongik.Config) *mongik.Mongik {
MongoClient: mongoClient,
CacheClient: cacheClient,
Config: &mongik.Config{
Debug: config.Debug,
Client: constants.BIGCACHE,
TTL: config.TTL,
FallbackToDefault: config.FallbackToDefault,
Expand Down
1 change: 1 addition & 0 deletions models/mongik.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
RedisConfig *RedisConfig
TTL time.Duration
FallbackToDefault bool
Debug bool
}

type RedisConfig struct {
Expand Down

0 comments on commit 345adbc

Please sign in to comment.