From 1b354e2bd5ad93512a0cbb4d89a24a579a3d5595 Mon Sep 17 00:00:00 2001 From: Mahadev Konar Date: Fri, 16 Jul 2010 18:37:50 +0000 Subject: [PATCH] HADOOP-6834. TFile.append compares initial key against null lastKey (hong tang via mahadev) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@964897 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 + .../apache/hadoop/io/file/tfile/TFile.java | 2 +- .../io/file/tfile/TestTFileComparator2.java | 106 ++++++++++++++++++ 3 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 src/test/core/org/apache/hadoop/io/file/tfile/TestTFileComparator2.java diff --git a/CHANGES.txt b/CHANGES.txt index 1720df98ea..75b82178b3 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -127,6 +127,9 @@ Trunk (unreleased changes) HADOOP-6647. balancer fails with "is not authorized for protocol interface NamenodeProtocol" in secure environment (boryas) + HADOOP-6834. TFile.append compares initial key against null lastKey + (hong tang via mahadev) + Release 0.21.0 - Unreleased INCOMPATIBLE CHANGES diff --git a/src/java/org/apache/hadoop/io/file/tfile/TFile.java b/src/java/org/apache/hadoop/io/file/tfile/TFile.java index 5ad4db89eb..0b9ed9d2b3 100644 --- a/src/java/org/apache/hadoop/io/file/tfile/TFile.java +++ b/src/java/org/apache/hadoop/io/file/tfile/TFile.java @@ -452,7 +452,7 @@ public class TFile { tfileIndex.setFirstKey(key, 0, len); } - if (tfileMeta.isSorted()) { + if (tfileMeta.isSorted() && tfileMeta.getRecordCount()>0) { byte[] lastKey = lastKeyBufferOS.getBuffer(); int lastLen = lastKeyBufferOS.size(); if (tfileMeta.getComparator().compare(key, 0, len, lastKey, 0, diff --git a/src/test/core/org/apache/hadoop/io/file/tfile/TestTFileComparator2.java b/src/test/core/org/apache/hadoop/io/file/tfile/TestTFileComparator2.java new file mode 100644 index 0000000000..9e13c71403 --- /dev/null +++ b/src/test/core/org/apache/hadoop/io/file/tfile/TestTFileComparator2.java @@ -0,0 +1,106 @@ +/** + * 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.hadoop.io.file.tfile; + +import java.io.DataOutputStream; +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FSDataInputStream; +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.io.BytesWritable; +import org.apache.hadoop.io.LongWritable; +import org.apache.hadoop.io.file.tfile.TFile.Writer; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class TestTFileComparator2 { + private static final String ROOT = System.getProperty("test.build.data", + "/tmp/tfile-test"); + private static final String name = "test-tfile-comparator2"; + private final static int BLOCK_SIZE = 512; + private static final String VALUE = "value"; + private static final String jClassLongWritableComparator = "jclass:" + + LongWritable.Comparator.class.getName(); + private static final long NENTRY = 10000; + + private static long cube(long n) { + return n*n*n; + } + + private static String buildValue(long i) { + return String.format("%s-%d", VALUE, i); + } + + @Test + public void testSortedLongWritable() throws IOException { + Configuration conf = new Configuration(); + Path path = new Path(ROOT, name); + FileSystem fs = path.getFileSystem(conf); + FSDataOutputStream out = fs.create(path); + try { + TFile.Writer writer = new Writer(out, BLOCK_SIZE, "gz", + jClassLongWritableComparator, conf); + try { + LongWritable key = new LongWritable(0); + for (long i=0; i