class Tkrzw::File

Generic file implementation. All operations except for “open” and “close” are thread-safe; Multiple threads can access the same file concurrently. You can specify a concrete class when you call the “open” method. Every opened file must be closed explicitly by the “close” method to avoid data corruption. Moreover, every unused file object should be destructed by the “destruct” method to free resources.

Public Class Methods

new() click to toggle source

Initializes the file object.

# File tkrzw.rb, line 1060
def initialize()
  # (native code)
end

Public Instance Methods

append(data, status=nil) click to toggle source

Appends data at the end of the file.

  • @param data The data to write.

  • @param status A status object to which the result status is assigned. It can be omitted.

  • @return The offset at which the data has been put, or nil on failure.

# File tkrzw.rb, line 1117
def append(data, status=nil)
  # (native code)
end
close() click to toggle source

Closes the file.

  • @return The result status.

# File tkrzw.rb, line 1092
def close()
  # (native code)
end
destruct() click to toggle source

Releases the resource explicitly.

# File tkrzw.rb, line 1065
def destruct()
  # (native code)
end
get_path() click to toggle source

Gets the path of the file.

  • @return The path of the file or nil on failure.

# File tkrzw.rb, line 1147
def get_path()
  # (native code)
end
get_size() click to toggle source

Gets the size of the file.

  • @return The size of the file or nil on failure.

# File tkrzw.rb, line 1141
def get_size()
  # (native code)
end
inspect() click to toggle source

Returns a string representation of the object.

  • @return The string representation of the object.

# File tkrzw.rb, line 1168
def inspect()
  # (native code)
end
open(path, writable, **params) click to toggle source

Opens a file.

  • @param path A path of the file.

  • @param writable If true, the file is writable. If false, it is read-only.

  • @param params Optional keyword parameters.

  • @return The result status.

The optional parameters can include an option for the concurrency tuning. By default, database operatins are done under the GIL (Global Interpreter Lock), which means that database operations are not done concurrently even if you use multiple threads. If the “concurrent” parameter is true, database operations are done outside the GIL, which means that database operations can be done concurrently if you use multiple threads. However, the downside is that swapping thread data is costly so the actual throughput is often worse in the concurrent mode than in the normal mode. Therefore, the concurrent mode should be used only if the database is huge and it can cause blocking of threads in multi-thread usage. By default, the encoding of retrieved record data by the “get” method is implicitly set as “ASCII-8BIT”. If you want to change the implicit encoding to “UTF-8” or others, set the encoding name as the value of the “encoding” parameter. The optional parameters can include options for the file opening operation.

  • truncate (bool): True to truncate the file.

  • no_create (bool): True to omit file creation.

  • no_wait (bool): True to fail if the file is locked by another process.

  • no_lock (bool): True to omit file locking.

  • sync_hard (bool): True to do physical synchronization when closing.

The optional parameter “file” specifies the internal file implementation class. The default file class is “MemoryMapAtomicFile”. The other supported classes are “StdFile”, “MemoryMapAtomicFile”, “PositionalParallelFile”, and “PositionalAtomicFile”. For the file “PositionalParallelFile” and “PositionalAtomicFile”, these optional parameters are supported.

  • block_size (int): The block size to which all blocks should be aligned.

  • access_options (str): Values separated by colon. “direct” for direct I/O. “sync” for synchrnizing I/O, “padding” for file size alignment by padding, “pagecache” for the mini page cache in the process.

# File tkrzw.rb, line 1086
def open(path, writable, **params)
  # (native code)
end
read(off, size, status=nil) click to toggle source

Reads data.

  • @param off The offset of a source region.

  • @param size The size to be read.

  • @param status A status object to which the result status is assigned. It can be omitted.

  • @return The read data or nil on failure.

# File tkrzw.rb, line 1101
def read(off, size, status=nil)
  # (native code)
end
synchronize(hard, off=0, size=0) click to toggle source

Synchronizes the content of the file to the file system.

  • @param hard True to do physical synchronization with the hardware or false to do only logical synchronization with the file system.

  • @param off The offset of the region to be synchronized.

  • @param size The size of the region to be synchronized. If it is zero, the length to the end of file is specified.

  • @return The result status.

The pysical file size can be larger than the logical size in order to improve performance by reducing frequency of allocation. Thus, you should call this function before accessing the file with external tools.

# File tkrzw.rb, line 1135
def synchronize(hard, off=0, size=0)
  # (native code)
end
to_s() click to toggle source

Returns a string representation of the content.

  • @return The string representation of the content.

# File tkrzw.rb, line 1162
def to_s()
  # (native code)
end
truncate(size) click to toggle source

Truncates the file.

  • @param size The new size of the file.

  • @return The result status.

If the file is shrunk, data after the new file end is discarded. If the file is expanded, null codes are filled after the old file end.

# File tkrzw.rb, line 1125
def truncate(size)
  # (native code)
end
write(off, data) click to toggle source

Writes data.

  • @param off The offset of the destination region.

  • @param data The data to write.

  • @return The result status.

# File tkrzw.rb, line 1109
def write(off, data)
  # (native code)
end