Skip to content

[SPARK-51867][ML][FOLLOW-UP] Make funtions in ReadWriteUtils private #50811

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 15 additions & 9 deletions mllib/src/main/scala/org/apache/spark/ml/util/ReadWrite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,11 @@ private[spark] class FileSystemOverwrite extends Logging {

private[spark] object ReadWriteUtils {

val localSavingModeState = new ThreadLocal[Boolean]() {
private[spark] val localSavingModeState = new ThreadLocal[Boolean]() {
override def initialValue: Boolean = false
}

def saveText(path: String, data: String, spark: SparkSession): Unit = {
private[spark] def saveText(path: String, data: String, spark: SparkSession): Unit = {
if (localSavingModeState.get()) {
val filePath = Paths.get(path)

Expand All @@ -836,15 +836,17 @@ private[spark] object ReadWriteUtils {
}
}

def loadText(path: String, spark: SparkSession): String = {
private[spark] def loadText(path: String, spark: SparkSession): String = {
if (localSavingModeState.get()) {
Files.readString(Paths.get(path))
} else {
spark.read.text(path).first().getString(0)
}
}

def saveObjectToLocal[T <: Product: ClassTag: TypeTag](path: String, data: T): Unit = {
private[spark] def saveObjectToLocal[T <: Product: ClassTag: TypeTag](
path: String, data: T
): Unit = {
val serializer = SparkEnv.get.serializer.newInstance()
val dataBuffer = serializer.serialize(data)
val dataBytes = new Array[Byte](dataBuffer.limit)
Expand All @@ -856,7 +858,7 @@ private[spark] object ReadWriteUtils {
Files.write(filePath, dataBytes)
}

def saveObject[T <: Product: ClassTag: TypeTag](
private[spark] def saveObject[T <: Product: ClassTag: TypeTag](
path: String, data: T, spark: SparkSession
): Unit = {
if (localSavingModeState.get()) {
Expand All @@ -866,14 +868,16 @@ private[spark] object ReadWriteUtils {
}
}

def loadObjectFromLocal[T <: Product: ClassTag: TypeTag](path: String): T = {
private[spark] def loadObjectFromLocal[T <: Product: ClassTag: TypeTag](path: String): T = {
val serializer = SparkEnv.get.serializer.newInstance()

val dataBytes = Files.readAllBytes(Paths.get(path))
serializer.deserialize[T](java.nio.ByteBuffer.wrap(dataBytes))
}

def loadObject[T <: Product: ClassTag: TypeTag](path: String, spark: SparkSession): T = {
private[spark] def loadObject[T <: Product: ClassTag: TypeTag](
path: String, spark: SparkSession
): T = {
if (localSavingModeState.get()) {
loadObjectFromLocal(path)
} else {
Expand All @@ -882,7 +886,7 @@ private[spark] object ReadWriteUtils {
}
}

def saveArray[T <: Product: ClassTag: TypeTag](
private[spark] def saveArray[T <: Product: ClassTag: TypeTag](
path: String, data: Array[T], spark: SparkSession,
numDataParts: Int = -1
): Unit = {
Expand All @@ -907,7 +911,9 @@ private[spark] object ReadWriteUtils {
}
}

def loadArray[T <: Product: ClassTag: TypeTag](path: String, spark: SparkSession): Array[T] = {
private[spark] def loadArray[T <: Product: ClassTag: TypeTag](
path: String, spark: SparkSession
): Array[T] = {
if (localSavingModeState.get()) {
val serializer = SparkEnv.get.serializer.newInstance()

Expand Down