1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright by The HDF Group.                                               *
3  * Copyright by the Board of Trustees of the University of Illinois.         *
4  * All rights reserved.                                                      *
5  *                                                                           *
6  * This file is part of HDF5.  The full HDF5 copyright notice, including     *
7  * terms governing use, modification, and redistribution, is contained in    *
8  * the COPYING file, which can be found at the root of the source code       *
9  * distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.  *
10  * If you do not have access to either file, you may request a copy from     *
11  * help@hdfgroup.org.                                                        *
12  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
13 
14 /************************************************************
15   This example shows how to set the conditions for
16   conversion between compact and dense (indexed) groups.
17  ************************************************************/
18 package examples.groups;
19 
20 import hdf.hdf5lib.H5;
21 import hdf.hdf5lib.HDF5Constants;
22 import hdf.hdf5lib.structs.H5G_info_t;
23 
24 import java.util.EnumSet;
25 import java.util.HashMap;
26 import java.util.Map;
27 
28 public class H5Ex_G_Phase {
29     private static String FILE = "H5Ex_G_Phase.h5";
30     private static int MAX_GROUPS = 7;
31     private static int MAX_COMPACT = 5;
32     private static int MIN_DENSE = 3;
33 
34     enum H5G_storage {
35         H5G_STORAGE_TYPE_UNKNOWN(-1),
36         H5G_STORAGE_TYPE_SYMBOL_TABLE(0),
37         H5G_STORAGE_TYPE_COMPACT(1),
38         H5G_STORAGE_TYPE_DENSE(2);
39 
40         private static final Map<Integer, H5G_storage> lookup = new HashMap<Integer, H5G_storage>();
41 
42         static {
43             for (H5G_storage s : EnumSet.allOf(H5G_storage.class))
s.getCode()44                 lookup.put(s.getCode(), s);
45         }
46 
47         private int code;
48 
H5G_storage(int layout_type)49         H5G_storage(int layout_type) {
50             this.code = layout_type;
51         }
52 
getCode()53         public int getCode() {
54             return this.code;
55         }
56 
get(int code)57         public static H5G_storage get(int code) {
58             return lookup.get(code);
59         }
60     }
61 
CreateGroup()62     private static void CreateGroup() {
63         long file_id = -1;
64         long group_id = -1;
65         long subgroup_id = -1;
66         long fapl_id = -1;
67         long gcpl_id = -1;
68         H5G_info_t ginfo;
69         String name = "G0"; // Name of subgroup_id
70         int i;
71 
72         // Set file access property list to allow the latest file format.This will allow the library to create new
73         // format groups.
74         try {
75             fapl_id = H5.H5Pcreate(HDF5Constants.H5P_FILE_ACCESS);
76             if (fapl_id >= 0)
77                 H5.H5Pset_libver_bounds(fapl_id, HDF5Constants.H5F_LIBVER_LATEST, HDF5Constants.H5F_LIBVER_LATEST);
78         }
79         catch (Exception e) {
80             e.printStackTrace();
81         }
82 
83         // Create group access property list and set the phase change conditions.
84         try {
85             gcpl_id = H5.H5Pcreate(HDF5Constants.H5P_GROUP_CREATE);
86             if (gcpl_id >= 0)
87                 H5.H5Pset_link_phase_change(gcpl_id, MAX_COMPACT, MIN_DENSE);
88         }
89         catch (Exception e) {
90             e.printStackTrace();
91         }
92 
93         // Create a new file using the default properties.
94         try {
95             if (fapl_id >= 0)
96                 file_id = H5.H5Fcreate(FILE, HDF5Constants.H5F_ACC_TRUNC, HDF5Constants.H5P_DEFAULT, fapl_id);
97         }
98         catch (Exception e) {
99             e.printStackTrace();
100         }
101 
102         // Create primary group.
103         try {
104             if ((file_id >= 0) && (gcpl_id >= 0))
105                 group_id = H5.H5Gcreate(file_id, name, HDF5Constants.H5P_DEFAULT, gcpl_id, HDF5Constants.H5P_DEFAULT);
106         }
107         catch (Exception e) {
108             e.printStackTrace();
109         }
110 
111         // Add subgroups to "group" one at a time, print the storage type for "group" after each subgroup is created.
112         for (i = 1; i <= MAX_GROUPS; i++) {
113             // Define the subgroup name and create the subgroup.
114             char append = (char) (((char) i) + '0');
115             name = name + append; /* G1, G2, G3 etc. */
116             try {
117                 if (group_id >= 0) {
118                     subgroup_id = H5.H5Gcreate(group_id, name, HDF5Constants.H5P_DEFAULT, HDF5Constants.H5P_DEFAULT,
119                             HDF5Constants.H5P_DEFAULT);
120                     H5.H5Gclose(subgroup_id);
121                 }
122             }
123             catch (Exception e) {
124                 e.printStackTrace();
125             }
126 
127             // Obtain the group info and print the group storage type
128             try {
129                 if (group_id >= 0) {
130                     ginfo = H5.H5Gget_info(group_id);
131                     System.out.print(ginfo.nlinks + " Group" + (ginfo.nlinks == 1 ? " " : "s") + ": Storage type is ");
132                     switch (H5G_storage.get(ginfo.storage_type)) {
133                     case H5G_STORAGE_TYPE_COMPACT:
134                         System.out.println("H5G_STORAGE_TYPE_COMPACT"); // New compact format
135                         break;
136                     case H5G_STORAGE_TYPE_DENSE:
137                         System.out.println("H5G_STORAGE_TYPE_DENSE"); // New dense (indexed) format
138                         break;
139                     case H5G_STORAGE_TYPE_SYMBOL_TABLE:
140                         System.out.println("H5G_STORAGE_TYPE_SYMBOL_TABLE"); // Original format
141                         break;
142                     case H5G_STORAGE_TYPE_UNKNOWN:
143                         System.out.println("H5G_STORAGE_TYPE_UNKNOWN");
144                         break;
145                     default:
146                         System.out.println("Storage Type Invalid");
147                         break;
148                     }
149                 }
150             }
151             catch (Exception e) {
152                 e.printStackTrace();
153             }
154         }
155 
156         System.out.println();
157 
158         // Delete subgroups one at a time, print the storage type for "group" after each subgroup is deleted.
159         for (i = MAX_GROUPS; i >= 1; i--) {
160             // Define the subgroup name and delete the subgroup.
161             try {
162                 H5.H5Ldelete(group_id, name, HDF5Constants.H5P_DEFAULT);
163             }
164             catch (Exception e) {
165                 e.printStackTrace();
166             }
167             name = name.substring(0, i + 1);
168 
169             // Obtain the group info and print the group storage type
170             try {
171                 if (group_id >= 0) {
172                     ginfo = H5.H5Gget_info(group_id);
173                     System.out.print(ginfo.nlinks + " Group" + (ginfo.nlinks == 1 ? " " : "s") + ": Storage type is ");
174                     switch (H5G_storage.get(ginfo.storage_type)) {
175                     case H5G_STORAGE_TYPE_COMPACT:
176                         System.out.println("H5G_STORAGE_TYPE_COMPACT"); // New compact format
177                         break;
178                     case H5G_STORAGE_TYPE_DENSE:
179                         System.out.println("H5G_STORAGE_TYPE_DENSE"); // New dense (indexed) format
180                         break;
181                     case H5G_STORAGE_TYPE_SYMBOL_TABLE:
182                         System.out.println("H5G_STORAGE_TYPE_SYMBOL_TABLE"); // Original format
183                         break;
184                     case H5G_STORAGE_TYPE_UNKNOWN:
185                         System.out.println("H5G_STORAGE_TYPE_UNKNOWN");
186                         break;
187                     default:
188                         System.out.println("Storage Type Invalid");
189                         break;
190                     }
191                 }
192             }
193             catch (Exception e) {
194                 e.printStackTrace();
195             }
196         }
197 
198         // Close and release resources
199         try {
200             if (fapl_id >= 0)
201                 H5.H5Pclose(fapl_id);
202         }
203         catch (Exception e) {
204             e.printStackTrace();
205         }
206 
207         try {
208             if (gcpl_id >= 0)
209                 H5.H5Pclose(gcpl_id);
210         }
211         catch (Exception e) {
212             e.printStackTrace();
213         }
214 
215         // Close the group
216         try {
217             if (group_id >= 0)
218                 H5.H5Gclose(group_id);
219         }
220         catch (Exception e) {
221             e.printStackTrace();
222         }
223 
224         // Close the file
225         try {
226             if (file_id >= 0)
227                 H5.H5Fclose(file_id);
228         }
229         catch (Exception e) {
230             e.printStackTrace();
231         }
232 
233     }
234 
main(String[] args)235     public static void main(String[] args) {
236         H5Ex_G_Phase.CreateGroup();
237     }
238 
239 }
240