项目作者: bruth

项目描述 :
A table data structure for NATS Streaming.
高级语言: Go
项目地址: git://github.com/bruth/ntable.git
创建时间: 2017-10-12T11:59:03Z
项目社区:https://github.com/bruth/ntable

开源协议:MIT License

下载


NTable

License MIT
Go Report Card Build Status GoDoc

A table data structure for NATS Streaming.

The primary use case for maintaining a lookup table of data derived from a stream. It does this by maintaining an internal key-value store. The default implementation uses an in-memory store, but stores are easy to implement since the interface is a basic key-value interface.

  1. type Store interface {
  2. Get(key []byte) ([]byte, error)
  3. Set(key, val []byte) error
  4. Del(key []byte) error
  5. }

Status

Design phase. Suggestions welcome.

Example

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "time"
  6. "github.com/bruth/ntable"
  7. "github.com/nats-io/go-nats-streaming"
  8. )
  9. // Record with an ID and some data.
  10. type Record struct {
  11. ID string
  12. Data string
  13. }
  14. func main() {
  15. conn, _ := stan.Connect("test-cluster", "test-client")
  16. t := &ntable.Table{
  17. Conn: conn,
  18. Channel: "users",
  19. // Takes a message, derives the key and value and updates the store.
  20. Handle: func(s ntable.Store, m *stan.Msg) {
  21. var r Record
  22. json.Unmarshal(m.Data, &r)
  23. s.Set([]byte(r.ID), []byte(r.Data))
  24. },
  25. }
  26. // Open connection (subscription) to channel.
  27. t.Open()
  28. defer t.Close()
  29. // Publish a record about "pam" (likely happening from another process or thread).
  30. b, _ := json.Marshal(&Record{
  31. ID: "pam",
  32. Data: "color=blue city=Philadelphia food=sushi",
  33. })
  34. conn.Publish("users", b)
  35. // A few moments pass..
  36. time.Sleep(5 * time.Millisecond)
  37. // We can get the data about pam using the table.
  38. val, _ := t.Get([]byte("pam"))
  39. fmt.Println(string(val))
  40. }

License

MIT.