Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
minhduc140583 committed Jun 30, 2024
1 parent 12c731f commit e34fb46
Show file tree
Hide file tree
Showing 21 changed files with 194 additions and 638 deletions.
80 changes: 79 additions & 1 deletion batch.go → batch/batch.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package elasticsearch
package batch

import (
"context"
Expand Down Expand Up @@ -74,6 +74,69 @@ func (w *BatchInserter) Write(ctx context.Context, model interface{}) ([]int, []
}
return successIndices, failureIndices, errors.New("invalid input")
}
func FindIdField(modelType reflect.Type) (int, string, string) {
return FindBsonField(modelType, "_id")
}
func FindBsonField(modelType reflect.Type, bsonName string) (int, string, string) {
numField := modelType.NumField()
for i := 0; i < numField; i++ {
field := modelType.Field(i)
bsonTag := field.Tag.Get("bson")
tags := strings.Split(bsonTag, ",")
json := field.Name
if tag1, ok1 := field.Tag.Lookup("json"); ok1 {
json = strings.Split(tag1, ",")[0]
}
for _, tag := range tags {
if strings.TrimSpace(tag) == bsonName {
return i, field.Name, json
}
}
}
return -1, "", ""
}

func BuildQueryWithoutIdFromObject(object interface{}) map[string]interface{} {
valueOf := reflect.Indirect(reflect.ValueOf(object))
idIndex, _, _ := FindIdField(valueOf.Type())
result := map[string]interface{}{}
for i := 0; i < valueOf.NumField(); i++ {
if i != idIndex {
_, jsonName := FindFieldByIndex(valueOf.Type(), i)
result[jsonName] = valueOf.Field(i).Interface()
}
}
return result
}
func FindFieldByIndex(modelType reflect.Type, fieldIndex int) (fieldName, jsonTagName string) {
if fieldIndex < modelType.NumField() {
field := modelType.Field(fieldIndex)
jsonTagName := ""
if jsonTag, ok := field.Tag.Lookup("json"); ok {
jsonTagName = strings.Split(jsonTag, ",")[0]
}
return field.Name, jsonTagName
}
return "", ""
}

func BuildIndicesResult(listIds, successIds, failIds []interface{}) (successIndices, failureIndices []int) {
if len(listIds) > 0 {
for _, idValue := range listIds {
for index, id := range successIds {
if id == idValue {
successIndices = append(successIndices, int(index))
}
}
for index, id := range failIds {
if id == idValue {
failureIndices = append(failureIndices, int(index))
}
}
}
}
return
}

func InsertMany(ctx context.Context, es *elasticsearch.Client, indexName string, modelType reflect.Type, model interface{}) ([]int, []int, error) {
value := reflect.Indirect(reflect.ValueOf(model))
Expand Down Expand Up @@ -184,3 +247,18 @@ func UpsertMany(ctx context.Context, es *elasticsearch.Client, indexName string,
}
return successIndices, failureIndices, errors.New("invalid input")
}
func FindListIdField(modelType reflect.Type, model interface{}) (listIdS []interface{}) {
value := reflect.Indirect(reflect.ValueOf(model))

if value.Kind() == reflect.Slice {
for i := 0; i < value.Len(); i++ {
sliceValue := value.Index(i).Interface()
if idIndex, _, _ := FindIdField(modelType); idIndex >= 0 {
modelValue := reflect.Indirect(reflect.ValueOf(sliceValue))
idValue := modelValue.Field(idIndex).String()
listIdS = append(listIdS, idValue)
}
}
}
return
}
11 changes: 11 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package client

import (
"github.com/elastic/go-elasticsearch/v8"
"time"
)

func NewClient(config Config, timeouts ...time.Duration) (*elasticsearch.Client, error) {
c := GetConfig(config, timeouts...)
return elasticsearch.NewClient(c)
}
22 changes: 11 additions & 11 deletions config.go → client/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package elasticsearch
package client

