Class: Hadoop::DFS::File

Inherits:
Object
  • Object
show all
Defined in:
ext/hdfs/hdfs.c

Instance Method Summary collapse

Instance Method Details

#write(bytes) ⇒ Object

Writes the string specified by bytes to the current file object, returning the number of bytes written as an Integer. If this fails, raises a FileError.



800
801
802
803
804
805
806
807
808
809
810
# File 'ext/hdfs/hdfs.c', line 800

VALUE HDFS_File_write(VALUE self, VALUE bytes) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  tSize bytes_written = hdfsWrite(data->fs, data->file, RSTRING_PTR(bytes),
      RSTRING_LEN(bytes));
  if (bytes_written == -1) {
    rb_raise(e_file_error, "Failed to write data");
  }
  return UINT2NUM(bytes_written);
}

#availableObject

Returns the number of bytes that can be read from this file without blocking. If this fails, raises a FileError.



872
873
874
875
876
877
878
879
880
881
# File 'ext/hdfs/hdfs.c', line 872

VALUE HDFS_File_available(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  int bytes_available = hdfsAvailable(data->fs, data->file);
  if (bytes_available < 0) {
    rb_raise(e_file_error, "Failed to get available data");
  }
  return INT2NUM(bytes_available);
}

#closeObject

Closes the current file. If this fails, raises a FileError.



889
890
891
892
893
894
895
896
897
898
899
900
# File 'ext/hdfs/hdfs.c', line 889

VALUE HDFS_File_close(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  if (data->file != NULL) {
    if (hdfsCloseFile(data->fs, data->file) < 0) {
      rb_raise(e_file_error, "Could not close file");
      return Qnil;
    }
    data->file = NULL;
  }
  return Qtrue;
}

#flushObject

Flushes all buffers currently being written to this file. When this finishes, new readers will see the data written. If this fails, raises a FileError.



855
856
857
858
859
860
861
862
863
# File 'ext/hdfs/hdfs.c', line 855

VALUE HDFS_File_flush(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  if (hdfsFlush(data->fs, data->file) < 0) {
    rb_raise(e_file_error, "Flush failed");
  }
  return Qtrue;
}

#read(length) ⇒ Object

Reads the number of bytes specified by length from the current file object, returning the bytes read as a String. If this fails, raises a FileError.



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'ext/hdfs/hdfs.c', line 769

VALUE HDFS_File_read(int argc, VALUE* argv, VALUE self) {
  VALUE length;
  rb_scan_args(argc, argv, "01", &length);
  tSize hdfsLength = NIL_P(length) ? HDFS_DEFAULT_BUFFER_SIZE : NUM2INT(length);
  // Checks whether we're reading more data than HDFS client can support.
  if (hdfsLength > HDFS_DEFAULT_BUFFER_SIZE) {
    rb_raise(e_file_error, "Can only read a max of %u bytes from HDFS",
        HDFS_DEFAULT_BUFFER_SIZE);
    return Qnil;
  }
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  char* buffer = ALLOC_N(char, hdfsLength);
  tSize bytes_read = hdfsRead(data->fs, data->file, buffer, hdfsLength);
  if (bytes_read == -1) {
    rb_raise(e_file_error, "Failed to read data");
  }
  VALUE string_output = rb_tainted_str_new(buffer, bytes_read);
  xfree(buffer);
  return string_output;
}

#read_open?Boolean

Returns True if this file is open for reading; otherwise returns False.

Returns:

  • (Boolean)


908
909
910
911
912
913
914
915
916
# File 'ext/hdfs/hdfs.c', line 908

VALUE HDFS_File_read_open(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  if (data->file) {
    return hdfsFileIsOpenForRead(data->file) ? Qtrue : Qfalse;
  } else {
    return Qfalse;
  }
}

#seek(offset) ⇒ Object

Seeks the file pointer to the supplied offset. If this fails, raises a FileError.



837
838
839
840
841
842
843
844
845
# File 'ext/hdfs/hdfs.c', line 837

VALUE HDFS_File_seek(VALUE self, VALUE offset) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  if (hdfsSeek(data->fs, data->file, NUM2ULONG(offset)) < 0) {
    rb_raise(e_file_error, "Failed to seek to position %u", NUM2ULONG(offset));
  }
  return Qtrue;
}

#tellObject

Returns the current byte position in the file as an Integer. If this fails, raises a FileError.



819
820
821
822
823
824
825
826
827
828
# File 'ext/hdfs/hdfs.c', line 819

VALUE HDFS_File_tell(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  tOffset offset = hdfsTell(data->fs, data->file);
  if (offset == -1) {
    rb_raise(e_file_error, "Failed to read position");
  }
  return ULONG2NUM(offset);
}

#write(bytes) ⇒ Object

Writes the string specified by bytes to the current file object, returning the number of bytes written as an Integer. If this fails, raises a FileError.



800
801
802
803
804
805
806
807
808
809
810
# File 'ext/hdfs/hdfs.c', line 800

VALUE HDFS_File_write(VALUE self, VALUE bytes) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  tSize bytes_written = hdfsWrite(data->fs, data->file, RSTRING_PTR(bytes),
      RSTRING_LEN(bytes));
  if (bytes_written == -1) {
    rb_raise(e_file_error, "Failed to write data");
  }
  return UINT2NUM(bytes_written);
}

#write_open?Boolean

Returns True if this file is open for writing; otherwise returns False.

Returns:

  • (Boolean)


924
925
926
927
928
929
930
931
932
# File 'ext/hdfs/hdfs.c', line 924

VALUE HDFS_File_write_open(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  if (data->file) {
    return hdfsFileIsOpenForWrite(data->file) ? Qtrue : Qfalse;
  } else {
    return Qfalse;
  }
}