Skip to content

Commit

Permalink
fix: fixing encoding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
himank committed Aug 3, 2022
1 parent d9f53fc commit 49285cd
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 9 deletions.
44 changes: 44 additions & 0 deletions lib/json/encoder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package json

import (
"bytes"

jsoniter "github.com/json-iterator/go"
)

func Encode(data map[string]any) ([]byte, error) {
var buffer bytes.Buffer
encoder := jsoniter.NewEncoder(&buffer)
err := encoder.Encode(data)
if err != nil {
return nil, err
}

return buffer.Bytes(), nil
}

func Decode(data []byte) (map[string]any, error) {
var decoded map[string]any

decoder := jsoniter.NewDecoder(bytes.NewReader(data))
decoder.UseNumber()
if err := decoder.Decode(&decoded); err != nil {
return nil, err
}

return decoded, nil
}
14 changes: 14 additions & 0 deletions lib/uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2022 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package uuid

import uuid2 "github.com/google/uuid"
Expand Down
15 changes: 10 additions & 5 deletions server/services/v1/search_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/apple/foundationdb/bindings/go/src/fdb/subspace"
jsoniter "github.com/json-iterator/go"
"github.com/tigrisdata/tigris/internal"
"github.com/tigrisdata/tigris/lib/json"
"github.com/tigrisdata/tigris/schema"
"github.com/tigrisdata/tigris/server/metadata"
"github.com/tigrisdata/tigris/server/transaction"
Expand Down Expand Up @@ -155,7 +156,7 @@ func CreateSearchKey(table []byte, fdbKey []byte) (string, error) {
case string:
value = t
case []byte:
value = string(t)
value = base64.StdEncoding.EncodeToString(t)
}
return value, nil
} else {
Expand All @@ -165,10 +166,9 @@ func CreateSearchKey(table []byte, fdbKey []byte) (string, error) {
}

func PackSearchFields(data *internal.TableData, collection *schema.DefaultCollection, id string) ([]byte, error) {
var err error
// better to decode it and then update the JSON
var decData map[string]any
if err = jsoniter.Unmarshal(data.RawData, &decData); err != nil {
decData, err := json.Decode(data.RawData)
if err != nil {
return nil, err
}

Expand All @@ -195,7 +195,12 @@ func PackSearchFields(data *internal.TableData, collection *schema.DefaultCollec
decData[schema.ReservedFields[schema.UpdatedAt]] = data.UpdatedAt.UnixNano()
}

return jsoniter.Marshal(decData)
encoded, err := json.Encode(decData)
if err != nil {
return nil, err
}

return encoded, nil
}

func UnpackSearchFields(doc map[string]interface{}, collection *schema.DefaultCollection) (string, *internal.TableData, map[string]interface{}, error) {
Expand Down
6 changes: 2 additions & 4 deletions server/services/v1/search_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package v1
import (
"context"

jsoniter "github.com/json-iterator/go"
api "github.com/tigrisdata/tigris/api/server/v1"
"github.com/tigrisdata/tigris/lib/json"
"github.com/tigrisdata/tigris/query/filter"
"github.com/tigrisdata/tigris/query/read"
qsearch "github.com/tigrisdata/tigris/query/search"
Expand Down Expand Up @@ -94,10 +94,8 @@ func (p *page) readRow(row *Row) bool {
}

var rawData []byte

// marshal the doc as bytes
rawData, p.err = jsoniter.Marshal(doc)
if p.err != nil {
if rawData, p.err = json.Encode(doc); p.err != nil {
return false
}

Expand Down

0 comments on commit 49285cd

Please sign in to comment.