/**
* Execute the FindOpenSeat procedure
* @throws SQLException
*/
private boolean executeFindOpenSeats(FindOpenSeats proc) throws SQLException {
final FlightId search_flight = this.profile.getRandomFlightId();
assert(search_flight != null);
Long airport_depart_id = search_flight.getDepartAirportId();
if (LOG.isTraceEnabled()) LOG.trace("Calling " + proc);
Object[][] results = proc.run(conn, search_flight.encode());
conn.commit();
int rowCount = results.length;
assert (rowCount <= SEATSConstants.FLIGHTS_NUM_SEATS) :
String.format("Unexpected %d open seats returned for %s", rowCount, search_flight);
// there is some tiny probability of an empty flight .. maybe 1/(20**150)
// if you hit this assert (with valid code), play the lottery!
if (rowCount == 0) return (true);
LinkedList<Reservation> cache = CACHE_RESERVATIONS.get(CacheType.PENDING_INSERTS);
assert(cache != null) : "Unexpected " + CacheType.PENDING_INSERTS;
// Store pending reservations in our queue for a later transaction
BitSet seats = getSeatsBitSet(search_flight);
tmp_reservations.clear();
for (Object row[] : results) {
if (row == null) continue; // || rng.nextInt(100) < 75) continue; // HACK
Integer seatnum = (Integer)row[1];
// We first try to get a CustomerId based at this departure airport
if (LOG.isTraceEnabled())
LOG.trace("Looking for a random customer to fly on " + search_flight);
CustomerId customer_id = profile.getRandomCustomerId(airport_depart_id);
// We will go for a random one if:
// (1) The Customer is already booked on this Flight
// (2) We already made a new Reservation just now for this Customer
int tries = SEATSConstants.FLIGHTS_NUM_SEATS;
while (tries-- > 0 && (customer_id == null)) { // || isCustomerBookedOnFlight(customer_id, flight_id))) {
customer_id = profile.getRandomCustomerId();
if (LOG.isTraceEnabled())
LOG.trace("RANDOM CUSTOMER: " + customer_id);
} // WHILE
assert(customer_id != null) :
String.format("Failed to find a unique Customer to reserve for seat #%d on %s", seatnum, search_flight);
Reservation r = new Reservation(profile.getNextReservationId(getId()),
search_flight,
customer_id,
seatnum.intValue());
seats.set(seatnum);
tmp_reservations.add(r);
if (LOG.isTraceEnabled())
LOG.trace("QUEUED INSERT: " + search_flight + " / " + search_flight.encode() + " -> " + customer_id);
} // WHILE
if (tmp_reservations.isEmpty() == false) {
Collections.shuffle(tmp_reservations);
cache.addAll(tmp_reservations);