Skip to content

Commit

Permalink
more SliceSpec tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simerplaha committed Oct 19, 2018
1 parent 6017390 commit 19780c4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Expand Up @@ -82,7 +82,7 @@ private[merge] object SegmentGrouper extends LazyLogging {
if (shouldGroupGroups(segmentKeyValues = keyValues, groupingStrategy = groupingStrategy, force = force)) {
//use segmentKeyValues.last.stats.position instead of keyValues.size because position is pre-calculated.
val keyValuesToGroup = Slice.create[KeyValue.WriteOnly](keyValues.last.stats.position)
//do not need to recalculate stats since all key-values are being groups.
//do not need to recalculate stats since all key-values are being grouped.
// keyValues foreach (keyValuesToGroup add _.updateStats(bloomFilterFalsePositiveRate, keyValuesToGroup.lastOption))
keyValues foreach (keyValuesToGroup add _)
Some(keyValuesToGroup)
Expand All @@ -99,7 +99,7 @@ private[merge] object SegmentGrouper extends LazyLogging {
private def keyValuesToGroup(segmentKeyValues: Iterable[KeyValue.WriteOnly],
bloomFilterFalsePositiveRate: Double,
groupingStrategy: KeyValueGroupingStrategyInternal,
force: Boolean): Try[Option[(Iterable[KeyValue.WriteOnly], Option[Transient.Group])]] =
force: Boolean): Try[Option[(Slice[KeyValue.WriteOnly], Option[Transient.Group])]] =
if (shouldGroupKeyValues(segmentKeyValues = segmentKeyValues, groupingStrategy = groupingStrategy, force = force)) {
//create a new list of key-values with stats updated.
val expectedGroupsKeyValueCount = segmentKeyValues.last.stats.position - segmentKeyValues.last.stats.groupsCount
Expand Down
6 changes: 3 additions & 3 deletions data/src/main/scala/swaydb/data/slice/Slice.scala
Expand Up @@ -246,7 +246,7 @@ object Slice {

class SliceBuilder[T: ClassTag](sizeHint: Int) extends mutable.Builder[T, Slice[T]] {
//max is used to in-case sizeHit == 0 which is possible for cases where (None ++ Some(Slice[T](...)))
protected var slice: Slice[T] = Slice.create[T](((sizeHint max 16) * 2.5).toInt)
protected var slice: Slice[T] = Slice.create[T](((sizeHint max 100) * 2.5).toInt)

def extendSlice(by: Int) = {
val extendedSlice = Slice.create[T](slice.size * by)
Expand Down Expand Up @@ -277,7 +277,7 @@ object Slice {
implicit def canBuildFrom[T: ClassTag]: CanBuildFrom[Slice[_], T, Slice[T]] =
new CanBuildFrom[Slice[_], T, Slice[T]] {
def apply(from: Slice[_]) =
new SliceBuilder[T](from.size max 16) //max is used in-case from.size == 0
new SliceBuilder[T](from.size max 100) //max is used in-case from.size == 0

def apply() =
new SliceBuilder[T](100)
Expand Down Expand Up @@ -518,7 +518,7 @@ class Slice[+T: ClassTag](array: Array[T],
array.length

override protected[this] def newBuilder: scala.collection.mutable.Builder[T, Slice[T]] =
new Slice.SliceBuilder[T](100)
new Slice.SliceBuilder[T](array.length max 100)

override def equals(that: Any): Boolean =
that match {
Expand Down
31 changes: 31 additions & 0 deletions data/src/test/scala/swaydb/data/slice/SliceSpec.scala
Expand Up @@ -401,4 +401,35 @@ class SliceSpec extends WordSpec with Matchers {
.addAll(Slice(3, 4))
}
}

"None ++ Some(Slice[T](...))" in {
val merged: Iterable[Slice[Int]] = Some(Slice[Int](1, 2, 3)) ++ None
merged.flatten
.toList should contain inOrderOnly(1, 2, 3)
}

"++ empty slices" in {
val merged: Slice[Int] = Slice.empty[Int] ++ Slice.empty[Int]
merged shouldBe empty
merged.written shouldBe 0
merged.isEmpty shouldBe true
merged.isFull shouldBe true
}

"++ empty and non empty slices" in {
val merged: Slice[Int] = Slice.empty[Int] ++ Slice(1)
merged should contain only 1
merged.written shouldBe 1
merged.isEmpty shouldBe false
merged.isFull shouldBe true
}

"++ non empty and empty slices" in {
val merged: Slice[Int] = Slice(1) ++ Slice.empty[Int]
merged should contain only 1
merged.written shouldBe 1
merged.isEmpty shouldBe false
merged.isFull shouldBe true
}

}

0 comments on commit 19780c4

Please sign in to comment.