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#include "input/CompanyFile.h"
39
40#include <cstdio>
41#include <cstring>
42
43#include "utilities/MiscConsts.h"
44
45using namespace TPCE;
46
47/*
48 * Constructor.
49 *
50 * PARAMETERS:
51 * IN str - file name of the CompanyCompetitor
52 * input flat file IN iConfiguredCustomerCount - total configured number of
53 * customers in the database IN iActiveCustomerCount - active number of
54 * customers in the database (provided for engineering purposes)
55 *
56 * RETURNS:
57 * not applicable.
58 */
59CCompanyFile::CCompanyFile(const CompanyDataFile_t &dataFile, TIdent iConfiguredCustomerCount,
60 TIdent iActiveCustomerCount)
61 : m_dataFile(&dataFile), m_iConfiguredCompanyCount(CalculateCompanyCount(iConfiguredCustomerCount)),
62 m_iActiveCompanyCount(CalculateCompanyCount(iActiveCustomerCount)) {
63}
64
65/*
66 * Calculate company count for the specified number of customers.
67 * Sort of a static method. Used in parallel generation of company related
68 * tables.
69 *
70 * PARAMETERS:
71 * IN iCustomerCount - number of customers
72 *
73 * RETURNS:
74 * number of company competitors.
75 */
76TIdent CCompanyFile::CalculateCompanyCount(TIdent iCustomerCount) const {
77 return iCustomerCount / iDefaultLoadUnitSize * iOneLoadUnitCompanyCount;
78}
79
80/*
81 * Calculate the first company id (0-based) for the specified customer id.
82 *
83 * PARAMETERS:
84 * IN iStartFromCustomer - customer id
85 *
86 * RETURNS:
87 * company competitor id.
88 */
89TIdent CCompanyFile::CalculateStartFromCompany(TIdent iStartFromCustomer) const {
90 return iStartFromCustomer / iDefaultLoadUnitSize * iOneLoadUnitCompanyCount;
91}
92
93/*
94 * Create company name with appended suffix based on the
95 * load unit number.
96 *
97 * PARAMETERS:
98 * IN iIndex - row number in the Company Competitor file (0-based)
99 * IN szOutput - output buffer for company name
100 * IN iOutputLen - size of the output buffer
101 *
102 * RETURNS:
103 * none.
104 */
105void CCompanyFile::CreateName(TIdent iIndex, // row number
106 char *szOutput, // output buffer
107 size_t iOutputLen) // size of the output buffer
108 const {
109 TIdent iFileIndex = iIndex % m_dataFile->size();
110 TIdent iAdd = iIndex / m_dataFile->size();
111
112 if (iAdd > 0) {
113 snprintf(szOutput, iOutputLen, "%s #%" PRId64, GetRecord(iFileIndex).CO_NAME_CSTR(), iAdd);
114 } else {
115 strncpy(szOutput, GetRecord(iFileIndex).CO_NAME_CSTR(), iOutputLen);
116 }
117}
118
119/*
120 * Return company id for the specified row.
121 * Index can exceed the size of the Company input file.
122 *
123 * PARAMETERS:
124 * IN iIndex - row number in the Company Competitor file (0-based)
125 *
126 * RETURNS:
127 * company id.
128 */
129TIdent CCompanyFile::GetCompanyId(TIdent iIndex) const {
130 // Index wraps around every 5000 companies.
131 //
132 return (*m_dataFile)[(int)(iIndex % m_dataFile->size())].CO_ID() + iTIdentShift +
133 iIndex / m_dataFile->size() * m_dataFile->size();
134}
135
136/*
137 * Return the number of companies in the database for
138 * the configured number of customers.
139 *
140 * PARAMETERS:
141 * none.
142 *
143 * RETURNS:
144 * number of rows in the file.
145 */
146TIdent CCompanyFile::GetSize() const {
147 return m_iConfiguredCompanyCount;
148}
149
150/*
151 * Return the number of companies in the database for
152 * the configured number of customers.
153 *
154 * PARAMETERS:
155 * none.
156 *
157 * RETURNS:
158 * configured company count.
159 */
160TIdent CCompanyFile::GetConfiguredCompanyCount() const {
161 return m_iConfiguredCompanyCount;
162}
163
164/*
165 * Return the number of companies in the database for
166 * the active number of customers.
167 *
168 * PARAMETERS:
169 * none.
170 *
171 * RETURNS:
172 * active company count.
173 */
174TIdent CCompanyFile::GetActiveCompanyCount() const {
175 return m_iActiveCompanyCount;
176}
177
178/*
179 * Overload GetRecord to wrap around indices that
180 * are larger than the flat file
181 *
182 * PARAMETERS:
183 * IN iIndex - row number in the Company file (0-based)
184 *
185 * RETURNS:
186 * reference to the row structure in the Company file.
187 */
188const CompanyDataFileRecord &CCompanyFile::GetRecord(TIdent index) const {
189 return (*m_dataFile)[(int)(index % m_dataFile->size())];
190}
191