Bläddra i källkod

Added ability to specify lock modes for all tables the bulk loading job is inserted into

Lacey Sanderson 12 år sedan
förälder
incheckning
386f0b6b5c

+ 17 - 1
tripal_bulk_loader/tripal_bulk_loader.admin.inc

@@ -98,7 +98,22 @@ function tripal_bulk_loader_configuration_form($form_state = NULL) {
     '#default_value' => variable_get('tripal_bulk_loader_transactions','row')
   );
 
-  $form['submit1'] = array(
+  $form['speed']['lock'] = array(
+    '#type' => 'radios',
+    '#title' => t('Lock Type'),
+    '#description' => t('The type of lock used by the bulk loading jobs. The lock is '
+      .'acquired at the beginning of the job and kept till the end. A lock of the type '
+      .'selected will be acquired for every table being inserted into.'),
+    '#options' => array(
+      'ROW EXCLUSIVE' => t('ROW EXCLUSIVE: The default lock type for insert queries.'),
+      'EXCLUSIVE' => t('EXCLUSIVE: Only Select Queries can access the table.'),
+      'ACCESS EXCLUSIVE' => t('ACCESS EXCLUSIVE: No other queries can access the table.'),
+    ),
+    '#default_value' => variable_get('tripal_bulk_loader_lock', 'ROW EXCLUSIVE'),
+  );
+
+
+ $form['submit1'] = array(
     '#type' => 'submit',
     '#value' => t('Save')
   );
@@ -115,6 +130,7 @@ function tripal_bulk_loader_configuration_form_submit($form, $form_state) {
   variable_set('tripal_bulk_loader_disable_triggers', $form_state['values']['disable_triggers']);
   variable_set('tripal_bulk_loader_skip_validation', $form_state['values']['no_validate']);
   variable_set('tripal_bulk_loader_transactions',$form_state['values']['transactions']);
+  variable_set('tripal_bulk_loader_lock', $form_state['values']['lock']);
 
 }
 

+ 11 - 1
tripal_bulk_loader/tripal_bulk_loader.loader.inc

@@ -142,7 +142,11 @@ function tripal_bulk_loader_load_data($nid) {
       continue;
     }
 
-    $tables[$record_array['table']] = $record_array['table'];
+    // Add tables being inserted into to a list to be treated differently
+    // this is used to acquire locks on these tables
+    if (preg_match('/insert/',$record_array['mode'])) {
+      $tables[$record_array['table']] = $record_array['table'];
+    }
 
     foreach ($record_array['fields'] as $field_index => $field_array) {
 
@@ -275,6 +279,12 @@ function tripal_bulk_loader_load_data($nid) {
       chado_query("SET CONSTRAINTS ALL DEFERRED");
     }
 
+    // Acquire Locks
+    $lockmode = variable_get('tripal_bulk_loader_lock', 'ROW EXCLUSIVE');
+    foreach ($tables as $table) {
+      chado_query("LOCK TABLE %s IN %s MODE", $table, $lockmode);
+    }
+
     while (!feof($file_handle)) {
 
       // Clear variables

+ 7 - 7
tripal_core/tripal_core.api.inc

@@ -197,12 +197,11 @@ function tripal_core_chado_insert($table, $values, $options) {
     }
 
     // make sure required fields have a value
-    $fields = $table_desc['fields'];
-    if (!is_array($fields)) {
-      $fields = array();
+    if (!is_array($table_desc['fields'])) {
+      $table_desc['fields'] = array();
       watchdog('tripal_core', "tripal_core_chado_insert: %table not defined in tripal schema api", array('%table' => $table), 'WATCHDOG WARNING');
     }
-    foreach ($fields as $field => $def) {
+    foreach ($table_desc['fields'] as $field => $def) {
       // a field is considered missing if it cannot be NULL and there is no default
       // value for it or it is of type 'serial'
       if ($def['not NULL'] == 1 and !array_key_exists($field, $insert_values) and !isset($def['default']) and strcmp($def['type'], serial) != 0) {
@@ -228,8 +227,9 @@ function tripal_core_chado_insert($table, $values, $options) {
       $itypes[] = "NULL";
       $idatatypes[] = "NULL";
     }
-    elseif(strcmp($fields[$field]['type'],'serial')==0 or
-      strcmp($fields[$field]['type'],'int')==0){
+    elseif(strcasecmp($table_desc['fields'][$field]['type'],'serial')==0 or
+      strcasecmp($table_desc['fields'][$field]['type'],'int')==0 or
+      strcasecmp($table_desc['fields'][$field]['type'],'integer')==0){
       $itypes[] = "%d";
       $idatatypes[] = 'int';
     }
@@ -1923,7 +1923,7 @@ function tripal_db_persistent_chado() {
 /**
  * Release a persistent chado connection
  */
-function tripal_db_release_persistent_chado () {
+function tripal_db_release_persistent_chado() {
   variable_del('tripal_perisistent_chado');
 }