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.



730
731
732
733
734
735
736
737
738
# File 'ext/hdfs/hdfs.c', line 730

VALUE HDFS_File_write(VALUE self, VALUE bytes) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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.



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

VALUE HDFS_File_available(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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.



815
816
817
818
819
820
821
822
823
# File 'ext/hdfs/hdfs.c', line 815

VALUE HDFS_File_close(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, data);
  if (data->file != NULL) {
    hdfsCloseFile(data->fs, data->file);
    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.



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

VALUE HDFS_File_flush(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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.



711
712
713
714
715
716
717
718
719
720
721
# File 'ext/hdfs/hdfs.c', line 711

VALUE HDFS_File_read(VALUE self, VALUE length) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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);
}

#seek(offset) ⇒ Object

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



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

VALUE HDFS_File_seek(VALUE self, VALUE offset) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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.



747
748
749
750
751
752
753
754
755
# File 'ext/hdfs/hdfs.c', line 747

VALUE HDFS_File_tell(VALUE self) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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.



730
731
732
733
734
735
736
737
738
# File 'ext/hdfs/hdfs.c', line 730

VALUE HDFS_File_write(VALUE self, VALUE bytes) {
  FileData* data = NULL;
  Data_Get_Struct(self, FileData, 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);
}