List:NDB Connectors« Previous MessageNext Message »
From:Monty Taylor Date:May 5 2007 6:27am
Subject:Rev 98: Added async transactions to C#. in http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/exceptions
View as plain text  
------------------------------------------------------------
revno: 98
revision-id: mtaylor@stripped
parent: mtaylor@stripped
committer: Monty Taylor <mtaylor@stripped>
branch nick: exceptions
timestamp: Fri 2007-05-04 23:27:19 -0700
message:
  Added async transactions to C#.
  Removed unused lisp dir.
removed:
  lisp/                          svn-v2:1@5fca6d9a-db22-0410-b55c-899b0a28da89-trunk-lisp
  lisp/Makefile                 
svn-v2:1@5fca6d9a-db22-0410-b55c-899b0a28da89-trunk-lisp%2fMakefile
added:
  csharp/testasync.cs            testasync.cs-20070505062716-9d1as4txgc7ti8ik-1
modified:
  csharp/ndbapi.i               
svn-v2:10@5fca6d9a-db22-0410-b55c-899b0a28da89-trunk-csharp%2fndb.i
=== removed directory 'lisp'
=== removed file 'lisp/Makefile'
--- a/lisp/Makefile	2006-11-22 20:17:43 +0000
+++ b/lisp/Makefile	1970-01-01 00:00:00 +0000
@@ -1,1 +0,0 @@
-swig -c++ -cffi -I/usr/include/mysql -I/usr/include/mysql/ndb
-I/usr/include/mysql/ndb/ndbapi -o ndb_wrap.cpp  ../ndb.i 

