/ParaBond/src/scaly/parabond/db/DbObject.scala
Scala | 158 lines | 57 code | 40 blank | 61 comment | 3 complexity | 2b23d72bf3906ae6297f709c0ec1dc3c MD5 | raw file
- /*
- * Copyright (c) Scaly Contributors
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the Scaly Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- package scaly.parabond.db
- import com.mongodb.casbah.MongoConnection
- import com.mongodb.casbah.commons.{ MongoDBObject, MongoDBList }
- import scala.io.Source
- import java.util.Calendar
- import java.util.Date
-
- class DbObject {
- /** Portfolios collection name */
- val COLL_PORTFOLIOS = "Portfolios"
-
- /** Bonds collection name */
- val COLL_BONDS = "Bonds"
-
- /** Bonds input data */
- val INPUT_BONDS = "bonds.txt"
-
- /** Portfolio input data */
- val INPUT_PORTFS = "portfs.txt"
-
- /** Creating DB Connection which is created by IP address of DB server and database name */
- val mongodb = MongoConnection("127.0.0.1")("parabond")
-
- /** Loads Bonds from the bonds.txt file */
- def loadBonds() = {
-
- // Connects to Bonds collection
- val mongo = mongodb(COLL_BONDS)
-
- // Creating an empty MongoDBObject List
- var list = List(MongoDBObject())
-
- // Dropping it to recreate with fresh data
- mongo.dropCollection()
-
- // Loop through input data file, convert each record to a mongo object,
- // and store in mongo
- for (record <- Source.fromFile(INPUT_BONDS).getLines()) {
- // Get the record details of a bond
- val details = record.split(",");
-
- val id = details(0).trim.toInt
-
- 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)
-
- mongo.insert(entry)
-
- println("loaded bond: "+id)
- }
-
- // Print the current size of Bond Collection in DB
- println(mongo.find().size + " documents inserted into collection: "+COLL_BONDS)
- }
-
- /** Loads Portfolio's from the portfs.txt file */
- def loadPortfolios() = {
-
- // Connects to Portfolio collection
- val mongo = mongodb(COLL_PORTFOLIOS)
-
- // Creating an empty MongoDBObject List
- var list = List(MongoDBObject())
-
- // Dropping it to recreate with fresh data
- mongo.dropCollection()
-
- // Loop through input data file, convert each record to a mongo object,
- // and store in mongo
- for (record <- Source.fromFile(INPUT_PORTFS).getLines()) {
- val ids = record.split(" ")
-
- var bondsList: List[Int] = List()
-
- for (id <- ids)
- bondsList = id.trim.toInt :: bondsList
-
- // Remove the first id since that id is the portfolio id
- bondsList = bondsList.dropRight(1)
-
- val portfId = ids(0).trim.toInt
-
- val entry = MongoDBObject("id" -> portfId, "instruments" -> bondsList)
-
- mongo.insert(entry)
-
- println("loaded portfolio: "+portfId)
- }
-
- // Print the current size of Bond Collection in DB
- println(mongo.find().size + " documents inserted into collection: "+COLL_PORTFOLIOS)
-
- }
-
- /**
- * Get data from the collection name and the Id provided
- * @param collection
- * @param id
- */
- def getData(collection: String, id: Int) = {
-
- // Connect to mongo collection specified
- val mongo = mongodb(collection)
-
- // Initialize the Start and Finish time
- val startTime = Calendar.getInstance()
- val finishTime = Calendar.getInstance()
-
- // MongoDBObject query to fetch based on Id
- val q = MongoDBObject("id" -> id)
-
- // Set fetch start time
- startTime setTime (new Date())
-
- // Fetch the data
- val list = mongo.find(q)
-
- // Set fetch finish time
- finishTime setTime (new Date())
-
- // Print all data fetched
- list.foreach(println(_))
-
- // Calculate time taken for fetch
- val startTimeLong = startTime getTimeInMillis
- val finishTimeLong = finishTime getTimeInMillis
- def difference = finishTimeLong - startTimeLong
-
- // Print fetch time (latency)
- println("Time to fetch " + difference + " ms for " + list.size)
- }
-
- }