You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.8 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "log"
  7. mgo "gopkg.in/mgo.v2"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. //MongoConfig stores the configuration of mongodb to connect
  11. type MongoConfig struct {
  12. Ip string `json:"ip"`
  13. Database string `json:"database"`
  14. }
  15. var mongoConfig MongoConfig
  16. func readMongodbConfig(path string) {
  17. file, e := ioutil.ReadFile(path)
  18. if e != nil {
  19. fmt.Println("error:", e)
  20. }
  21. content := string(file)
  22. json.Unmarshal([]byte(content), &mongoConfig)
  23. }
  24. func getSession() (*mgo.Session, error) {
  25. session, err := mgo.Dial("mongodb://" + mongoConfig.Ip)
  26. if err != nil {
  27. panic(err)
  28. }
  29. //defer session.Close()
  30. // Optional. Switch the session to a monotonic behavior.
  31. session.SetMode(mgo.Monotonic, true)
  32. // Optional. Switch the session to a monotonic behavior.
  33. session.SetMode(mgo.Monotonic, true)
  34. return session, err
  35. }
  36. func getCollection(session *mgo.Session, collection string) *mgo.Collection {
  37. c := session.DB(mongoConfig.Database).C(collection)
  38. return c
  39. }
  40. func saveBlock(c *mgo.Collection, block BlockModel) {
  41. //first, check if the item already exists
  42. result := BlockModel{}
  43. err := c.Find(bson.M{"hash": block.Hash}).One(&result)
  44. if err != nil {
  45. //item not found, so let's add a new entry
  46. err = c.Insert(block)
  47. check(err)
  48. } else {
  49. err = c.Update(bson.M{"hash": block.Hash}, &block)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. }
  54. }
  55. func getAllNodes() ([]NodeModel, error) {
  56. result := []NodeModel{}
  57. iter := nodeCollection.Find(bson.M{}).Limit(10000).Iter()
  58. err := iter.All(&result)
  59. return result, err
  60. }
  61. func saveNode(c *mgo.Collection, node NodeModel) {
  62. //first, check if the node already exists
  63. result := NodeModel{}
  64. err := c.Find(bson.M{"id": node.Id, "group": node.Group}).One(&result)
  65. if err != nil {
  66. //node not found, so let's add a new entry
  67. err = c.Insert(node)
  68. check(err)
  69. } else {
  70. err = c.Update(bson.M{"id": node.Id, "group": node.Group}, &node)
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74. }
  75. }
  76. func getAllEdges() ([]EdgeModel, error) {
  77. result := []EdgeModel{}
  78. iter := edgeCollection.Find(bson.M{}).Limit(10000).Iter()
  79. err := iter.All(&result)
  80. return result, err
  81. }
  82. func saveEdge(c *mgo.Collection, edge EdgeModel) {
  83. //first, check if the edge already exists
  84. result := EdgeModel{}
  85. err := c.Find(bson.M{"txid": edge.Txid, "to": edge.To, "from": edge.From, "blockheight": edge.BlockHeight, "label": edge.Label}).One(&result)
  86. if err != nil {
  87. //edge not found, so let's add a new entry
  88. err = c.Insert(edge)
  89. check(err)
  90. } else {
  91. err = c.Update(bson.M{"txid": edge.Txid, "to": edge.To, "from": edge.From, "blockheight": edge.BlockHeight, "label": edge.Label}, &edge)
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. }
  96. }
  97. func nodeInNodes(nodes []NodeModel, node NodeModel) bool {
  98. for _, n := range nodes {
  99. if n.Id == node.Id {
  100. return true
  101. }
  102. }
  103. return false
  104. }