Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support large schemas #182

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import treehugger.forest._
import definitions._
import treehuggerDSL._

import java.nio.charset.StandardCharsets
import scala.jdk.CollectionConverters._

// only companions, so no doc generation is required here
Expand Down Expand Up @@ -52,7 +53,10 @@ object SpecificObjectTree {
case None => OBJECTDEF(schema.getName)
}
val schemaDef = VAL(REF("SCHEMA$")) := {
(NEW(ParserClass)) APPLY(Nil) DOT "parse" APPLY(LIT(schema.toString))
NEW(ParserClass) APPLY (Nil) DOT "parse" APPLY (schema.toString match {
case schemaString if schemaString.getBytes(StandardCharsets.UTF_8).length > 65535 => LIST(schemaString.split("},\\{\"").map(LIT)) DOT "mkString" APPLY (LIT("},{\""))
case schemaString => LIT(schemaString)
})
}
val externalReader = VAL("READER$") := NEW("org.apache.avro.specific.SpecificDatumReader").APPLYTYPE(TYPE_REF(schema.getName)).APPLY(
REF(s"${schema.getFullName()}.SCHEMA$$")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ package specific

import avrohugger._
import avrohugger.format.SpecificRecord
import org.apache.avro.Schema
import org.specs2._

import java.io.File
import scala.io.Source
import scala.language.postfixOps

class SpecificStringToFileSpec extends Specification {

def is = s2"""
Expand All @@ -31,6 +36,8 @@ class SpecificStringToFileSpec extends Specification {


correctly generate a protocol with no ADT when asked $e21
correctly generate large string of embedded schema $e23

"""
// correctly generate logical types from IDL $e22
// """
Expand Down Expand Up @@ -249,4 +256,39 @@ class SpecificStringToFileSpec extends Specification {
// source === util.Util.readFile("avrohugger-core/src/test/expected/specific/example/idl/LogicalIdl.scala")
// }


def e23 = {

val min = 70000 * 2

val schema = {
val primitives = List("null", "boolean", "int", "long", "float", "double", "bytes", "string")
val fields = primitives.map(n => s"""{"name":"${n}Field","type":"$n"}""").mkString(",")
LazyList.from(1).scanLeft(s"""{"type":"record","name":"massive._0","fields":[$fields]}""") { case (field, i) =>
val fields = primitives.map(n => s"""{"name":"${n}Field","type":${field.replace("_", s"_$n")}}""")
s"""{"type":"record","name":"massive._$i","fields":[${fields.mkString(",")}]}"""
}.filter(_.length > min).map(new Schema.Parser().parse(_)).find(_.toString().length > min).get
}

val schemaString = schema.toString()
val gen = Generator(format = SpecificRecord)
val outDir = gen.defaultOutputDir + "/specific/"
val outDirF = new File(outDir, "massive")
outDirF.mkdirs()
outDirF.list().foreach(new File(outDirF, _).delete())
gen.stringToFile(schemaString, outDir)

val sources = outDirF.list().map(f => new File(outDirF, f)).map { f =>
f -> {
val src = Source.fromFile(f)
try {
src.mkString("")
} finally {
src.close()
}
}
}.toMap

sources.collect { case f -> source if source.contains("mkString") => f } must not beEmpty
}
}
Loading