HCE project C++ developers source code library  1.1.1
HCE project developer library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
DRCETaskReducersManager.cpp
Go to the documentation of this file.
2 #include "Exceptions.hpp"
4 
8 
9 namespace HCE {
10 namespace drce{
11 namespace reduce_task {
12 
13 
15 
17 {
18  return HCE::types::MessageType::mtDrce;
19 }
20 
21 bool DRCETaskReducersManager::isReducerExist(unsigned long long reduceTaskId)
22 {
23  return taskReduceResults_.find( reduceTaskId ) != taskReduceResults_.end();
24 }
25 
26 void DRCETaskReducersManager::createReducer(unsigned long long reduceTaskId)
27 {
28  auto iter = taskReduceResults_.find( reduceTaskId );
29 
30  if( iter == taskReduceResults_.end() ) {
31  taskReduceResults_[ reduceTaskId ] = new DRCEResultData();
32  }
33 }
34 
35 void DRCETaskReducersManager::addDataInReducer(unsigned long long reduceTaskId, const std::string& json)
36 {
37  auto iter = taskReduceResults_.find( reduceTaskId );
38  if( iter == taskReduceResults_.end() ) {
39  throw NotFoundByKeyException( "Not exist task id " + std::to_string( reduceTaskId ) );
40  }
41 
42  DRCEResultData tmpResult;
43 
44  DRCEResultDataSerializator resultSerializator( tmpResult );
45 
46  if( resultSerializator.unserialize( json ) ) {
47 
48  Poco::SharedPtr< DRCEResultData > resultData = iter->second;
49 
50  for( size_t i = 0, n = tmpResult.getItemsCount(); i < n; i++ ) {
51  resultData->addDataItem( tmpResult.getDataItem( i ) );
52  }
53  } else {
54  throw WrongJSONStructureException( resultSerializator.getErrorMsg() );
55  }
56 }
57 
58 std::string DRCETaskReducersManager::runReduceTaskForTaskId(unsigned long long reduceTaskId)
59 {
60  auto iter = taskReduceResults_.find( reduceTaskId );
61 
62  if( iter == taskReduceResults_.end() ) {
63  throw NotFoundByKeyException( "Not exist task id " + std::to_string( reduceTaskId ) );
64  }
65 
66  Poco::SharedPtr< DRCEResultData > resultData = iter->second;
67 
68  DRCEResultDataSerializator resultSerializator( *resultData );
69  std::string json;
70 
71  if( resultSerializator.serialize( json ) ) {
72  return json;
73  }
74 
75  throw NotFoundByKeyException( resultSerializator.getErrorMsg() );
76 }
77 
78 void DRCETaskReducersManager::deleteReducerBy(unsigned long long reduceTaskId)
79 {
80  if( taskReduceResults_.erase( reduceTaskId ) != 1 ) {
81  throw NotFoundByKeyException( "Not exist task id " + std::to_string( reduceTaskId ) );
82  }
83 }
84 
85 }
86 }
87 }