-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathResourceStorage.h
More file actions
126 lines (105 loc) · 4.12 KB
/
ResourceStorage.h
File metadata and controls
126 lines (105 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/**
* Copyright (c) 2017 HERE Europe B.V.
* See the LICENSE file in the root of this project for license details.
*/
#pragma once
#include "ArrayView.h"
#include "ExternalVector.h"
#include "MemoryDescriptor.h"
#include "MultiVector.h"
#include "internal/ResourceStorageCommon.h"
#include "internal/Writer.h"
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
#include <fstream>
#include <memory>
namespace flatdata
{
class MemoryMappedFileStorage;
/**
* @brief Hierarchical Resource Storage.
*
* Manages and returns resources corresponding to their keys. Keys can be slash-separated('/').
* Manages schema for each resource and checks it on query.
* Resource storage is expected to provide read-write access to resources
*/
class ResourceStorage : private boost::noncopyable
{
public:
virtual ~ResourceStorage( ) = default;
/**
* @brief Read resource.
* @param key resource key.
* @param schema expected resource schema.
* @return resource or empty object in case resource schema doesn't match one provided.
*/
template < typename T >
boost::optional< T > read( const char* resource_name, const char* schema );
/**
* @brief Write resource.
* @param data resource data.
* @param key resource key.
* @param schema resource schema to store.
* @return true if operation is successful, false - otherwise
*/
template < typename T >
bool write( const char* resource_name, const char* schema, T data );
/**
* @brief Creates managed external vector, allowing to conserve memory usage
* by flushing data to the resource storage from time to time, in case
* it is not memory-based.
* @return growable array or nullptr on error
*/
template < typename T >
ExternalVector< T > create_external_vector( const char* resource_name, const char* schema );
/**
* @brief Creates managed multi-vector, allowing to conserve memory usage
* by flushing data to the resource storage from time to time, in case
* it is not memory-based.
* @return MultiVector or nullptr on error
*/
template < typename IndexType, typename... Args >
MultiVector< IndexType, Args... > create_multi_vector( const char* resource_name,
const char* schema );
/**
* @brief Provides memory descriptor for a resource schema associated with the given key
* @return memory descriptor associated with the key or nullptr on error
*/
MemoryDescriptor read_schema( const char* key );
public:
/**
* @brief Create a directory by given key. Succeeds if directory already exists.
* @return ResourceStorage corresponding to the directory or nullptr on error
*/
virtual std::unique_ptr< ResourceStorage > create_directory( const char* key ) = 0;
/**
* @brief Get a directory by given key
* @return ResourceStorage corresponding to the directory or nullptr on error
*/
virtual std::unique_ptr< ResourceStorage > directory( const char* key ) = 0;
/**
* @brief Check if resource exists
*/
virtual bool exists( const char* key ) = 0;
protected:
/**
* @brief Creates output stream for given key
* @return Output stream associated with the key or nullptr on error
*/
virtual std::shared_ptr< std::ostream > create_output_stream( const char* key ) = 0;
/**
* @brief Provides memory descriptor for a resource associated with the given key
* @return memory descriptor associated with the key or nullptr on error
*/
virtual MemoryDescriptor read_resource( const char* key ) = 0;
private:
bool write_to_stream( const char* resource_name,
const char* schema,
const unsigned char* data,
size_t size_in_bytes );
bool write_schema( const char* resource_name, const char* schema );
MemoryDescriptor read_and_check_schema( const char* resource_name,
const char* expected_schema );
};
} // namespace flatdata
#include "internal/ResourceStorage.inl"