mirror of
https://github.com/Combodo/iTop.git
synced 2026-02-13 07:24:13 +01:00
1627 - Ticket ref sometimes duplicate
This code is a drop in replacement based on an abstract counter based on an abstract keyValue store. The counter can be - class based (in this case the counter is initialized on max(id) + 1 - key based (in this case the counter starts at 0) Important: on both cases the counter is no more kept aligned with the primary key. This lead to a MySQL8 compatible implementation.
This commit is contained in:
committed by
OИUЯd da silva
parent
9e015ba59a
commit
c6f5b8b1f9
@@ -33,6 +33,7 @@ MetaModel::IncludeModule('core/tagsetfield.class.inc.php');
|
||||
MetaModel::IncludeModule('synchro/synchrodatasource.class.inc.php');
|
||||
MetaModel::IncludeModule('core/backgroundtask.class.inc.php');
|
||||
MetaModel::IncludeModule('core/inlineimage.class.inc.php');
|
||||
MetaModel::IncludeModule('core/counter.class.inc.php');
|
||||
|
||||
MetaModel::IncludeModule('webservices/webservices.basic.php');
|
||||
|
||||
|
||||
@@ -793,6 +793,9 @@ class CMDBSource
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @deprecated 2.7.0 N°1627 use ItopCounter insted
|
||||
*
|
||||
* @param string $sTable
|
||||
*
|
||||
* @return int
|
||||
|
||||
162
core/counter.class.inc.php
Normal file
162
core/counter.class.inc.php
Normal file
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright (C) 2013-2019 Combodo SARL
|
||||
*
|
||||
* This file is part of iTop.
|
||||
*
|
||||
* iTop is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* iTop is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Class ItopCounter
|
||||
*
|
||||
*/
|
||||
final class ItopCounter
|
||||
{
|
||||
/**
|
||||
* Key based counter.
|
||||
* The counter is protected against concurrency script.
|
||||
*
|
||||
* @param $sCounterName
|
||||
* @param null|callable $oNewObjectValueProvider optional callable that must return an integer. Used when no key is found
|
||||
*
|
||||
* @return int the counter starting at
|
||||
* * `0` when no $oNewObjectValueProvider is given (or null)
|
||||
* * `$oNewObjectValueProvider() + 1` otherwise
|
||||
*
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreCannotSaveObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreOqlMultipleResultsForbiddenException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
* @throws \OQLException
|
||||
*/
|
||||
public static function Inc($sCounterName, $oNewObjectValueProvider = null)
|
||||
{
|
||||
$sSelfClassName = self::class;
|
||||
$oiTopMutex = new iTopMutex("$sSelfClassName-$sCounterName");
|
||||
$oiTopMutex->Lock();
|
||||
|
||||
$oFilter = DBObjectSearch::FromOQL('SELECT KeyValueStore WHERE key_name=:key_name', array(
|
||||
'key_name' => $sCounterName,
|
||||
));
|
||||
$oCounter = $oFilter->GetFirstResult();
|
||||
if (is_null($oCounter))
|
||||
{
|
||||
if (null != $oNewObjectValueProvider)
|
||||
{
|
||||
$iComputedValue = $oNewObjectValueProvider();
|
||||
}
|
||||
else
|
||||
{
|
||||
$iComputedValue = 0;
|
||||
}
|
||||
$oCounter = MetaModel::NewObject('KeyValueStore', array(
|
||||
'key_name' => $sCounterName,
|
||||
'value' => $iComputedValue,
|
||||
));
|
||||
}
|
||||
|
||||
$iCurrentValue = (int) $oCounter->Get('value');
|
||||
$iCurrentValue++;
|
||||
|
||||
$oCounter->Set('value', $iCurrentValue);
|
||||
$oCounter->DBWrite();
|
||||
|
||||
$oiTopMutex->Unlock();
|
||||
|
||||
return $iCurrentValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* handle a counter for the root class of given $sLeafClass.
|
||||
* If no counter exist initialize it with the `max(id) + 1`
|
||||
*
|
||||
* @param $sLeafClass
|
||||
*
|
||||
* @return int
|
||||
* @throws \ArchivedObjectException
|
||||
* @throws \CoreCannotSaveObjectException
|
||||
* @throws \CoreException
|
||||
* @throws \CoreOqlMultipleResultsForbiddenException
|
||||
* @throws \CoreUnexpectedValue
|
||||
* @throws \MySQLException
|
||||
* @throws \OQLException
|
||||
*/
|
||||
public static function IncClass($sLeafClass)
|
||||
{
|
||||
$sRootClass = MetaModel::GetRootClass($sLeafClass);
|
||||
|
||||
$oNewObjectCallback = function() use ($sRootClass)
|
||||
{
|
||||
$sRootTable = MetaModel::DBGetTable($sRootClass);
|
||||
$sIdField = MetaModel::DBGetKey($sRootClass);
|
||||
|
||||
return CMDBSource::QueryToScalar("SELECT max(`$sIdField`) FROM `$sRootTable`");
|
||||
};
|
||||
|
||||
return self::Inc($sRootClass, $oNewObjectCallback);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Persistent classes for a CMDB
|
||||
*
|
||||
* @copyright Copyright (C) 2010-2017 Combodo SARL
|
||||
* @license http://opensource.org/licenses/AGPL-3.0
|
||||
*/
|
||||
|
||||
class KeyValueStore extends DBObject
|
||||
{
|
||||
public static function Init()
|
||||
{
|
||||
$aParams = array(
|
||||
'category' => 'bizmodel',
|
||||
'key_type' => 'autoincrement',
|
||||
'name_attcode' => array('key_name'),
|
||||
'state_attcode' => '',
|
||||
'reconc_keys' => array(''),
|
||||
'db_table' => 'key_value_store',
|
||||
'db_key_field' => 'id',
|
||||
'db_finalclass_field' => '',
|
||||
'indexes' => array (
|
||||
array (
|
||||
0 => 'key_name',
|
||||
),
|
||||
),);
|
||||
MetaModel::Init_Params($aParams);
|
||||
MetaModel::Init_InheritAttributes();
|
||||
MetaModel::Init_AddAttribute(new AttributeString("key_name", array("allowed_values"=>null, "sql"=>'key_name', "default_value"=>'', "is_null_allowed"=>false, "depends_on"=>array(), "always_load_in_tables"=>false)));
|
||||
MetaModel::Init_AddAttribute(new AttributeString("value", array("allowed_values"=>null, "sql"=>'value', "default_value"=>'0', "is_null_allowed"=>false, "depends_on"=>array(), "always_load_in_tables"=>false)));
|
||||
|
||||
MetaModel::Init_SetZListItems('details', array (
|
||||
0 => 'key_name',
|
||||
1 => 'value',
|
||||
));
|
||||
MetaModel::Init_SetZListItems('standard_search', array (
|
||||
0 => 'key_name',
|
||||
1 => 'value',
|
||||
));
|
||||
MetaModel::Init_SetZListItems('list', array (
|
||||
0 => 'key_name',
|
||||
1 => 'value',
|
||||
));
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -6935,6 +6935,8 @@ abstract class MetaModel
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated 2.7.0 N°1627, use ItopCounter::incRootClass($sClass) instead
|
||||
*
|
||||
* @param string $sClass
|
||||
*
|
||||
* @return int
|
||||
@@ -6942,10 +6944,7 @@ abstract class MetaModel
|
||||
*/
|
||||
public static function GetNextKey($sClass)
|
||||
{
|
||||
$sRootClass = MetaModel::GetRootClass($sClass);
|
||||
$sRootTable = MetaModel::DBGetTable($sRootClass);
|
||||
|
||||
return CMDBSource::GetNextInsertId($sRootTable);
|
||||
return ItopCounter::IncClass($sClass);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -140,6 +140,9 @@ return array(
|
||||
'ContextTag' => $baseDir . '/core/contexttag.class.inc.php',
|
||||
'CoreCannotSaveObjectException' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'CoreException' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'CoreOqlException' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'CoreOqlMultipleResultsForbiddenException' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'CorePortalInvalidActionRuleException' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'CoreServices' => $baseDir . '/core/restservices.class.inc.php',
|
||||
'CoreUnexpectedValue' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'CoreWarning' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
@@ -255,8 +258,10 @@ return array(
|
||||
'Introspection' => $baseDir . '/core/introspection.class.inc.php',
|
||||
'InvalidConfigParamException' => $baseDir . '/core/coreexception.class.inc.php',
|
||||
'IssueLog' => $baseDir . '/core/log.class.inc.php',
|
||||
'ItopCounter' => $baseDir . '/core/counter.class.inc.php',
|
||||
'JSButtonItem' => $baseDir . '/application/applicationextension.inc.php',
|
||||
'JSPopupMenuItem' => $baseDir . '/application/applicationextension.inc.php',
|
||||
'KeyValueStore' => $baseDir . '/core/counter.class.inc.php',
|
||||
'ListExpression' => $baseDir . '/core/oql/expression.class.inc.php',
|
||||
'ListOqlExpression' => $baseDir . '/core/oql/oqlquery.class.inc.php',
|
||||
'LogAPI' => $baseDir . '/core/log.class.inc.php',
|
||||
|
||||
@@ -361,6 +361,9 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
|
||||
'ContextTag' => __DIR__ . '/../..' . '/core/contexttag.class.inc.php',
|
||||
'CoreCannotSaveObjectException' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'CoreException' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'CoreOqlException' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'CoreOqlMultipleResultsForbiddenException' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'CorePortalInvalidActionRuleException' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'CoreServices' => __DIR__ . '/../..' . '/core/restservices.class.inc.php',
|
||||
'CoreUnexpectedValue' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'CoreWarning' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
@@ -476,8 +479,10 @@ class ComposerStaticInit0018331147de7601e7552f7da8e3bb8b
|
||||
'Introspection' => __DIR__ . '/../..' . '/core/introspection.class.inc.php',
|
||||
'InvalidConfigParamException' => __DIR__ . '/../..' . '/core/coreexception.class.inc.php',
|
||||
'IssueLog' => __DIR__ . '/../..' . '/core/log.class.inc.php',
|
||||
'ItopCounter' => __DIR__ . '/../..' . '/core/counter.class.inc.php',
|
||||
'JSButtonItem' => __DIR__ . '/../..' . '/application/applicationextension.inc.php',
|
||||
'JSPopupMenuItem' => __DIR__ . '/../..' . '/application/applicationextension.inc.php',
|
||||
'KeyValueStore' => __DIR__ . '/../..' . '/core/counter.class.inc.php',
|
||||
'ListExpression' => __DIR__ . '/../..' . '/core/oql/expression.class.inc.php',
|
||||
'ListOqlExpression' => __DIR__ . '/../..' . '/core/oql/oqlquery.class.inc.php',
|
||||
'LogAPI' => __DIR__ . '/../..' . '/core/log.class.inc.php',
|
||||
|
||||
Reference in New Issue
Block a user