Skip to content

Commit

Permalink
#9 Add nbreplicas and nbshards paramter when index is created
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed May 6, 2017
1 parent 1afacc6 commit 72d4aa1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
25 changes: 22 additions & 3 deletions app/org/elastic4play/database/DBIndex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@ import scala.concurrent.blocking
import com.sksamuel.elastic4s.ElasticDsl.{ RichFuture, index, mapping, search }
import com.sksamuel.elastic4s.IndexesAndTypes.apply

import org.elastic4play.Timed
import play.api.Configuration
import org.elastic4play.models.{ ChildModelDef, ModelAttributes, ModelDef }

@Singleton
class DBIndex @Inject() (
class DBIndex(
db: DBConfiguration,
nbShards: Int,
nbReplicas: Int,
implicit val ec: ExecutionContext) {

@Inject() def this(
configuration: Configuration,
db: DBConfiguration,
ec: ExecutionContext) = this(
db,
configuration.getInt("search.nbshards").getOrElse(5),
configuration.getInt("search.nbreplicas").getOrElse(1),
ec)

/**
* Create a new index. Collect mapping for all attributes of all entities
*
* @param models list of all ModelAttributes to used in order to build index mapping
* @return a future which is completed when index creation is finished
*/
Expand All @@ -29,13 +41,18 @@ class DBIndex @Inject() (
}
.toSeq
db.execute {
com.sksamuel.elastic4s.ElasticDsl.create index db.indexName mappings (modelsMapping: _*)
com.sksamuel.elastic4s.ElasticDsl.create
.index(db.indexName)
.mappings(modelsMapping: _*)
.shards(4)
.replicas(3)
}
.map { _ () }
}

/**
* Tests whether the index exists
*
* @return future of true if the index exists
*/
def getIndexStatus: Future[Boolean] = {
Expand All @@ -48,6 +65,7 @@ class DBIndex @Inject() (

/**
* Tests whether the index exists
*
* @return true if the index exists
*/
def indexStatus: Boolean = blocking {
Expand All @@ -56,6 +74,7 @@ class DBIndex @Inject() (

/**
* Get the number of document of this type
*
* @param modelName name of the document type from which the count must be done
* @return document count
*/
Expand Down
6 changes: 3 additions & 3 deletions app/org/elastic4play/services/MigrationSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class MigrationSrv @Inject() (
dbfind: DBFind,
dbget: DBGet,
dblists: DBLists,
dbindex: DBIndex,
modelSrv: ModelSrv,
eventSrv: EventSrv,
implicit val ec: ExecutionContext,
Expand All @@ -73,7 +74,7 @@ class MigrationSrv @Inject() (
private val currentdbfind = dbfind.switchTo(db)
override def version: Int = db.version
override def source(tableName: String): Source[JsObject, NotUsed] = currentdbfind.apply(Some("all"), Nil)(indexName search.in(indexName tableName).query(QueryDSL.any.query))._1
override def count(tableName: String): Future[Long] = new DBIndex(db, ec).getSize(tableName)
override def count(tableName: String): Future[Long] = new DBIndex(db, 0, 0, ec).getSize(tableName)
override def getEntity(tableName: String, entityId: String): Future[JsObject] = dbget(tableName, entityId)
}

Expand All @@ -86,7 +87,7 @@ class MigrationSrv @Inject() (
}

def migrationPath(db: DBConfiguration): Future[(Int, DatabaseState)] = {
new DBIndex(db, ec).getIndexStatus.flatMap {
new DBIndex(db, 0, 0, ec).getIndexStatus.flatMap {
case true
logger.info(s"Initiate database migration from version ${db.version}")
Future.successful(db.version OriginState(db))
Expand Down Expand Up @@ -134,7 +135,6 @@ class MigrationSrv @Inject() (

private var migrationProcess = Future.successful(())
def migrate: Future[Unit] = {
val dbindex = new DBIndex(db, ec)
if (!dbindex.indexStatus && migrationProcess.isCompleted) {
val models = modelSrv.list
migrationProcess = migrationPath(db)
Expand Down

0 comments on commit 72d4aa1

Please sign in to comment.