-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathExternalVector.h
More file actions
115 lines (95 loc) · 2.65 KB
/
ExternalVector.h
File metadata and controls
115 lines (95 loc) · 2.65 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
/**
* 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 "internal/ResourceHandle.h"
#include <type_traits>
#include <vector>
#include <boost/noncopyable.hpp>
#include <boost/optional.hpp>
namespace flatdata
{
template < typename T >
class ExternalVector
{
public:
using ValueType = typename T::MutatorType;
using ConstValueType = typename T::AccessorType;
using StreamType = typename T::MutatorType::StreamType;
using ConstStreamType = typename T::AccessorType::StreamType;
public:
explicit ExternalVector( std::unique_ptr< ResourceHandle > impl );
size_t size( ) const;
bool empty( ) const;
/**
* @brief Add new element to the array.
* @note might flush existing elements to disk
* @return element mutator
*/
ValueType grow( );
/**
* @brief Flushes remaining elements in buffer to disk and closes the vector.
* After the vector is closed, no elements can be added to it anymore.
* @return ArrayView to the written data. May fail, in this case ArrayView is
* invalid (cf. bool operator of ArrayView).
*/
ArrayView< T > close( );
private:
void flush( );
private:
std::vector< typename std::remove_pointer< StreamType >::type > m_data;
std::unique_ptr< ResourceHandle > m_array;
size_t m_size = 0;
};
// -----------------------------------------------------------------------------
template < typename T >
ExternalVector< T >::ExternalVector( std::unique_ptr< ResourceHandle > impl )
: m_array( std::move( impl ) )
{
m_data.resize( PADDING_SIZE );
}
template < typename T >
size_t
ExternalVector< T >::size( ) const
{
return m_size;
}
template < typename T >
bool
ExternalVector< T >::empty( ) const
{
return m_size == 0;
}
template < typename T >
typename ExternalVector< T >::ValueType
ExternalVector< T >::grow( )
{
if ( m_data.size( ) > 1024 * 1024 * 32 )
{
flush( );
}
size_t old_size = m_data.size( );
m_data.resize( old_size + ValueType::size_in_bytes( ) );
m_size++;
return ValueType{ m_data.data( ) + old_size - PADDING_SIZE };
}
template < typename T >
void
ExternalVector< T >::flush( )
{
m_array->write( m_data.data( ), m_data.size( ) - PADDING_SIZE );
m_data.resize( 0 );
m_data.resize( PADDING_SIZE );
}
template < typename T >
ArrayView< T >
ExternalVector< T >::close( )
{
flush( );
MemoryDescriptor data = m_array->close( );
return data ? ArrayView< T >{ data.data( ), data.data( ) + data.size_in_bytes( ) }
: ArrayView< T >{ };
}
} // namespace flatdata