From 354d79cd2c3420a865dbd08a16a02458bd24a182 Mon Sep 17 00:00:00 2001
From: unknown <tomas@poseidon.ndb.mysql.com>
Date: Mon, 22 Aug 2005 11:41:37 +0200
Subject: [PATCH] generalized option for specifying ndb distribution

---
 sql/ha_ndbcluster.cc | 14 +++++++++-----
 sql/ha_ndbcluster.h  |  2 ++
 sql/mysqld.cc        | 36 +++++++++++++++++++++++++-----------
 3 files changed, 36 insertions(+), 16 deletions(-)

diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc
index 5d8cfd9913..db58ad1e94 100644
--- a/sql/ha_ndbcluster.cc
+++ b/sql/ha_ndbcluster.cc
@@ -34,7 +34,7 @@
 
 // options from from mysqld.cc
 extern my_bool opt_ndb_optimized_node_selection;
-extern my_bool opt_ndb_linear_hash;
+extern enum ndb_distribution opt_ndb_distribution_id;
 extern const char *opt_ndbcluster_connectstring;
 
 // Default value for parallelism
@@ -7450,7 +7450,7 @@ uint ha_ndbcluster::set_up_partition_info(partition_info *part_info,
 
 static void ndb_set_fragmentation(NDBTAB &tab, TABLE *form, uint pk_length)
 {
-  NDBTAB::FragmentType ftype;
+  NDBTAB::FragmentType ftype= NDBTAB::DistrKeyHash;
   ushort node_group[MAX_PARTITIONS];
   uint no_nodes= g_ndb_cluster_connection->no_db_nodes(), no_fragments, i;
   DBUG_ENTER("ndb_set_fragmentation");
@@ -7481,10 +7481,14 @@ static void ndb_set_fragmentation(NDBTAB &tab, TABLE *form, uint pk_length)
   node_group[0]= 0;
   for (i= 1; i < no_fragments; i++)
     node_group[i]= UNDEF_NODEGROUP;
-  if (opt_ndb_linear_hash)
+  switch (opt_ndb_distribution_id)
+  {
+  case ND_KEYHASH:
+    break;
+  case ND_LINHASH:
     ftype= NDBTAB::DistrKeyLin;
-  else
-    ftype= NDBTAB::DistrKeyHash;
+    break;
+  }
   tab.setFragmentType(ftype);
   tab.setNodeGroupIds(&node_group, no_fragments);
   DBUG_VOID_RETURN;
diff --git a/sql/ha_ndbcluster.h b/sql/ha_ndbcluster.h
index de38ab2d72..bf9891c364 100644
--- a/sql/ha_ndbcluster.h
+++ b/sql/ha_ndbcluster.h
@@ -113,6 +113,8 @@ struct negated_function_mapping
   NDB_FUNC_TYPE neg_fun;
 };
 
+enum ndb_distribution { ND_KEYHASH= 0, ND_LINHASH= 1 };
+
 /*
   Define what functions can be negated in condition pushdown.
   Note, these HAVE to be in the same order as in definition enum
diff --git a/sql/mysqld.cc b/sql/mysqld.cc
index 24d308e969..4563b37201 100644
--- a/sql/mysqld.cc
+++ b/sql/mysqld.cc
@@ -362,7 +362,12 @@ my_bool	opt_ndb_shm, opt_ndb_optimized_node_selection;
 ulong opt_ndb_cache_check_time;
 const char *opt_ndb_mgmd;
 ulong opt_ndb_nodeid;
-bool opt_ndb_linear_hash;
+
+const char *ndb_distribution_names[]= {"KEYHASH", "LINHASH", NullS};
+TYPELIB ndb_distribution_typelib= { array_elements(ndb_distribution_names)-1,
+				    "", ndb_distribution_names, NULL };
+const char *opt_ndb_distribution= ndb_distribution_names[ND_KEYHASH];
+enum ndb_distribution opt_ndb_distribution_id= ND_KEYHASH;
 #endif
 my_bool opt_readonly, use_temp_pool, relay_log_purge;
 my_bool opt_sync_frm, opt_allow_suspicious_udfs;
@@ -4879,16 +4884,11 @@ Disable with --skip-ndbcluster (will save memory).",
    (gptr*) &global_system_variables.ndb_autoincrement_prefetch_sz,
    (gptr*) &global_system_variables.ndb_autoincrement_prefetch_sz,
    0, GET_ULONG, REQUIRED_ARG, 32, 1, 256, 0, 0, 0},
-  {"ndb-use-linear-hash", OPT_NDB_LINEAR_HASH,
-   "Flag to indicate whether to use linear hash for default in new tables",
-   (gptr*) &opt_ndb_linear_hash,
-   (gptr*) &opt_ndb_linear_hash,
-   0, GET_BOOL, OPT_ARG, 1, 0, 0, 0, 0, 0},
-  {"ndb_use_linear_hash", OPT_NDB_LINEAR_HASH,
-   "Flag to indicate whether to use linear hash for default in new tables",
-   (gptr*) &opt_ndb_linear_hash,
-   (gptr*) &opt_ndb_linear_hash,
-   0, GET_BOOL, OPT_ARG, 1, 0, 0, 0, 0, 0},
+  {"ndb-distibution", OPT_NDB_DISTRIBUTION,
+   "Default distribution for new tables in ndb",
+   (gptr*) &opt_ndb_distribution,
+   (gptr*) &opt_ndb_distribution,
+   0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
   {"ndb-force-send", OPT_NDB_FORCE_SEND,
    "Force send of buffers to ndb immediately without waiting for "
    "other threads.",
@@ -6674,6 +6674,20 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
       opt_ndb_constrbuf[opt_ndb_constrbuf_len]= 0;
     opt_ndbcluster_connectstring= opt_ndb_constrbuf;
     break;
+  case OPT_NDB_DISTRIBUTION:
+    int id;
+    if ((id= find_type(argument, &ndb_distribution_typelib, 2)) <= 0)
+    {
+      fprintf(stderr, 
+	      "Unknown ndb distribution type: '%s' "
+	      "(should be '%s' or '%s')\n", 
+	      argument,
+              ndb_distribution_names[ND_KEYHASH],
+              ndb_distribution_names[ND_LINHASH]);
+      exit(1);
+    }
+    opt_ndb_distribution_id= (enum ndb_distribution)(id-1);
+    break;
 #endif
   case OPT_INNODB:
 #ifdef HAVE_INNOBASE_DB
-- 
2.30.9