public class K2hashAttrPack
extends com.sun.jna.Structure
Note: K2hashAttrPack is a public class because the com.sun.jna.Library package has access to it.
An Usage Example:
Suppose you want to get attributes of a key. You could write this as:
public Map<String, String> getAttributes(String path, String key) {
if (path == null || path.isEmpty() || key == null || key.isEmpty()) {
throw new IllegalArgumentException("invalid key. key must contains any lettrs");
}
K2hashLibrary INSTANCE = (K2hashLibrary)
Native.synchronizedLibrary(Native.loadLibrary("k2hash", K2hashLibrary.class));
assert(INSTANCE != null);
long handle = K2H_INVALID_HANDLE;
try {
handle = INSTANCE.k2h_open(path,
DEFAULT_IS_READONLY, DEFAULT_IS_REMOVE_FILE, DEFAULT_IS_FULLMAP, DEFAULT_MASK_BITS,
DEFAULT_CMASK_BITS, DEFAULT_MAX_ELEMENT, DEFAULT_PAGE_SIZE);
IntByReference iref = new IntByReference();
// k2h_get_str_direct_attrs returns a pointer of K2hashAttrPack.
K2hashAttrPack pp = INSTANCE.k2h_get_str_direct_attrs(handle, key, iref);
if (pp == null || iref.getValue() == 0) {
System.err.println("no attribute exists");
INSTANCE.k2h_close(handle);
return null;
}
K2hashAttrPack[] attrs = (K2hashAttrPack[]) pp.toArray(iref.getValue());
// Note: We must copy data because attrs must be free later.
K2hashAttrPack[] newAttrs = Arrays.copyOfRange(attrs, 0, attrs.length);
Map<String, String> map = new HashMap<>();
Stream<K2hashAttrPack> stream = Stream.of(newAttrs);
stream.forEach(
attr -> {
map.put(attr.pkey, attr.pval);
});
INSTANCE.k2h_free_attrpack(attrs, iref.getValue());
INSTANCE.k2h_close(handle);
return map;
} catch (IOException e) {
if(handle != K2H_INVALID_HANDLE) {
INSTANCE.k2h_close(handle);
}
}
return null;
}
Modifier and Type | Field and Description |
---|---|
static List<String> |
FIELDS
Fields of K2hashAttrPack.
|
int |
keylength
The length of an attribute key.
|
String |
pkey
An attribute key.
|
String |
pval
An attribute value.
|
int |
vallength
The length of an attribute value.
|
Constructor and Description |
---|
K2hashAttrPack()
Constructor of K2hashAttrPack
|
Modifier and Type | Method and Description |
---|---|
protected List<String> |
getFieldOrder()
Returns fields in the same order with the k2hash C API
K2hashAttrPack structure.
|
allocateMemory, allocateMemory, autoAllocate, autoRead, autoRead, autoWrite, autoWrite, cacheTypeInfo, calculateSize, clear, createFieldsOrder, createFieldsOrder, createFieldsOrder, createFieldsOrder, dataEquals, dataEquals, ensureAllocated, equals, fieldOffset, getAutoRead, getAutoWrite, getFieldList, getFields, getNativeAlignment, getNativeSize, getNativeSize, getPointer, getStringEncoding, getStructAlignment, hashCode, newInstance, newInstance, read, readField, readField, setAlignType, setAutoRead, setAutoSynch, setAutoWrite, setStringEncoding, size, sortFields, toArray, toArray, toString, toString, useMemory, useMemory, write, writeField, writeField, writeField
Copyright © 2020. All rights reserved.