|
| 1 | +/********************************************************************* |
| 2 | + * Copyright 2018, UCAR/Unidata |
| 3 | + * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
| 4 | + *********************************************************************/ |
| 5 | + |
| 6 | +#include "zincludes.h" |
| 7 | + |
| 8 | +#define MINIMIM_CSL_REP_RAW "{\"metadata\":{},\"zarr_consolidated_format\":1}" |
| 9 | + |
| 10 | +/// @brief Retrieve the group and variable names contained within a group specified by `key`. |
| 11 | +/// The order of the names may be arbitrary |
| 12 | +/// @param zfile - The zarr file info structure |
| 13 | +/// @param key - the key of the node - group |
| 14 | +/// @param groups - NClist where names will be added |
| 15 | +/// @param variables - NClist where names will be added |
| 16 | +/// @return `NC_NOERR` if succeeding |
| 17 | +int NCZMD_v2_list_nodes(NCZ_FILE_INFO_T *zfile, const char * key, NClist *groups, NClist *vars); |
| 18 | + |
| 19 | +/// @brief Retrieve the group names contained within a group specified by `key`. |
| 20 | +/// The order of the names may be arbitrary |
| 21 | +/// @param zfile - The zarr file info structure |
| 22 | +/// @param key - the key of the node - group |
| 23 | +/// @param groups - NClist where names will be added |
| 24 | +/// @return `NC_NOERR` if succeeding |
| 25 | +int NCZMD_v2_list_groups(NCZ_FILE_INFO_T *zfile, const char * key, NClist *groups); |
| 26 | + |
| 27 | +/// @brief Retrieve the variable names contained by a group specified by `key`. |
| 28 | +/// The order of the names may be arbitrary |
| 29 | +/// @param zfile - The zarr file info structure |
| 30 | +/// @param key - the key of the node - group |
| 31 | +/// @param variables - NClist where names will be added |
| 32 | +/// @return `NC_NOERR` if succeeding |
| 33 | +int NCZMD_v2_list_variables(NCZ_FILE_INFO_T *zfile, const char * key, NClist * variables); |
| 34 | + |
| 35 | +/// @brief Retrieve JSON metadata of a given type for the specified `key` from the storage |
| 36 | +/// @param zfile - The zarr file info structure |
| 37 | +/// @param zobj - The type of metadata to set |
| 38 | +/// @param key - the key of the node - group or array |
| 39 | +/// @param jobj - JSON to be written |
| 40 | +/// @return `NC_NOERR` if succeeding |
| 41 | +int fetch_json_content_v2(NCZ_FILE_INFO_T *zfile, NCZMD_MetadataType zarr_obj_type, const char *key, NCjson **jobj); |
| 42 | + |
| 43 | +/// @brief Write JSON metadata of a given type for the specified `key` to the storage |
| 44 | +/// @param zfile - The zarr file info structure |
| 45 | +/// @param zobj - The type of metadata to set |
| 46 | +/// @param key - the key of the node - group or array |
| 47 | +/// @param jobj - JSON to be written |
| 48 | +/// @return `NC_NOERR` if succeeding |
| 49 | +int update_json_content_v2(NCZ_FILE_INFO_T *zfile, NCZMD_MetadataType zobj, const char *key, const NCjson *jobj); |
| 50 | + |
| 51 | + |
| 52 | +/// @brief Place holder for non consolidated handler |
| 53 | +/// @param json - Not used! |
| 54 | +/// @return `NC_NOERR` always |
| 55 | +int validate_consolidated_json_noop_v2(const NCjson *json); |
| 56 | + |
| 57 | +static const NCZ_Metadata NCZ_md2_table = { |
| 58 | + ZARRFORMAT2, |
| 59 | + NCZ_METADATA_VERSION, |
| 60 | + ZARR_NOT_CONSOLIDATED, |
| 61 | + .jcsl = NULL, |
| 62 | + |
| 63 | + .list_nodes = NCZMD_v2_list_nodes, |
| 64 | + .list_groups = NCZMD_v2_list_groups, |
| 65 | + .list_variables = NCZMD_v2_list_variables, |
| 66 | + |
| 67 | + .fetch_json_content = fetch_json_content_v2, |
| 68 | + .update_json_content = update_json_content_v2, |
| 69 | + .validate_consolidated = validate_consolidated_json_noop_v2, |
| 70 | +}; |
| 71 | + |
| 72 | +const NCZ_Metadata *NCZ_metadata_handler2 = &NCZ_md2_table; |
| 73 | + |
| 74 | +int NCZMD_v2_list_nodes(NCZ_FILE_INFO_T *zfile, const char * key, NClist *groups, NClist *variables) |
| 75 | +{ |
| 76 | + size_t i; |
| 77 | + int stat = NC_NOERR; |
| 78 | + char *subkey = NULL; |
| 79 | + char *zkey = NULL; |
| 80 | + NClist *matches = nclistnew(); |
| 81 | + |
| 82 | + if ((stat = nczmap_search(zfile->map, key, matches))) |
| 83 | + goto done; |
| 84 | + for (i = 0; i < nclistlength(matches); i++) |
| 85 | + { |
| 86 | + const char *name = nclistget(matches, i); |
| 87 | + if (name[0] == NCZM_DOT) |
| 88 | + continue; |
| 89 | + if ((stat = nczm_concat(key, name, &subkey))) |
| 90 | + goto done; |
| 91 | + if ((stat = nczm_concat(subkey, Z2GROUP, &zkey))) |
| 92 | + goto done; |
| 93 | + if (NC_NOERR == nczmap_exists(zfile->map, zkey) && groups != NULL) |
| 94 | + nclistpush(groups, strdup(name)); |
| 95 | + |
| 96 | + nullfree(zkey); |
| 97 | + zkey = NULL; |
| 98 | + if ((stat = nczm_concat(subkey, Z2ARRAY, &zkey))) |
| 99 | + goto done; |
| 100 | + if (NC_NOERR == nczmap_exists(zfile->map, zkey) && variables != NULL) |
| 101 | + nclistpush(variables, strdup(name)); |
| 102 | + stat = NC_NOERR; |
| 103 | + |
| 104 | + nullfree(subkey); |
| 105 | + subkey = NULL; |
| 106 | + nullfree(zkey); |
| 107 | + zkey = NULL; |
| 108 | + } |
| 109 | + |
| 110 | +done: |
| 111 | + nullfree(subkey); |
| 112 | + nullfree(zkey); |
| 113 | + nclistfreeall(matches); |
| 114 | + return stat; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +int NCZMD_v2_list_groups(NCZ_FILE_INFO_T *zfile, const char * key, NClist *groups) |
| 119 | +{ |
| 120 | + return NCZMD_v2_list_nodes(zfile, key, groups, NULL); |
| 121 | +} |
| 122 | + |
| 123 | +int NCZMD_v2_list_variables(NCZ_FILE_INFO_T *zfile, const char * key, NClist *variables) |
| 124 | +{ |
| 125 | + return NCZMD_v2_list_nodes(zfile, key, NULL, variables); |
| 126 | +} |
| 127 | + |
| 128 | +static int zarr_obj_type2suffix(NCZMD_MetadataType zarr_obj_type, const char **suffix){ |
| 129 | + switch (zarr_obj_type) |
| 130 | + { |
| 131 | + case NCZMD_GROUP: |
| 132 | + *suffix = Z2GROUP; |
| 133 | + break; |
| 134 | + case NCZMD_ATTRS: |
| 135 | + *suffix = Z2ATTRS; |
| 136 | + break; |
| 137 | + case NCZMD_ARRAY: |
| 138 | + *suffix = Z2ARRAY; |
| 139 | + break; |
| 140 | + default: |
| 141 | + return NC_EINVAL; |
| 142 | + } |
| 143 | + return NC_NOERR; |
| 144 | +} |
| 145 | + |
| 146 | +int fetch_json_content_v2(NCZ_FILE_INFO_T *zfile, NCZMD_MetadataType zobj, const char *prefix, NCjson **jobj) |
| 147 | +{ |
| 148 | + int stat = NC_NOERR; |
| 149 | + const char *suffix; |
| 150 | + char * key = NULL; |
| 151 | + if ((stat = zarr_obj_type2suffix(zobj, &suffix)) |
| 152 | + || (stat = nczm_concat(prefix, suffix, &key))){ |
| 153 | + goto done; |
| 154 | + } |
| 155 | + |
| 156 | + stat = NCZ_downloadjson(zfile->map, key, jobj); |
| 157 | +done: |
| 158 | + nullfree(key); |
| 159 | + return stat; |
| 160 | +} |
| 161 | + |
| 162 | +int update_json_content_v2(NCZ_FILE_INFO_T *zfile, NCZMD_MetadataType zobj, const char *prefix, const NCjson *jobj) |
| 163 | +{ |
| 164 | + int stat = NC_NOERR; |
| 165 | + const char *suffix; |
| 166 | + char * key = NULL; |
| 167 | + if ((stat = zarr_obj_type2suffix(zobj, &suffix)) |
| 168 | + || (stat = nczm_concat(prefix, suffix, &key))){ |
| 169 | + goto done; |
| 170 | + } |
| 171 | + |
| 172 | + stat = NCZ_uploadjson(zfile->map, key, jobj); |
| 173 | +done: |
| 174 | + nullfree(key); |
| 175 | + return stat; |
| 176 | +} |
| 177 | + |
| 178 | +int validate_consolidated_json_noop_v2(const NCjson *json){ |
| 179 | + NC_UNUSED(json); |
| 180 | + return NC_NOERR; |
| 181 | +} |
0 commit comments