-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
501 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
schema/src/main/scala/org/constellation/schema/snapshot/FilterData.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.constellation.schema.snapshot | ||
|
||
import io.circe.{Decoder, Encoder} | ||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} | ||
|
||
case class FilterData( | ||
contents: Array[Byte], | ||
capacity: Long | ||
) | ||
|
||
object FilterData { | ||
implicit val encoder: Encoder[FilterData] = deriveEncoder | ||
implicit val decoder: Decoder[FilterData] = deriveDecoder | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
schema/src/main/scala/org/constellation/schema/snapshot/MajorityInfo.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/scala/org/constellation/concurrency/cuckoo/CuckooFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.constellation.concurrency.cuckoo | ||
|
||
import java.io.ByteArrayInputStream | ||
|
||
import net.cinnom.nanocuckoo.NanoCuckooFilter | ||
import org.constellation.schema.snapshot.FilterData | ||
|
||
case class CuckooFilter(filterData: FilterData) { | ||
|
||
private lazy val filter: NanoCuckooFilter = { | ||
val stream = new ByteArrayInputStream(filterData.contents) | ||
try { | ||
val resultingFilter = new NanoCuckooFilter.Builder(filterData.capacity).build() | ||
resultingFilter.readMemory(stream) | ||
resultingFilter | ||
} finally stream.close() | ||
} | ||
|
||
def contains[T](item: T)(implicit convert: T => String): Boolean = convert.andThen(filter.contains)(item) | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
src/main/scala/org/constellation/concurrency/cuckoo/MutableCuckooFilter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.constellation.concurrency.cuckoo | ||
|
||
import java.io.{ByteArrayInputStream, ByteArrayOutputStream} | ||
|
||
import cats.effect.Sync | ||
import net.cinnom.nanocuckoo.NanoCuckooFilter | ||
import org.constellation.schema.snapshot.FilterData | ||
import cats.syntax.all._ | ||
|
||
class MutableCuckooFilter[F[_], T](filter: NanoCuckooFilter)(implicit F: Sync[F]) { | ||
|
||
def insert(item: T)(implicit convert: T => String): F[Boolean] = { | ||
val converted = convert(item) | ||
for { | ||
insertResult <- tryInsert(converted) | ||
finalResult <- if (insertResult) F.pure(true) | ||
else expand >> tryInsert(converted) | ||
|
||
} yield finalResult | ||
} | ||
|
||
private def tryInsert(convertedItem: String): F[Boolean] = F.delay { | ||
filter.insert(convertedItem) | ||
} | ||
|
||
private def expand: F[Unit] = F.delay { | ||
filter.expand() | ||
} | ||
|
||
def delete(item: T)(implicit convert: T => String): F[Boolean] = F.delay { | ||
filter.delete(convert(item)) | ||
} | ||
|
||
def contains(item: T)(implicit convert: T => String): F[Boolean] = F.delay { | ||
filter.contains(convert(item)) | ||
} | ||
|
||
def getFilterData: F[FilterData] = | ||
for { | ||
capacity <- getCapacity | ||
contents <- getFilterContents | ||
} yield FilterData(contents, capacity) | ||
|
||
def clear: F[Unit] = | ||
for { | ||
capacity <- getCapacity | ||
contents <- MutableCuckooFilter(capacity).getFilterContents | ||
_ <- setFilterContents(contents) | ||
} yield () | ||
|
||
private def getCapacity: F[Long] = F.delay { | ||
filter.getCapacity | ||
} | ||
|
||
private def getFilterContents: F[Array[Byte]] = F.delay { | ||
val stream = new ByteArrayOutputStream() | ||
try filter.writeMemory(stream) | ||
finally stream.close() | ||
stream.toByteArray | ||
} | ||
|
||
private def setFilterContents(contents: Array[Byte]): F[Unit] = F.delay { | ||
val stream = new ByteArrayInputStream(contents) | ||
try filter.readMemory(stream) | ||
finally stream.close() | ||
} | ||
|
||
} | ||
|
||
object MutableCuckooFilter { | ||
|
||
def apply[F[_]: Sync, T](capacity: Long = 512): MutableCuckooFilter[F, T] = | ||
new MutableCuckooFilter[F, T](new NanoCuckooFilter.Builder(capacity).build()) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.