Last active
March 2, 2020 04:32
-
-
Save buraksezer/045b7ec09463e38b383d0413ad9bcc57 to your computer and use it in GitHub Desktop.
Distributed queries on Olric
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
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"time" | |
"github.com/buraksezer/olric" | |
"github.com/buraksezer/olric/config" | |
"github.com/buraksezer/olric/query" | |
) | |
func bkey(i int) string { | |
return fmt.Sprintf("%09d", i) | |
} | |
func main() { | |
c := config.New("local") | |
ctx, cancel := context.WithCancel(context.Background()) | |
c.Started = func() { | |
defer cancel() | |
log.Println("[INFO] Olric is ready to accept connections") | |
} | |
// This creates a single-node Olric cluster. It's good enough for experimenting. | |
db, err := olric.New(c) | |
if err != nil { | |
log.Fatalf("Failed to create Olric object: %v", err) | |
} | |
go func() { | |
// Call Start at background. It's a blocker call. | |
err = db.Start() | |
if err != nil { | |
log.Fatalf("Failed to call Start: %v", err) | |
} | |
}() | |
<-ctx.Done() | |
// Put 10 items into the DMap object. | |
dm, err := db.NewDMap("mydmap") | |
if err != nil { | |
log.Fatalf("Failed to call NewDMap: %v", err) | |
} | |
var key string | |
for i := 0; i < 10; i++ { | |
if i%2 == 0 { | |
key = "even:" + bkey(i) | |
} else { | |
key = "odd:" + bkey(i) | |
} | |
err = dm.Put(key, i) | |
if err != nil { | |
log.Printf("Put call failed: %v", err) | |
} | |
} | |
// Distributed query implementation | |
// Find even numbers | |
q1, err := dm.Query( | |
query.M{ | |
"$onKey": query.M{ | |
"$regexMatch": "even:", | |
}, | |
}, | |
) | |
if err != nil { | |
log.Printf("Failed to run query on even numbers %v", err) | |
} | |
defer q1.Close() | |
fmt.Println("EVEN NUMBERS:") | |
fmt.Println("=============") | |
err = q1.Range(func(key string, value interface{}) bool { | |
fmt.Printf("KEY: %s, VALUE: %v\n", key, value) | |
return true | |
}) | |
fmt.Println("") | |
// Find odd numbers | |
q2, err := dm.Query( | |
query.M{ | |
"$onKey": query.M{ | |
"$regexMatch": "odd:", | |
}, | |
}, | |
) | |
if err != nil { | |
log.Printf("Failed to run query on even numbers %v", err) | |
} | |
defer q2.Close() | |
fmt.Println("ODD NUMBERS:") | |
fmt.Println("=============") | |
err = q2.Range(func(key string, value interface{}) bool { | |
fmt.Printf("KEY: %s, VALUE: %v\n", key, value) | |
return true | |
}) | |
fmt.Println("") | |
// Find even numbers and ignore the values | |
q3, err := dm.Query( | |
query.M{ | |
"$onKey": query.M{ | |
"$regexMatch": "even:", | |
"$options": query.M{ | |
"$onValue": query.M{ | |
"$ignore": true, | |
}, | |
}, | |
}, | |
}, | |
) | |
if err != nil { | |
log.Printf("Failed to run query on even numbers %v", err) | |
} | |
defer q3.Close() | |
fmt.Println("EVEN NUMBERS WITHOUT VALUES:") | |
fmt.Println("============================") | |
err = q3.Range(func(key string, value interface{}) bool { | |
fmt.Printf("KEY: %s, VALUE: %v\n", key, value) | |
return true | |
}) | |
fmt.Println("") | |
q4, err := dm.Query( | |
query.M{ | |
"$onKey": query.M{ | |
"$regexMatch": "", | |
}, | |
}, | |
) | |
if err != nil { | |
log.Printf("Failed to run query on dmap %v", err) | |
} | |
defer q4.Close() | |
fmt.Println("ITERATOR ON DMAP:") | |
fmt.Println("=================") | |
err = q4.Range(func(key string, value interface{}) bool { | |
fmt.Printf("KEY: %s, VALUE: %v\n", key, value) | |
return true | |
}) | |
// Don't forget the call Shutdown when you want to leave the cluster. | |
ctx, _ = context.WithTimeout(context.Background(), 10*time.Second) | |
err = db.Shutdown(ctx) | |
if err != nil { | |
log.Printf("Failed to shutdown Olric: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment