]> SALOME platform Git repositories - modules/yacs.git/blob - src/SALOMESDS/SALOMESDS_Transaction.cxx
Salome HOME
Implementation of producer/consumer mechanism and transaction mechanism for global...
[modules/yacs.git] / src / SALOMESDS / SALOMESDS_Transaction.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony GEAY (EDF R&D)
20
21 #include "SALOMESDS_Transaction.hxx"
22 #include "SALOMESDS_Exception.hxx"
23 #include "SALOMESDS_PickelizedPyObjServer.hxx"
24
25 #include <sstream>
26
27 using namespace SALOMESDS;
28
29 void Transaction::FromByteSeqToVB(const SALOME::ByteVec& bsToBeConv, std::vector<unsigned char>& ret)
30 {
31   std::size_t sz(bsToBeConv.length());
32   ret.resize(sz);
33   unsigned char *buf(const_cast<unsigned char *>(&ret[0]));
34   for(std::size_t i=0;i<sz;i++)
35     buf[i]=bsToBeConv[i];
36 }
37
38 void Transaction::FromVBToByteSeq(const std::vector<unsigned char>& bsToBeConv, SALOME::ByteVec& ret)
39 {
40   std::size_t sz(bsToBeConv.size());
41   ret.length(sz);
42   for(std::size_t i=0;i<sz;i++)
43     ret[i]=bsToBeConv[i];
44 }
45
46 Transaction::~Transaction()
47 {
48 }
49
50 TransactionVarCreate::TransactionVarCreate(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& constValue):Transaction(dsct,varName)
51 {
52   FromByteSeqToVB(constValue,_data);
53 }
54
55 void TransactionVarCreate::prepareRollBackInCaseOfFailure()
56 {//nothing it is not a bug
57   checkNotAlreadyExisting();
58 }
59
60 void TransactionVarCreate::rollBack()
61 {
62   if(_dsct->existVar(_var_name.c_str()))
63     _dsct->deleteVar(_var_name.c_str());
64 }
65
66 /*!
67  TODO : To be implemented.
68  */
69 void TransactionVarCreate::notify()
70 {
71 }
72
73 void TransactionRdOnlyVarCreate::perform()
74 {
75   SALOME::ByteVec data2;
76   FromVBToByteSeq(_data,data2);
77   _dsct->createRdOnlyVarInternal(_var_name,data2);
78 }
79
80 void TransactionRdExtVarCreate::perform()
81 {
82   SALOME::ByteVec data2;
83   FromVBToByteSeq(_data,data2);
84   _dsct->createRdExtVarInternal(_var_name,data2);
85 }
86
87 void TransactionRdWrVarCreate::perform()
88 {
89   SALOME::ByteVec data2;
90   FromVBToByteSeq(_data,data2);
91   _dsct->createRdWrVarInternal(_var_name,data2);
92 }
93
94 TransactionDictModify::TransactionDictModify(DataScopeServerTransaction *dsct, const std::string& varName):Transaction(dsct,varName),_varc(0)
95 {
96   _varc=checkVarExistingAndDict();
97 }
98
99 void TransactionDictModify::prepareRollBackInCaseOfFailure()
100 {
101   _zeDataBefore.clear();
102   PyObject *zeDictPy(_varc->getPyObj());
103   Py_XINCREF(zeDictPy);
104   _zeDataBefore=_varc->pickelize(zeDictPy);
105 }
106
107 void TransactionDictModify::rollBack()
108 {
109   PyObject *obj(_varc->getPyObjFromPickled(_zeDataBefore));
110   _varc->setNewPyObj(obj);
111   _zeDataBefore.clear();
112 }
113
114 TransactionAddKeyValue::TransactionAddKeyValue(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):TransactionDictModify(dsct,varName)
115 {
116   std::vector<unsigned char> key2,value2;
117   FromByteSeqToVB(key,key2);
118   FromByteSeqToVB(value,value2);
119   _key=PickelizedPyObjServer::GetPyObjFromPickled(key2,_dsct);
120   _value=PickelizedPyObjServer::GetPyObjFromPickled(value2,_dsct);
121 }
122
123 void TransactionAddKeyValue::prepareRollBackInCaseOfFailure()
124 {
125   TransactionDictModify::prepareRollBackInCaseOfFailure();
126   _dsct->pingKey(_key);// check that key is OK with all waiting keys
127 }
128
129 void TransactionAddKeyValue::notify()
130 {
131   _dsct->notifyKey(_key,_value);
132 }
133
134 TransactionAddKeyValue::~TransactionAddKeyValue()
135 {
136   Py_XDECREF(_key);
137   Py_XDECREF(_value);
138 }
139
140 TransactionAddKeyValueHard::TransactionAddKeyValueHard(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):TransactionAddKeyValue(dsct,varName,key,value)
141 {
142 }
143
144 void TransactionAddKeyValueHard::perform()
145 {
146   _varc->addKeyValueHard(_key,_value);
147 }
148
149 TransactionAddKeyValueErrorIfAlreadyExisting::TransactionAddKeyValueErrorIfAlreadyExisting(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value):TransactionAddKeyValue(dsct,varName,key,value)
150 {
151   _varc->checkKeyNotAlreadyPresent(_key);
152 }
153
154 void TransactionAddKeyValueErrorIfAlreadyExisting::perform()
155 {
156   _varc->addKeyValueErrorIfAlreadyExisting(_key,_value);
157 }
158
159 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::TransactionRemoveKeyInVarErrorIfNotAlreadyExisting(DataScopeServerTransaction *dsct, const std::string& varName, const SALOME::ByteVec& key):TransactionDictModify(dsct,varName),_key(0)
160 {
161   std::vector<unsigned char> key2;
162   FromByteSeqToVB(key,key2);
163   _key=PickelizedPyObjServer::GetPyObjFromPickled(key2,_dsct);
164 }
165
166 void TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::perform()
167 {
168   _varc->removeKeyInVarErrorIfNotAlreadyExisting(_key);
169 }
170
171 /*!
172  * not implementation it is not a bug !
173  */
174 void TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::notify()
175 {
176 }
177
178 TransactionRemoveKeyInVarErrorIfNotAlreadyExisting::~TransactionRemoveKeyInVarErrorIfNotAlreadyExisting()
179 {
180   Py_XDECREF(_key);
181 }