-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Custom merging strategy | ||
|
||
In this example we are creating a column called `location` that contains a JSON-encoded position and velocity. The position is updated by calling a `MergeString()` function with the velocity vector, the updates are then merged atomically using a merging function specified. | ||
|
||
The merge happens when transaction is committed to ensure consistency. Hence, this technique allows for two concurrent transactions to update the same position. | ||
|
||
## Example output | ||
|
||
```json | ||
00: {"position":[1,2],"velocity":[1,2]} | ||
01: {"position":[2,4],"velocity":[1,2]} | ||
02: {"position":[3,6],"velocity":[1,2]} | ||
03: {"position":[4,8],"velocity":[1,2]} | ||
04: {"position":[5,10],"velocity":[1,2]} | ||
05: {"position":[6,12],"velocity":[1,2]} | ||
06: {"position":[7,14],"velocity":[1,2]} | ||
07: {"position":[8,16],"velocity":[1,2]} | ||
08: {"position":[9,18],"velocity":[1,2]} | ||
09: {"position":[10,20],"velocity":[1,2]} | ||
10: {"position":[11,22],"velocity":[1,2]} | ||
11: {"position":[12,24],"velocity":[1,2]} | ||
12: {"position":[13,26],"velocity":[1,2]} | ||
13: {"position":[14,28],"velocity":[1,2]} | ||
14: {"position":[15,30],"velocity":[1,2]} | ||
15: {"position":[16,32],"velocity":[1,2]} | ||
16: {"position":[17,34],"velocity":[1,2]} | ||
17: {"position":[18,36],"velocity":[1,2]} | ||
18: {"position":[19,38],"velocity":[1,2]} | ||
19: {"position":[20,40],"velocity":[1,2]} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright (c) Roman Atachiants and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for details. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/kelindar/column" | ||
) | ||
|
||
// Movement represents a movement with a position and velocity | ||
type Movement struct { | ||
Position [2]float64 `json:"position,omitempty"` | ||
Velocity [2]float64 `json:"velocity,omitempty"` | ||
} | ||
|
||
func main() { | ||
|
||
// A merging function that accepts a velocity vector and updates | ||
// the movement structure accordingly. | ||
mergeVectors := func(value, delta string) string { | ||
movement, ok := parseMovement(value) | ||
if !ok { | ||
movement = Movement{ | ||
Position: [2]float64{0, 0}, | ||
} | ||
} | ||
|
||
// Parse the incoming delta value | ||
velocity, ok := parseVector(delta) | ||
if !ok { | ||
return value | ||
} | ||
|
||
// Update the current velocity and recalculate the position | ||
movement.Velocity = velocity | ||
movement.Position[0] += velocity[0] // Update X | ||
movement.Position[1] += velocity[1] // Update Y | ||
|
||
// Encode the movement as JSON and return the updated value | ||
return encode(movement) | ||
} | ||
|
||
// Create a column with a specified merge function | ||
db := column.NewCollection() | ||
db.CreateColumn("location", column.ForString( | ||
column.WithMerge(mergeVectors), // use our merging function | ||
)) | ||
|
||
// Insert an empty row | ||
id, _ := db.Insert(func(r column.Row) error { | ||
r.SetString("location", "{}") | ||
return nil | ||
}) | ||
|
||
// Update 100 times | ||
for i := 0; i < 20; i++ { | ||
|
||
// Move the location by applying a same velocity vector | ||
db.Query(func(txn *column.Txn) error { | ||
location := txn.String("location") | ||
return txn.QueryAt(id, func(r column.Row) error { | ||
location.Merge(encode([2]float64{1, 2})) | ||
return nil | ||
}) | ||
}) | ||
|
||
// Print out current location | ||
db.Query(func(txn *column.Txn) error { | ||
location := txn.String("location") | ||
return txn.QueryAt(id, func(r column.Row) error { | ||
value, _ := location.Get() | ||
fmt.Printf("%.2d: %v \n", i, value) | ||
return nil | ||
}) | ||
}) | ||
} | ||
|
||
} | ||
|
||
// parseMovement parses a value string into a Movement struct | ||
func parseMovement(value string) (out Movement, ok bool) { | ||
if err := json.Unmarshal([]byte(value), &out); err != nil { | ||
return Movement{}, false | ||
} | ||
return out, true | ||
} | ||
|
||
// parseVector parses a value string into 2 dimensional array | ||
func parseVector(value string) (out [2]float64, ok bool) { | ||
if err := json.Unmarshal([]byte(value), &out); err != nil { | ||
return [2]float64{}, false | ||
} | ||
return out, true | ||
} | ||
|
||
// encodes encodes the value as JSON | ||
func encode(value any) string { | ||
encoded, _ := json.Marshal(value) | ||
return string(encoded) | ||
} |