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

Introduce kudo writer. #2559

Merged
merged 10 commits into from
Nov 6, 2024
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
16 changes: 15 additions & 1 deletion src/main/java/com/nvidia/spark/rapids/jni/Preconditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
/**
* This class contains utility methods for checking preconditions.
*/
class Preconditions {
public class Preconditions {
/**
* Check if the condition is true, otherwise throw an IllegalStateException with the given message.
*/
Expand All @@ -39,4 +39,18 @@ public static void ensure(boolean condition, Supplier<String> messageSupplier) {
throw new IllegalStateException(messageSupplier.get());
}
}

/**
* Check if the value is non-negative, otherwise throw an IllegalArgumentException with the given message.
* @param value the value to check
* @param name the name of the value
* @return the value if it is non-negative
* @throws IllegalArgumentException if the value is negative
*/
public static int ensureNonNegative(int value, String name) {
if (value < 0) {
throw new IllegalArgumentException(name + " must be non-negative, but was " + value);
}
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.nvidia.spark.rapids.jni.kudo;

import ai.rapids.cudf.HostMemoryBuffer;

import java.io.DataOutputStream;
import java.io.IOException;

/**
* Visible for testing
*/
class DataOutputStreamWriter extends DataWriter {
private final byte[] arrayBuffer = new byte[1024 * 128];
private final DataOutputStream dout;

public DataOutputStreamWriter(DataOutputStream dout) {
this.dout = dout;
}

@Override
public void writeInt(int i) throws IOException {
dout.writeInt(i);
}

@Override
public void copyDataFrom(HostMemoryBuffer src, long srcOffset, long len) throws IOException {
long dataLeft = len;
while (dataLeft > 0) {
int amountToCopy = (int) Math.min(arrayBuffer.length, dataLeft);
src.getBytes(arrayBuffer, 0, srcOffset, amountToCopy);
dout.write(arrayBuffer, 0, amountToCopy);
srcOffset += amountToCopy;
dataLeft -= amountToCopy;
}
}

@Override
public void flush() throws IOException {
dout.flush();
}

@Override
public void write(byte[] arr, int offset, int length) throws IOException {
dout.write(arr, offset, length);
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/nvidia/spark/rapids/jni/kudo/DataWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.nvidia.spark.rapids.jni.kudo;

import ai.rapids.cudf.HostMemoryBuffer;

import java.io.IOException;

/**
* Visible for testing
*/
abstract class DataWriter {

public abstract void writeInt(int i) throws IOException;

/**
* Copy data from src starting at srcOffset and going for len bytes.
*
* @param src where to copy from.
* @param srcOffset offset to start at.
* @param len amount to copy.
*/
public abstract void copyDataFrom(HostMemoryBuffer src, long srcOffset, long len) throws IOException;

public void flush() throws IOException {
// NOOP by default
}

public abstract void write(byte[] arr, int offset, int length) throws IOException;
}
Loading
Loading