-
Notifications
You must be signed in to change notification settings - Fork 9
/
RtDataStore.cpp
177 lines (138 loc) · 4.7 KB
/
RtDataStore.cpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*=========================================================================
* RtDataStore.cpp defines a class for data storage and retreival
*
* Copyright 2007-2013, the MURFI dev team.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#include"RtDataStore.h"
#define INITIAL_NOTIFY_LIST_LEN 128
RtDataStore::RtDataStore() {
notifyList.reserve(INITIAL_NOTIFY_LIST_LEN);
}
// destructor
RtDataStore::~RtDataStore() {
}
// add an output to be notified when new data arrives
void RtDataStore::addListener(RtDataListener *lis) {
notifyList.push_back(lis);
}
//*** data methods ***//
// hand off some data to be output
void RtDataStore::setData(RtData *data) {
mut.acquire();
// special check for processing done
if(data->getDataID().getModuleID() == "end-task") {
// update value of latestTR
if (data->getDataID().getTimePoint() != DATAID_NUM_UNSET_VALUE &&
data->getDataID().getTimePoint() != DATAID_NUM_WILDCARD_VALUE) {
latestTR = data->getDataID().getTimePoint();
}
mut.release();
return;
}
// don't allow wildcards on insertion
RtDataID insert = data->getDataID();
if(insert.hasWildcards()) {
insert.eliminateWildcards();
}
// put data into datastore with its dataID as the key
store[data->getDataID()] = data;
// add to availableData (needs a hard copy of the dataID)
setAvailableData(data->getDataID());
mut.release();
//debug
// set<RtDataID>::const_iterator it = getAvailableData();
//endebug
// notify listeners
for(vector<RtDataListener*>::iterator i = notifyList.begin();
i != notifyList.end(); i++) {
(*i)->notify(data->getDataID());
}
}
void RtDataStore::setAvailableData(RtDataID dataID) {
// remove timepoint if one is set
if(dataID.getTimePoint() != DATAID_NUM_UNSET_VALUE) {
dataID.setTimePoint(DATAID_NUM_WILDCARD_VALUE);
}
// put data id into available data (insert will only add a unique value)
// TODO will a position iterator add any efficiency here?
availableData.insert(dataID);
}
// get data by id
//
// NOTE: this function is much more efficient if the requested data is
// specified fully, i.e. there are no wildcards in the dataID (see RtDataID.h)
// avoid using wildcards if possible
RtData *RtDataStore::getData(RtDataID dataID) {
// fill in wildcards (search through available data makes this inefficient)
if(dataID.hasWildcards()) {
// find corresponding full id by an exhaustive search through available
// data
for(set<RtDataID>::const_iterator id = availableData.begin();
id != availableData.end(); id++) {
RtDataID avail = (*id);
// handle potential timepoint-less data
unsigned int tp = avail.getTimePoint();
if(dataID.getTimePoint() != DATAID_NUM_UNSET_VALUE) {
avail.setTimePoint(DATAID_NUM_WILDCARD_VALUE);
}
else {
avail.setTimePoint(DATAID_NUM_UNSET_VALUE);
}
if (avail == dataID) { // == respects wildcards
// copy, preserving timepoint if not a timepointless datum
unsigned int origTp = dataID.getTimePoint();
dataID = avail;
if(tp != DATAID_NUM_UNSET_VALUE) {
dataID.setTimePoint(origTp);
}
else {
dataID.setTimePoint(tp);
}
break;
}
}
// finally, handle wildcard in timepoint field by using the latest timepoint
if (dataID.getTimePoint() == DATAID_NUM_WILDCARD_VALUE) {
dataID.setTimePoint(latestTR);
}
if(dataID.hasWildcards()) {
cerr << "filling in wildcards failed. no match found." << endl;
return NULL;
}
}
// iterator for map
map<RtDataID,RtData*,RtDataIDCompare>::const_iterator it;
mut.acquire();
it = store.find(dataID);
// if not found
if(it == store.end()) {
mut.release();
return NULL;
}
mut.release();
return (*it).second;
}
// get latest data by id
RtData *RtDataStore::getLatestData(RtDataID dataID) {
// set timepoint in dataID
dataID.setTimePoint(latestTR);
return getData(dataID);
}
// get available data
set<RtDataID>::const_iterator RtDataStore::getAvailableData() {
set<RtDataID>::const_iterator it = availableData.begin();
return it;
}