PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/scala/com/comcast/xfinity/sirius/api/impl/paxos/LeaderHelper.scala

https://github.com/edwardt/sirius
Scala | 61 lines | 17 code | 6 blank | 38 comment | 0 complexity | 9e8f642b6107e5a906f6922d1a23c976 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * Copyright 2012-2014 Comcast Cable Communications Management, LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.comcast.xfinity.sirius.api.impl.paxos
  17. import com.comcast.xfinity.sirius.api.impl.paxos.PaxosMessages._
  18. import collection.immutable.SortedMap
  19. import com.comcast.xfinity.sirius.util.RichJTreeMap
  20. /**
  21. * Some helpers for more complex operations on the Leader. Ideally this
  22. * stuff would be in a companion object, but because we want to mock it,
  23. * and Maven doesn't support ScalaMock (yet), we have this.
  24. */
  25. class LeaderHelper {
  26. /**
  27. * Overlays x with all entries from y, with the side effect of modifying x.
  28. *
  29. * Returns reference to x- this is an artifact of when this was immutable and more
  30. * complex, this method should get factored out soon.
  31. *
  32. * @param x a RichJTreeMap which is modified in place, being overlayed with all values
  33. * from y
  34. * @param y values to overlay on x
  35. * @return reference to x, which is mutated
  36. */
  37. def update[T](x: RichJTreeMap[Long, T], y: RichJTreeMap[Long, T]): RichJTreeMap[Long, T] = {
  38. x.putAll(y)
  39. x
  40. }
  41. /**
  42. * Takes a Set of PValues and produces a java TreeMap from slot (Long) to Command, retaining the Command
  43. * associated with the highest Ballot for each slot.
  44. *
  45. * @param pvals
  46. * @return
  47. */
  48. def pmax(pvals: Set[PValue]): RichJTreeMap[Long, Command] = {
  49. val maxPValBySlot = pvals.groupBy(_.slotNum).mapValues(
  50. pvals => pvals.maxBy(_.ballot)
  51. )
  52. val slotsToCommands = maxPValBySlot.mapValues(_.proposedCommand)
  53. RichJTreeMap(slotsToCommands)
  54. }
  55. }