storage.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package kyoketsu
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go.mongodb.org/mongo-driver/mongo"
  7. "go.mongodb.org/mongo-driver/mongo/options"
  8. )
  9. type TopologyDatabaseIO interface {
  10. /*
  11. This interface defines the Input and output methods that will be necessary
  12. for an appropriate implementation of the data storage that the distributed system will use.
  13. When I get around to implementing the client-to-client format of this, it could be anything.
  14. */
  15. AddHostToDb(*Host) error // Add a host to the hosts table
  16. UpdateHostEntry(string, *Host) error //Update a host entry, indexing by its ip address
  17. RemoveHostEntry(string) error // Remove a host from the database
  18. }
  19. type MongoClient struct {
  20. conn *mongo.Client
  21. }
  22. func NewMongoClient(host string, port int) *MongoClient {
  23. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  24. defer cancel()
  25. client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%v", host, port)))
  26. defer func() {
  27. if err = client.Disconnect(ctx); err != nil {
  28. panic(err)
  29. }
  30. }()
  31. return &MongoClient{conn: client}
  32. }
  33. func (m *MongoClient) addDocument(id string, data interface{}) error {
  34. return nil
  35. }
  36. func (m *MongoClient) AddHostToDb(host *Host) error {
  37. return nil
  38. }
  39. func (m *MongoClient) UpdateHostEntry(id string, host *Host) error {
  40. return nil
  41. }
  42. func (m *MongoClient) RemoveHostEntry(id string) error {
  43. return nil
  44. }