Skip to content

Commit

Permalink
Support overrideable json Marshaler/Unmarshaler (blevesearch#1880)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinavdangeti authored Sep 11, 2023
1 parent d1cd873 commit 6e512d5
Show file tree
Hide file tree
Showing 35 changed files with 175 additions and 138 deletions.
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) {
}

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

0 comments on commit 6e512d5

Please sign in to comment.