Examples and Updates

This commit is contained in:
Sarah Jamie Lewis 2020-03-01 18:15:43 -08:00
parent 30cc850845
commit 02f1ee0508
7 changed files with 97 additions and 1 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.chaos/
.idea/

1
4count.cst Normal file
View File

@ -0,0 +1 @@
{"Structures":{"1":{"Elements":{}},"2":{"Elements":{}},"3":{"Elements":{}},"4":{"Elements":{}}},"Commands":{"add1":["1"],"add2":["2"],"add3":["3"],"add4":["4"]},"LookupIn": ["1","2","3","4"],"LookupNotIn":[]}

56
README.md Normal file
View File

@ -0,0 +1,56 @@
# Chaos - Distributed Structures
Chaos is a framework for building distributed conflict-free data structures by composing G-Sets (Grow Only Sets)
From these simple sets, more complicated objects can be made e.g. we can build a 2-Phase Set that allows us to remove elements with a simple template (`2pset.cst`)
{
"Structures":{
"add":{"Elements":{}},
"remove":{"Elements":{}}
},
"Commands":{
"add":["add"],"remove":["remove"]
},
"LookupIn":["add"],
"LookupNotIn":["remove"]
}
We can also build distributed counters, e.g. a counter to synchronize 4 nodes might look like this ([`4count.cst`](4count.cst):
{
"Structures":{
"1":{"Elements":{}},
"2":{"Elements":{}},
"3":{"Elements":{}},
"4":{"Elements":{}}
},
"Commands":{
"add1":["1"],
"add2":["2"],
"add3":["3"],
"add4":["4"]
},
"LookupIn": ["1","2","3","4"],
"LookupNotIn":[]
}
We can then use `chaos`:
chaos new 4count counter
# Receive from 1
chaos add1 counter 1
# Receive from 2
chaos add2 counter 1
# Receive from 3
chaos add3 counter 1
# Receive from 4
chaos add4 counter 1
# Get Max Counter Value
seq 4 | xargs -I{} chaos len counter | sort -nr | head -1
1
# Receive from 4
chaos add4 counter "in reality this would be update event 2"
# Get Max Counter Value
seq 4 | xargs -I{} chaos len counter | sort -nr | head -1
2

View File

@ -59,7 +59,28 @@ func cmd_lookup(args []string) {
fmt.Printf("Cannot parse \"%v\"", args[0])
return
}
fmt.Printf("%v", template.Lookup(args[1]))
fmt.Printf("%v\n", template.Lookup(args[1]))
} else {
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0])
return
}
} else {
fmt.Printf("Usage: chaos lookup <structure> <key>\n")
}
}
func cmd_len(args []string) {
if len(args) == 2 {
path := path.Join(".", ".chaos", args[0])
if _, err := os.Stat(path); err == nil {
data, _ := ioutil.ReadFile(path)
template := chaos.NewTemplate()
err := json.Unmarshal(data, template)
if err != nil {
fmt.Printf("Cannot parse \"%v\"", args[0])
return
}
fmt.Printf("%v\n", template.Len(args[1]))
} else {
fmt.Printf("Chaos structure \"%v\" does not exist\n", args[0])
return
@ -78,6 +99,8 @@ func main() {
cmd_new(os.Args[2:])
case "lookup":
cmd_lookup(os.Args[2:])
case "len":
cmd_len(os.Args[2:])
default:
cmd(os.Args[1], os.Args[2:])
}

View File

@ -12,6 +12,10 @@ func (gs *GSet) Add(element string) {
gs.Elements[element] = true
}
func (gs *GSet) Len() int {
return len(gs.Elements)
}
func (gs *GSet) Lookup(element string) bool {
_, exists := gs.Elements[element]
return exists

3
max.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/sh
# Find the Max Counter Value in an n-Count structure
seq $2 | xargs -I{} chaos len $1 {} | sort -nr | head -1

View File

@ -28,6 +28,13 @@ func (t *Template) ExecCmd(cmd string, element string) {
}
}
func (t *Template) Len(structure string) int{
if _,exists := t.Structures[structure]; exists {
return t.Structures[structure].Len()
}
return 0
}
func (t *Template) Lookup(element string) bool {
for _, structure := range t.LookupIn {
if t.Structures[structure].Lookup(element) == false {