=== added file 'csharp/testasync.cs'
--- a/csharp/testasync.cs	1970-01-01 00:00:00 +0000
+++ b/csharp/testasync.cs	2007-05-05 06:27:19 +0000
@@ -0,0 +1,227 @@
+/*  ndb-connectors: Wrappers for the NDBAPI
+    Copyright (C) 2006 MySQL, Inc.
+    
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or 
+    (at your option) any later version.
+    
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+*/
+
+
+using System;
+using MySql.Data.MySqlClient;
+using System.Collections.Generic;
+using MySql.Cluster.NdbApi; 
+
+//public delegate void AsynchDelegate(int retval, NdbTransaction trans, object
anyObject);
+
+
+class CSCallback : BaseCallback { 
+  
+  NdbRecAttr results; 
+
+  public CSCallback(NdbRecAttr results) { 
+    this.results = results ; 
+  }
+  
+  public override void callback(int ret, NdbTransaction trans) { 
+
+    uint foo=this.results.u_32_value();
+    Console.WriteLine("In callback from C#! {0}",foo);
+  }
+}
+
+class testasync { 
+
+  static public int Main(string[] args) {
+    
+    
+    List<ulong> ids = new List<ulong>();
+
+    if (args.Length != 2) { 
+      Console.WriteLine("Usage:\n\ttest.exe NUM_OF_ITERATIONS NUM_OF_ROWS ");
+      return(-1);
+    }
+    
+    int num_iter=Convert.ToInt32(args[0]);
+    int INSERT_NUM=Convert.ToInt32(args[1]);
+    int BATCH_SIZE=1000;
+    
+    MySqlConnection conn = new MySqlConnection();
+    conn.ConnectionString = "Server=127.0.0.1;User Id=root;Password=;Connection
Timeout=15;Database=test";
+    conn.Open();
+    
+    Console.WriteLine("Creating schema");
+    
+    MySqlCommand cur = conn.CreateCommand();
+    
+    //cur.CommandText="CREATE DATABASE if not exists test";
+    //cur.ExecuteNonQuery();
+    
+    //conn.ChangeDatabase("test");
+    
+    try { 
+      cur.CommandText = "DROP table if exists mytablename";
+      cur.ExecuteNonQuery();
+    }
+    catch (MySqlException e) { 
+      Console.WriteLine(e.Message);  
+    }
+
+    cur.CommandText = "CREATE TABLE if not exists" + 
+      " mytablename " +
+      " (ATTR1 INT UNSIGNED not null, " +
+      "  ATTR2 INT UNSIGNED NOT NULL, " +
+      "  PRIMARY KEY USING HASH (ATTR1)) " + 
+      " ENGINE=NDBCLUSTER ";
+    cur.ExecuteNonQuery();
+    
+// Connect to Cluster
+    
+    ndbapi.ndb_init();
+    
+    NdbClusterConnection connection = NdbFactory.createNdbClusterConnection();
+    
+    if (connection.connect(5,3,1) != 0) { 
+      Console.WriteLine("Connect to cluster management server failed.");
+      return -1;
+    }
+    
+    if (connection.wait_until_ready(30,30) != 0) { 
+      Console.WriteLine("Cluster was not ready within 30 secs.");
+      return -1; 
+    }
+    
+    Ndb myNdb = new Ndb(connection,"test");
+    
+    if (myNdb.init(4) == -1) {
+      Console.WriteLine(myNdb.getNdbError());
+      return -1;
+    }
+    
+// Fill Database
+    
+    Console.WriteLine("Filling Database with NDB API"); 
+
+    DateTime start_time = DateTime.Now;
+    
+    for (int t=0;t<=System.Math.Ceiling((decimal)(INSERT_NUM/BATCH_SIZE));t++) { 
+     
+      using (NdbTransaction myTransaction = myNdb.startTransaction()) { 
+	int val = (t+1)*BATCH_SIZE-INSERT_NUM;
+	int offset = (val > 0 ) ? val : 0;
+	
+	for (int i=0;i<BATCH_SIZE-offset;i++) { 
+	  
+	  NdbOperation myOperation=myTransaction.getNdbOperation("mytablename");
+	  myOperation.insertTuple();
+	  
+	  ulong auto_id = myNdb.getAutoIncrementValue("mytablename",(uint)BATCH_SIZE);
+	  
+	  //auto_id=(t*BATCH_SIZE)+i    
+	  myOperation.equal("ATTR1",auto_id);
+	  myOperation.setValue("ATTR2", i);
+	  
+	}
+	
+	int ret = myTransaction.execute( ExecType.Commit ); 
+	if (ret == -1) { 
+	  Console.WriteLine(myTransaction.getNdbError().getMessage());
+	}
+	myTransaction.close();
+	
+      }
+      
+    }
+
+    DateTime end_time = DateTime.Now; 
+    Console.WriteLine("Insert time for {0}:\t{1}",INSERT_NUM,end_time-start_time);
+
+// Get list of ids
+
+    Console.WriteLine("Fetching list of ids");
+
+    using (NdbTransaction myTransaction = myNdb.startTransaction()) { 
+      
+      NdbScanOperation myScanOperation =
myTransaction.getNdbScanOperation("mytablename");
+      
+      if (myScanOperation.readTuples(NdbOperation.LockMode.LM_CommittedRead) == -1) {
+	Console.WriteLine(myScanOperation.getNdbError().getMessage());
+      }
+      
+      NdbRecAttr myRecAttr=myScanOperation.getValue("ATTR1");
+      
+      
+      myTransaction.execute(ExecType.NoCommit);
+      
+      while(true) {
+	if (myScanOperation.nextResult(true) != 0)
+	  break;
+	ulong random_id = myRecAttr.u_32_value();
+	ids.Add(random_id);
+	
+      }
+      
+    myTransaction.close();
+
+    }
+
+    start_time=DateTime.Now;
+
+    Random rand = new Random(); 
+
+    List<NdbTransaction> translist = new List<NdbTransaction>();
+
+    for(int f=0;f<num_iter;f++) { 
+
+      try { 
+	NdbTransaction myTransaction = myNdb.startTransaction();
+	if (myTransaction==null) 
+	  Console.WriteLine(myNdb.getNdbError().getMessage()); 
+	
+	NdbOperation myOper = myTransaction.getNdbOperation("mytablename");
+	myOper.readTuple(NdbOperation.LockMode.LM_Read);
+	
+	
+	
+	ulong id_num = ids[rand.Next(0,ids.Count)];
+	myOper.equal("ATTR1",id_num);
+	
+	NdbRecAttr myRecAttr= myOper.getValue("ATTR2");
+	
+	CSCallback cb = new CSCallback(myRecAttr);
+	
+	myTransaction.executeAsynchPrepare( ExecType.Commit , cb, AbortOption.AO_IgnoreError  );
+	
+	translist.Add(myTransaction);
+      } catch (NdbApiException e) { 
+	Console.WriteLine(e);
+      }
+    }
+
+    myNdb.sendPollNdb(3000,1);
+
+    foreach (NdbTransaction trans in translist) { 
+      trans.close();
+    }
+    
+    end_time=DateTime.Now; 
+    Console.WriteLine("Fetch time for NDBAPI - {0}:\t{1}",num_iter,end_time-start_time);
+    
+    ndbapi.ndb_end(1);
+    return 0;
+
+
+  }
+  
+}
+  

