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
26namespace vrs {
27using std::map;
28using std::string;
29using std::string_view;
30using std::vector;
31
40struct FileSpec {
43 using Extras = map<string, string, std::less<>>;
44
45 FileSpec() = default;
46 FileSpec(string filehandler, const vector<string>& chunksIn)
47 : fileHandlerName{std::move(filehandler)}, chunks{chunksIn} {}
48 FileSpec(string filehandler, const vector<string>&& chunksIn)
49 : fileHandlerName{std::move(filehandler)}, chunks{chunksIn} {}
50 explicit FileSpec(const vector<string>& chunksIn) : chunks{chunksIn} {}
51 explicit FileSpec(vector<string>&& chunksIn) : chunks{std::move(chunksIn)} {}
52
54 void clear();
55
56 bool empty() const;
57
58 bool isDiskFile() const;
59
60 static int parseUri(string_view uri, string& outScheme, string& outPath, Extras& outQueryParams);
61
68 int fromPathJsonUri(string_view pathJsonUri, string_view defaultFileHandlerName = {});
69
71 string toPathJsonUri() const;
72
73 // Parse json and extract file specs, with optional extra parameters.
74 // @param jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
75 // "filename":"file.vrs"}.
76 // @return True if parsing the json succeeded.
77 bool fromJson(string_view jsonStr);
78
79 // Generate json string from a file spec.
80 // @return jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
81 // "filename":"file.vrs"}.
82 string toJson() const;
83
85 int parseUri();
86
88 bool hasChunkSizes() const;
90 int64_t getFileSize() const;
92 string getSourceLocation() const;
94 string getEasyPath() const;
96 string getFileName() const;
97
99 string getXXHash() const;
100
102 bool operator==(const FileSpec& rhs) const;
103
105 const string& getExtra(string_view name) const;
107 bool getExtra(string_view name, string& outValue) const;
109 bool hasExtra(string_view name) const;
112 int getExtraAsInt(string_view name, int defaultValue = 0) const;
115 int64_t getExtraAsInt64(string_view name, int64_t defaultValue = 0) const;
118 uint64_t getExtraAsUInt64(string_view name, uint64_t defaultValue = 0) const;
121 double getExtraAsDouble(string_view name, double defaultValue = 0) const;
124 bool getExtraAsBool(string_view name, bool defaultValue = false) const;
125
126 static int decodeQuery(string_view query, string& outKey, string& outValue);
127 static int urldecode(string_view in, string& out);
128
129 void setExtra(string_view name, string_view value);
130 void setExtra(string_view name, const char* value);
131 void setExtra(string_view name, bool value);
132 void setExtra(string_view name, const string& value);
133 template <typename T>
134 void setExtra(string_view name, T value) {
135 extras[string(name)] = std::to_string(value);
136 }
137
139 void unsetExtra(string_view name);
140
141 string fileHandlerName;
142 string fileName;
143 string uri;
144 vector<string> chunks;
145 vector<int64_t> chunkSizes;
146 Extras extras;
147};
148
149} // namespace vrs
Definition Compressor.cpp:113
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:40
string toPathJsonUri() const
Reverse operation as fromPathJsonUri, as possible.
Definition FileSpec.cpp:208
bool hasChunkSizes() const
Tell if we have chunks and all of them has a file size.
Definition FileSpec.cpp:279
int64_t getExtraAsInt64(string_view name, int64_t defaultValue=0) const
Definition FileSpec.cpp:431
bool operator==(const FileSpec &rhs) const
Test equality (for testing)
Definition FileSpec.cpp:400
string getSourceLocation() const
Get the location of the object, which is the uri (if any), or the file handler.
Definition FileSpec.cpp:306
void unsetExtra(string_view name)
Unset an extra parameter.
Definition FileSpec.cpp:451
int parseUri()
Parse the uri field already set, overwriting other fields on success.
Definition FileSpec.cpp:271
bool hasExtra(string_view name) const
Tell if an extra parameter is defined.
Definition FileSpec.cpp:422
const string & getExtra(string_view name) const
Get an extra parameter, or the empty string.
Definition FileSpec.cpp:407
map< string, string, std::less<> > Extras
Definition FileSpec.h:43
double getExtraAsDouble(string_view name, double defaultValue=0) const
Definition FileSpec.cpp:441
uint64_t getExtraAsUInt64(string_view name, uint64_t defaultValue=0) const
Definition FileSpec.cpp:436
int64_t getFileSize() const
Get the total size of the object, or -1 if don't know.
Definition FileSpec.cpp:283
string getXXHash() const
Get signature of the path.
Definition FileSpec.cpp:384
int fromPathJsonUri(string_view pathJsonUri, string_view defaultFileHandlerName={})
Definition FileSpec.cpp:168
void clear()
clear all the fields.
Definition FileSpec.cpp:90
bool getExtraAsBool(string_view name, bool defaultValue=false) const
Definition FileSpec.cpp:446
string getFileName() const
Get filename, if possible.
Definition FileSpec.cpp:374
string getEasyPath() const
Logical reverse operation from fromPathJsonUri, but kept minimal for logging.
Definition FileSpec.cpp:325
int getExtraAsInt(string_view name, int defaultValue=0) const
Definition FileSpec.cpp:426