/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->zarr.zarr_version = 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->zarr.zarr_version; |
36 | |
|
37 | 0 | zfile->zarr.nczarr_version.major = 0; |
38 | 0 | zfile->zarr.nczarr_version.minor = 0; |
39 | 0 | zfile->zarr.nczarr_version.release = 0; |
40 | |
|
41 | 0 | if (zarrformat == 2) { |
42 | | /* Fetch /.zattrs and /.zgroup contents */ |
43 | 0 | if ((stat = NCZMD_fetch_json_group(zfile, "/", &zobjs->obj)) || |
44 | 0 | (stat = NCZMD_fetch_json_attrs(zfile, "/", &zobjs->atts))) { |
45 | 0 | stat = NC_ENCZARR; |
46 | 0 | goto done; |
47 | 0 | } |
48 | | |
49 | | /* Look for superblock; first in .zattrs and then in .zgroup */ |
50 | 0 | if ((NCJ_OK == NCJdictget(zobjs->atts, NCZ_V2_SUPERBLOCK, &jsuperblock) && |
51 | 0 | jsuperblock != NULL) || |
52 | 0 | (NCJ_OK == NCJdictget(zobjs->obj, NCZ_V2_SUPERBLOCK, &jsuperblock) && |
53 | 0 | jsuperblock != NULL && (1 == (zobjs->nczv1 = 1)))) { |
54 | |
|
55 | 0 | if (1 == zobjs->nczv1) { // Key stored in .zgroup!! |
56 | 0 | zfile->controls.flags |= FLAG_NCZARR_KEY; |
57 | 0 | file->no_write = 1; |
58 | 0 | } |
59 | |
|
60 | 0 | if (NCJsort(jsuperblock) == NCJ_DICT && |
61 | 0 | NCJ_OK == NCJdictget(jsuperblock, "version", &jnczarrversion) && |
62 | 0 | jnczarrversion != NULL && NCJsort(jnczarrversion) == NCJ_STRING) { |
63 | |
|
64 | 0 | if (sscanf(NCJstring(jnczarrversion), "%lu.%lu.%lu", |
65 | 0 | &zfile->zarr.nczarr_version.major, |
66 | 0 | &zfile->zarr.nczarr_version.minor, |
67 | 0 | &zfile->zarr.nczarr_version.release) != 3) { |
68 | 0 | nclog(NCLOGERR, "Issue detecting NCZARR version from %s", |
69 | 0 | NCJstring(jnczarrversion)); |
70 | 0 | stat = NC_ENCZARR; |
71 | 0 | } |
72 | 0 | } |
73 | 0 | } else { |
74 | 0 | zfile->controls.flags |= FLAG_PUREZARR; |
75 | 0 | stat = NC_NOERR; |
76 | 0 | } |
77 | 0 | } |
78 | 0 | done: |
79 | 0 | return THROW(stat); |
80 | 0 | } |
81 | | |
82 | | |
83 | | int NCZ_get_map(NC_FILE_INFO_T *file, NCURI *uri, mode_t mode, |
84 | 0 | size64_t constraints, void *params, NCZMAP **mapp) { |
85 | 0 | int stat = NC_NOERR; |
86 | 0 | NCZ_FILE_INFO_T *zfile = NULL; |
87 | 0 | NCZM_IMPL impl = NCZM_UNDEF; |
88 | 0 | char *path = NULL; |
89 | |
|
90 | 0 | zfile = (NCZ_FILE_INFO_T *)file->format_file_info; |
91 | 0 | assert(zfile != NULL); |
92 | |
|
93 | 0 | impl = zfile->controls |
94 | 0 | .mapimpl; // TODO: properly infer map implementation from uri |
95 | |
|
96 | 0 | if ((path = ncuribuild(uri, NULL, NULL, NCURIALL)) == NULL) { |
97 | 0 | stat = NC_ENCZARR; |
98 | 0 | goto done; |
99 | 0 | } |
100 | | |
101 | 0 | if (zfile->creating) { |
102 | 0 | stat = |
103 | 0 | nczmap_create(impl, path, (int)mode, zfile->controls.flags, NULL, mapp); |
104 | 0 | } else { |
105 | 0 | stat = |
106 | 0 | nczmap_open(impl, path, (int)mode, zfile->controls.flags, NULL, mapp); |
107 | 0 | } |
108 | |
|
109 | 0 | done: |
110 | | nullfree(path); |
111 | 0 | return stat; |
112 | 0 | } |