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.

128 lines
2.9 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}).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}, &node)
  71. if err != nil {
  72. log.Fatal(err)
  73. }
  74. */
  75. }
  76. }
  77. func getAllEdges() ([]EdgeModel, error) {
  78. result := []EdgeModel{}
  79. iter := edgeCollection.Find(bson.M{}).Limit(10000).Iter()
  80. err := iter.All(&result)
  81. return result, err
  82. }
  83. func saveEdge(c *mgo.Collection, edge EdgeModel) {
  84. //first, check if the edge already exists
  85. result := EdgeModel{}
  86. err := c.Find(bson.M{"txid": edge.Txid, "to": edge.To, "from": edge.From, "blockheight": edge.BlockHeight, "label": edge.Label}).One(&result)
  87. if err != nil {
  88. //edge not found, so let's add a new entry
  89. err = c.Insert(edge)
  90. check(err)
  91. } else {
  92. err = c.Update(bson.M{"txid": edge.Txid, "to": edge.To, "from": edge.From, "blockheight": edge.BlockHeight, "label": edge.Label}, &edge)
  93. if err != nil {
  94. log.Fatal(err)
  95. }
  96. }
  97. }
  98. func nodeInNodes(nodes []NodeModel, node NodeModel) bool {
  99. for _, n := range nodes {
  100. if n.Id == node.Id {
  101. return true
  102. }
  103. }
  104. return false
  105. }
  106. func edgeInEdges(edges []EdgeModel, edge EdgeModel) bool {
  107. for _, e := range edges {
  108. if e.From == edge.From && e.To == edge.To {
  109. return true
  110. }
  111. }
  112. return false
  113. }