PageRenderTime 26ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/indexer/src/main/scala/mongo/MongoGeocodeStorageService.scala

https://gitlab.com/18runt88/twofishes
Scala | 84 lines | 70 code | 13 blank | 1 comment | 0 complexity | c78a6faa4039392ed2ab16ec371501cd MD5 | raw file
  1. // Copyright 2012 Foursquare Labs Inc. All Rights Reserved.
  2. package com.foursquare.twofishes.mongo
  3. import com.foursquare.twofishes.{GeocodeRecord, BoundingBox, DisplayName}
  4. import com.foursquare.twofishes.util.StoredFeatureId
  5. import com.mongodb.Bytes
  6. import com.mongodb.casbah.Imports._
  7. import com.mongodb.casbah.MongoConnection
  8. import com.novus.salat._
  9. import com.novus.salat.annotations._
  10. import com.novus.salat.dao._
  11. import com.novus.salat.global._
  12. class MongoGeocodeStorageService extends GeocodeStorageWriteService {
  13. def getById(id: StoredFeatureId): Iterator[GeocodeRecord] = {
  14. val geocodeCursor = MongoGeocodeDAO.find(MongoDBObject("_id" -> id.longId))
  15. geocodeCursor.option = Bytes.QUERYOPTION_NOTIMEOUT
  16. geocodeCursor
  17. }
  18. def getNameIndexByIdLangAndName(id: StoredFeatureId, lang: String, name: String): Iterator[NameIndex] = {
  19. val nameCursor = NameIndexDAO.find(MongoDBObject("fid" -> id.longId, "lang" -> lang, "name" -> name))
  20. nameCursor.option = Bytes.QUERYOPTION_NOTIMEOUT
  21. nameCursor
  22. }
  23. def insert(record: GeocodeRecord) {
  24. MongoGeocodeDAO.insert(record)
  25. }
  26. def insert(records: List[GeocodeRecord]) {
  27. records.foreach(record => insert(record))
  28. }
  29. def addBoundingBoxToRecord(bbox: BoundingBox, id: StoredFeatureId) {
  30. MongoGeocodeDAO.update(MongoDBObject("_id" -> id.longId),
  31. MongoDBObject("$set" -> MongoDBObject("boundingbox" -> grater[BoundingBox].asDBObject(bbox))),
  32. false, false)
  33. }
  34. def addNameToRecord(name: DisplayName, id: StoredFeatureId) {
  35. MongoGeocodeDAO.update(MongoDBObject("_id" -> id.longId),
  36. MongoDBObject("$addToSet" -> MongoDBObject("displayNames" -> grater[DisplayName].asDBObject(name))),
  37. false, false)
  38. }
  39. def addNameIndex(name: NameIndex) {
  40. NameIndexDAO.insert(name)
  41. }
  42. def addNameIndexes(names: List[NameIndex]) {
  43. NameIndexDAO.insert(names)
  44. }
  45. def updateFlagsOnNameIndexByIdLangAndName(id: StoredFeatureId, lang: String, name: String, flags: Int) {
  46. NameIndexDAO.update(MongoDBObject("fid" -> id.longId, "lang" -> lang, "name" -> name),
  47. MongoDBObject("$set" -> MongoDBObject("flags" -> flags)),
  48. upsert = false, multi = true)
  49. }
  50. def addPolygonToRecord(id: StoredFeatureId, polyId: ObjectId) {
  51. MongoGeocodeDAO.update(MongoDBObject("_id" -> id.longId),
  52. MongoDBObject("$set" ->
  53. MongoDBObject(
  54. "hasPoly" -> true,
  55. "polyId" -> polyId
  56. )
  57. ),
  58. false, false)
  59. }
  60. def addSlugToRecord(id: StoredFeatureId, slug: String) {
  61. MongoGeocodeDAO.update(MongoDBObject("_id" -> id.longId),
  62. MongoDBObject("$set" -> MongoDBObject("slug" -> slug)),
  63. false, false)
  64. }
  65. def setRecordNames(id: StoredFeatureId, names: List[DisplayName]) {
  66. MongoGeocodeDAO.update(MongoDBObject("_id" -> id.longId),
  67. MongoDBObject("$set" -> MongoDBObject(
  68. "displayNames" -> names.map(n => grater[DisplayName].asDBObject(n)))),
  69. false, false)
  70. }
  71. }