Package ax.antpick.k2hash
Class K2hashAttrPack
- java.lang.Object
-
- com.sun.jna.Structure
-
- ax.antpick.k2hash.K2hashAttrPack
-
public class K2hashAttrPack extends com.sun.jna.Structure
Information about an attribute of a key. K2hashAttrPack in Java corresponds to the K2HATTRPCK structure in C. Note: K2hashAttrPack must be a public class because com.sun.jna.Library has access to it.Usage Examples. 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; }
-
-
Constructor Summary
Constructors Constructor Description K2hashAttrPack()
Constructor of a K2hash attribute.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected List<String>
getFieldOrder()
Returns fields in the same order with the K2hash C API K2hashAttrPack structure.-
Methods inherited from class com.sun.jna.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
-
-