Skip to content

Commit

Permalink
deduplication, opts
Browse files Browse the repository at this point in the history
  • Loading branch information
pshirshov committed Sep 9, 2024
1 parent a0150ef commit 7fea2e7
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 171 deletions.
6 changes: 3 additions & 3 deletions json-sick-scala/src/main/scala/izumi/sick/SICK.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package izumi.sick

import io.circe.Json
import izumi.sick.SICK.EBA
import izumi.sick.indexes.{IndexRO, IndexRW, SICKSettings}
import izumi.sick.indexes.{EBAStructure, EBABuilder, SICKSettings}
import izumi.sick.model.Ref

trait SICK {
def pack(json: Json, name: String, dedup: Boolean, settings: SICKSettings = SICKSettings.default): EBA = {
import izumi.sick.sickcirce.CirceTraverser.*
val rwIndex = IndexRW(dedup = dedup)
val rwIndex = EBABuilder(dedup = dedup)
val root = rwIndex.append(name, json)
EBA(rwIndex.freeze(settings), root, rwIndex)
}
}

object SICK {
case class EBA(index: IndexRO, root: Ref, source: IndexRW)
case class EBA(index: EBAStructure, root: Ref, source: EBABuilder)
object Default extends SICK {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import izumi.sick.model
import izumi.sick.model.*
import izumi.sick.tables.{DeduplicatingRefTableBuilder, GenericRefTableBuilder}

object IndexRW {
def apply(dedup: Boolean): IndexRW = {
object EBABuilder {
def apply(dedup: Boolean): EBABuilder = {
val strings = GenericRefTableBuilder[String]("Strings", dedup = true)

val ints = GenericRefTableBuilder[Int]("Integers", dedup = true)
Expand All @@ -20,7 +20,7 @@ object IndexRW {
val objs = GenericRefTableBuilder[Obj]("Objects", dedup)
val roots = GenericRefTableBuilder[Root]("Roots", dedup)

new IndexRW(
new EBABuilder(
strings,
ints,
longs,
Expand All @@ -34,7 +34,7 @@ object IndexRW {
)
}
}
class IndexRW private (
class EBABuilder private(
strings: GenericRefTableBuilder[String],
ints: GenericRefTableBuilder[Int],
longs: GenericRefTableBuilder[Long],
Expand All @@ -47,8 +47,8 @@ class IndexRW private (
roots: GenericRefTableBuilder[Root],
) {

def freeze(settings: SICKSettings): IndexRO = {
new IndexRO(
def freeze(settings: SICKSettings): EBAStructure = {
new EBAStructure(
settings,
ints.freeze(),
longs.freeze(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package izumi.sick.indexes

import izumi.sick.indexes.IndexRO.Packed
import izumi.sick.model.{Arr, Obj, Root, ToBytes, ToBytesTable}
import izumi.sick.tables.RefTableRO
import izumi.sick.indexes.EBAStructure.Packed
import izumi.sick.model.{Arr, Obj, Root, SICKWriterParameters, ToBytes, ToBytesTable}
import izumi.sick.tables.EBATable
import izumi.sick.thirdparty.akka.util.ByteString

import java.io.FileOutputStream
import java.nio.file.{Files, Path}
import scala.collection.mutable

final class IndexRO(
final class EBAStructure(
val settings: SICKSettings,
val ints: RefTableRO[Int],
val longs: RefTableRO[Long],
val bigints: RefTableRO[BigInt],
val floats: RefTableRO[Float],
val doubles: RefTableRO[Double],
val bigDecimals: RefTableRO[BigDecimal],
val strings: RefTableRO[String],
val arrs: RefTableRO[Arr],
val objs: RefTableRO[Obj],
val roots: RefTableRO[Root],
val ints: EBATable[Int],
val longs: EBATable[Long],
val bigints: EBATable[BigInt],
val floats: EBATable[Float],
val doubles: EBATable[Double],
val bigDecimals: EBATable[BigDecimal],
val strings: EBATable[String],
val arrs: EBATable[Arr],
val objs: EBATable[Obj],
val roots: EBATable[Root],
) {
def findRoot(str: String): Option[Root] = {
roots.asIterable.find(r => strings(r.id) == str)
Expand All @@ -34,12 +34,12 @@ final class IndexRO(
parts.map(_._1).filterNot(_.isEmpty).mkString("\n\n")
}

def packFile(): Packed = {
def packFile(params: SICKWriterParameters): Packed = {
val f = Files.createTempFile("sick", "bin")
packFile(f)
packFile(f, params)
}

def packFile(f: Path): Packed = {
def packFile(f: Path, params: SICKWriterParameters): Packed = {
val out = new FileOutputStream(f.toFile, false)

try {
Expand All @@ -58,7 +58,7 @@ final class IndexRO(
val sizes = mutable.ArrayBuffer.empty[Int]
parts.foreach {
case (p, codec) =>
val len = codec.asInstanceOf[ToBytesTable[Any]].write(out, p)
val len = codec.asInstanceOf[ToBytesTable[Any]].write(out, p, params)
sizes.append(len.intValue)
}

Expand All @@ -76,7 +76,7 @@ final class IndexRO(
}
}

def parts: Seq[(RefTableRO[Any], ToBytesTable[Seq[Any]])] = {
def parts: Seq[(EBATable[Any], ToBytesTable[Seq[Any]])] = {
import izumi.sick.model.ToBytes.*
Seq(
(ints, implicitly[ToBytesTable[Int]]),
Expand All @@ -89,17 +89,17 @@ final class IndexRO(
(arrs, implicitly[ToBytesTable[Arr]]),
(objs, toBytesFixedSizeArray(new ObjToBytes(strings, settings))),
(roots, implicitly[ToBytesTable[Root]]),
).map { case (c, codec) => (c.asInstanceOf[RefTableRO[Any]], codec.asInstanceOf[ToBytesTable[Seq[Any]]]) }
).map { case (c, codec) => (c.asInstanceOf[EBATable[Any]], codec.asInstanceOf[ToBytesTable[Seq[Any]]]) }
}
}

object IndexRO {
object EBAStructure {
final case class Packed(version: Int, headerLen: Int, offsets: Seq[Int], length: Long, data: Path)
}

final case class SICKSettings(
objectIndexBucketCount: Short,
minObjectKeysBeforeIndexing: Short,
objectIndexBucketCount: Short,
minObjectKeysBeforeIndexing: Short,
)

object SICKSettings {
Expand Down
Loading

0 comments on commit 7fea2e7

Please sign in to comment.