Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #14 from glycerine/fence_len
Browse files Browse the repository at this point in the history
length option to FENCE, to support batch updates and dedup
  • Loading branch information
tidwall authored Jan 19, 2017
2 parents 56ec060 + 8a084ed commit db2c4e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
18 changes: 15 additions & 3 deletions machine/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,22 @@ func (m *Machine) doDbsize(a finn.Applier, conn redcon.Conn, cmd redcon.Command,
}

func (m *Machine) doFence(a finn.Applier, conn redcon.Conn, cmd redcon.Command, tx *buntdb.Tx) (interface{}, error) {
// FENCE token
if len(cmd.Args) != 2 {
// FENCE token {optional length}
narg := len(cmd.Args)
if narg != 2 && narg != 3 {
return nil, finn.ErrWrongNumberOfArguments
}
var incr uint64 = 1
if narg == 3 {
var err error
incr, err = strconv.ParseUint(string(cmd.Args[2]), 10, 64)
if err != nil {
return nil, err
}
if incr < 1 {
return nil, fmt.Errorf("length argument to FENCE must be > 0")
}
}
key := sdbMetaPrefix + "fence:" + string(cmd.Args[1])
return m.writeDoApply(a, conn, cmd, tx, func(tx *buntdb.Tx) (interface{}, error) {
var n uint64
Expand All @@ -70,7 +82,7 @@ func (m *Machine) doFence(a finn.Applier, conn redcon.Conn, cmd redcon.Command,
return nil, err
}
}
n++
n += incr
val = strconv.FormatUint(n, 10)
_, _, err = tx.Set(key, val, nil)
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions machine/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func transactions_GET_test(mc *mockCluster) error {
}

func transactions_FENCE_test(mc *mockCluster) error {
return mc.DoBatch([][]interface{}{
err := mc.DoBatch([][]interface{}{
{"FENCEGET", "mytoken1"}, {"0"},
{"FENCEGET", "mytoken2"}, {"0"},
{"FENCE", "mytoken1"}, {"1"},
Expand All @@ -180,7 +180,10 @@ func transactions_FENCE_test(mc *mockCluster) error {
{"FENCE", "mytoken2"}, {"5"},
{"FENCEGET", "mytoken2"}, {"5"},
})
return mc.DoBatch([][]interface{}{
if err != nil {
return err
}
err = mc.DoBatch([][]interface{}{
{"FENCEGET", "mytoken1"}, {"5"},
{"FENCEGET", "mytoken2"}, {"5"},
{"FENCE", "mytoken1"}, {"6"},
Expand All @@ -196,4 +199,17 @@ func transactions_FENCE_test(mc *mockCluster) error {
{"FENCE", "mytoken2"}, {"9"},
{"FENCE", "mytoken2"}, {"10"},
})
if err != nil {
return err
}
// check: FENCE token length
err = mc.DoBatch([][]interface{}{
{"FENCEGET", "mytoken1"}, {"10"},
{"FENCEGET", "mytoken2"}, {"10"},
{"FENCE", "mytoken1", "20"}, {"30"},
{"FENCE", "mytoken2", "5"}, {"15"},
{"FENCEGET", "mytoken1"}, {"30"},
{"FENCEGET", "mytoken2"}, {"15"},
})
return err
}

0 comments on commit db2c4e5

Please sign in to comment.