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

Support overrideable json Marshaler/Unmarshaler #1880

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package bleve

import (
"encoding/json"
"fmt"

"github.com/blevesearch/bleve/v2/document"
"github.com/blevesearch/bleve/v2/index/scorch"
"github.com/blevesearch/bleve/v2/mapping"
"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"
)

Expand Down Expand Up @@ -63,7 +63,7 @@ func newBuilder(path string, mapping mapping.IndexMapping, config map[string]int

// the builder does not have an API to interact with internal storage
// however we can pass k/v pairs through the config
mappingBytes, err := json.Marshal(mapping)
mappingBytes, err := util.MarshalJSON(mapping)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions geo/geo_s2plugin_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"encoding/json"
"sync"

"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"

"github.com/blevesearch/geo/geojson"
"github.com/blevesearch/geo/s2"
)
Expand Down Expand Up @@ -224,7 +224,7 @@ func (p *Point) Type() string {
}

func (p *Point) Value() ([]byte, error) {
return json.Marshal(p)
return util.MarshalJSON(p)
}

func (p *Point) Intersects(s index.GeoJSON) (bool, error) {
Expand Down Expand Up @@ -267,7 +267,7 @@ func (br *boundedRectangle) Type() string {
}

func (br *boundedRectangle) Value() ([]byte, error) {
return json.Marshal(br)
return util.MarshalJSON(br)
}

func (p *boundedRectangle) Intersects(s index.GeoJSON) (bool, error) {
Expand Down Expand Up @@ -309,7 +309,7 @@ func (bp *boundedPolygon) Type() string {
}

func (bp *boundedPolygon) Value() ([]byte, error) {
return json.Marshal(bp)
return util.MarshalJSON(bp)
}

func (p *boundedPolygon) Intersects(s index.GeoJSON) (bool, error) {
Expand Down Expand Up @@ -355,7 +355,7 @@ func (p *pointDistance) Type() string {
}

func (p *pointDistance) Value() ([]byte, error) {
return json.Marshal(p)
return util.MarshalJSON(p)
}

func NewPointDistance(centerLat, centerLon,
Expand Down
6 changes: 3 additions & 3 deletions index/scorch/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package scorch

import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
Expand All @@ -25,6 +24,7 @@ import (

"github.com/RoaringBitmap/roaring"
"github.com/blevesearch/bleve/v2/index/scorch/mergeplan"
"github.com/blevesearch/bleve/v2/util"
segment "github.com/blevesearch/scorch_segment_api/v2"
)

Expand Down Expand Up @@ -198,12 +198,12 @@ func (s *Scorch) parseMergePlannerOptions() (*mergeplan.MergePlanOptions,
error) {
mergePlannerOptions := mergeplan.DefaultMergePlanOptions
if v, ok := s.config["scorchMergePlanOptions"]; ok {
b, err := json.Marshal(v)
b, err := util.MarshalJSON(v)
if err != nil {
return &mergePlannerOptions, err
}

err = json.Unmarshal(b, &mergePlannerOptions)
err = util.UnmarshalJSON(b, &mergePlannerOptions)
if err != nil {
return &mergePlannerOptions, err
}
Expand Down
6 changes: 3 additions & 3 deletions index/scorch/persister.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package scorch
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"log"
Expand All @@ -31,6 +30,7 @@ import (
"time"

"github.com/RoaringBitmap/roaring"
"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"
segment "github.com/blevesearch/scorch_segment_api/v2"
bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -324,12 +324,12 @@ func (s *Scorch) parsePersisterOptions() (*persisterOptions, error) {
MemoryPressurePauseThreshold: DefaultMemoryPressurePauseThreshold,
}
if v, ok := s.config["scorchPersisterOptions"]; ok {
b, err := json.Marshal(v)
b, err := util.MarshalJSON(v)
if err != nil {
return &po, err
}

err = json.Unmarshal(b, &po)
err = util.UnmarshalJSON(b, &po)
if err != nil {
return &po, err
}
Expand Down
5 changes: 3 additions & 2 deletions index/scorch/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
package scorch

import (
"encoding/json"
"reflect"
"sync/atomic"

"github.com/blevesearch/bleve/v2/util"
)

// Stats tracks statistics about the index, fields that are
Expand Down Expand Up @@ -151,5 +152,5 @@ func (s *Stats) ToMap() map[string]interface{} {
// MarshalJSON implements json.Marshaler, and in contrast to standard
// json marshaling provides atomic safety
func (s *Stats) MarshalJSON() ([]byte, error) {
return json.Marshal(s.ToMap())
return util.MarshalJSON(s.ToMap())
}
4 changes: 2 additions & 2 deletions index/upsidedown/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package upsidedown

import (
"encoding/json"
"sync/atomic"

"github.com/blevesearch/bleve/v2/util"
"github.com/blevesearch/upsidedown_store_api"
)

Expand Down Expand Up @@ -51,5 +51,5 @@ func (i *indexStat) statsMap() map[string]interface{} {

func (i *indexStat) MarshalJSON() ([]byte, error) {
m := i.statsMap()
return json.Marshal(m)
return util.MarshalJSON(m)
}
6 changes: 4 additions & 2 deletions index/upsidedown/store/boltdb/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

package boltdb

import "encoding/json"
import (
"github.com/blevesearch/bleve/v2/util"
)

type stats struct {
s *Store
}

func (s *stats) MarshalJSON() ([]byte, error) {
bs := s.s.db.Stats()
return json.Marshal(bs)
return util.MarshalJSON(bs)
}
5 changes: 2 additions & 3 deletions index/upsidedown/store/metrics/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
package metrics

import (
"encoding/json"

"github.com/blevesearch/bleve/v2/util"
store "github.com/blevesearch/upsidedown_store_api"
)

Expand Down Expand Up @@ -47,5 +46,5 @@ func (s *stats) statsMap() map[string]interface{} {

func (s *stats) MarshalJSON() ([]byte, error) {
m := s.statsMap()
return json.Marshal(m)
return util.MarshalJSON(m)
}
5 changes: 3 additions & 2 deletions index/upsidedown/store/metrics/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/blevesearch/bleve/v2/registry"
"github.com/blevesearch/bleve/v2/util"
"github.com/blevesearch/go-metrics"
store "github.com/blevesearch/upsidedown_store_api"
)
Expand Down Expand Up @@ -206,7 +207,7 @@ func (s *Store) WriteJSON(w io.Writer) (err error) {
}
}
var buf []byte
buf, err = json.Marshal(se)
buf, err = util.MarshalJSON(se)
if err == nil {
_, err = w.Write(buf)
if err != nil {
Expand All @@ -226,7 +227,7 @@ func (s *Store) WriteJSON(w io.Writer) (err error) {
if o, ok := s.o.(store.KVStoreStats); ok {
storeStats := o.Stats()
var storeBytes []byte
storeBytes, err = json.Marshal(storeStats)
storeBytes, err = util.MarshalJSON(storeStats)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions index/upsidedown/store/moss/lower.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
package moss

import (
"encoding/json"
"fmt"
"os"
"sync"

"github.com/couchbase/moss"

"github.com/blevesearch/bleve/v2/registry"
"github.com/blevesearch/bleve/v2/util"
store "github.com/blevesearch/upsidedown_store_api"
)

Expand Down Expand Up @@ -463,12 +463,12 @@ func InitMossStore(config map[string]interface{}, options moss.CollectionOptions
}
v, ok := config["mossStoreOptions"]
if ok {
b, err := json.Marshal(v) // Convert from map[string]interface{}.
b, err := util.MarshalJSON(v) // Convert from map[string]interface{}.
if err != nil {
return nil, nil, nil, nil, err
}

err = json.Unmarshal(b, &storeOptions)
err = util.UnmarshalJSON(b, &storeOptions)
if err != nil {
return nil, nil, nil, nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions index/upsidedown/store/moss/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
package moss

import (
"encoding/json"

"github.com/blevesearch/bleve/v2/util"
store "github.com/blevesearch/upsidedown_store_api"
)

Expand Down Expand Up @@ -55,5 +54,5 @@ func (s *stats) statsMap() map[string]interface{} {

func (s *stats) MarshalJSON() ([]byte, error) {
m := s.statsMap()
return json.Marshal(m)
return util.MarshalJSON(m)
}
5 changes: 3 additions & 2 deletions index/upsidedown/store/moss/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/couchbase/moss"

"github.com/blevesearch/bleve/v2/registry"
"github.com/blevesearch/bleve/v2/util"
store "github.com/blevesearch/upsidedown_store_api"
)

Expand Down Expand Up @@ -77,13 +78,13 @@ func New(mo store.MergeOperator, config map[string]interface{}) (

v, ok = config["mossCollectionOptions"]
if ok {
b, err := json.Marshal(v) // Convert from map[string]interface{}.
b, err := util.MarshalJSON(v) // Convert from map[string]interface{}.
if err != nil {
return nil, fmt.Errorf("moss store,"+
" could not marshal config[mossCollectionOptions]: %v, err: %v", v, err)
}

err = json.Unmarshal(b, &options)
err = util.UnmarshalJSON(b, &options)
if err != nil {
return nil, fmt.Errorf("moss store,"+
" could not unmarshal config[mossCollectionOptions]: %v, err: %v", v, err)
Expand Down
16 changes: 8 additions & 8 deletions index/upsidedown/upsidedown.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,22 @@ func (udc *UpsideDownCouch) loadSchema(kvreader store.KVReader) (err error) {
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file have unrelated changes coming from go fmt,
we have another PR raised for "go fmt" changes (#1874) based off of master.
We can rebase this PR on top of that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a go fmt and given that it'll be backported as is - let's leave this as is for now and rebase #1874 instead.

type rowBuffer struct {
buf []byte
buf []byte
}

var rowBufferPool sync.Pool

func GetRowBuffer() *rowBuffer {
if rb, ok := rowBufferPool.Get().(*rowBuffer); ok {
return rb
} else {
buf := make([]byte, RowBufferSize)
return &rowBuffer{buf: buf}
}
if rb, ok := rowBufferPool.Get().(*rowBuffer); ok {
return rb
} else {
buf := make([]byte, RowBufferSize)
return &rowBuffer{buf: buf}
}
}

func PutRowBuffer(rb *rowBuffer) {
rowBufferPool.Put(rb)
rowBufferPool.Put(rb)
}

func (udc *UpsideDownCouch) batchRows(writer store.KVWriter, addRowsAll [][]UpsideDownCouchRow, updateRowsAll [][]UpsideDownCouchRow, deleteRowsAll [][]UpsideDownCouchRow) (err error) {
Expand Down
6 changes: 3 additions & 3 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package bleve

import (
"context"
"encoding/json"
"fmt"
"io"
"os"
Expand All @@ -35,6 +34,7 @@ import (
"github.com/blevesearch/bleve/v2/search/collector"
"github.com/blevesearch/bleve/v2/search/facet"
"github.com/blevesearch/bleve/v2/search/highlight"
"github.com/blevesearch/bleve/v2/util"
index "github.com/blevesearch/bleve_index_api"
"github.com/blevesearch/geo/s2"
)
Expand Down Expand Up @@ -120,7 +120,7 @@ func newIndexUsing(path string, mapping mapping.IndexMapping, indexType string,
}(&rv)

// now persist the mapping
mappingBytes, err := json.Marshal(mapping)
mappingBytes, err := util.MarshalJSON(mapping)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -203,7 +203,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
}

var im *mapping.IndexMappingImpl
err = json.Unmarshal(mappingBytes, &im)
err = util.UnmarshalJSON(mappingBytes, &im)
if err != nil {
return nil, fmt.Errorf("error parsing mapping JSON: %v\nmapping contents:\n%s", err, string(mappingBytes))
}
Expand Down
Loading