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