/src/netcdf-c/libnczarr/zinfer.c
Line | Count | Source |
1 | | #include "zincludes.h" |
2 | | |
3 | 0 | int NCZ_infer_zarr_format(NC_FILE_INFO_T *file) { |
4 | 0 | int stat = NC_ENOTZARR; |
5 | 0 | NCZ_FILE_INFO_T *zfile = (NCZ_FILE_INFO_T *)file->format_file_info; |
6 | |
|
7 | 0 | int try_consolidated = NCZ_use_consolidated(zfile); |
8 | 0 | if(NC_NOERR == nczmap_exists(zfile->map, Z2METADATA)){ |
9 | 0 | zfile->format.zarr = 2; |
10 | 0 | return NC_NOERR; |
11 | 0 | } |
12 | | |
13 | 0 | struct ZarrObjects { |
14 | 0 | const char *name; |
15 | 0 | int format; |
16 | 0 | } zarrobjects[] = { |
17 | 0 | {"/zarr.json", 3}, {"/.zgroup", 2}, {"/.zarray", 2}, |
18 | 0 | {"/.zattrs", 2}, {NULL, -1}, |
19 | 0 | }; |
20 | 0 | struct ZarrObjects *zo = NULL; |
21 | | |
22 | | /* check for the existence of **any** of the keys, infer format based on it*/ |
23 | 0 | for (zo = zarrobjects; zo->name; zo++) { |
24 | 0 | if (NC_NOERR == nczmap_exists(zfile->map, zo->name)) { |
25 | 0 | zfile->format.zarr = zo->format; |
26 | 0 | stat = NC_NOERR; |
27 | 0 | break; |
28 | 0 | } |
29 | 0 | } |
30 | |
|
31 | 0 | return stat; |
32 | 0 | } |
33 | | |
34 | 0 | int NCZ_infer_nczarr_format(NC_FILE_INFO_T *file) { |
35 | 0 | int stat = NC_ENOTZARR; |
36 | 0 | const NCjson *jsuperblock = NULL, *jnczarrversion = NULL; |
37 | 0 | NCZ_FILE_INFO_T *zfile = (NCZ_FILE_INFO_T *)file->format_file_info; |
38 | 0 | struct ZARROBJ *zobjs = |
39 | 0 | &(((NCZ_GRP_INFO_T *)file->root_grp->format_grp_info)->zgroup); |
40 | |
|
41 | 0 | int zarrformat = zfile->format.zarr; |
42 | 0 | int nczarrformat = NCZARRFORMAT0; |
43 | |
|
44 | 0 | if (zarrformat == ZARRFORMAT2) { |
45 | | /* Fetch /.zattrs and /.zgroup contents */ |
46 | 0 | if ((stat = NCZMD_fetch_json_group(zfile, "/", &zobjs->obj)) || |
47 | 0 | (stat = NCZMD_fetch_json_attrs(zfile, "/", &zobjs->atts))) { |
48 | 0 | stat = NC_ENCZARR; |
49 | 0 | goto done; |
50 | 0 | } |
51 | | |
52 | | /* Look for superblock; first in .zattrs and then in .zgroup */ |
53 | 0 | if ((NCJ_OK == NCJdictget(zobjs->atts, NCZ_V2_SUPERBLOCK, &jsuperblock) && |
54 | 0 | jsuperblock != NULL) || |
55 | 0 | (NCJ_OK == NCJdictget(zobjs->obj, NCZ_V2_SUPERBLOCK, &jsuperblock) && |
56 | 0 | jsuperblock != NULL && (1 == (zobjs->nczv1 = 1)))) { |
57 | |
|
58 | 0 | if (1 == zobjs->nczv1) { // Key stored in .zgroup!! |
59 | 0 | zfile->controls.flags |= FLAG_NCZARR_KEY; |
60 | 0 | file->no_write = 1; |
61 | 0 | } |
62 | |
|
63 | 0 | if (NCJsort(jsuperblock) == NCJ_DICT && |
64 | 0 | NCJ_OK == NCJdictget(jsuperblock, "version", &jnczarrversion) && |
65 | 0 | jnczarrversion != NULL && NCJsort(jnczarrversion) == NCJ_STRING) { |
66 | |
|
67 | 0 | if (sscanf(NCJstring(jnczarrversion), NCZARR_FORMAT_VERSION_TEMPLATE, |
68 | 0 | &nczarrformat) != 1) { |
69 | 0 | nclog(NCLOGERR, "Issue detecting NCZARR version from %s", |
70 | 0 | NCJstring(jnczarrversion)); |
71 | 0 | stat = NC_ENCZARR; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | } |
75 | 0 | } |
76 | | |
77 | 0 | if(nczarrformat == NCZARRFORMAT0) { |
78 | 0 | zfile->controls.flags |= FLAG_PUREZARR; |
79 | 0 | } |
80 | 0 | zfile->format.nczarr = nczarrformat; |
81 | | |
82 | |
|
83 | 0 | done: |
84 | 0 | return THROW(stat); |
85 | 0 | } |
86 | | |
87 | | |
88 | | int NCZ_get_map(NC_FILE_INFO_T *file, NCURI *uri, mode_t mode, |
89 | 0 | size64_t constraints, void *params, NCZMAP **mapp) { |
90 | 0 | int stat = NC_NOERR; |
91 | 0 | NCZ_FILE_INFO_T *zfile = NULL; |
92 | 0 | NCZM_IMPL impl = NCZM_UNDEF; |
93 | 0 | char *path = NULL; |
94 | |
|
95 | 0 | zfile = (NCZ_FILE_INFO_T *)file->format_file_info; |
96 | 0 | assert(zfile != NULL); |
97 | |
|
98 | 0 | impl = zfile->controls |
99 | 0 | .mapimpl; // TODO: properly infer map implementation from uri |
100 | |
|
101 | 0 | if ((path = ncuribuild(uri, NULL, NULL, NCURIALL)) == NULL) { |
102 | 0 | stat = NC_ENCZARR; |
103 | 0 | goto done; |
104 | 0 | } |
105 | | |
106 | 0 | if (zfile->creating) { |
107 | 0 | stat = |
108 | 0 | nczmap_create(impl, path, (int)mode, zfile->controls.flags, NULL, mapp); |
109 | 0 | } else { |
110 | 0 | stat = |
111 | 0 | nczmap_open(impl, path, (int)mode, zfile->controls.flags, NULL, mapp); |
112 | 0 | } |
113 | |
|
114 | 0 | done: |
115 | | nullfree(path); |
116 | 0 | return stat; |
117 | 0 | } |