PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/indexer/src/main/scala/output/FidMap.scala

https://gitlab.com/18runt88/twofishes
Scala | 53 lines | 47 code | 5 blank | 1 comment | 7 complexity | bed4a157291976cb497ec9f132758288 MD5 | raw file
  1. package com.foursquare.twofishes.output
  2. import com.foursquare.twofishes.mongo.MongoGeocodeDAO
  3. import com.foursquare.twofishes.util.{DurationUtils, StoredFeatureId}
  4. import com.mongodb.Bytes
  5. import com.mongodb.casbah.Imports._
  6. import com.novus.salat._
  7. import com.novus.salat.annotations._
  8. import com.novus.salat.dao._
  9. import com.novus.salat.global._
  10. import java.io._
  11. import org.apache.hadoop.hbase.util.Bytes._
  12. import scala.collection.mutable.HashMap
  13. import scalaj.collection.Implicits._
  14. class FidMap(preload: Boolean) extends DurationUtils {
  15. val fidMap = new HashMap[StoredFeatureId, Option[StoredFeatureId]]
  16. if (preload) {
  17. logPhase("preloading fids") {
  18. var i = 0
  19. val total = MongoGeocodeDAO.collection.count()
  20. val geocodeCursor = MongoGeocodeDAO.find(MongoDBObject())
  21. geocodeCursor.option = Bytes.QUERYOPTION_NOTIMEOUT
  22. geocodeCursor.foreach(geocodeRecord => {
  23. geocodeRecord.featureIds.foreach(id => {
  24. fidMap(id) = Some(geocodeRecord.featureId)
  25. })
  26. i += 1
  27. if (i % (100*1000) == 0) {
  28. logger.info("preloaded %d/%d fids".format(i, total))
  29. }
  30. })
  31. }
  32. }
  33. def get(fid: StoredFeatureId): Option[StoredFeatureId] = {
  34. if (preload) {
  35. fidMap.getOrElse(fid, None)
  36. } else {
  37. if (!fidMap.contains(fid)) {
  38. val longidOpt = MongoGeocodeDAO.primitiveProjection[Long](
  39. MongoDBObject("_id" -> fid.longId), "_id")
  40. fidMap(fid) = longidOpt.flatMap(StoredFeatureId.fromLong _)
  41. if (longidOpt.isEmpty) {
  42. //println("missing fid: %s".format(fid))
  43. }
  44. }
  45. fidMap.getOrElseUpdate(fid, None)
  46. }
  47. }
  48. }