diff --git a/hdfs/CHANGES.txt b/hdfs/CHANGES.txt index d2ece0c508..8855c3bded 100644 --- a/hdfs/CHANGES.txt +++ b/hdfs/CHANGES.txt @@ -290,6 +290,8 @@ Trunk (unreleased changes) HDFS-2058. Change Data Transfer wire protocol to use protocol buffers. (todd) + HDFS-2055. Add hflush support to libhdfs. (Travis Crawford via eli) + IMPROVEMENTS HDFS-1875. MiniDFSCluster hard-codes dfs.datanode.address to localhost diff --git a/hdfs/src/c++/libhdfs/hdfs.c b/hdfs/src/c++/libhdfs/hdfs.c index a1fea3c72a..d970cda7e1 100644 --- a/hdfs/src/c++/libhdfs/hdfs.c +++ b/hdfs/src/c++/libhdfs/hdfs.c @@ -1006,6 +1006,38 @@ int hdfsFlush(hdfsFS fs, hdfsFile f) +int hdfsHFlush(hdfsFS fs, hdfsFile f) +{ + //Get the JNIEnv* corresponding to current thread + JNIEnv* env = getJNIEnv(); + if (env == NULL) { + errno = EINTERNAL; + return -1; + } + + //Parameters + jobject jOutputStream = (jobject)(f ? f->file : 0); + + //Caught exception + jthrowable jExc = NULL; + + //Sanity check + if (!f || f->type != OUTPUT) { + errno = EBADF; + return -1; + } + + if (invokeMethod(env, NULL, &jExc, INSTANCE, jOutputStream, + HADOOP_OSTRM, "hflush", "()V") != 0) { + errno = errnoFromException(jExc, env, HADOOP_OSTRM "::hflush"); + return -1; + } + + return 0; +} + + + int hdfsAvailable(hdfsFS fs, hdfsFile f) { // JAVA EQUIVALENT diff --git a/hdfs/src/c++/libhdfs/hdfs.h b/hdfs/src/c++/libhdfs/hdfs.h index 1dcd9a89c7..0ee29d50ad 100644 --- a/hdfs/src/c++/libhdfs/hdfs.h +++ b/hdfs/src/c++/libhdfs/hdfs.h @@ -239,6 +239,16 @@ extern "C" { int hdfsFlush(hdfsFS fs, hdfsFile file); + /** + * hdfsHFlush - Flush out the data in client's user buffer. After the + * return of this call, new readers will see the data. + * @param fs configured filesystem handle + * @param file file handle + * @return 0 on success, -1 on error and sets errno + */ + int hdfsHFlush(hdfsFS fs, hdfsFile file); + + /** * hdfsAvailable - Number of bytes that can be read from this * input stream without blocking. diff --git a/hdfs/src/c++/libhdfs/hdfs_test.c b/hdfs/src/c++/libhdfs/hdfs_test.c index fd6b8f8499..2e6545de5a 100644 --- a/hdfs/src/c++/libhdfs/hdfs_test.c +++ b/hdfs/src/c++/libhdfs/hdfs_test.c @@ -95,6 +95,12 @@ int main(int argc, char **argv) { } fprintf(stderr, "Flushed %s successfully!\n", writePath); + if (hdfsHFlush(fs, writeFile)) { + fprintf(stderr, "Failed to 'hflush' %s\n", writePath); + exit(-1); + } + fprintf(stderr, "HFlushed %s successfully!\n", writePath); + hdfsCloseFile(fs, writeFile); }