List:NDB Connectors« Previous MessageNext Message »
From:Monty Taylor Date:May 3 2007 9:01am
Subject:Rev 96: Added Async Transactions and callbacks to Ruby. in http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/mgm
View as plain text  
------------------------------------------------------------
revno: 96
revision-id: mtaylor@stripped
parent: mtaylor@stripped
committer: Monty Taylor <mtaylor@stripped>
branch nick: mgm
timestamp: Thu 2007-05-03 02:00:26 -0700
message:
  Added Async Transactions and callbacks to Ruby.
  Started attempt at making some sense of MGMAPI.
added:
  ruby/testasync.rb              testasync.rb-20070503085248-pb7j4e3h32mb81vs-1
  ruby/testmgm.rb                testmgm.rb-20070503080527-ifoxhogy5lui5uyb-1
modified:
  ruby/mgmapi.i                  mgmapi.i-20070415091411-plc5gtpiwg93c2lx-1
  ruby/ndbapi.i                  ndb.i-20070228212451-7arjxk90dkwcn5xr-3
=== added file 'ruby/testasync.rb'
--- a/ruby/testasync.rb	1970-01-01 00:00:00 +0000
+++ b/ruby/testasync.rb	2007-05-03 09:00:26 +0000
@@ -0,0 +1,67 @@
+require 'mysql/cluster/ndbapi'
+
+class NdbCallback
+  def initialize(myRecAttr)
+    @myRecAttr=myRecAttr
+  end
+  def call(trans)
+    puts @myRecAttr.int_32_value
+  end
+end  
+
+Ndbapi::ndb_init
+
+num_iter=ARGV[0]
+insert_num=ARGV[1]
+
+BATCH_SIZE=10
+
+puts "Connecting to cluster"
+
+#connection=Ndbapi::NdbFactory.createNdbClusterConnection()
+connection=Ndbapi::NdbFactory.create_ndb_cluster_connection("127.0.0.1")
+
+begin
+  connection.connect(1,1,1)
+  connection.wait_until_ready(30, 30)
+
+rescue NdbApiException => e
+  puts "ERROR:" + e
+  exit 
+end
+
+myNdb = Ndbapi::Ndb.new(connection,"test")
+
+myNdb.init(4) 
+
+puts "running tests"
+
+myTransaction = myNdb.start_transaction
+
+myOperation = myTransaction.get_ndb_operation("mytablename")
+myOperation.insert_tuple
+auto_id = myNdb.get_auto_increment_value("mytablename",BATCH_SIZE)
+
+puts "Inserting" , auto_id
+
+myOperation.equal("ATTR1",auto_id)
+myOperation.set_value("ATTR2",1234)
+
+myTransaction.execute(Ndbapi::Commit)
+
+myTransaction.close
+
+myTransaction = myNdb.start_transaction
+
+myOperation = myTransaction.get_ndb_operation("mytablename")
+myOperation.read_tuple(Ndbapi::NdbOperation::LM_Read)
+
+myOperation.equal("ATTR1", auto_id)
+myRecAttr = myOperation.get_value("ATTR2")
+
+cb=NdbCallback.new(myRecAttr)
+
+myTransaction.execute_asynch_prepare(Ndbapi::Commit,cb)
+
+myNdb.send_poll_ndb(3000,1)
+

=== added file 'ruby/testmgm.rb'
--- a/ruby/testmgm.rb	1970-01-01 00:00:00 +0000
+++ b/ruby/testmgm.rb	2007-05-03 09:00:26 +0000
@@ -0,0 +1,29 @@
+require 'mysql/cluster/mgmapi'
+
+# This program doesn't actually do much yet
+
+Mgmapi::ndb_init
+h=Mgmapi::ndb_mgm_create_handle
+Mgmapi::ndb_mgm_set_connectstring(h,"127.0.0.1")
+Mgmapi::ndb_mgm_connect(h,1,1,1)
+a=[15,Mgmapi::NDB_MGM_EVENT_CATEGORY_STATISTIC]
+le=Mgmapi::ndb_mgm_create_logevent_handle(h,[a])
+if le==0
+  puts "crap"
+end
+
+conf=Mgmapi::ndb_mgm_get_configuration(h,0)
+
+event=Mgmapi::NdbLogevent.new
+
+run=true
+while run
+  ret = Mgmapi::ndb_logevent_get_next(le,event,5000)
+  if ret != 0
+    run=false
+  end
+  puts event.type
+  puts event.source_nodeid
+end
+
+

=== modified file 'ruby/mgmapi.i'
--- a/ruby/mgmapi.i	2007-04-15 09:56:16 +0000
+++ b/ruby/mgmapi.i	2007-05-03 09:00:26 +0000
@@ -20,4 +20,24 @@
 %module "mgmapi"
 
 %include "globals.i"
+
+%typemap(in) int filter[] { 
+  // Input will be array of tuples
+  int size = RARRAY($input)->len;
+//  int filter[size*2+1];
+  $1 = (int *) malloc((size*2+1)*sizeof(int));
+  int i=0;
+  for (i=0;i<size;i++) { 
+    VALUE elem = rb_ary_entry($input,i);
+    int elemsize = RARRAY(elem)->len;
+    for (int j=0;j<elemsize;j++) { 
+      $1[i++]=NUM2INT(rb_ary_entry(elem,j));
+    }
+  }
+  //filter[i]=0; 
+  $1[i]=0;
+}
+
+//%apply ndb_logevent *OUTPUT { ndb_logevent *dst };
+
 %include "Mgm.i"

=== modified file 'ruby/ndbapi.i'
--- a/ruby/ndbapi.i	2007-05-02 21:31:41 +0000
+++ b/ruby/ndbapi.i	2007-05-03 09:00:26 +0000
@@ -84,13 +84,31 @@
   return exception;
 }
 
-
+static void RubyCallBack(int result, 
+                        NdbTransaction *trans,
+                        void *aObject)
+{
+  VALUE cb_obj = (VALUE)aObject;
+  VALUE trans_obj = SWIG_NewPointerObj(SWIG_as_voidptr(trans), SWIGTYPE_p_NdbTransaction,
0 |  0 );
+  rb_funcall(cb_obj,rb_intern("call"),1,trans_obj); //,trans);
+  
+}
  %}
 
 %include "NdbFactory.i"
 %include "NdbClusterConnection.i"
 %include "Ndb.i"
 %include "NdbTransaction.i"
+
+%extend NdbTransaction {
+  void executeAsynchPrepare(ExecType          execType,
+                            VALUE             cb_obj,
+                            AbortOption       abortOption = AbortOnError)
+  {
+    self->executeAsynchPrepare(execType,RubyCallBack,(void *)cb_obj,abortOption);
+  };
+};
+
 %include "NdbOperation.i"
 %include "NdbScanOperation.i"
 %include "NdbRecAttr.i"

Thread
Rev 96: Added Async Transactions and callbacks to Ruby. in http://bazaar.launchpad.net/~ndb-connectors/ndb-connectors/mgmMonty Taylor3 May