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 "main/EGenTables_stdafx.h"
39
40using namespace TPCE;
41
42// Percentages used in determining gender.
43const int iPercentGenderIsMale = 49;
44
45/*
46 * Initializes in-memory representation of names files.
47 */
48CPerson::CPerson(const DataFileManager &dfm, TIdent iStartFromCustomer, bool bCacheEnabled)
49 : m_LastNames(dfm.LastNameDataFile()), m_MaleFirstNames(dfm.MaleFirstNameDataFile()),
50 m_FemaleFirstNames(dfm.FemaleFirstNameDataFile()), m_bCacheEnabled(bCacheEnabled), m_iCacheSize(0),
51 m_iCacheOffset(0), INVALID_NAME_CACHE_ENTRY(-1), INVALID_GENDER_CACHE_ENTRY('X') {
52 if (m_bCacheEnabled) {
53 m_iCacheSize = iDefaultLoadUnitSize;
54 m_iCacheOffset = iTIdentShift + iStartFromCustomer;
55 m_FirstNameCache = new int[m_iCacheSize];
56 m_LastNameCache = new int[m_iCacheSize];
57 m_GenderCache = new char[m_iCacheSize];
58 for (int i = 0; i < m_iCacheSize; i++) {
59 m_FirstNameCache[i] = INVALID_NAME_CACHE_ENTRY;
60 m_LastNameCache[i] = INVALID_NAME_CACHE_ENTRY;
61 m_GenderCache[i] = INVALID_GENDER_CACHE_ENTRY;
62 }
63 }
64}
65
66/*
67 * Deallocate in-memory representation of names files.
68 */
69CPerson::~CPerson() {
70 if (m_bCacheEnabled) {
71 delete[] m_FirstNameCache;
72 delete[] m_LastNameCache;
73 delete[] m_GenderCache;
74 }
75}
76
77/*
78 * Resets the cache.
79 */
80void CPerson::InitNextLoadUnit(TIdent iCacheOffsetIncrement) {
81 if (m_bCacheEnabled) {
82 m_iCacheOffset += iCacheOffsetIncrement;
83 for (int i = 0; i < m_iCacheSize; i++) {
84 m_FirstNameCache[i] = INVALID_NAME_CACHE_ENTRY;
85 m_LastNameCache[i] = INVALID_NAME_CACHE_ENTRY;
86 m_GenderCache[i] = INVALID_GENDER_CACHE_ENTRY;
87 }
88 }
89}
90
91/*
92 * Returns the last name for a particular customer id.
93 * It'll always be the same for the same customer id.
94 */
95const string &CPerson::GetLastName(TIdent CID) {
96 return getName<LastNameDataFile_t>(CID, m_LastNames, m_LastNameCache, RNGSeedBaseLastName);
97}
98
99/*
100 * Returns the first name for a particular customer id.
101 * Determines gender first.
102 */
103const string &CPerson::GetFirstName(TIdent CID) {
104 if (IsMaleGender(CID)) {
105 return getName<MaleFirstNameDataFile_t>(CID, m_MaleFirstNames, m_FirstNameCache, RNGSeedBaseFirstName);
106 } else {
107 return getName<FemaleFirstNameDataFile_t>(CID, m_FemaleFirstNames, m_FirstNameCache, RNGSeedBaseFirstName);
108 }
109}
110/*
111 * Returns the middle name.
112 */
113char CPerson::GetMiddleName(TIdent CID) {
114 RNGSEED OldSeed;
115 char cMiddleInitial[2];
116
117 OldSeed = m_rnd.GetSeed();
118 m_rnd.SetSeed(m_rnd.RndNthElement(RNGSeedBaseMiddleInitial, (RNGSEED)CID));
119 cMiddleInitial[1] = '\0';
120 m_rnd.RndAlphaNumFormatted(cMiddleInitial, "a");
121 m_rnd.SetSeed(OldSeed);
122 return (cMiddleInitial[0]);
123}
124
125/*
126 * Returns the gender character for a particular customer id.
127 */
128char CPerson::GetGender(TIdent CID) {
129 RNGSEED OldSeed;
130 char cGender;
131
132 // It is possible (and expected) to get CID values that are oustide the
133 // current load unit. For example, AccountPermission CIDs and Broker IDs
134 // can be outside the current load unit. These "out of bounds" CIDs are not
135 // cached so we need to account for this.
136 TIdent index = CID - m_iCacheOffset;
137 bool bCheckCache = (index >= 0 && index < m_iCacheSize);
138
139 // Use the cache if we can.
140 if (m_bCacheEnabled && bCheckCache && (INVALID_GENDER_CACHE_ENTRY != m_GenderCache[index])) {
141 return m_GenderCache[index];
142 }
143
144 // We couldn't use the cache.
145
146 OldSeed = m_rnd.GetSeed();
147 m_rnd.SetSeed(m_rnd.RndNthElement(RNGSeedBaseGender, (RNGSEED)CID));
148
149 // Find out gender
150 if (m_rnd.RndPercent(iPercentGenderIsMale)) {
151 cGender = 'M';
152 } else {
153 cGender = 'F';
154 }
155
156 // Cache the result if appropriate.
157 if (m_bCacheEnabled && bCheckCache) {
158 m_GenderCache[index] = cGender;
159 }
160
161 // Restore the RNG
162 m_rnd.SetSeed(OldSeed);
163 return (cGender);
164}
165
166/*
167 * Returns TRUE is a customer id is male
168 */
169bool CPerson::IsMaleGender(TIdent CID) {
170 return GetGender(CID) == 'M';
171}
172
173/*
174 * Generate tax id
175 */
176void CPerson::GetTaxID(TIdent CID, char *buf) {
177 RNGSEED OldSeed;
178
179 OldSeed = m_rnd.GetSeed();
180
181 // NOTE: the call to RndAlphaNumFormatted "consumes" an RNG value
182 // for EACH character in the format string. Therefore, to avoid getting
183 // tax ID's that overlap N-1 out of N characters, multiply the offset into
184 // the sequence by N to get a unique range of values.
185 m_rnd.SetSeed(m_rnd.RndNthElement(RNGSeedBaseTaxID, ((RNGSEED)CID * TaxIDFmt_len)));
186 m_rnd.RndAlphaNumFormatted(buf, TaxIDFmt);
187 m_rnd.SetSeed(OldSeed);
188}
189
190/*
191 * Get first name, last name, and tax id.
192 */
193void CPerson::GetFirstLastAndTaxID(TIdent C_ID, char *szFirstName, char *szLastName, char *szTaxID) {
194 // Fill in the last name
195 strncpy(szLastName, GetLastName(C_ID).c_str(), cL_NAME_len);
196 // Fill in the first name
197 strncpy(szFirstName, GetFirstName(C_ID).c_str(), cF_NAME_len);
198 // Fill in the tax id
199 GetTaxID(C_ID, szTaxID);
200}
201