go sonic
1.0.0
Paket ini mengimplementasikan semua perintah untuk bekerja dengan sonic. Jika ada satu yang hilang, buka terbitannya! :)
Sonik: https://github.com/valeriansaliou/sonic
go get github.com/expectedsh/go-sonic
package main
import (
"fmt"
"github.com/expectedsh/go-sonic/sonic"
)
func main () {
ingester , err := sonic . NewIngester ( "localhost" , 1491 , "SecretPassword" )
if err != nil {
panic ( err )
}
// I will ignore all errors for demonstration purposes
_ = ingester . BulkPush ( "movies" , "general" , 3 , []sonic. IngestBulkRecord {
{ "id:6ab56b4kk3" , "Star wars" },
{ "id:5hg67f8dg5" , "Spider man" },
{ "id:1m2n3b4vf6" , "Batman" },
{ "id:68d96h5h9d0" , "This is another movie" },
})
search , err := sonic . NewSearch ( "localhost" , 1491 , "SecretPassword" )
if err != nil {
panic ( err )
}
results , _ := search . Query ( "movies" , "general" , "man" , 10 , 0 )
fmt . Println ( results )
}
Metode BulkPush dan BulkPop menggunakan kumpulan koneksi khusus dengan algoritma pengiriman goroutine. Ini patokannya (file sonic/ingester_test.go):
goos: linux
goarch: amd64
pkg: github.com/expectedsh/go-sonic/sonic
BenchmarkIngesterChannel_BulkPushMaxCPUs-8 2 662657959 ns/op
BenchmarkIngesterChannel_BulkPush10-8 2 603779977 ns/op
BenchmarkIngesterChannel_Push-8 1 1023322864 ns/op
PASS
Dorongan massal lebih cepat daripada perulangan for pada Push. Detail perangkat keras: CPU Intel(R) Core(TM) i7-8550U @ 1,80GHz
Pengemudi itu sendiri tidak aman untuk thread. Anda dapat menggunakan kunci atau saluran untuk menghindari kerusakan.
package main
import (
"fmt"
"github.com/expectedsh/go-sonic/sonic"
)
func main () {
events := make ( chan [] string , 1 )
event := [] string { "some_text" , "some_id" }
tryCrash := func () {
for {
// replace "event" with whatever is giving you events: pubsub, amqp messages…
events <- event
}
}
go tryCrash ()
go tryCrash ()
go tryCrash ()
go tryCrash ()
ingester , _ := sonic . NewIngester ( "localhost" , 1491 , "SecretPassword" )
for {
msg := <- events
// Or use some buffering along with BulkPush
ingester . Push ( "collection" , "bucket" , msg [ 1 ], msg [ 0 ])
}
}