Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bhaskar cleanups #179

Merged
merged 5 commits into from
Jul 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@ _includes/*

#emacs files
*~
\#*\#
\#*\#

# jenv java version mgmt
.java-version
8 changes: 7 additions & 1 deletion app/collins/solr/SolrPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import AssetMeta.ValueType
import AssetMeta.ValueType._

import CollinsQueryDSL._
import Solr.AssetSolrDocument

class SolrPlugin(app: Application) extends Plugin {

Expand Down Expand Up @@ -127,7 +128,7 @@ class SolrPlugin(app: Application) extends Plugin {

def updateItems[T](items: Seq[T], serializer: SolrSerializer[T], indexTime: Date, commit: Boolean = true) {
_server.map{server =>
val docs = items.map{item => Solr.prepForInsertion(serializer.serialize(item, indexTime))}
val docs = items.map{item => prepForInsertion(serializer.serialize(item, indexTime))}
if (docs.size > 0) {
val fuckingJava = new java.util.ArrayList[SolrInputDocument]
docs.foreach{doc => fuckingJava.add(doc)}
Expand Down Expand Up @@ -159,4 +160,9 @@ class SolrPlugin(app: Application) extends Plugin {
_server.foreach{case s: EmbeddedSolrServer => s.shutdown}
}

def prepForInsertion(typedMap: AssetSolrDocument): SolrInputDocument = {
val input = new SolrInputDocument
typedMap.foreach{case(key,value) => input.addField(key.resolvedName,value.value)}
input
}
}
17 changes: 8 additions & 9 deletions app/collins/solr/SolrUpdater.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package collins.solr
import akka.actor._
import akka.util.duration._
import play.api.Logger

import models.{Asset, AssetLog}

import java.util.Collections
import java.util.Date
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean
import scala.collection.JavaConverters._
import java.util.concurrent.atomic.AtomicReference

//TODO: refactor all this

Expand All @@ -23,9 +22,11 @@ import scala.collection.JavaConverters._
*/
class AssetSolrUpdater extends Actor {

private[this] val set = Collections.newSetFromMap[String](
private[this] def newAssetTagSet = Collections.newSetFromMap[String](
new ConcurrentHashMap[String,java.lang.Boolean]()
)

private[this] val assetTagsRef = new AtomicReference(newAssetTagSet)
private[this] val logger = Logger("SolrUpdater")

//mutex to prevent multiple concurrent scheduler calls
Expand All @@ -42,7 +43,7 @@ class AssetSolrUpdater extends Actor {
*/
def receive = {
case asset: Asset =>
set.add(asset.tag)
assetTagsRef.get.add(asset.tag)
if (scheduled.compareAndSet(false, true)) {
logger.debug("Scheduling update, saw %s".format(asset.tag))
context.system.scheduler.scheduleOnce(10 milliseconds, self, Reindex)
Expand All @@ -51,13 +52,11 @@ class AssetSolrUpdater extends Actor {
}
case Reindex =>
if (scheduled.get == true) {
val toRemove = set.asScala.toSeq
val assetTags = assetTagsRef.getAndSet(newAssetTagSet).asScala.toSeq
val indexTime = new Date
val assets = toRemove.map(t => Asset.findByTag(t)).filter(_.isDefined).map(_.get)
logger.debug("Got Reindex task, working on %d assets, set is %d".format(toRemove.size, set.size))
val assets = assetTags.map(t => Asset.findByTag(t)).flatMap(a => a)
logger.debug("Got Reindex task, working on %d assets".format(assetTags.size))
Solr.plugin.foreach(_.updateAssets(assets, indexTime))
set.removeAll(toRemove.asJava)
logger.debug("Set size now %d".format(set.size))
scheduled.set(false)
}
}
Expand Down
6 changes: 0 additions & 6 deletions app/util/plugins/Solr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ object Solr {
//TODO: Rename
type AssetSolrDocument = Map[SolrKey, SolrValue]

def prepForInsertion(typedMap: AssetSolrDocument): SolrInputDocument = {
val input = new SolrInputDocument
typedMap.foreach{case(key,value) => input.addField(key.resolvedName,value.value)}
input
}

def server: Option[SolrServer] = Play.maybeApplication.flatMap { app =>
app.plugin[SolrPlugin].filter(_.enabled).map{_.server}
}
Expand Down