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 <map>
20#include <string>
21#include <utility>
22#include <vector>
23
24namespace vrs {
25using std::map;
26using std::string;
27using std::vector;
28
37struct FileSpec {
38 FileSpec() = default;
39 FileSpec(string filehandler, const vector<string>& chunks)
40 : fileHandlerName{std::move(filehandler)}, chunks{chunks} {}
41 FileSpec(string filehandler, const vector<string>&& chunks)
42 : fileHandlerName{std::move(filehandler)}, chunks{chunks} {}
43 explicit FileSpec(const vector<string>& chunks) : chunks{chunks} {}
44 explicit FileSpec(vector<string>&& chunks) : chunks{std::move(chunks)} {}
45
47 void clear();
48
49 bool empty() const;
50
51 bool isDiskFile() const;
52
53 static int parseUri(
54 const string& uri,
55 string& outScheme,
56 string& outPath,
57 map<string, string>& outQueryParams);
58
65 int fromPathJsonUri(const string& pathJsonUri, const string& defaultFileHandlerName = {});
66
68 string toPathJsonUri() const;
69
70 // Parse json and extract file specs, with optional extra parameters.
71 // @param jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
72 // "filename":"file.vrs"}.
73 // @return True if parsing the json succeeded.
74 bool fromJson(const string& jsonStr);
75
76 // Generate json string from a file spec.
77 // @return jsonStr: ex. {"storage": "mystorage", "chunks":["chunk1", "chunk2"],
78 // "filename":"file.vrs"}.
79 string toJson() const;
80
82 int parseUri();
83
85 bool hasChunkSizes() const;
87 int64_t getFileSize() const;
89 string getSourceLocation() const;
91 string getEasyPath() const;
93 string getFileName() const;
94
96 string getXXHash() const;
97
99 bool operator==(const FileSpec& rhs) const;
100
102 const string& getExtra(const string& name) const;
104 const string& getExtra(const string& name, const string& defaultValue) const;
106 bool hasExtra(const string& name) const;
109 int getExtraAsInt(const string& name, int defaultValue = 0) const;
112 int64_t getExtraAsInt64(const string& name, int64_t defaultValue = 0) const;
115 uint64_t getExtraAsUInt64(const string& name, uint64_t defaultValue = 0) const;
118 double getExtraAsDouble(const string& name, double defaultValue = 0) const;
121 bool getExtraAsBool(const string& name, bool defaultValue = false) const;
122
123 static int decodeQuery(const string& query, string& outKey, string& outValue);
124 static int urldecode(const string& in, string& out);
125
126 void setExtra(const string& name, const string& value);
127 void setExtra(const string& name, const char* value);
128 void setExtra(const string& name, bool value);
129 template <typename T>
130 void setExtra(const string& name, T value) {
131 extras[name] = std::to_string(value);
132 }
133
135 void unsetExtra(const string& name);
136
137 string fileHandlerName;
138 string fileName;
139 string uri;
140 vector<string> chunks;
141 vector<int64_t> chunkSizes;
142 map<string, string> extras;
143};
144
145} // namespace vrs
Definition AsyncDiskFileChunk.hpp:49
Generalized file descriptor class, allowing the efficient representation of complex file objects,...
Definition FileSpec.h:37
double getExtraAsDouble(const string &name, double defaultValue=0) const
Definition FileSpec.cpp:415
uint64_t getExtraAsUInt64(const string &name, uint64_t defaultValue=0) const
Definition FileSpec.cpp:410
string toPathJsonUri() const
Reverse operation as fromPathJsonUri, as possible.
Definition FileSpec.cpp:186
bool hasChunkSizes() const
Tell if we have chunks and all of them has a file size.
Definition FileSpec.cpp:257
bool operator==(const FileSpec &rhs) const
Test equality (for testing)
Definition FileSpec.cpp:378
string getSourceLocation() const
Get the location of the object, which is the uri (if any), or the file handler.
Definition FileSpec.cpp:284
int parseUri()
Parse the uri field already set, overwriting other fields on success.
Definition FileSpec.cpp:249
int fromPathJsonUri(const string &pathJsonUri, const string &defaultFileHandlerName={})
Definition FileSpec.cpp:146
const string & getExtra(const string &name) const
Get an extra parameter, or the empty string.
Definition FileSpec.cpp:385
bool hasExtra(const string &name) const
Tell if an extra parameter is defined.
Definition FileSpec.cpp:396
int64_t getExtraAsInt64(const string &name, int64_t defaultValue=0) const
Definition FileSpec.cpp:405
int64_t getFileSize() const
Get the total size of the object, or -1 if don't know.
Definition FileSpec.cpp:261
bool getExtraAsBool(const string &name, bool defaultValue=false) const
Definition FileSpec.cpp:420
void unsetExtra(const string &name)
Unset an extra parameter.
Definition FileSpec.cpp:425
string getXXHash() const
Get signature of the path.
Definition FileSpec.cpp:362
int getExtraAsInt(const string &name, int defaultValue=0) const
Definition FileSpec.cpp:400
void clear()
clear all the fields.
Definition FileSpec.cpp:49
string getFileName() const
Get filename, if possible.
Definition FileSpec.cpp:352
string getEasyPath() const
Logical reverse operation from fromPathJsonUri, but kept minimal for logging.
Definition FileSpec.cpp:303