--[[ Copyright (C) 2008 optivo GmbH This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; version 2 of the License. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA --]] --- A partition lookup service based on modulus.
-- -- This implementation just does a modulo on the partition value. If the value is anything but -- a number then it will be converted to a string and a checksum will be calculated. -- For each value between 0 and the modulus value a table with the suffix _[value] will be created. -- All tables are spread across all backends. This makes it impossible to add new backend servers -- since then the indexes start to shift. -- -- Important note: This is just a simple demo partition lookup. You should never use it in production. -- -- @author $Author$ -- @release $Date$ $Rev$ module("optivo.hscale.modulusPartitionLookup", package.seeall) local _module = {} local utils = require("optivo.common.utils") local _tableKeyColumns local _partitionsPerTable --- Initialize the module using the current configuration.
-- The following configuration parameters are required: -- -- @see optivo.hscale.config function _module.init(config) _tableKeyColumns = config.get("tableKeyColumns") _partitionsPerTable = tonumber(config.get("partitionsPerTable")) assert(_partitionsPerTable > 0, "Invalid value for option partitionsPerTable.") end --- Query the table / key combinations. function _module.getTableKeyColumns() return _tableKeyColumns end --- Query all available partitions for a given table. -- @return all partition table names and the backend indexes (Example: { {name=tab1, backend=1}, {name=tab2, backend=2} }) function _module.getAllPartitions(tableName) local tables = {} for a = 0, _partitionsPerTable -1 do table.insert(tables, { name = tableName .. "_" .. tostring(a), backend = (a % #proxy.backends) + 1 }) end return tables end --- Query a partition. -- @return the partition table for the given partition key and the backend index (Example: name, backend = getPartition("test", "a")) function _module.getPartition(tableName, partitionKey) assert(_tableKeyColumns[tableName], "Table '" .. tableName .. "' is not supported.") local number = tonumber(partitionKey) if (not number) then number = utils.calculateSimpleChecksum(tostring(partitionKey)) end local index = number % _partitionsPerTable return tableName .. "_" .. tostring(index), (index % #proxy.backends) + 1 end return _module