Skip to content
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
6 changes: 6 additions & 0 deletions api/src/main/java/org/apache/iceberg/FileContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public enum FileContent {
DATA_MANIFEST(3),
DELETE_MANIFEST(4);

private static final FileContent[] VALUES = FileContent.values();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a nice trick


private final int id;

FileContent(int id) {
Expand All @@ -35,4 +37,8 @@ public enum FileContent {
public int id() {
return id;
}

public static FileContent fromId(int id) {
return VALUES[id];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we should throw an IllegalArg here, instead of letting it have an ArrayOutOfBound, thoughts ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that I agree with this. The reason to use a values array is to have a quick lookup, since this is resolved in a tight loop. Having an extra check basically to throw the exception as a different class doesn't seem worth it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Reverted.

}
}
48 changes: 48 additions & 0 deletions api/src/test/java/org/apache/iceberg/TestFileContent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.iceberg;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.util.stream.IntStream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;

class TestFileContent {

@ParameterizedTest
@EnumSource(FileContent.class)
void fromId(FileContent content) {
assertThat(FileContent.fromId(content.id())).isEqualTo(content);
}

static IntStream invalidContentTypeIds() {
return IntStream.of(-1, FileContent.values().length);
}

@ParameterizedTest
@MethodSource("invalidContentTypeIds")
void fromIdInvalid(int id) {
assertThatThrownBy(() -> FileContent.fromId(id))
.isInstanceOf(ArrayIndexOutOfBoundsException.class)
.hasMessageContaining(String.valueOf(id));
}
}
4 changes: 2 additions & 2 deletions core/src/main/java/org/apache/iceberg/BaseFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ abstract class BaseFile<F> extends SupportsIndexProjection
StructLike,
SpecificData.SchemaConstructable,
Serializable {
private static final FileContent[] FILE_CONTENT_VALUES = FileContent.values();

static final Types.StructType EMPTY_STRUCT_TYPE = Types.StructType.of();
static final PartitionData EMPTY_PARTITION_DATA =
new PartitionData(EMPTY_STRUCT_TYPE) {
Expand Down Expand Up @@ -316,7 +316,7 @@ public void put(int i, Object value) {
protected <T> void internalSet(int pos, T value) {
switch (pos) {
case 0:
this.content = value != null ? FILE_CONTENT_VALUES[(Integer) value] : FileContent.DATA;
this.content = value != null ? FileContent.fromId((Integer) value) : FileContent.DATA;
return;
case 1:
// always coerce to String for Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

public abstract class SparkContentFile<F> implements ContentFile<F> {

private static final FileContent[] FILE_CONTENT_VALUES = FileContent.values();

private final int fileContentPosition;
private final int filePathPosition;
private final int fileFormatPosition;
Expand Down Expand Up @@ -139,7 +137,7 @@ public FileContent content() {
if (wrapped.isNullAt(fileContentPosition)) {
return null;
}
return FILE_CONTENT_VALUES[wrapped.getInt(fileContentPosition)];
return FileContent.fromId(wrapped.getInt(fileContentPosition));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

public abstract class SparkContentFile<F> implements ContentFile<F> {

private static final FileContent[] FILE_CONTENT_VALUES = FileContent.values();

private final int fileContentPosition;
private final int filePathPosition;
private final int fileFormatPosition;
Expand Down Expand Up @@ -139,7 +137,7 @@ public FileContent content() {
if (wrapped.isNullAt(fileContentPosition)) {
return null;
}
return FILE_CONTENT_VALUES[wrapped.getInt(fileContentPosition)];
return FileContent.fromId(wrapped.getInt(fileContentPosition));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

public abstract class SparkContentFile<F> implements ContentFile<F> {

private static final FileContent[] FILE_CONTENT_VALUES = FileContent.values();

private final int fileContentPosition;
private final int filePathPosition;
private final int fileFormatPosition;
Expand Down Expand Up @@ -139,7 +137,7 @@ public FileContent content() {
if (wrapped.isNullAt(fileContentPosition)) {
return null;
}
return FILE_CONTENT_VALUES[wrapped.getInt(fileContentPosition)];
return FileContent.fromId(wrapped.getInt(fileContentPosition));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

public abstract class SparkContentFile<F> implements ContentFile<F> {

private static final FileContent[] FILE_CONTENT_VALUES = FileContent.values();

private final int fileContentPosition;
private final int filePathPosition;
private final int fileFormatPosition;
Expand Down Expand Up @@ -139,7 +137,7 @@ public FileContent content() {
if (wrapped.isNullAt(fileContentPosition)) {
return null;
}
return FILE_CONTENT_VALUES[wrapped.getInt(fileContentPosition)];
return FileContent.fromId(wrapped.getInt(fileContentPosition));
}

@Override
Expand Down
Loading