PageRenderTime 39ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/ParaBond/src/scaly/parabond/db/DbObject.scala

http://scaly.googlecode.com/
Scala | 158 lines | 57 code | 40 blank | 61 comment | 3 complexity | 2b23d72bf3906ae6297f709c0ec1dc3c MD5 | raw file
  1. /*
  2. * Copyright (c) Scaly Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the Scaly Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package scaly.parabond.db
  28. import com.mongodb.casbah.MongoConnection
  29. import com.mongodb.casbah.commons.{ MongoDBObject, MongoDBList }
  30. import scala.io.Source
  31. import java.util.Calendar
  32. import java.util.Date
  33. class DbObject {
  34. /** Portfolios collection name */
  35. val COLL_PORTFOLIOS = "Portfolios"
  36. /** Bonds collection name */
  37. val COLL_BONDS = "Bonds"
  38. /** Bonds input data */
  39. val INPUT_BONDS = "bonds.txt"
  40. /** Portfolio input data */
  41. val INPUT_PORTFS = "portfs.txt"
  42. /** Creating DB Connection which is created by IP address of DB server and database name */
  43. val mongodb = MongoConnection("127.0.0.1")("parabond")
  44. /** Loads Bonds from the bonds.txt file */
  45. def loadBonds() = {
  46. // Connects to Bonds collection
  47. val mongo = mongodb(COLL_BONDS)
  48. // Creating an empty MongoDBObject List
  49. var list = List(MongoDBObject())
  50. // Dropping it to recreate with fresh data
  51. mongo.dropCollection()
  52. // Loop through input data file, convert each record to a mongo object,
  53. // and store in mongo
  54. for (record <- Source.fromFile(INPUT_BONDS).getLines()) {
  55. // Get the record details of a bond
  56. val details = record.split(",");
  57. val id = details(0).trim.toInt
  58. val entry = MongoDBObject("id" -> id, "coupon" -> details(1).trim.toDouble, "freq" -> details(2).trim.toInt, "tenor" -> details(3).trim.toInt, "maturity" -> details(4).trim.toDouble)
  59. mongo.insert(entry)
  60. println("loaded bond: "+id)
  61. }
  62. // Print the current size of Bond Collection in DB
  63. println(mongo.find().size + " documents inserted into collection: "+COLL_BONDS)
  64. }
  65. /** Loads Portfolio's from the portfs.txt file */
  66. def loadPortfolios() = {
  67. // Connects to Portfolio collection
  68. val mongo = mongodb(COLL_PORTFOLIOS)
  69. // Creating an empty MongoDBObject List
  70. var list = List(MongoDBObject())
  71. // Dropping it to recreate with fresh data
  72. mongo.dropCollection()
  73. // Loop through input data file, convert each record to a mongo object,
  74. // and store in mongo
  75. for (record <- Source.fromFile(INPUT_PORTFS).getLines()) {
  76. val ids = record.split(" ")
  77. var bondsList: List[Int] = List()
  78. for (id <- ids)
  79. bondsList = id.trim.toInt :: bondsList
  80. // Remove the first id since that id is the portfolio id
  81. bondsList = bondsList.dropRight(1)
  82. val portfId = ids(0).trim.toInt
  83. val entry = MongoDBObject("id" -> portfId, "instruments" -> bondsList)
  84. mongo.insert(entry)
  85. println("loaded portfolio: "+portfId)
  86. }
  87. // Print the current size of Bond Collection in DB
  88. println(mongo.find().size + " documents inserted into collection: "+COLL_PORTFOLIOS)
  89. }
  90. /**
  91. * Get data from the collection name and the Id provided
  92. * @param collection
  93. * @param id
  94. */
  95. def getData(collection: String, id: Int) = {
  96. // Connect to mongo collection specified
  97. val mongo = mongodb(collection)
  98. // Initialize the Start and Finish time
  99. val startTime = Calendar.getInstance()
  100. val finishTime = Calendar.getInstance()
  101. // MongoDBObject query to fetch based on Id
  102. val q = MongoDBObject("id" -> id)
  103. // Set fetch start time
  104. startTime setTime (new Date())
  105. // Fetch the data
  106. val list = mongo.find(q)
  107. // Set fetch finish time
  108. finishTime setTime (new Date())
  109. // Print all data fetched
  110. list.foreach(println(_))
  111. // Calculate time taken for fetch
  112. val startTimeLong = startTime getTimeInMillis
  113. val finishTimeLong = finishTime getTimeInMillis
  114. def difference = finishTimeLong - startTimeLong
  115. // Print fetch time (latency)
  116. println("Time to fetch " + difference + " ms for " + list.size)
  117. }
  118. }