-
Notifications
You must be signed in to change notification settings - Fork 13
/
Channel.h
126 lines (104 loc) · 4.36 KB
/
Channel.h
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
#ifndef CHANNEL_INCLUDE_H
#define CHANNEL_INCLUDE_H
// C++
#include <string>
#include <vector>
// Local includes
#include "ChannelInfo.h"
#include "DataSample.h"
#include "KVS.h"
#include "Tile.h"
/// \class Channel Channel.h
///
/// Implements channel reference to KVS.
///
/// It's OK for there to be multiple Channel instances in multiple processes referring to the same channel in the KVS.
// TODO: compute these instead of hardcoding
#define BT_CHANNEL_MAX_TILE_SIZE (1024*1024)
#define BT_CHANNEL_DOUBLE_SAMPLES 32768
#define BT_CHANNEL_STRING_SAMPLES 8192
class Channel {
public:
Channel(KVS &kvs, int owner_id, const std::string &name, size_t max_tile_size=BT_CHANNEL_MAX_TILE_SIZE);
Channel(KVS &kvs, const std::string &uid_and_name, size_t max_tile_size=BT_CHANNEL_MAX_TILE_SIZE);
/// \class Locker Channel.h
///
/// Lock channel upon construction and unlock channel upon destruction.
///
/// Lock is an advisory lock only; various calls to modify channel will be allowed whether Channel is locked or not, and regardless
/// of which process might own the lock. In order to implement proper synchronization, the caller for Channel should obtain lock for Channel
/// when appropriate.
class Locker {
public:
Locker(const Channel &ch);
~Locker();
private:
const Channel &m_ch;
KVSLocker m_locker;
};
/// Read channel metainformation
/// \param info Returns metainformation, if read
/// \return true if channel exists and read successful; false if channel does not exist
bool read_info(ChannelInfo &info) const;
void write_info(const ChannelInfo &info);
bool has_tile(TileIndex ti) const;
bool read_tile(TileIndex ti, Tile &tile) const;
void write_tile(TileIndex ti, const Tile &tile);
bool delete_tile(TileIndex ti);
void create_tile(TileIndex ti);
double level_from_rate(double samples_per_second) const;
void add_data(const std::vector<DataSample<double> > &data, DataRanges *channel_ranges = NULL);
void add_data(const std::vector<DataSample<std::string> > &data, DataRanges *channel_ranges = NULL);
void read_data(std::vector<DataSample<double> > &data, double begin, double end) const;
std::string tile_key(TileIndex ti) const;
bool tile_exists(TileIndex ti) const;
std::string dump_tile_summaries() const;
bool read_tile_or_closest_ancestor(TileIndex ti, TileIndex &ret_index, Tile &ret) const;
void read_tiles_in_range(Range times, bool (*callback)(const Tile &t, Range times), int desired_level) const;
std::string descriptor() const;
/// Get subchannel names
/// \param kvs store
/// \param owner_id Owner id
/// \param prefix Channel name prefix. Empty string for top level
/// \param names Subchannel names are added to this vector of strings
/// \param nlevels Number of levels to recurse; if omitted, recurse all levels
static void get_subchannel_names(KVS &kvs, int owner_id,
const std::string &prefix,
std::vector<std::string> &names,
unsigned int nlevels = -1);
static int total_tiles_read;
static int total_tiles_written;
static int verbosity;
TileIndex find_child_overlapping_time(TileIndex ti, double t, int desired_level) const;
TileIndex find_successive_tile(TileIndex root, TileIndex ti, int desired_level) const;
private:
KVS &m_kvs;
int m_owner_id;
std::string m_name;
size_t m_max_tile_size;
std::string dump_tile_summaries_internal(TileIndex ti=TileIndex::null(), int level=0) const;
std::string key_prefix() const;
std::string metainfo_key() const;
TileIndex split_tile_if_needed(TileIndex ti, Tile &tile);
void create_parent_tile_from_children(TileIndex ti, Tile &parent, Tile children[]);
void move_root_upwards(TileIndex new_root, TileIndex old_root);
template <class T>
void add_data_internal(const std::vector<DataSample<T> > &data, DataRanges *channel_ranges);
};
/// \class ChannelLocker Channel.h
///
/// Lock channel upon construction and unlock channel upon destruction.
///
/// Lock is an advisory lock only; various calls to modify channel will be allowed whether Channel is locked or not, and regardless
/// of which process might own the lock. In order to implement proper synchronization, the caller for Channel should obtain lock for Channel
/// when appropriate.
class ChannelLocker {
public:
///
ChannelLocker(Channel &ch);
/// Unlock channel upon destruction
~ChannelLocker();
private:
Channel &m_ch;
};
#endif