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 <functional>
20#include <map>
21#include <string>
22#include <string_view>
23#include <utility>
24#include <vector>
25
26#include <vrs/VrsExport.h>
27
28namespace vrs {
29using std::map;
30using std::string;
31using std::string_view;
32using std::vector;
33
42struct VRS_API FileSpec {
45 using Extras = map<string, string, std::less<>>;
46
47 FileSpec() = default;
48 FileSpec(string filehandler, const vector<string>& chunksIn)
49 : fileHandlerName{std::move(filehandler)}, chunks{chunksIn} {}
50 FileSpec(string filehandler, const vector<string>&& chunksIn)
51 : fileHandlerName{std::move(filehandler)}, chunks{chunksIn} {}
52 explicit FileSpec(const vector<string>& chunksIn) : chunks{chunksIn} {}
53 explicit FileSpec(vector<string>&& chunksIn) : chunks{std::move(chunksIn)} {}
54
56 void clear();
57
58 bool empty() const;
59
60 bool isDiskFile() const;
61
62 static int parseUri(string_view uri, string& outScheme, string& outPath, Extras& outQueryParams);
63
70 int fromPathJsonUri(string_view pathJsonUri, string_view defaultFileHandlerName = {});
71
73 string toPathJsonUri() const;
74
75 // Parse json and extract file specs, with optional extra parameters.
76 // @param jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
77 // "filename":"file.vrs"}.
78 // @return True if parsing the json succeeded.
79 bool fromJson(string_view jsonStr);
80
81 // Generate json string from a file spec.
82 // @return jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
83 // "filename":"file.vrs"}.
84 string toJson() const;
85
87 int parseUri();
88
90 bool hasChunkSizes() const;
92 int64_t getFileSize() const;
94 string getSourceLocation() const;
96 string getEasyPath() const;
98 string getFileName() const;
99
101 string getXXHash() const;
102
104 bool operator==(const FileSpec& rhs) const;
105
107 const string& getExtra(string_view name) const;
109 bool getExtra(string_view name, string& outValue) const;
111 bool hasExtra(string_view name) const;
114 int getExtraAsInt(string_view name, int defaultValue = 0) const;
117 int64_t getExtraAsInt64(string_view name, int64_t defaultValue = 0) const;
120 uint64_t getExtraAsUInt64(string_view name, uint64_t defaultValue = 0) const;
123 double getExtraAsDouble(string_view name, double defaultValue = 0) const;
126 bool getExtraAsBool(string_view name, bool defaultValue = false) const;
127
128 static int decodeQuery(string_view query, string& outKey, string& outValue);
129 static int urldecode(string_view in, string& out);
130
131 void setExtra(string_view name, string_view value);
132 void setExtra(string_view name, const char* value);
133 void setExtra(string_view name, bool value);
134 void setExtra(string_view name, const string& value);
135 template <typename T>
136 void setExtra(string_view name, T value) {
137 extras[string(name)] = std::to_string(value);
138 }
139
141 void unsetExtra(string_view name);
142
143 string fileHandlerName;
144 string fileName;
145 string uri;
146 vector<string> chunks;
147 vector<int64_t> chunkSizes;
148 Extras extras;
149};
150
151} // namespace vrs
Definition Compressor.cpp:113
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:42
map< string, string, std::less<> > Extras
Definition FileSpec.h:45