From c52b6a2d91f19fe9d8481665a86cb02e2619dc2c Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Tue, 4 Aug 2015 08:15:48 +0200 Subject: [PATCH] Support of multi key,var append in global vars. --- idl/SALOME_SDS.idl | 5 +- src/SALOMESDS/SALOMESDS_DataScopeServer.cxx | 95 ++++++++++++++----- src/SALOMESDS/SALOMESDS_DataScopeServer.hxx | 4 +- .../SALOMESDS_PickelizedPyObjServer.cxx | 13 ++- .../SALOMESDS_PickelizedPyObjServer.hxx | 1 + src/SALOMESDS/TestSalomeSDS.py | 23 +++++ 6 files changed, 111 insertions(+), 30 deletions(-) diff --git a/idl/SALOME_SDS.idl b/idl/SALOME_SDS.idl index a7bb219bd..b3ceaa958 100644 --- a/idl/SALOME_SDS.idl +++ b/idl/SALOME_SDS.idl @@ -25,6 +25,7 @@ module SALOME { typedef sequence StringVec; typedef sequence ByteVec; + typedef sequence SeqOfByteVec; interface DataScopeServer; @@ -65,6 +66,8 @@ module SALOME boolean existVar(in string varName) raises (SALOME::SALOME_Exception); void deleteVar(in string varName) raises (SALOME::SALOME_Exception); void shutdownIfNotHostedByDSM(); + ByteVec fetchSerializedContent(in string varName) raises (SALOME::SALOME_Exception); + SeqOfByteVec getAllKeysOfVarWithTypeDict(in string varName) raises (SALOME::SALOME_Exception); }; interface DataScopeServer : DataScopeServerBase @@ -88,12 +91,12 @@ module SALOME interface DataScopeServerTransaction : DataScopeServerBase { - ByteVec fetchSerializedContent(in string varName) raises (SALOME::SALOME_Exception); Transaction createRdOnlyVarTransac(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction createRdExtVarTransac(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction createRdWrVarTransac(in string varName, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction addKeyValueInVarHard(in string varName, in ByteVec keyValue, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction addKeyValueInVarErrorIfAlreadyExisting(in string varName, in ByteVec keyValue, in ByteVec constValue) raises (SALOME::SALOME_Exception); + void addKeyValueInVarErrorIfAlreadyExistingNow(in string varName, in ByteVec keyValue, in ByteVec constValue) raises (SALOME::SALOME_Exception); Transaction removeKeyInVarErrorIfNotAlreadyExisting(in string varName, in ByteVec keyValue) raises (SALOME::SALOME_Exception); KeyWaiter waitForKeyInVar(in string varName, in ByteVec keyVal) raises (SALOME::SALOME_Exception); KeyWaiter waitForKeyInVarAndKillIt(in string varName, in ByteVec keyVal, out Transaction transac) raises (SALOME::SALOME_Exception); diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx index ee1f9e07b..9dec327d6 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.cxx @@ -156,6 +156,52 @@ void DataScopeServerBase::shutdownIfNotHostedByDSM() } } +SALOME::ByteVec *DataScopeServerBase::fetchSerializedContent(const char *varName) +{ + BasicDataServer *var(retrieveVarInternal2(varName)); + PickelizedPyObjServer *varc(dynamic_cast(var)); + if(!varc) + { + std::ostringstream oss; oss << "DataScopeServerBase::fetchSerializedContent : var \"" << varName << "\" exists but it is not serialized !"; + throw Exception(oss.str()); + } + return varc->fetchSerializedContent(); +} + +SALOME::SeqOfByteVec *DataScopeServerBase::getAllKeysOfVarWithTypeDict(const char *varName) +{ + BasicDataServer *var(retrieveVarInternal2(varName)); + PickelizedPyObjServer *varc(dynamic_cast(var)); + if(!varc) + { + std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not serialized !"; + throw Exception(oss.str()); + } + if(!varc->isDict()) + { + std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" exists but it is not a PyDict !"; + throw Exception(oss.str()); + } + PyObject *keys(PyDict_Keys(varc->getPyObj())); + if(!PyList_Check(keys)) + { + std::ostringstream oss; oss << "DataScopeServerBase::getAllKeysOfVarWithTypeDict : var \"" << varName << "\" has keys but not of type list !"; + throw Exception(oss.str()); + } + Py_ssize_t sz(PyList_Size(keys)); + SALOME::SeqOfByteVec *ret(new SALOME::SeqOfByteVec); + ret->length(sz); + for(Py_ssize_t i=0;ipickelize(item));//item consumed + PickelizedPyObjServer::FromCppToByteSeq(pickel,(*ret)[i]); + } + Py_XDECREF(keys); + return ret; +} + void DataScopeServerBase::initializePython(int argc, char *argv[]) { Py_Initialize(); @@ -376,18 +422,6 @@ void DataScopeServerTransaction::createRdWrVarInternal(const std::string& varNam _vars.push_back(p); } -SALOME::ByteVec *DataScopeServerTransaction::fetchSerializedContent(const char *varName) -{ - BasicDataServer *var(retrieveVarInternal2(varName)); - PickelizedPyObjServer *varc(dynamic_cast(var)); - if(!varc) - { - std::ostringstream oss; oss << "DataScopeServerTransaction::fetchSerializedContent : var \"" << varName << "\"exists but it is not serialized !"; - throw Exception(oss.str()); - } - return varc->fetchSerializedContent(); -} - SALOME::Transaction_ptr DataScopeServerTransaction::createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue) { checkNotAlreadyExistingVar(varName); @@ -500,6 +534,31 @@ SALOME::Transaction_ptr DataScopeServerTransaction::addKeyValueInVarErrorIfAlrea TransactionAddKeyValueErrorIfAlreadyExisting *ret(new TransactionAddKeyValueErrorIfAlreadyExisting(this,varName,key,value)); CORBA::Object_var obj(ret->activate()); return SALOME::Transaction::_narrow(obj); +}; + +class TrustTransaction +{ +public: + TrustTransaction():_must_rollback(0),_ptr(0) { } + void setTransaction(Transaction *t, bool *mustRollback) { if(!t || !mustRollback) throw Exception("TrustTransaction Error #1"); _ptr=t; _must_rollback=mustRollback; _ptr->prepareRollBackInCaseOfFailure(); } + void operate() { _ptr->perform(); } + ~TrustTransaction() { if(!_ptr) return ; if(*_must_rollback) _ptr->rollBack(); } +private: + bool *_must_rollback; + Transaction *_ptr; +}; + +void DataScopeServerTransaction::addKeyValueInVarErrorIfAlreadyExistingNow(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value) +{ + checkVarExistingAndDict(varName); + TransactionAddKeyValueErrorIfAlreadyExisting ret(this,varName,key,value); + { + bool mustRollback(true); + TrustTransaction t; + t.setTransaction(&ret,&mustRollback); + t.operate(); + mustRollback=false; + } } SALOME::Transaction_ptr DataScopeServerTransaction::removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const SALOME::ByteVec& key) @@ -546,18 +605,6 @@ SALOME::ByteVec *DataScopeServerTransaction::waitForMonoThrRev(SALOME::KeyWaiter retc->waitForMonoThr(); } -class TrustTransaction -{ -public: - TrustTransaction():_must_rollback(0),_ptr(0) { } - void setTransaction(Transaction *t, bool *mustRollback) { if(!t || !mustRollback) throw Exception("TrustTransaction Error #1"); _ptr=t; _must_rollback=mustRollback; _ptr->prepareRollBackInCaseOfFailure(); } - void operate() { _ptr->perform(); } - ~TrustTransaction() { if(!_ptr) return ; if(*_must_rollback) _ptr->rollBack(); } -private: - bool *_must_rollback; - Transaction *_ptr; -}; - void DataScopeServerTransaction::atomicApply(const SALOME::ListOfTransaction& transactions) { std::size_t sz(transactions.length()); diff --git a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx index db76fbf41..ab7e810dc 100644 --- a/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx +++ b/src/SALOMESDS/SALOMESDS_DataScopeServer.hxx @@ -53,6 +53,8 @@ namespace SALOMESDS BasicDataServer *retrieveVarInternal2(const std::string& varName); void deleteVar(const char *varName); void shutdownIfNotHostedByDSM(); + SALOME::ByteVec *fetchSerializedContent(const char *varName); + SALOME::SeqOfByteVec *getAllKeysOfVarWithTypeDict(const char *varName); ~DataScopeServerBase(); public: void initializePython(int argc, char *argv[]); @@ -111,12 +113,12 @@ namespace SALOMESDS void notifyKey(PyObject *keyObj, PyObject *valueObj); SALOME::ByteVec *waitForMonoThrRev(SALOME::KeyWaiter_ptr kw); public://remotely callable - SALOME::ByteVec *fetchSerializedContent(const char *varName); SALOME::Transaction_ptr createRdOnlyVarTransac(const char *varName, const SALOME::ByteVec& constValue); SALOME::Transaction_ptr createRdExtVarTransac(const char *varName, const SALOME::ByteVec& constValue); SALOME::Transaction_ptr createRdWrVarTransac(const char *varName, const SALOME::ByteVec& constValue); SALOME::Transaction_ptr addKeyValueInVarHard(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); SALOME::Transaction_ptr addKeyValueInVarErrorIfAlreadyExisting(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); + void addKeyValueInVarErrorIfAlreadyExistingNow(const char *varName, const SALOME::ByteVec& key, const SALOME::ByteVec& value); SALOME::Transaction_ptr removeKeyInVarErrorIfNotAlreadyExisting(const char *varName, const SALOME::ByteVec& key); SALOME::KeyWaiter_ptr waitForKeyInVar(const char *varName, const SALOME::ByteVec& keyVal); SALOME::KeyWaiter_ptr waitForKeyInVarAndKillIt(const char *varName, const SALOME::ByteVec& keyVal, SALOME::Transaction_out transac); diff --git a/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx b/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx index 2989150d4..f770129eb 100644 --- a/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx +++ b/src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx @@ -101,14 +101,19 @@ void PickelizedPyObjServer::FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, buf[i]=bsToBeConv[i]; } -SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv) +void PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv, SALOME::ByteVec& ret) { - SALOME::ByteVec *ret(new SALOME::ByteVec); const char *buf(strToBeConv.c_str()); std::size_t sz(strToBeConv.size()); - ret->length(sz); + ret.length(sz); for(std::size_t i=0;i& pickledData, DataScopeServerBase *dsb); diff --git a/src/SALOMESDS/TestSalomeSDS.py b/src/SALOMESDS/TestSalomeSDS.py index 9a5fd83e0..dc779c77a 100644 --- a/src/SALOMESDS/TestSalomeSDS.py +++ b/src/SALOMESDS/TestSalomeSDS.py @@ -195,6 +195,29 @@ class SalomeSDSTest(unittest.TestCase): dss.atomicApply([t2]) self.assertEqual(str2Obj(dss.fetchSerializedContent(varName)),{'ab':[4,5,6]}) + def testTransaction5(self): + """ Like testTransaction2 but without transactions. """ + scopeName="Scope1" + varName="a" + dsm=salome.naming_service.Resolve("/DataServerManager") + dsm.cleanScopesInNS() + if scopeName in dsm.listScopes(): + dsm.removeDataScope(scopeName) + dss,isCreated=dsm.giveADataScopeTransactionCalled(scopeName) + self.assertTrue(isCreated) + # + t0=dss.createRdExtVarTransac(varName,obj2Str({"ab":[4,5,6]})) + dss.atomicApply([t0]) + # + self.assertRaises(SALOME.SALOME_Exception,dss.addKeyValueInVarErrorIfAlreadyExistingNow,varName,obj2Str("ab"),obj2Str([7,8,9,10]))#raises because ab is already a key ! + dss.addKeyValueInVarErrorIfAlreadyExistingNow(varName,obj2Str("cd"),obj2Str([7,8,9,10])) + # + self.assertEqual(str2Obj(dss.fetchSerializedContent(varName)),{'ab':[4,5,6],'cd':[7,8,9,10]}) + wk=dss.waitForKeyInVar(varName,obj2Str("cd")) + self.assertEqual(str2Obj(wk.waitFor()),[7,8,9,10]) + keys=[str2Obj(elt) for elt in dss.getAllKeysOfVarWithTypeDict(varName)] + self.assertEqual(keys,['ab','cd']) + def setUp(self): salome.salome_init() pass -- 2.39.2