~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/math/rand.cc

This patch completes the first step in the splitting of
the XA resource manager API from the storage engine API,
as outlined in the specification here:

http://drizzle.org/wiki/XaStorageEngine

* Splits plugin::StorageEngine into a base StorageEngine
  class and two derived classes, TransactionalStorageEngine
  and XaStorageEngine.  XaStorageEngine derives from
  TransactionalStorageEngine and creates the XA Resource
  Manager API for storage engines.

  - The methods moved from StorageEngine to TransactionalStorageEngine
    include releaseTemporaryLatches(), startConsistentSnapshot(), 
    commit(), rollback(), setSavepoint(), releaseSavepoint(),
    rollbackToSavepoint() and hasTwoPhaseCommit()
  - The methods moved from StorageEngine to XaStorageEngine
    include recover(), commitXid(), rollbackXid(), and prepare()

* Places all static "EngineVector"s into their proper
  namespaces (typedefs belong in header files, not implementation files)
  and places all static methods corresponding
  to either only transactional engines or only XA engines
  into their respective files in /drizzled/plugin/

* Modifies the InnoDB "handler" files to extend plugin::XaStorageEngine
  and not plugin::StorageEngine

The next step, as outlined in the wiki spec page above, is to isolate
the XA Resource Manager API into its own plugin class and modify
plugin::XaStorageEngine to implement plugin::XaResourceManager via
composition.  This is necessary to enable building plugins which can
participate in an XA transaction *without having to have that plugin
implement the entire storage engine API*

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
#include "config.h"
 
21
#include <drizzled/function/math/rand.h>
 
22
#include <drizzled/session.h>
 
23
 
 
24
namespace drizzled
 
25
{
 
26
 
 
27
static uint32_t sql_rnd()
 
28
{
 
29
  return (uint32_t) (rand() * 0xffffffff); /* make all bits random */
 
30
}
 
31
 
 
32
 
 
33
void Item_func_rand::seed_random(Item *arg)
 
34
{
 
35
  /*
 
36
    TODO: do not do reinit 'rand' for every execute of PS/SP if
 
37
    args[0] is a constant.
 
38
  */
 
39
  uint64_t tmp= (uint64_t) arg->val_int();
 
40
  _seed_random_int(tmp * 0x10001L + 55555555L, tmp * 0x10000001L);
 
41
}
 
42
 
 
43
void Item_func_rand::_seed_random_int(uint64_t new_seed1, uint64_t new_seed2)
 
44
{
 
45
  max_value= 0x3FFFFFFFL;
 
46
  max_value_dbl=(double) max_value;
 
47
  seed1= new_seed1 % max_value;
 
48
  seed2= new_seed2 % max_value;
 
49
}
 
50
 
 
51
bool Item_func_rand::fix_fields(Session *session,Item **ref)
 
52
{
 
53
  if (Item_real_func::fix_fields(session, ref))
 
54
    return true;
 
55
 
 
56
  used_tables_cache|= RAND_TABLE_BIT;
 
57
  if (arg_count)
 
58
  {                                     // Only use argument once in query
 
59
    /*
 
60
      No need to send a Rand log event if seed was given eg: RAND(seed),
 
61
      as it will be replicated in the query as such.
 
62
    */
 
63
    if (args[0]->const_item())
 
64
      seed_random(args[0]);
 
65
  }
 
66
  else
 
67
  {
 
68
    uint64_t tmp= sql_rnd();
 
69
    _seed_random_int(tmp + (uint64_t) ref, tmp + (uint64_t) session->thread_id);
 
70
  }
 
71
 
 
72
  return false;
 
73
}
 
74
 
 
75
void Item_func_rand::update_used_tables()
 
76
{
 
77
  Item_real_func::update_used_tables();
 
78
  used_tables_cache|= RAND_TABLE_BIT;
 
79
}
 
80
 
 
81
double Item_func_rand::val_real()
 
82
{
 
83
  assert(fixed == 1);
 
84
  if (arg_count && !args[0]->const_item())
 
85
    seed_random(args[0]);
 
86
 
 
87
  seed1= (seed1 * 3 + seed2) % max_value;
 
88
  seed2= (seed1 + seed2 + 33) % max_value;
 
89
  return (((double) seed1) / max_value_dbl);
 
90
}
 
91
 
 
92
} /* namespace drizzled */