Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY committed Jun 25, 2024
1 parent e312b24 commit d12e26d
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 70 deletions.
14 changes: 9 additions & 5 deletions ast/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ var bytesPool = sync.Pool{}

func (self *Node) MarshalJSON() ([]byte, error) {
buf := newBuffer()

self.tryLock()
err := self.encode(buf)
self.tryUnlock()

if err != nil {
freeBuffer(buf)
return nil, err
Expand All @@ -120,12 +124,12 @@ func freeBuffer(buf *[]byte) {
}

func (self *Node) encode(buf *[]byte) error {
if self.IsRaw() {
if self.isRaw() {
return self.encodeRaw(buf)
}
switch self.Type() {
switch int(self.itype()) {
case V_NONE : return ErrNotExist
case V_ERROR : return self.Check()
case V_ERROR : return self.check()
case V_NULL : return self.encodeNull(buf)
case V_TRUE : return self.encodeTrue(buf)
case V_FALSE : return self.encodeFalse(buf)
Expand Down Expand Up @@ -196,7 +200,7 @@ func (self *Node) encodeArray(buf *[]byte) error {
var started bool
for i := 0; i < nb; i++ {
n := self.nodeAt(i)
if !n.Exists() {
if !n.exists() {
continue
}
if started {
Expand Down Expand Up @@ -242,7 +246,7 @@ func (self *Node) encodeObject(buf *[]byte) error {
var started bool
for i := 0; i < nb; i++ {
n := self.pairAt(i)
if n == nil || !n.Value.Exists() {
if n == nil || !n.Value.exists() {
continue
}
if started {
Expand Down
33 changes: 27 additions & 6 deletions ast/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,31 @@ func (self *Iterator) Len() int {

// HasNext reports if it is the end of iteration or has error.
func (self *Iterator) HasNext() bool {
if self == nil || self.p == nil {
return false
}
self.p.tryRLock()
if !self.p.isLazy() {
return self.p.Valid() && self.i < self.p.len()
ret := self.p.valid() && self.i < self.p.len()
self.p.tryRUnlock()
return ret
} else if self.p.t == _V_ARRAY_LAZY {
return self.p.skipNextNode().Valid()
self.p.tryRUnlock()
self.p.tryLock()
n := self.p.skipNextNode()
self.p.tryUnlock()
return n.valid()
} else if self.p.t == _V_OBJECT_LAZY {
self.p.tryRUnlock()
self.p.tryLock()
pair := self.p.skipNextPair()
self.p.tryUnlock()
if pair == nil {
return false
}
return pair.Value.Valid()
return pair.Value.valid()
}
self.p.tryRUnlock()
return false
}

Expand All @@ -95,9 +109,11 @@ next_start:
if !self.HasNext() {
return nil
} else {
self.p.tryRLock()
n := self.p.nodeAt(self.i)
self.p.tryRUnlock()
self.i++
if !n.Exists() {
if !n.exists() {
goto next_start
}
return n
Expand All @@ -120,9 +136,11 @@ next_start:
if !self.HasNext() {
return nil
} else {
self.p.tryRLock()
n := self.p.pairAt(self.i)
self.p.tryRUnlock()
self.i++
if n == nil || !n.Value.Exists() {
if n == nil || !n.Value.exists() {
goto next_start
}
return n
Expand Down Expand Up @@ -168,8 +186,10 @@ type Scanner func(path Sequence, node *Node) bool
//
// NOTICE: A unsetted node WON'T trigger sc, but its index still counts into Path.Index
func (self *Node) ForEach(sc Scanner) error {
self.tryLock()
switch self.itype() {
case types.V_ARRAY:
self.tryRUnlock()
iter, err := self.Values()
if err != nil {
return err
Expand All @@ -194,10 +214,11 @@ func (self *Node) ForEach(sc Scanner) error {
v = iter.next()
}
default:
if self.Check() != nil {
if self.check() != nil {
return self
}
sc(Sequence{-1, nil}, self)
}
return nil
}

Loading

0 comments on commit d12e26d

Please sign in to comment.