1
1
/*****************************************************************************
3
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All Rights Reserved.
3
Copyright (c) 2000, 2012, Oracle and/or its affiliates. All Rights Reserved.
4
4
Copyright (c) 2008, 2009 Google Inc.
5
5
Copyright (c) 2009, Percona Inc.
1448
1451
INSERT INTO T VALUES(), (), ();
1450
innobase_next_autoinc() will be called with increment set to
1451
n * 3 where autoinc_lock_mode != TRADITIONAL because we want
1452
to reserve 3 values for the multi-value INSERT above.
1453
innobase_next_autoinc() will be called with increment set to 3 where
1454
autoinc_lock_mode != TRADITIONAL because we want to reserve 3 values for
1455
the multi-value INSERT above.
1453
1456
@return the next value */
1456
1459
innobase_next_autoinc(
1457
1460
/*==================*/
1458
1461
ulonglong current, /*!< in: Current value */
1459
ulonglong increment, /*!< in: increment current by */
1462
ulonglong need, /*!< in: count of values needed */
1463
ulonglong step, /*!< in: AUTOINC increment step */
1460
1464
ulonglong offset, /*!< in: AUTOINC offset */
1461
1465
ulonglong max_value) /*!< in: max value for type */
1463
1467
ulonglong next_value;
1468
ulonglong block = need * step;
1465
1470
/* Should never be 0. */
1466
ut_a(increment > 0);
1473
ut_a(max_value > 0);
1475
/* Current value should never be greater than the maximum. */
1476
ut_a(current <= max_value);
1468
1478
/* According to MySQL documentation, if the offset is greater than
1469
the increment then the offset is ignored. */
1470
if (offset > increment) {
1479
the step then the offset is ignored. */
1480
if (offset > block) {
1474
if (max_value <= current) {
1484
/* Check for overflow. */
1485
if (block >= max_value
1486
|| offset > max_value
1487
|| current == max_value
1488
|| max_value - offset <= offset) {
1475
1490
next_value = max_value;
1476
} else if (offset <= 1) {
1477
/* Offset 0 and 1 are the same, because there must be at
1478
least one node in the system. */
1479
if (max_value - current <= increment) {
1492
ut_a(max_value > current);
1494
ulonglong free = max_value - current;
1496
if (free < offset || free - offset <= block) {
1480
1497
next_value = max_value;
1482
next_value = current + increment;
1484
} else if (max_value > current) {
1503
if (next_value == 0) {
1485
1506
if (current > offset) {
1486
next_value = ((current - offset) / increment) + 1;
1507
next = (current - offset) / step;
1488
next_value = ((offset - current) / increment) + 1;
1509
next = (offset - current) / step;
1491
ut_a(increment > 0);
1492
ut_a(next_value > 0);
1512
ut_a(max_value > next);
1513
next_value = next * step;
1494
1514
/* Check for multiplication overflow. */
1495
if (increment > (max_value / next_value)) {
1497
next_value = max_value;
1499
next_value *= increment;
1501
ut_a(max_value >= next_value);
1503
/* Check for overflow. */
1504
if (max_value - next_value <= offset) {
1515
ut_a(next_value >= next);
1516
ut_a(max_value > next_value);
1518
/* Check for overflow */
1519
if (max_value - next_value >= block) {
1521
next_value += block;
1523
if (max_value - next_value >= offset) {
1524
next_value += offset;
1505
1526
next_value = max_value;
1507
next_value += offset;
1529
next_value = max_value;
1511
next_value = max_value;
1533
ut_a(next_value != 0);
1514
1534
ut_a(next_value <= max_value);
1516
1536
return(next_value);
3746
3766
nor the offset, so use a default increment of 1. */
3748
3768
auto_inc = innobase_next_autoinc(
3749
read_auto_inc, 1, 1, col_max_value);
3769
read_auto_inc, 1, 1, 0, col_max_value);
5246
5266
if (auto_inc <= col_max_value) {
5247
5267
ut_a(prebuilt->autoinc_increment > 0);
5250
5269
ulonglong offset;
5270
ulonglong increment;
5252
5272
offset = prebuilt->autoinc_offset;
5253
need = prebuilt->autoinc_increment;
5273
increment = prebuilt->autoinc_increment;
5255
5275
auto_inc = innobase_next_autoinc(
5257
need, offset, col_max_value);
5277
1, increment, offset,
5259
5280
err = innobase_set_max_autoinc(
5523
5544
if (auto_inc <= col_max_value && auto_inc != 0) {
5526
5546
ulonglong offset;
5547
ulonglong increment;
5528
5549
offset = prebuilt->autoinc_offset;
5529
need = prebuilt->autoinc_increment;
5550
increment = prebuilt->autoinc_increment;
5531
5552
auto_inc = innobase_next_autoinc(
5532
auto_inc, need, offset, col_max_value);
5553
auto_inc, 1, increment, offset, col_max_value);
5534
5555
error = innobase_set_max_autoinc(auto_inc);
10137
10163
/* With old style AUTOINC locking we only update the table's
10138
10164
AUTOINC counter after attempting to insert the row. */
10139
10165
if (innobase_autoinc_lock_mode != AUTOINC_OLD_STYLE_LOCKING) {
10141
10166
ulonglong current;
10142
10167
ulonglong next_value;
10144
10169
current = *first_value > col_max_value ? autoinc : *first_value;
10145
need = *nb_reserved_values * increment;
10147
10171
/* Compute the last value in the interval */
10148
10172
next_value = innobase_next_autoinc(
10149
current, need, offset, col_max_value);
10173
current, *nb_reserved_values, increment, offset,
10151
10176
prebuilt->autoinc_last_value = next_value;