Skip to content

Commit

Permalink
Clarify how writers flush. (#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbranco80 authored Jul 2, 2024
1 parent 3ccc2b4 commit 6bb3c87
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import org.graalvm.polyglot.Value

import java.io.{BufferedOutputStream, IOException, OutputStream}

class PolyglotBinaryWriter(os: OutputStream) {
final class PolyglotBinaryWriter(os: OutputStream) {

def writeValue(v: Value): Unit = {
def writeAndFlush(v: Value): Unit = {
if (v.isException) {
v.throwException()
} else if (v.isNull) {} else if (v.hasBufferElements) {
Expand Down
20 changes: 11 additions & 9 deletions client/src/main/scala/raw/client/writers/PolyglotCsvWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import java.time.format.DateTimeFormatter
import java.util.Base64
import scala.util.control.NonFatal

class PolyglotCsvWriter(os: OutputStream) extends Closeable {
final class PolyglotCsvWriter(os: OutputStream) extends Closeable {

private val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
private val zonedDateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-ddOOOO")
Expand All @@ -37,7 +37,7 @@ class PolyglotCsvWriter(os: OutputStream) extends Closeable {
factory.createGenerator(os, JsonEncoding.UTF8)
}

def writeValue(v: Value): Unit = {
def write(v: Value): Unit = {
if (v.isException) {
try {
v.throwException()
Expand Down Expand Up @@ -128,26 +128,26 @@ class PolyglotCsvWriter(os: OutputStream) extends Closeable {
gen.writeString(s.toString())
} else if (v.hasIterator) {
val v1 = v.getIterator
writeValue(v1)
write(v1)
} else if (v.isIterator) {
while (v.hasIteratorNextElement) {
val v1 = v.getIteratorNextElement
writeValue(v1)
write(v1)
}
if (v.canInvokeMember("close")) {
v.invokeMember("close")
}
} else if (v.hasArrayElements) {
for (i <- 0L until v.getArraySize) {
val v1 = v.getArrayElement(i)
writeValue(v1)
write(v1)
}
} else if (v.hasMembers) {
gen.writeStartObject()
v.getMemberKeys.forEach { key =>
gen.writeFieldName(key)
val value = v.getMember(key)
writeValue(value)
write(value)
}
gen.writeEndObject()
} else {
Expand All @@ -156,10 +156,12 @@ class PolyglotCsvWriter(os: OutputStream) extends Closeable {
}
}

def flush(): Unit = {
gen.flush()
}

override def close(): Unit = {
if (gen != null) {
gen.close()
}
gen.close()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Base64

class PolyglotJsonWriter(os: OutputStream) extends Closeable {
final class PolyglotJsonWriter(os: OutputStream) extends Closeable {

private val dateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
private val zonedDateFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-ddOOOO")
Expand Down Expand Up @@ -179,10 +179,12 @@ class PolyglotJsonWriter(os: OutputStream) extends Closeable {
}
}

def flush(): Unit = {
gen.flush()
}

override def close(): Unit = {
if (gen != null) {
gen.close()
}
gen.close()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@ import org.graalvm.polyglot.Value
import java.io.{IOException, OutputStream}
import java.nio.charset.Charset

class PolyglotTextWriter(os: OutputStream) {
final class PolyglotTextWriter(os: OutputStream) {

def writeValue(v: Value): Unit = {
def writeAndFlush(v: Value): Unit = {
if (v.isException) {
v.throwException()
} else if (v.isNull) {} else if (v.isString) {
val s = v.asString()
os.write(s.getBytes(Charset.forName("UTF-8")))
os.flush()
} else {
throw new IOException("unsupported type")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.graalvm.polyglot.Value
import raw.client.api._
import raw.client.utils.RecordFieldsNaming

import java.io.{IOException, OutputStream}
import java.io.{Closeable, IOException, OutputStream}
import java.time.format.DateTimeFormatter
import java.util.Base64
import scala.annotation.tailrec
Expand All @@ -35,7 +35,7 @@ object TypedPolyglotCsvWriter {

}

class TypedPolyglotCsvWriter(os: OutputStream, lineSeparator: String) {
class TypedPolyglotCsvWriter(os: OutputStream, lineSeparator: String) extends Closeable {

final private val gen =
try {
Expand Down Expand Up @@ -188,7 +188,11 @@ class TypedPolyglotCsvWriter(os: OutputStream, lineSeparator: String) {
}
}

def close(): Unit = {
def flush(): Unit = {
gen.flush()
}

override def close(): Unit = {
gen.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import org.graalvm.polyglot.Value
import raw.client.api._
import raw.client.utils.RecordFieldsNaming

import java.io.{IOException, OutputStream}
import java.io.{Closeable, IOException, OutputStream}
import java.time.format.DateTimeFormatter
import java.util.Base64
import scala.util.control.NonFatal
Expand All @@ -32,7 +32,7 @@ object TypedPolyglotJsonWriter {

}

class TypedPolyglotJsonWriter(os: OutputStream) {
class TypedPolyglotJsonWriter(os: OutputStream) extends Closeable {

final private val gen =
try {
Expand Down Expand Up @@ -165,7 +165,11 @@ class TypedPolyglotJsonWriter(os: OutputStream) {
}
}

def close(): Unit = {
def flush(): Unit = {
gen.flush()
}

override def close(): Unit = {
gen.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,21 @@ class PythonCompilerService(engineDefinition: (Engine, Boolean))(implicit protec
// case Some("true") => true
// case _ => programContext.settings.config.getBoolean("raw.compiler.windows-line-ending")
// }
val csvWriter = new PolyglotCsvWriter(outputStream)
val w = new PolyglotCsvWriter(outputStream)
try {
csvWriter.writeValue(v)
w.write(v)
w.flush()
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
} finally {
RawUtils.withSuppressNonFatalException(csvWriter.close())
RawUtils.withSuppressNonFatalException(w.close())
}
case Some("json") =>
val w = new PolyglotJsonWriter(outputStream)
try {
w.write(v)
w.flush()
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
Expand All @@ -157,15 +159,15 @@ class PythonCompilerService(engineDefinition: (Engine, Boolean))(implicit protec
case Some("text") =>
val w = new PolyglotTextWriter(outputStream)
try {
w.writeValue(v)
w.writeAndFlush(v)
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
}
case Some("binary") =>
val w = new PolyglotBinaryWriter(outputStream)
try {
w.writeValue(v)
w.writeAndFlush(v)
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ import com.fasterxml.jackson.dataformat.csv.{CsvFactory, CsvSchema}
import com.fasterxml.jackson.dataformat.csv.CsvGenerator.Feature.STRICT_CHECK_FOR_QUOTING
import raw.client.utils.RecordFieldsNaming

import java.io.IOException
import java.io.OutputStream
import java.io.{Closeable, IOException, OutputStream}
import java.time.format.DateTimeFormatter
import java.util.Base64
import scala.annotation.tailrec
import scala.util.control.NonFatal

class Rql2CsvWriter(os: OutputStream, lineSeparator: String) {
final class Rql2CsvWriter(os: OutputStream, lineSeparator: String) extends Closeable {

final private val gen =
try {
Expand Down Expand Up @@ -191,7 +190,12 @@ class Rql2CsvWriter(os: OutputStream, lineSeparator: String) {
}
}

def flush(): Unit = {
gen.flush()
}

def close(): Unit = {
gen.close()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ import raw.client.utils.RecordFieldsNaming
import raw.compiler.rql2.Rql2TypeUtils
import raw.compiler.rql2.source._

import java.io.{IOException, OutputStream}
import java.io.{Closeable, IOException, OutputStream}
import java.time.format.DateTimeFormatter
import java.util.Base64
import scala.util.control.NonFatal

class Rql2JsonWriter(os: OutputStream) {
final class Rql2JsonWriter(os: OutputStream) extends Closeable {

final private val gen =
try {
Expand Down Expand Up @@ -145,7 +145,11 @@ class Rql2JsonWriter(os: OutputStream) {
}
}

def close(): Unit = {
def flush(): Unit = {
gen.flush()
}

override def close(): Unit = {
gen.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,15 @@ class Rql2TruffleCompilerService(engineDefinition: (Engine, Boolean))(implicit p
case _ => programContext.settings.config.getBoolean("raw.compiler.windows-line-ending")
}
val lineSeparator = if (windowsLineEnding) "\r\n" else "\n"
val csvWriter = new Rql2CsvWriter(outputStream, lineSeparator)
val w = new Rql2CsvWriter(outputStream, lineSeparator)
try {
csvWriter.write(v, tipe.asInstanceOf[Rql2TypeWithProperties])
w.write(v, tipe.asInstanceOf[Rql2TypeWithProperties])
w.flush()
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
} finally {
RawUtils.withSuppressNonFatalException(csvWriter.close())
RawUtils.withSuppressNonFatalException(w.close())
}
case Some("json") =>
if (!JsonPackage.outputWriteSupport(tipe)) {
Expand All @@ -295,6 +296,7 @@ class Rql2TruffleCompilerService(engineDefinition: (Engine, Boolean))(implicit p
val w = new Rql2JsonWriter(outputStream)
try {
w.write(v, tipe.asInstanceOf[Rql2TypeWithProperties])
w.flush()
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
Expand All @@ -307,7 +309,7 @@ class Rql2TruffleCompilerService(engineDefinition: (Engine, Boolean))(implicit p
}
val w = new PolyglotTextWriter(outputStream)
try {
w.writeValue(v)
w.writeAndFlush(v)
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
Expand All @@ -318,7 +320,7 @@ class Rql2TruffleCompilerService(engineDefinition: (Engine, Boolean))(implicit p
}
val w = new PolyglotBinaryWriter(outputStream)
try {
w.writeValue(v)
w.writeAndFlush(v)
ExecutionSuccess
} catch {
case ex: IOException => ExecutionRuntimeFailure(ex.getMessage)
Expand Down

0 comments on commit 6bb3c87

Please sign in to comment.