VRS
A file format for sensor data.
Loading...
Searching...
No Matches
FileSpec.h
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <cstdint>
20#include <functional>
21#include <map>
22#include <string>
23#include <string_view>
24#include <utility>
25#include <vector>
26
27#include <vrs/VrsExport.h>
28
29namespace vrs {
30using std::map;
31using std::string;
32using std::string_view;
33using std::vector;
34
43struct VRS_API FileSpec {
46 using Extras = map<string, string, std::less<>>;
47
48 FileSpec() = default;
49 FileSpec(string filehandler, const vector<string>& chunksIn)
50 : fileHandlerName{std::move(filehandler)}, chunks{chunksIn} {}
51 FileSpec(string filehandler, const vector<string>&& chunksIn)
52 : fileHandlerName{std::move(filehandler)}, chunks{chunksIn} {}
53 explicit FileSpec(const vector<string>& chunksIn) : chunks{chunksIn} {}
54 explicit FileSpec(vector<string>&& chunksIn) : chunks{std::move(chunksIn)} {}
55
57 void clear();
58
59 bool empty() const;
60
61 bool isDiskFile() const;
62
63 static int parseUri(string_view uri, string& outScheme, string& outPath, Extras& outQueryParams);
64
71 int fromPathJsonUri(string_view pathJsonUri, string_view defaultFileHandlerName = {});
72
74 string toPathJsonUri() const;
75
76 // Parse json and extract file specs, with optional extra parameters.
77 // @param jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
78 // "filename":"file.vrs"}.
79 // @return True if parsing the json succeeded.
80 bool fromJson(string_view jsonStr);
81
82 // Generate json string from a file spec.
83 // @return jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
84 // "filename":"file.vrs"}.
85 string toJson() const;
86
88 int parseUri();
89
91 bool hasChunkSizes() const;
93 int64_t getFileSize() const;
95 string getSourceLocation() const;
97 string getEasyPath() const;
99 string getFileName() const;
100
102 string getXXHash() const;
103
105 bool operator==(const FileSpec& rhs) const;
106
108 const string& getExtra(string_view name) const;
110 bool getExtra(string_view name, string& outValue) const;
112 bool hasExtra(string_view name) const;
115 int getExtraAsInt(string_view name, int defaultValue = 0) const;
118 int64_t getExtraAsInt64(string_view name, int64_t defaultValue = 0) const;
121 uint64_t getExtraAsUInt64(string_view name, uint64_t defaultValue = 0) const;
124 double getExtraAsDouble(string_view name, double defaultValue = 0) const;
127 bool getExtraAsBool(string_view name, bool defaultValue = false) const;
128
129 static int decodeQuery(string_view query, string& outKey, string& outValue);
130 static int urldecode(string_view in, string& out);
131
132 void setExtra(string_view name, string_view value);
133 void setExtra(string_view name, const char* value);
134 void setExtra(string_view name, bool value);
135 void setExtra(string_view name, const string& value);
136 template <typename T>
137 void setExtra(string_view name, T value) {
138 extras[string(name)] = std::to_string(value);
139 }
140
142 void unsetExtra(string_view name);
143
144 string fileHandlerName;
145 string fileName;
146 string uri;
147 vector<string> chunks;
148 vector<int64_t> chunkSizes;
149 Extras extras;
150};
151
152} // namespace vrs
Definition Compressor.cpp:113
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:43
map< string, string, std::less<> > Extras
Definition FileSpec.h:46