=== modified file 'csharp/ndbapi.i'
--- a/csharp/ndbapi.i	2007-05-03 03:28:55 +0000
+++ b/csharp/ndbapi.i	2007-05-05 06:27:19 +0000
@@ -17,8 +17,10 @@
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
-%module ndbapi
-
+%module(directors="1") ndbapi
+
+
+ 
 %typemap(cscode) NdbTransaction %{
   private Ndb ndbReference;
   internal void addReference(Ndb ndbref) { 
@@ -36,25 +38,26 @@
     return ret;
   }
 
-%typemap(csfinalize) Ndb_cluster_connection %{
+/*%typemap(csfinalize) Ndb_cluster_connection %{
   ~$csclassname() {
-  //foo
     Dispose();
     ndbapi.ndb_end(0);
   }
 %}
+*/
 
 /*
 %typemap(csfinalize) NdbTransaction %{
   ~$csclassname() {
     // This is only if we haven't managed to kill this guy yet.
     // Don't depend on this.
-    this.close(this);
+    this.close();
     Dispose();
     }
 %}
 */
 
+
 %{
 #define NDB_exception(excp,msg) { SWIG_CSharpSetPendingExceptionCustom(excp,msg); }
 %}
@@ -66,6 +69,7 @@
   typedef void (SWIGSTDCALL* CSharpExceptionCallback_t)(int excp, const char *);
   CSharpExceptionCallback_t customExceptionCallback = NULL;
 
+  
   extern "C" SWIGEXPORT
   void SWIGSTDCALL CustomExceptionRegisterCallback(CSharpExceptionCallback_t
customCallback) {
     customExceptionCallback = customCallback;
@@ -79,9 +83,11 @@
 %}
 
 %pragma(csharp) imclasscode=%{
+
   class CustomExceptionHelper {
     // C# delegate for the C/C++ customExceptionCallback
     public delegate void CustomExceptionDelegate(int excp, string message);
+
     static CustomExceptionDelegate customDelegate =
                                    new
CustomExceptionDelegate(SetPendingCustomException);
 
@@ -129,13 +135,63 @@
   
 %}
 
+ %{ 
+
+#include <NdbTransaction.hpp>
+
+  class BaseCallback { 
+
+  public:
+    virtual ~BaseCallback() {}
+    virtual void callback(int result, NdbTransaction * trans) { 
+    printf("In BaseCallback::callback. Return Value: %d. Transaction:
%p\n",result,trans);
+  }
+
+  };
+
+  %}
+
 %include "globals.i"
 %include "NdbFactory.i"
 %include "NdbClusterConnection.i"
 %include "Ndb.i"
+
+
+%{ 
+
+  void CSharpCallback(int ret, NdbTransaction * trans, void * anyObject) {
+    BaseCallback * cb = (BaseCallback *)anyObject; 
+    cb->callback(ret, trans);
+  }
+
+%}
+
+
 %include "NdbTransaction.i"
+
+
+%extend NdbTransaction { 
+
+  void executeAsynchPrepare(ExecType execType,
+			    BaseCallback * cb,
+			    AbortOption abortOption = AbortOnError) {
+    self->executeAsynchPrepare(execType,CSharpCallback,(void *)cb,abortOption);
+  };
+ };
+
+
 %include "NdbOperation.i"
 %include "NdbScanOperation.i"
 %include "NdbRecAttr.i"
 %include "NdbError.i"
 
+%feature("director") BaseCallback;
+
+class BaseCallback {
+  public:
+  virtual ~BaseCallback() {};
+  virtual void callback(int result, NdbTransaction * trans);
+  
+};
+
+

Thread
Rev 98: Added async transactions to C#. in http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/exceptionsMonty Taylor5 May