Class 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;
     }
     
    • Nested Class Summary

      • Nested classes/interfaces inherited from class com.sun.jna.Structure

        com.sun.jna.Structure.ByReference, com.sun.jna.Structure.ByValue, com.sun.jna.Structure.FieldOrder, com.sun.jna.Structure.StructField
    • Field Summary

      Fields 
      Modifier and Type Field 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.
      • Fields inherited from class com.sun.jna.Structure

        ALIGN_DEFAULT, ALIGN_GNUC, ALIGN_MSVC, ALIGN_NONE, CALCULATE_SIZE
    • 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
    • Field Detail

      • FIELDS

        public static final List<String> FIELDS
        Fields of K2hashAttrPack.
      • pkey

        public String pkey
        An attribute key.
      • keylength

        public int keylength
        The length of an attribute key.
      • pval

        public String pval
        An attribute value.
      • vallength

        public int vallength
        The length of an attribute value.
    • Constructor Detail

      • K2hashAttrPack

        public K2hashAttrPack()
        Constructor of a K2hash attribute.
    • Method Detail

      • getFieldOrder

        protected List<String> getFieldOrder()
        Returns fields in the same order with the K2hash C API K2hashAttrPack structure.
        Overrides:
        getFieldOrder in class com.sun.jna.Structure
        Returns:
        fields