diff options
author | vlad <vlad@driver.xyz> | 2018-01-25 14:12:31 -0800 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2018-01-25 14:12:31 -0800 |
commit | a0877d81ca2844d75dc361b5ce7c99afacd6e25f (patch) | |
tree | 8fe49f45cbcddbbb9a3d167099abe7aa2625e56b /src/main/scala/xyz/driver/pdsuicommon/synchronization/db/SlickDbDiff.scala | |
parent | 46a22e9ab324a0068a85952cdc809800f360f445 (diff) | |
download | rest-query-a0877d81ca2844d75dc361b5ce7c99afacd6e25f.tar.gz rest-query-a0877d81ca2844d75dc361b5ce7c99afacd6e25f.tar.bz2 rest-query-a0877d81ca2844d75dc361b5ce7c99afacd6e25f.zip |
Extracting query library
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/synchronization/db/SlickDbDiff.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuicommon/synchronization/db/SlickDbDiff.scala | 52 |
1 files changed, 0 insertions, 52 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/synchronization/db/SlickDbDiff.scala b/src/main/scala/xyz/driver/pdsuicommon/synchronization/db/SlickDbDiff.scala deleted file mode 100644 index c226659..0000000 --- a/src/main/scala/xyz/driver/pdsuicommon/synchronization/db/SlickDbDiff.scala +++ /dev/null @@ -1,52 +0,0 @@ -package xyz.driver.pdsuicommon.synchronization.db - -import xyz.driver.pdsuicommon.synchronization.domain.FakeId -import xyz.driver.pdsuicommon.synchronization.utils.{FakeIdGen, Refiner} - -import scala.annotation.tailrec -import scala.collection.breakOut -import scala.collection.immutable.SortedSet - -object SlickDbDiff { - - /** - * Calculates DB-actions to synchronize origEntities with draftEntities. - */ - def calc[DraftT, OrigT](origEntities: Iterable[OrigT], draftEntities: Iterable[DraftT])( - implicit draftFakeIdGen: FakeIdGen[DraftT], - origFakeIdGen: FakeIdGen[OrigT], - refiner: Refiner[DraftT, OrigT]): List[SlickDbAction[OrigT]] = { - val origMap: Map[FakeId, OrigT] = origEntities.map(x => origFakeIdGen(x) -> x)(breakOut) - val uniqueDraftEntities = SortedSet.newBuilder[DraftT](Ordering.by[DraftT, FakeId](draftFakeIdGen)) - uniqueDraftEntities ++= draftEntities - - loop(origMap, uniqueDraftEntities.result(), List.empty) - } - - @tailrec private def loop[DraftT, OrigT](origEntitiesMap: Map[FakeId, OrigT], - draftEntities: Iterable[DraftT], - actions: List[SlickDbAction[OrigT]])( - implicit draftFakeIdGen: FakeIdGen[DraftT], - refiner: Refiner[DraftT, OrigT]): List[SlickDbAction[OrigT]] = { - draftEntities.headOption match { - case None => - // The rest original entities are not a part of draft, so we will delete them - val toDelete: List[SlickDbAction[OrigT]] = origEntitiesMap.values.map(x => SlickDbAction.Delete(x))(breakOut) - actions ++ toDelete - - case Some(currRaw) => - val rawCore = draftFakeIdGen.getFor(currRaw) - val action: Option[SlickDbAction[OrigT]] = origEntitiesMap.get(rawCore) match { - // It is a new entity, because it doesn't exist among originals - case None => Some(SlickDbAction.Create(refiner.refine(currRaw))) - case Some(orig) => - val draft = refiner.refresh(orig, currRaw) - if (draft == orig) None - else Some(SlickDbAction.Update(draft)) - } - - loop(origEntitiesMap - rawCore, draftEntities.tail, action.map(_ :: actions).getOrElse(actions)) - } - } - -} |