Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra checks tags index #248

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions process/elasticproc/converters/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
)

const (
// MaxIDSize is the maximum size of a document id
MaxIDSize = 512

attributesSeparator = ";"
keyValuesSeparator = ":"
valuesSeparator = ","
Expand Down
4 changes: 4 additions & 0 deletions process/elasticproc/tags/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ func (tc *tagsCount) Serialize(buffSlice *data.BufferSlice, index string) error
}

base64Tag := base64.StdEncoding.EncodeToString([]byte(tag))
if len(base64Tag) > converters.MaxIDSize {
base64Tag = base64Tag[:converters.MaxIDSize]
bogdan-rosianu marked this conversation as resolved.
Show resolved Hide resolved
}
meta := []byte(fmt.Sprintf(`{ "update" : {"_index":"%s", "_id" : "%s" } }%s`, index, converters.JsonEscape(base64Tag), "\n"))

codeToExecute := `
ctx._source.count += params.count;
ctx._source.tag = params.tag
`

serializedDataStr := fmt.Sprintf(`{"script": {"source": "%s","lang": "painless","params": {"count": %d, "tag": "%s"}},"upsert": {"count": %d, "tag":"%s"}}`,
converters.FormatPainlessSource(codeToExecute), count, converters.JsonEscape(tag), count, converters.JsonEscape(tag),
)
Expand Down
24 changes: 24 additions & 0 deletions process/elasticproc/tags/serialize_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package tags

import (
"crypto/rand"
"encoding/base64"
"fmt"
"testing"

"github.com/multiversx/mx-chain-es-indexer-go/data"
"github.com/multiversx/mx-chain-es-indexer-go/process/elasticproc/converters"
"github.com/stretchr/testify/require"
)

Expand All @@ -24,3 +28,23 @@ func TestTagsCount_Serialize(t *testing.T) {
`
require.Equal(t, expected, buffSlice.Buffers()[0].String())
}

func TestTagsCount_TruncateID(t *testing.T) {
t.Parallel()

tagsC := NewTagsCount()

randomBytes := make([]byte, 600)
_, _ = rand.Read(randomBytes)

tagsC.ParseTags([]string{string(randomBytes)})

buffSlice := data.NewBufferSlice(data.DefaultMaxBulkSize)
err := tagsC.Serialize(buffSlice, "tags")
require.Nil(t, err)

expected := fmt.Sprintf(`{ "update" : {"_index":"tags", "_id" : "%s" } }
{"script": {"source": "ctx._source.count += params.count; ctx._source.tag = params.tag","lang": "painless","params": {"count": 1, "tag": "%s"}},"upsert": {"count": 1, "tag":"%s"}}
`, base64.StdEncoding.EncodeToString(randomBytes)[:converters.MaxIDSize], converters.JsonEscape(string(randomBytes)), converters.JsonEscape(string(randomBytes)))
require.Equal(t, expected, buffSlice.Buffers()[0].String())
}
Loading