import (
"crypto/tls"
Expand All @@ -14,12 +14,12 @@ type TransportConfig struct {
Timeout *int64 `yaml:"timeout" mapstructure:"timeout" json:"timeout,omitempty" gorm:"column:timeout" bson:"timeout,omitempty" dynamodbav:"timeout,omitempty" firestore:"timeout,omitempty"`
}
type Config struct {
Addresses []string `yaml:"addresses" mapstructure:"addresses" json:"addresses,omitempty" gorm:"column:addresses" bson:"addresses,omitempty" dynamodbav:"addresses,omitempty" firestore:"addresses,omitempty"`
Username *string `yaml:"username" mapstructure:"username" json:"username,omitempty" gorm:"column:username" bson:"username,omitempty" dynamodbav:"username,omitempty" firestore:"username,omitempty"`
Password *string `yaml:"password" mapstructure:"password" json:"password,omitempty" gorm:"column:password" bson:"password,omitempty" dynamodbav:"password,omitempty" firestore:"password,omitempty"`
CloudID *string `yaml:"cloud_id" mapstructure:"cloud_id" json:"cloudID,omitempty" gorm:"column:cloudid" bson:"cloudID,omitempty" dynamodbav:"cloudID,omitempty" firestore:"cloudID,omitempty"`
APIKey *string `yaml:"api_key" mapstructure:"api_key" json:"apiKey,omitempty" gorm:"column:apikey" bson:"apiKey,omitempty" dynamodbav:"apiKey,omitempty" firestore:"apiKey,omitempty"`
DisableRetry *bool `yaml:"disable_retry" mapstructure:"disable_retry" json:"disableRetry,omitempty" gorm:"column:disableretry" bson:"disableRetry,omitempty" dynamodbav:"disableRetry,omitempty" firestore:"disableRetry,omitempty"`
Addresses []string `yaml:"addresses" mapstructure:"addresses" json:"addresses,omitempty" gorm:"column:addresses" bson:"addresses,omitempty" dynamodbav:"addresses,omitempty" firestore:"addresses,omitempty"`
Username *string `yaml:"username" mapstructure:"username" json:"username,omitempty" gorm:"column:username" bson:"username,omitempty" dynamodbav:"username,omitempty" firestore:"username,omitempty"`
Password *string `yaml:"password" mapstructure:"password" json:"password,omitempty" gorm:"column:password" bson:"password,omitempty" dynamodbav:"password,omitempty" firestore:"password,omitempty"`
CloudID *string `yaml:"cloud_id" mapstructure:"cloud_id" json:"cloudID,omitempty" gorm:"column:cloudid" bson:"cloudID,omitempty" dynamodbav:"cloudID,omitempty" firestore:"cloudID,omitempty"`
APIKey *string `yaml:"api_key" mapstructure:"api_key" json:"apiKey,omitempty" gorm:"column:apikey" bson:"apiKey,omitempty" dynamodbav:"apiKey,omitempty" firestore:"apiKey,omitempty"`
DisableRetry *bool `yaml:"disable_retry" mapstructure:"disable_retry" json:"disableRetry,omitempty" gorm:"column:disableretry" bson:"disableRetry,omitempty" dynamodbav:"disableRetry,omitempty" firestore:"disableRetry,omitempty"`
// EnableRetryOnTimeout *bool `yaml:"enable_retry_on_timeout" mapstructure:"enable_retry_on_timeout" json:"enableRetryOnTimeout,omitempty" gorm:"column:enableretryontimeout" bson:"enableRetryOnTimeout,omitempty" dynamodbav:"enableRetryOnTimeout,omitempty" firestore:"enableRetryOnTimeout,omitempty"`
MaxRetries *int `yaml:"max_retries" mapstructure:"max_retries" json:"maxRetries,omitempty" gorm:"column:maxretries" bson:"maxRetries,omitempty" dynamodbav:"maxRetries,omitempty" firestore:"maxRetries,omitempty"`
DiscoverNodesOnStart *bool `yaml:"discover_nodes_on_start" mapstructure:"discover_nodes_on_start" json:"discoverNodesOnStart,omitempty" gorm:"column:discovernodesonstart" bson:"discoverNodesOnStart,omitempty" dynamodbav:"discoverNodesOnStart,omitempty" firestore:"discoverNodesOnStart,omitempty"`
Expand Down Expand Up @@ -69,10 +69,10 @@ func GetConfig(conf Config, timeouts ...time.Duration) elasticsearch.Config {
c.DisableRetry = *conf.DisableRetry
}
/*
if conf.EnableRetryOnTimeout != nil {
c.EnableRetryOnTimeout = *conf.EnableRetryOnTimeout
}
*/
if conf.EnableRetryOnTimeout != nil {
c.EnableRetryOnTimeout = *conf.EnableRetryOnTimeout
}
*/
if conf.MaxRetries != nil {
c.MaxRetries = *conf.MaxRetries
}
Expand Down
Loading

0 comments on commit e34fb46

Please sign in to comment.