Skip to content

Commit

Permalink
JSON parsing logic (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
xichen2020 committed Nov 20, 2018
1 parent 3f5b4de commit 0277f95
Show file tree
Hide file tree
Showing 17 changed files with 67,664 additions and 36 deletions.
50 changes: 50 additions & 0 deletions parser/json/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package json

// The json parser is adapted from
// https://github.com/mailru/easyjson/ and
// https://github.com/valyala/fastjson/.
//
// Easyjson MIT license:
//
// Copyright (c) 2016 Mail.Ru Group
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in the
// Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// Fastjson MIT license:
//
// The MIT License (MIT)
//
// Copyright (c) 2018 Aliaksandr Valialkin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
79 changes: 79 additions & 0 deletions parser/json/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package json

import "github.com/xichen2020/eventdb/value"

const (
defaultMaxDepth = 3
)

// Options provide a set of parsing options.
type Options struct {
maxDepth int
valuePool *value.Pool
valueArrayPool *value.BucketizedArrayPool
kvArrayPool *value.BucketizedKVArrayPool
}

// NewOptions creates a new set of parsing options.
func NewOptions() *Options {
o := &Options{
maxDepth: defaultMaxDepth,
valuePool: value.NewPool(nil),
valueArrayPool: value.NewBucketizedArrayPool(nil, nil),
kvArrayPool: value.NewBucketizedKVArrayPool(nil, nil),
}
o.initPools()
return o
}

// SetMaxDepth sets the maximum depth eligible for parsing.
func (o *Options) SetMaxDepth(v int) *Options {
opts := *o
opts.maxDepth = v
return &opts
}

// MaxDepth returns the maximum depth eligible for parsing.
func (o *Options) MaxDepth() int { return o.maxDepth }

// SetValuePool sets the pool for values.
func (o *Options) SetValuePool(v *value.Pool) *Options {
opts := *o
opts.valuePool = v
return &opts
}

// ValuePool returns the pool for values.
func (o *Options) ValuePool() *value.Pool { return o.valuePool }

// SetValueArrayPool sets the pool for value arrays.
func (o *Options) SetValueArrayPool(v *value.BucketizedArrayPool) *Options {
opts := *o
opts.valueArrayPool = v
return &opts
}

// ValueArrayPool returns the pool for value arrays.
func (o *Options) ValueArrayPool() *value.BucketizedArrayPool { return o.valueArrayPool }

// SetKVArrayPool sets the pool for KV arrays.
func (o *Options) SetKVArrayPool(v *value.BucketizedKVArrayPool) *Options {
opts := *o
opts.kvArrayPool = v
return &opts
}

// KVArrayPool returns the pool for KV arrays.
func (o *Options) KVArrayPool() *value.BucketizedKVArrayPool { return o.kvArrayPool }

func (o *Options) initPools() {
o.valuePool.Init(func() *value.Value { return value.NewEmptyValue(o.valuePool) })
o.valueArrayPool.Init(func(capacity int) value.Array {
values := make([]*value.Value, 0, capacity)
return value.NewArray(values, o.valueArrayPool)
})
o.kvArrayPool.Init(func(capacity int) value.KVArray {
kvs := make([]value.KV, 0, capacity)
return value.NewKVArray(kvs, o.kvArrayPool)
})
}
Loading

0 comments on commit 0277f95

Please sign in to comment.