Skip to content

Commit

Permalink
Added -O flag, Optimistic Set
Browse files Browse the repository at this point in the history
This option can be used when the caller expects that a value at the
specified keypath already exists.

It can speed up an operation by as much as 6x, but slow down as much as
20% when the value does not exist.
  • Loading branch information
tidwall committed Oct 25, 2016
1 parent a7e85d0 commit 94ee627
Show file tree
Hide file tree
Showing 5 changed files with 446 additions and 74 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Usage menu:
```
$ jsoned -h
usage: jsoned [-v value] [-s] [-D] [-i infile] [-o outfile] keypath
usage: jsoned [-v value] [-r] [-D] [-O] [-i infile] [-o outfile] keypath
examples: jsoned keypath read value from stdin
or: jsoned -i infile keypath read value from infile
Expand All @@ -40,11 +40,13 @@ examples: jsoned keypath read value from stdin
options:
-v value Edit JSON key path value
-r Use raw values, otherwise types are auto-detected
-O Performance boost for value updates.
-D Delete the value at the specified key path
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
-r Use raw values, otherwise types are auto-detected
keypath JSON key path (like "name.last")
```


Expand Down Expand Up @@ -161,6 +163,14 @@ $ echo '{"friends":["Andy","Carol"]}' | ./jsoned -D friends.-1
{"friends":["Andy"]}
```

### Optimisticlly update a value

The `-O` option can be used when the caller expects that a value at the
specified keypath already exists.

Using this option can speed up an operation by as much as 6x, but
slow down as much as 20% when the value does not exist.

## Contact
Josh Baker [@tidwall](http://twitter.com/tidwall)

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

VERSION="0.1.1"
VERSION="0.2.1"
PROTECTED_MODE="no"

export GO15VENDOREXPERIMENT=1
Expand Down
18 changes: 14 additions & 4 deletions cmd/jsoned/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
version = "0.0.1"
tag = "jsoned - JSON Stream Editor " + version
usage = `
usage: jsoned [-v value] [-s] [-D] [-i infile] [-o outfile] keypath
usage: jsoned [-v value] [-r] [-D] [-O] [-i infile] [-o outfile] keypath
examples: jsoned keypath read value from stdin
or: jsoned -i infile keypath read value from infile
Expand All @@ -24,10 +24,11 @@ examples: jsoned keypath read value from stdin
options:
-v value Edit JSON key path value
-r Use raw values, otherwise types are auto-detected
-O Performance boost for value updates.
-D Delete the value at the specified key path
-i infile Use input file instead of stdin
-o outfile Use output file instead of stdout
-r Use raw values, otherwise types are auto-detected
keypath JSON key path (like "name.last")
for more info: https://github.com/tidwall/jsoned
Expand All @@ -40,6 +41,7 @@ type args struct {
value *string
raw bool
del bool
opt bool
keypath string
}

Expand Down Expand Up @@ -86,6 +88,8 @@ func parseArgs() args {
a.raw = true
case "-D":
a.del = true
case "-O":
a.opt = true
case "-h", "--help", "-?":
help()
}
Expand Down Expand Up @@ -133,12 +137,18 @@ func main() {
raw = true
}
}
opts := &sjson.Options{}
if a.opt {
opts.Optimistic = true
opts.ReplaceInPlace = true
}
if raw {
// set as raw block
outb, err = sjson.SetRawBytes(input, a.keypath, []byte(val))
outb, err = sjson.SetRawBytesOptions(
input, a.keypath, []byte(val), opts)
} else {
// set as a string
outb, err = sjson.SetBytes(input, a.keypath, val)
outb, err = sjson.SetBytesOptions(input, a.keypath, val, opts)
}
if err != nil {
goto fail
Expand Down
Loading

0 comments on commit 94ee627

Please sign in to comment.