/src/netcdf-c/libnczarr/zmetadata.c
Line | Count | Source |
1 | | /********************************************************************* |
2 | | * Copyright 2018, UCAR/Unidata |
3 | | * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
4 | | *********************************************************************/ |
5 | | |
6 | | #include "zincludes.h" |
7 | | |
8 | | static int |
9 | | cmpstrings(const void* a1, const void* a2) |
10 | 0 | { |
11 | 0 | const char** s1 = (const char**)a1; |
12 | 0 | const char** s2 = (const char**)a2; |
13 | 0 | return strcmp(*s1,*s2); |
14 | 0 | } |
15 | | |
16 | | int NCZMD_list_nodes(NCZ_FILE_INFO_T *zfile, const char * key, NClist *groups, NClist *vars) |
17 | 0 | { |
18 | 0 | int stat = NC_NOERR; |
19 | 0 | if((stat = zfile->metadata.list_nodes(zfile,key, groups, vars))){ |
20 | 0 | return stat; |
21 | 0 | } |
22 | 0 | qsort(groups->content, groups->length, sizeof(char*), cmpstrings); |
23 | 0 | qsort(vars->content, vars->length, sizeof(char*), cmpstrings); |
24 | 0 | return stat; |
25 | 0 | } |
26 | | |
27 | | int NCZMD_list_groups(NCZ_FILE_INFO_T *zfile, const char * key, NClist *subgrpnames) |
28 | 0 | { |
29 | 0 | int stat = NC_NOERR; |
30 | 0 | if((stat = zfile->metadata.list_groups(zfile,key, subgrpnames))){ |
31 | 0 | return stat; |
32 | 0 | } |
33 | 0 | qsort(subgrpnames->content, subgrpnames->length, sizeof(char*), cmpstrings); |
34 | 0 | return stat; |
35 | 0 | } |
36 | | |
37 | | int NCZMD_list_variables(NCZ_FILE_INFO_T *zfile, const char * key, NClist *varnames) |
38 | 0 | { |
39 | 0 | int stat = NC_NOERR; |
40 | 0 | if((stat = zfile->metadata.list_variables(zfile, key, varnames))){ |
41 | 0 | return stat; |
42 | 0 | } |
43 | 0 | qsort(varnames->content, varnames->length, sizeof(char*), cmpstrings); |
44 | 0 | return stat; |
45 | 0 | } |
46 | | |
47 | 0 | int NCZMD_fetch_json_group(NCZ_FILE_INFO_T *zfile, const char *key, NCjson **jgroup) { |
48 | 0 | return zfile->metadata.fetch_json_content(zfile, NCZMD_GROUP, key, jgroup); |
49 | 0 | } |
50 | | |
51 | 0 | int NCZMD_fetch_json_attrs(NCZ_FILE_INFO_T *zfile, const char *key, NCjson **jattrs) { |
52 | 0 | return zfile->metadata.fetch_json_content(zfile, NCZMD_ATTRS, key, jattrs); |
53 | 0 | } |
54 | | |
55 | 0 | int NCZMD_fetch_json_array(NCZ_FILE_INFO_T *zfile, const char *key, NCjson **jarray) { |
56 | 0 | return zfile->metadata.fetch_json_content(zfile, NCZMD_ARRAY, key, jarray); |
57 | 0 | } |
58 | | |
59 | 0 | int NCZMD_update_json_group(NCZ_FILE_INFO_T *zfile, const char *key, const NCjson *jgroup) { |
60 | 0 | return zfile->metadata.update_json_content(zfile, NCZMD_GROUP, key, jgroup); |
61 | 0 | } |
62 | | |
63 | 0 | int NCZMD_update_json_attrs(NCZ_FILE_INFO_T *zfile, const char *key, const NCjson *jattrs) { |
64 | 0 | return zfile->metadata.update_json_content(zfile, NCZMD_ATTRS, key , jattrs); |
65 | 0 | } |
66 | | |
67 | 0 | int NCZMD_update_json_array(NCZ_FILE_INFO_T *zfile, const char *key, const NCjson *jarray) { |
68 | 0 | return zfile->metadata.update_json_content(zfile, NCZMD_ARRAY, key, jarray); |
69 | 0 | } |
70 | | |
71 | | int NCZMD_get_metadata_format(NCZ_FILE_INFO_T *zfile, int *zarrformat) |
72 | 0 | { |
73 | 0 | NCZ_Metadata *zmd = &(zfile->metadata); |
74 | |
|
75 | 0 | if (zmd->zarr_format >= ZARRFORMAT2) |
76 | 0 | { |
77 | 0 | *zarrformat = zmd->zarr_format; |
78 | 0 | return NC_NOERR; |
79 | 0 | } |
80 | | |
81 | 0 | if (!nczmap_exists(zfile->map, "/" Z2ATTRS) && !nczmap_exists(zfile->map, "/" Z2GROUP) && !nczmap_exists(zfile->map, "/" Z2ARRAY)) |
82 | 0 | { |
83 | 0 | return NC_ENOTZARR; |
84 | 0 | } |
85 | | |
86 | 0 | *zarrformat = ZARRFORMAT2; |
87 | 0 | return NC_NOERR; |
88 | 0 | } |
89 | | |
90 | 0 | int use_consolidated_metadata(NCZ_FILE_INFO_T *zfile) { |
91 | 0 | int use_consolidated = NCZARR_CONSOLIDATED_DEFAULT || (zfile->controls.flags & FLAG_CONSOLIDATED); |
92 | 0 | const char *e = getenv(NCZARR_CONSOLIDATED_ENV); |
93 | |
|
94 | 0 | int env_use_consolidated = (e != NULL) && ( |
95 | 0 | (atoi(e) > 0) ||(strcasecmp(e, "true") == 0) || (strcasecmp(e, "yes") == 0) |
96 | 0 | ); |
97 | |
|
98 | 0 | return use_consolidated || env_use_consolidated; |
99 | 0 | } |
100 | | |
101 | 0 | int NCZMD_set_metadata_handler(NCZ_FILE_INFO_T *zfile) { |
102 | 0 | NCjson *jcsl = NULL; |
103 | |
|
104 | 0 | int use_consolidated = use_consolidated_metadata(zfile); |
105 | 0 | if (!use_consolidated){ |
106 | 0 | nclog(NCLOGNOTE, "Not using consolidated metadata! Doing so could improve reading performance"); |
107 | 0 | } |
108 | |
|
109 | 0 | if (use_consolidated && zfile->creating) { |
110 | 0 | zfile->metadata = *NCZ_csl_metadata_handler2; |
111 | 0 | return NC_NOERR; |
112 | 0 | } |
113 | | |
114 | 0 | zfile->metadata = *NCZ_metadata_handler2; |
115 | 0 | if (!use_consolidated) |
116 | 0 | return NC_NOERR; |
117 | | |
118 | 0 | if (NCZ_downloadjson(zfile->map, Z2METADATA, &jcsl) || jcsl == NULL) { |
119 | 0 | nclog(NCLOGNOTE, "Dataset not consolidated! Doing so will improve performance"); |
120 | 0 | return NC_NOERR; |
121 | 0 | } |
122 | | |
123 | 0 | if (NCZ_csl_metadata_handler2->validate_consolidated(jcsl) != NC_NOERR) { |
124 | 0 | nclog(NCLOGWARN,"Consolidated metadata is invalid, ignoring it!"); |
125 | 0 | return NC_EZARRMETA; |
126 | 0 | } |
127 | | |
128 | 0 | zfile->metadata = *NCZ_csl_metadata_handler2; |
129 | 0 | zfile->metadata.jcsl = jcsl; |
130 | 0 | return NC_NOERR; |
131 | 0 | } |
132 | | |
133 | 0 | void NCZMD_free_metadata_handler(NCZ_Metadata * zmd){ |
134 | 0 | if (zmd == NULL) return; |
135 | 0 | NCJreclaim(zmd->jcsl); |
136 | 0 | zmd->jcsl = NULL; |
137 | 0 | } |
138 | | |
139 | | int NCZMD_consolidate(NCZ_FILE_INFO_T *zfile) |
140 | 0 | { |
141 | 0 | int stat = NC_NOERR; |
142 | 0 | if (zfile->creating == 1 && zfile->metadata.jcsl !=NULL){ |
143 | 0 | stat = NCZ_uploadjson(zfile->map, Z2METADATA ,zfile->metadata.jcsl); |
144 | 0 | } |
145 | 0 | return stat; |
146 | 0 | } |