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.

147 lines
3.4 KiB

  1. package main
  2. import (
  3. "log"
  4. mgo "gopkg.in/mgo.v2"
  5. "gopkg.in/mgo.v2/bson"
  6. )
  7. func getSession() (*mgo.Session, error) {
  8. session, err := mgo.Dial("mongodb://" + config.Mongodb.IP)
  9. if err != nil {
  10. panic(err)
  11. }
  12. //defer session.Close()
  13. // Optional. Switch the session to a monotonic behavior.
  14. session.SetMode(mgo.Monotonic, true)
  15. // Optional. Switch the session to a monotonic behavior.
  16. session.SetMode(mgo.Monotonic, true)
  17. return session, err
  18. }
  19. func getCollection(session *mgo.Session, collection string) *mgo.Collection {
  20. c := session.DB(config.Mongodb.Database).C(collection)
  21. return c
  22. }
  23. /*
  24. func saveBlock(c *mgo.Collection, block BlockModel) {
  25. //first, check if the item already exists
  26. result := BlockModel{}
  27. err := c.Find(bson.M{"hash": block.Hash}).One(&result)
  28. if err != nil {
  29. //item not found, so let's add a new entry
  30. err = c.Insert(block)
  31. check(err)
  32. } else {
  33. err = c.Update(bson.M{"hash": block.Hash}, &block)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. }
  38. }
  39. */
  40. func getAllNodes() ([]NodeModel, error) {
  41. result := []NodeModel{}
  42. iter := nodeCollection.Find(bson.M{}).Limit(10000).Iter()
  43. err := iter.All(&result)
  44. return result, err
  45. }
  46. func saveNode(c *mgo.Collection, node NodeModel) {
  47. //first, check if the node already exists
  48. result := NodeModel{}
  49. err := c.Find(bson.M{"id": node.Id}).One(&result)
  50. if err != nil {
  51. //node not found, so let's add a new entry
  52. err = c.Insert(node)
  53. check(err)
  54. } else {
  55. /*err = c.Update(bson.M{"id": node.Id}, &node)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. */
  60. }
  61. }
  62. func getAllEdges() ([]EdgeModel, error) {
  63. result := []EdgeModel{}
  64. iter := edgeCollection.Find(bson.M{}).Limit(10000).Iter()
  65. err := iter.All(&result)
  66. return result, err
  67. }
  68. func saveEdge(c *mgo.Collection, edge EdgeModel) {
  69. //first, check if the edge already exists
  70. result := EdgeModel{}
  71. err := c.Find(bson.M{"txid": edge.Txid, "to": edge.To, "from": edge.From, "blockheight": edge.BlockHeight, "label": edge.Label}).One(&result)
  72. if err != nil {
  73. //edge not found, so let's add a new entry
  74. err = c.Insert(edge)
  75. check(err)
  76. } else {
  77. err = c.Update(bson.M{"txid": edge.Txid, "to": edge.To, "from": edge.From, "blockheight": edge.BlockHeight, "label": edge.Label}, &edge)
  78. if err != nil {
  79. log.Fatal(err)
  80. }
  81. }
  82. }
  83. func nodeInNodes(nodes []NodeModel, node NodeModel) bool {
  84. for _, n := range nodes {
  85. if n.Id == node.Id {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. func edgeInEdges(edges []EdgeModel, edge EdgeModel) bool {
  92. for _, e := range edges {
  93. if e.From == edge.From && e.To == edge.To && e.Label == edge.Label && e.BlockHeight == edge.BlockHeight {
  94. return true
  95. }
  96. }
  97. return false
  98. }
  99. func saveAddress(address AddressModel) {
  100. result := AddressModel{}
  101. err := addressCollection.Find(bson.M{"hash": address.Hash}).One(&result)
  102. if err != nil {
  103. //address not found, so let's add a new entry
  104. err = addressCollection.Insert(address)
  105. check(err)
  106. //stats addr
  107. stats := getStats()
  108. stats.AddrCount++
  109. updateStats(stats)
  110. }
  111. }
  112. func saveTx(tx TxModel) {
  113. result := TxModel{}
  114. err := txCollection.Find(bson.M{"txid": tx.Txid}).One(&result)
  115. if err != nil {
  116. //tx not found, so let's add a new entry
  117. err = txCollection.Insert(tx)
  118. check(err)
  119. }
  120. }
  121. func saveBlock(block BlockModel) {
  122. result := BlockModel{}
  123. err := blockCollection.Find(bson.M{"hash": block.Hash}).One(&result)
  124. if err != nil {
  125. //block not found, so let's add a new entry
  126. err = blockCollection.Insert(block)
  127. check(err)
  128. }
  129. }