]> SALOME platform Git repositories - modules/kernel.git/commitdiff
Salome HOME
Support of multi key,var append in global vars.
authorAnthony Geay <anthony.geay@edf.fr>
Tue, 4 Aug 2015 06:15:48 +0000 (08:15 +0200)
committerAnthony Geay <anthony.geay@edf.fr>
Tue, 4 Aug 2015 06:15:48 +0000 (08:15 +0200)
idl/SALOME_SDS.idl
src/SALOMESDS/SALOMESDS_DataScopeServer.cxx
src/SALOMESDS/SALOMESDS_DataScopeServer.hxx
src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.cxx
src/SALOMESDS/SALOMESDS_PickelizedPyObjServer.hxx
src/SALOMESDS/TestSalomeSDS.py

index a7bb219bdbe39d93c971fb2011a3c3cf6209d973..b3ceaa9585be96d981af434dc77c9532c8d4a413 100644 (file)
@@ -25,6 +25,7 @@ module SALOME
 {
   typedef sequence<string> StringVec;
   typedef sequence<octet> ByteVec;
+  typedef sequence<ByteVec> 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);
index ee1f9e07b41c2b22ef9d4767fc7a9663f2a49618..9dec327d6da837ba22c874b5ed2fbbce6c04578b 100644 (file)
@@ -156,6 +156,52 @@ void DataScopeServerBase::shutdownIfNotHostedByDSM()
     }
 }
 
+SALOME::ByteVec *DataScopeServerBase::fetchSerializedContent(const char *varName)
+{
+  BasicDataServer *var(retrieveVarInternal2(varName));
+  PickelizedPyObjServer *varc(dynamic_cast<PickelizedPyObjServer *>(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<PickelizedPyObjServer *>(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;i<sz;i++)
+    {
+      PyObject *item(PyList_GetItem(keys,i));
+      Py_XINCREF(item);
+      std::string pickel(varc->pickelize(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<PickelizedPyObjServer *>(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());
index db76fbf413baade45e029d71112c35a09af5aea0..ab7e810dc73eefd1935119903bdfaf521382eef4 100644 (file)
@@ -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);
index 2989150d486b79632d2f8171f22ca44c1e0f2e32..f770129eb4825c0b09c6522e3ed7fd33661b8469 100644 (file)
@@ -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<sz;i++)
-    (*ret)[i]=buf[i];
+    ret[i]=buf[i];
+}
+
+SALOME::ByteVec *PickelizedPyObjServer::FromCppToByteSeq(const std::string& strToBeConv)
+{
+  SALOME::ByteVec *ret(new SALOME::ByteVec);
+  FromCppToByteSeq(strToBeConv,*ret);
   return ret;
 }
 
index a52e27eea0ad8e579ac68df91dc913ac88f23890..2b64906511f26c302cca5db7544495a6e205ed79 100644 (file)
@@ -50,6 +50,7 @@ namespace SALOMESDS
     PyObject *getPyObj() const { return _self; }
   public:
     static void FromByteSeqToCpp(const SALOME::ByteVec& bsToBeConv, std::string& ret);
+    static void FromCppToByteSeq(const std::string& strToBeConv, SALOME::ByteVec& ret);
     static SALOME::ByteVec *FromCppToByteSeq(const std::string& strToBeConv);
     static PyObject *GetPyObjFromPickled(const std::string& pickledData, DataScopeServerBase *dsb);
     static PyObject *GetPyObjFromPickled(const std::vector<unsigned char>& pickledData, DataScopeServerBase *dsb);
index 9a5fd83e095a63433490d851f8a4f3205979977d..dc779c77aa8eb3679e94704abdda4f4ca2533c62 100644 (file)
@@ -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