Class IgniteNativeIoLib
- java.lang.Object
-
- org.apache.ignite.internal.processors.cache.persistence.file.IgniteNativeIoLib
-
public class IgniteNativeIoLib extends Object
Native IO library based on *nix C library, enabled for Linux, kernel version >= 2.4.10.
Uses JNA library (https://github.com/java-native-access/jna) to access native calls.
-
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_OPEN_MODEDefault access mask for newly created files.static intE_INVALInvalid argument.static intO_CREATFile shall be created.static intO_DIRECTTry to minimize cache effects of the I/O to and from this file.static intO_RDONLYOpen for reading only.static intO_RDWROpen for reading and writing.static intO_SYNCWrite operations on the file will complete according to the requirements of synchronized I/O file integrity completion.static intO_TRUNCIf the file exists and is a regular file length shall be truncated to 0.static intO_WRONLYOpen for writing only.static intPOSIX_FADV_DONTNEEDThe specified data will not be accessed in the near future.static intS_IRGRPFlag for newly created files: group has read permission.static intS_IROTHFlag for newly created files: others have read permission.static intS_IRUSRFlag for newly created files: user has read permission.static intS_IWUSRFlag for newly created files: user has write permission.static intSEEK_CURSeek option: change file position to offsetstatic intSEEK_SETSeek option: set file offset to offset
-
Constructor Summary
Constructors Constructor Description IgniteNativeIoLib()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intclose(int fd)See "man 2 close".static intfdatasync(int fd)Synchronize a file's in-core state with storage device.static voidfree(com.sun.jna.Pointer ptr)Frees the memory space pointed to by ptr, which must have been returned by a previous call to native allocation methods.static intfsync(int fd)Synchronize a file's in-core state with storage device.static intftruncate(int fd, long len)Causes regular file referenced by fd to be truncated to a size of precisely length bytes.static intgetDirectIOBlockSize(String storageDir, IgniteLogger log)Determines FS and OS block size.static intgetpagesize()The function getpagesize() returns the number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mappingstatic booleanisJnaAvailable()static longlseek(int fd, long off, int whence)Repositions the file offset of the open file description associated with the file descriptorfdto the argument offset according to the directivewhencestatic intopen(String pathname, int flags, int mode)Open a file.static com.sun.jna.NativeLongpathconf(String path, int name)Return path (FS) configuration parameter value.static intposix_fadvise(int fd, long off, long len, int flag)Allows to announce an intention to access file data in a specific pattern in the future, thus allowing the kernel to perform appropriate optimizations.static intposix_memalign(com.sun.jna.ptr.PointerByReference memptr, com.sun.jna.NativeLong alignment, com.sun.jna.NativeLong size)Allocates size bytes and places the address of the allocated memory inmemptr.static com.sun.jna.NativeLongpread(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt, com.sun.jna.NativeLong off)Reads up tocntbytes from file descriptorfdat offsetoff(from the start of the file) into the buffer starting atbuf.static com.sun.jna.NativeLongpwrite(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt, com.sun.jna.NativeLong off)Writes up tocntbytes to the buffer starting atbufto the file descriptorfdat offsetoffset.static com.sun.jna.NativeLongread(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt)Reads up tocntbytes from file descriptorfdinto the buffer starting atbuf.static Stringstrerror(int errnum)Function returns a string that describes the error code passed in the argumenterrnum.static com.sun.jna.NativeLongwrite(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt)Writes up tocntbytes to the buffer starting atbufto the file descriptorfd.
-
-
-
Field Detail
-
O_RDONLY
public static final int O_RDONLY
Open for reading only.- See Also:
- Constant Field Values
-
O_WRONLY
public static final int O_WRONLY
Open for writing only.- See Also:
- Constant Field Values
-
O_RDWR
public static final int O_RDWR
Open for reading and writing.- See Also:
- Constant Field Values
-
O_CREAT
public static final int O_CREAT
File shall be created. If the file exists, this flag has no effect.- See Also:
- Constant Field Values
-
O_TRUNC
public static final int O_TRUNC
If the file exists and is a regular file length shall be truncated to 0.- See Also:
- Constant Field Values
-
O_DIRECT
public static final int O_DIRECT
Try to minimize cache effects of the I/O to and from this file.- See Also:
- Constant Field Values
-
O_SYNC
public static final int O_SYNC
Write operations on the file will complete according to the requirements of synchronized I/O file integrity completion. By the time write(2) (or similar) returns, the output data and associated file metadata have been transferred to the underlying hardware.- See Also:
- Constant Field Values
-
POSIX_FADV_DONTNEED
public static final int POSIX_FADV_DONTNEED
The specified data will not be accessed in the near future. See fadvise.h and "man 2 posix_fadvise".- See Also:
- Constant Field Values
-
S_IRUSR
public static final int S_IRUSR
Flag for newly created files: user has read permission.- See Also:
- Constant Field Values
-
S_IWUSR
public static final int S_IWUSR
Flag for newly created files: user has write permission.- See Also:
- Constant Field Values
-
S_IRGRP
public static final int S_IRGRP
Flag for newly created files: group has read permission.- See Also:
- Constant Field Values
-
S_IROTH
public static final int S_IROTH
Flag for newly created files: others have read permission.- See Also:
- Constant Field Values
-
DEFAULT_OPEN_MODE
public static final int DEFAULT_OPEN_MODE
Default access mask for newly created files.- See Also:
- Constant Field Values
-
E_INVAL
public static final int E_INVAL
Invalid argument.- See Also:
- Constant Field Values
-
SEEK_SET
public static final int SEEK_SET
Seek option: set file offset to offset- See Also:
- Constant Field Values
-
SEEK_CUR
public static final int SEEK_CUR
Seek option: change file position to offset- See Also:
- Constant Field Values
-
-
Method Detail
-
getDirectIOBlockSize
public static int getDirectIOBlockSize(String storageDir, IgniteLogger log)
Determines FS and OS block size. Returns file system block size for use with storageDir see "man 3 posix_memalign"- Parameters:
storageDir- storage path, base path to check (FS) configuration parameters.log- Logger.- Returns:
- FS block size to be used in Direct IO and memory alignments.
- or -1 Operating System is not applicable for enabling Direct IO.
- and -1 if failed to determine block size.
- and -1 if JNA is not available or init failed.
-
isJnaAvailable
public static boolean isJnaAvailable()
- Returns:
- Flag indicating JNA library available and initialized. Always
falsefor non linux systems.
-
open
public static int open(String pathname, int flags, int mode)
Open a file. See "man 3 open".- Parameters:
pathname- pathname naming the file.flags- flag/open options. Flags are constructed by a bitwise-inclusive OR of flags.mode- create file mode creation mask.- Returns:
- file descriptor.
-
close
public static int close(int fd)
See "man 2 close".- Parameters:
fd- The file descriptor of the file to close.- Returns:
- 0 on success, -1 on error.
-
pwrite
public static com.sun.jna.NativeLong pwrite(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt, com.sun.jna.NativeLong off)Writes up tocntbytes to the buffer starting atbufto the file descriptorfdat offsetoffset. The file offset is not changed. See "man 2 pwrite".- Parameters:
fd- file descriptor.buf- pointer to buffer with data.cnt- bytes to write.off- position in file to write data.- Returns:
- the number of bytes written. Note that is not an error for a successful call to transfer fewer bytes than requested.
-
write
public static com.sun.jna.NativeLong write(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt)Writes up tocntbytes to the buffer starting atbufto the file descriptorfd. The file offset is changed. See "man 2 write".- Parameters:
fd- file descriptor.buf- pointer to buffer with data.cnt- bytes to write.- Returns:
- the number of bytes written. Note that is not an error for a successful call to transfer fewer bytes than requested.
-
pread
public static com.sun.jna.NativeLong pread(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt, com.sun.jna.NativeLong off)Reads up tocntbytes from file descriptorfdat offsetoff(from the start of the file) into the buffer starting atbuf. The file offset is not changed. See "man 2 pread".- Parameters:
fd- file descriptor.buf- pointer to buffer to place the data.cnt- bytes to read.- Returns:
- On success, the number of bytes read is returned (zero indicates end of file), on error, -1 is returned, and errno is set appropriately.
-
read
public static com.sun.jna.NativeLong read(int fd, com.sun.jna.Pointer buf, com.sun.jna.NativeLong cnt)Reads up tocntbytes from file descriptorfdinto the buffer starting atbuf. The file offset is changed. See "man 2 read".- Parameters:
fd- file descriptor.buf- pointer to buffer to place the data.cnt- bytes to read.- Returns:
- On success, the number of bytes read is returned (zero indicates end of file), on error, -1 is returned, and errno is set appropriately.
-
fsync
public static int fsync(int fd)
Synchronize a file's in-core state with storage device. See "man 2 fsync".- Parameters:
fd- file descriptor.- Returns:
- On success return zero. On error, -1 is returned, and errno is set appropriately.
-
fdatasync
public static int fdatasync(int fd)
Synchronize a file's in-core state with storage device. See "man 2 fsync". Similar tofsync(int), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled.- Parameters:
fd- file descriptor.- Returns:
- On success return zero. On error, -1 is returned, and errno is set appropriately.
-
posix_memalign
public static int posix_memalign(com.sun.jna.ptr.PointerByReference memptr, com.sun.jna.NativeLong alignment, com.sun.jna.NativeLong size)Allocates size bytes and places the address of the allocated memory inmemptr. The address of the allocated memory will be a multiple ofalignment. See "man 3 posix_memalign".- Parameters:
memptr- out memory pointer.alignment- memory alignment, must be a power of two and a multiple of sizeof(void *).size- size of buffer.- Returns:
- returns zero on success, or one of the error values.
-
free
public static void free(com.sun.jna.Pointer ptr)
Frees the memory space pointed to by ptr, which must have been returned by a previous call to native allocation methods. POSIX requires that memory obtained fromposix_memalign(com.sun.jna.ptr.PointerByReference, com.sun.jna.NativeLong, com.sun.jna.NativeLong)can be freed using free. See "man 3 free".- Parameters:
ptr- pointer to free.
-
strerror
public static String strerror(int errnum)
Function returns a string that describes the error code passed in the argumenterrnum. See "man 3 strerror".- Parameters:
errnum- error code.- Returns:
- displayable error information.
-
pathconf
public static com.sun.jna.NativeLong pathconf(String path, int name)
Return path (FS) configuration parameter value.
Helps to determine alignment restrictions, for example, on buffers used for direct block device I/O.
POSIX specifies the pathconf(path,_PC_REC_XFER_ALIGN) call that tells what alignment is needed.- Parameters:
path- base path to check settings.name- variable name to query.
-
getpagesize
public static int getpagesize()
The function getpagesize() returns the number of bytes in a memory page, where "page" is a fixed-length block, the unit for memory allocation and file mapping
-
posix_fadvise
public static int posix_fadvise(int fd, long off, long len, int flag)Allows to announce an intention to access file data in a specific pattern in the future, thus allowing the kernel to perform appropriate optimizations. The advice applies to a (not necessarily existent) region starting atoffand extending forlenbytes (or until the end of the file if len is 0) within the file referred to by fd. See "man 2 posix_fadvise".- Parameters:
fd- file descriptor.off- region start.len- region end.flag- advice (option) to apply.- Returns:
- On success, zero is returned. On error, an error number is returned.
-
ftruncate
public static int ftruncate(int fd, long len)Causes regular file referenced by fd to be truncated to a size of precisely length bytes. If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0'). The file offset is not changed.- Parameters:
fd- file descriptor.len- required length.- Returns:
- On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
-
lseek
public static long lseek(int fd, long off, int whence)Repositions the file offset of the open file description associated with the file descriptorfdto the argument offset according to the directivewhence- Parameters:
fd- file descriptor.off- required position offset.whence- position base.- Returns:
- On error, the value -1 is returned and errno is set to indicate the error.
-
-