|
| 1 | +package scattergather |
| 2 | + |
| 3 | +import collection.immutable.HashMap |
| 4 | +import akka.actor.{ReceiveTimeout, ActorRef, Actor} |
| 5 | + |
| 6 | +/** |
| 7 | + * A message representing a document to add to the search tree. |
| 8 | + */ |
| 9 | +case class SearchableDocument(content: String) |
| 10 | + |
| 11 | +trait BaseHeadNode { self: AdaptiveSearchNode => |
| 12 | + var children = IndexedSeq[ActorRef]() |
| 13 | + var currentIdx = 0 |
| 14 | + def parentNode: PartialFunction[Any, Unit] = { |
| 15 | + case SearchQuery(q, max, responder) => |
| 16 | + // TODO - use gatherer scheudler |
| 17 | + val gatherer = Actor.actorOf(new GathererNode { |
| 18 | + val maxDocs = max |
| 19 | + val maxResponses = children.size |
| 20 | + val query = q |
| 21 | + val client = responder |
| 22 | + }) |
| 23 | + gatherer.start |
| 24 | + for (node <- children) { |
| 25 | + node ! SearchQuery(q, max, gatherer) |
| 26 | + } |
| 27 | + case s @ SearchableDocument(_) => getNextChild ! s |
| 28 | + } |
| 29 | + |
| 30 | + // Round Robin |
| 31 | + private def getNextChild = { |
| 32 | + currentIdx = (1 + currentIdx) % children.size |
| 33 | + children(currentIdx) |
| 34 | + } |
| 35 | + |
| 36 | +} |
| 37 | + |
| 38 | +trait BaseChildNode { self: AdaptiveSearchNode => |
| 39 | + final val maxNoOfDocuments = 10 |
| 40 | + var documents: Vector[String] = Vector() |
| 41 | + var index: HashMap[String, Seq[(Double, String)]] = HashMap() |
| 42 | + |
| 43 | + def leafNode: PartialFunction[Any, Unit] = { |
| 44 | + case SearchQuery(query, maxDocs, handler) => executeLocalQuery(query, maxDocs, handler) |
| 45 | + case SearchableDocument(content) => addDocumentToLocalIndex(content) |
| 46 | + } |
| 47 | + private def executeLocalQuery(query: String, maxDocs: Int, handler: ActorRef) = { |
| 48 | + val result = for { |
| 49 | + results <- index.get(query).toList |
| 50 | + resultList <- results |
| 51 | + } yield resultList |
| 52 | + handler ! QueryResponse(result take maxDocs) |
| 53 | + } |
| 54 | + |
| 55 | + private def addDocumentToLocalIndex(content: String) = { |
| 56 | + for( (key,value) <- content.split("\\s+").groupBy(identity)) { |
| 57 | + val list = index.get(key) getOrElse Seq() |
| 58 | + index += ((key, ((value.length.toDouble, content)) +: list)) |
| 59 | + } |
| 60 | + documents = documents :+ content |
| 61 | + // Split on size.... |
| 62 | + if (documents.size > maxNoOfDocuments) split() |
| 63 | + } |
| 64 | + |
| 65 | + /** Abstract method to split this actor. */ |
| 66 | + protected def split(): Unit |
| 67 | + |
| 68 | + protected def clearIndex(): Unit = { |
| 69 | + documents = Vector() |
| 70 | + index = HashMap() |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +class AdaptiveSearchNode extends Actor with BaseHeadNode with BaseChildNode { |
| 76 | + def receive = leafNode |
| 77 | + |
| 78 | + /** Splits this search node into a tree of search nodes if there are too many documents. */ |
| 79 | + protected def split(): Unit = { |
| 80 | + children = (for(docs <- documents grouped 5) yield { |
| 81 | + // TODO - use search scheduler + hook up to supervisor... |
| 82 | + val child = Actor.actorOf[AdaptiveSearchNode] |
| 83 | + child.start() |
| 84 | + docs foreach (child ! SearchableDocument(_)) |
| 85 | + child |
| 86 | + }).toIndexedSeq |
| 87 | + clearIndex() |
| 88 | + this become parentNode |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments