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 read as an Integer. If this fails, raises a FileError.



781
782
783
784
785
786
787
788
789
790
# File 'ext/hdfs/hdfs.c', line 781

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 INT2NUM(bytes_written);
}

#availableObject

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



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

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

#closeObject

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



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

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.



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

VALUE HDFS_File_flush(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  int result = hdfsFlush(data->fs, data->file);
  if (result != 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.



761
762
763
764
765
766
767
768
769
770
771
772
# File 'ext/hdfs/hdfs.c', line 761

VALUE HDFS_File_read(VALUE self, VALUE length) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  ensure_file_open(data);
  char* buffer = ALLOC_N(char, length);
  MEMZERO(buffer, char, length);
  tSize bytes_read = hdfsRead(data->fs, data->file, buffer, NUM2INT(length));
  if (bytes_read == -1) {
    rb_raise(e_file_error, "Failed to read data");
  }
  return rb_tainted_str_new2(buffer);
}

#read_open?Boolean

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

Returns:

  • (Boolean)


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

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.



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

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

#tellObject

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



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

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

#write(bytes) ⇒ Object

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



781
782
783
784
785
786
787
788
789
790
# File 'ext/hdfs/hdfs.c', line 781

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 INT2NUM(bytes_written);
}

#write_open?Boolean

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

Returns:

  • (Boolean)


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

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;
  }
}