| 1 | /* |
| 2 | * Legal Notice |
| 3 | * |
| 4 | * This document and associated source code (the "Work") is a part of a |
| 5 | * benchmark specification maintained by the TPC. |
| 6 | * |
| 7 | * The TPC reserves all right, title, and interest to the Work as provided |
| 8 | * under U.S. and international laws, including without limitation all patent |
| 9 | * and trademark rights therein. |
| 10 | * |
| 11 | * No Warranty |
| 12 | * |
| 13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION |
| 14 | * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE |
| 15 | * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER |
| 16 | * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, |
| 17 | * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, |
| 18 | * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF |
| 20 | * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. |
| 21 | * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, |
| 22 | * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT |
| 23 | * WITH REGARD TO THE WORK. |
| 24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO |
| 25 | * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE |
| 26 | * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS |
| 27 | * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, |
| 28 | * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, |
| 29 | * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT |
| 30 | * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD |
| 31 | * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. |
| 32 | * |
| 33 | * Contributors |
| 34 | * - Sergey Vasilevskiy |
| 35 | * - Doug Johnson |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * Class representing the Customer Taxrates table. |
| 40 | */ |
| 41 | #ifndef CUSTOMER_TAX_RATE_TABLE_H |
| 42 | #define CUSTOMER_TAX_RATE_TABLE_H |
| 43 | |
| 44 | #include "EGenTables_common.h" |
| 45 | #include "CustomerTable.h" |
| 46 | #include "AddressTable.h" |
| 47 | |
| 48 | #include "input/DataFileManager.h" |
| 49 | |
| 50 | namespace TPCE { |
| 51 | |
| 52 | const UINT iTaxRatesPerCust = 2; // number of tax rates per customer |
| 53 | const int iMaxDivOrCtryName = 6; |
| 54 | |
| 55 | // Number of RNG calls to skip for one row in order |
| 56 | // to not use any of the random values from the previous row. |
| 57 | const int iRNGSkipOneRowCustomerTaxrate = 5; // real max count in v3.5: 2 |
| 58 | |
| 59 | typedef struct CUSTOMER_TAXRATE_ROWS { |
| 60 | CUSTOMER_TAXRATE_ROW |
| 61 | m_row[iTaxRatesPerCust]; // multiple tax rates rows per customer |
| 62 | } * PCUSTOMER_TAXRATE_ROWS; |
| 63 | |
| 64 | class CCustomerTaxRateTable : public TableTemplate<CUSTOMER_TAXRATE_ROWS> { |
| 65 | CCustomerTable m_cust; |
| 66 | CAddressTable m_addr; |
| 67 | const TaxRateDivisionDataFile_t &m_division_rates; |
| 68 | const TaxRateCountryDataFile_t &m_country_rates; |
| 69 | |
| 70 | /* |
| 71 | * Reset the state for the next load unit |
| 72 | */ |
| 73 | void InitNextLoadUnit() { |
| 74 | m_rnd.SetSeed( |
| 75 | m_rnd.RndNthElement(RNGSeedTableDefault, (RNGSEED)m_cust.GetCurrentC_ID() * iRNGSkipOneRowCustomerTaxrate)); |
| 76 | |
| 77 | ClearRecord(); // this is needed for EGenTest to work |
| 78 | } |
| 79 | |
| 80 | // generate the tax row deterministically for a given customer and country |
| 81 | // or division code |
| 82 | const ITaxRateFileRecord &GetTaxRow(TIdent C_ID, UINT iCode, bool bCtry) { |
| 83 | RNGSEED OldSeed; |
| 84 | UINT iThreshold; |
| 85 | // const vector<TTaxRateInputRow> *pRates; |
| 86 | |
| 87 | OldSeed = m_rnd.GetSeed(); |
| 88 | m_rnd.SetSeed(m_rnd.RndNthElement(RNGSeedBaseTaxRateRow, (RNGSEED)C_ID)); |
| 89 | |
| 90 | if (bCtry) { |
| 91 | // Return appropriate country record. |
| 92 | iThreshold = (UINT)m_rnd.RndIntRange(0, m_country_rates.getBucket(iCode).size() - 1); |
| 93 | m_rnd.SetSeed(OldSeed); |
| 94 | return m_country_rates.getBucket(iCode)[iThreshold]; |
| 95 | } |
| 96 | |
| 97 | // It's not a country so return the appropriate division record. |
| 98 | iThreshold = (UINT)m_rnd.RndIntRange(0, m_division_rates.getBucket(iCode).size() - 1); |
| 99 | m_rnd.SetSeed(OldSeed); |
| 100 | return m_division_rates.getBucket(iCode)[iThreshold]; |
| 101 | } |
| 102 | |
| 103 | public: |
| 104 | CCustomerTaxRateTable(const DataFileManager &dfm, TIdent iCustomerCount, TIdent iStartFromCustomer, |
| 105 | bool bCacheEnabled = false) |
| 106 | : TableTemplate<CUSTOMER_TAXRATE_ROWS>(), m_cust(dfm, iCustomerCount, iStartFromCustomer), |
| 107 | m_addr(dfm, iCustomerCount, iStartFromCustomer, true, bCacheEnabled), |
| 108 | m_division_rates(dfm.TaxRateDivisionDataFile()), m_country_rates(dfm.TaxRateCountryDataFile()){}; |
| 109 | |
| 110 | /* |
| 111 | * Generates all column values for the next row. |
| 112 | */ |
| 113 | bool GenerateNextRecord() { |
| 114 | UINT iDivCode, iCtryCode; |
| 115 | |
| 116 | if (m_cust.GetCurrentC_ID() % iDefaultLoadUnitSize == 0) { |
| 117 | InitNextLoadUnit(); |
| 118 | } |
| 119 | |
| 120 | ++m_iLastRowNumber; |
| 121 | |
| 122 | m_cust.GenerateNextC_ID(); // next customer id |
| 123 | m_addr.GenerateNextAD_ID(); // next address id (to get the one for this |
| 124 | // customer) |
| 125 | m_addr.GetDivisionAndCountryCodes(iDivCode, iCtryCode); |
| 126 | // Fill the country tax rate row |
| 127 | m_row.m_row[0].CX_C_ID = m_cust.GetCurrentC_ID(); // fill the customer |
| 128 | // id |
| 129 | // Select the country rate |
| 130 | strncpy(m_row.m_row[0].CX_TX_ID, GetCountryTaxRow(m_cust.GetCurrentC_ID(), iCtryCode).TX_ID_CSTR(), |
| 131 | sizeof(m_row.m_row[0].CX_TX_ID)); |
| 132 | |
| 133 | // Fill the division tax rate row |
| 134 | m_row.m_row[1].CX_C_ID = m_cust.GetCurrentC_ID(); // fill the customer |
| 135 | // id |
| 136 | // Select the division rate |
| 137 | strncpy(m_row.m_row[1].CX_TX_ID, GetDivisionTaxRow(m_cust.GetCurrentC_ID(), iDivCode).TX_ID_CSTR(), |
| 138 | sizeof(m_row.m_row[0].CX_TX_ID)); |
| 139 | |
| 140 | m_bMoreRecords = m_cust.MoreRecords(); |
| 141 | |
| 142 | return (MoreRecords()); |
| 143 | } |
| 144 | |
| 145 | const CUSTOMER_TAXRATE_ROW &GetRowByIndex(UINT i) { |
| 146 | if (i < iTaxRatesPerCust) |
| 147 | return m_row.m_row[i]; |
| 148 | else |
| 149 | throw std::range_error("Customer Taxrate row index out of bounds." ); |
| 150 | } |
| 151 | |
| 152 | UINT GetTaxRatesCount() { |
| 153 | return iTaxRatesPerCust; |
| 154 | } // tax rates per customer |
| 155 | |
| 156 | // generate country tax row for a given customer |
| 157 | const ITaxRateFileRecord &GetCountryTaxRow(TIdent C_ID, UINT iCtryCode) { |
| 158 | return GetTaxRow(C_ID, iCtryCode, true); |
| 159 | } |
| 160 | |
| 161 | // generate division tax row for a given customer |
| 162 | const ITaxRateFileRecord &GetDivisionTaxRow(TIdent C_ID, UINT iDivCode) { |
| 163 | return GetTaxRow(C_ID, iDivCode, false); |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | } // namespace TPCE |
| 168 | |
| 169 | #endif // CUSTOMER_TAX_RATE_TABLE_H |
| 170 | |