Package com.oltpbenchmark.benchmarks.auctionmark.util

Examples of com.oltpbenchmark.benchmarks.auctionmark.util.ItemStatus


        }
        int col = 1;
        long i_num_bids = results.getLong(col++);
        double i_current_price = results.getDouble(col++);
        Timestamp i_end_date = results.getTimestamp(col++);
        ItemStatus i_status = ItemStatus.CLOSED;
        long ib_id = results.getLong(col++);
        long ib_buyer_id = results.getLong(col++);
        double u_balance = results.getDouble(col++);
        results.close();
       
        // Make sure that the buyer has enough money to cover this charge
        // We can add in a credit for the buyer's account
        if (i_current_price > (buyer_credit + u_balance)) {
            String msg = String.format("Buyer #%d does not have enough money in account to purchase Item #%d" +
                                       "[maxBid=%.2f, balance=%.2f, credit=%.2f]",
                                       ib_buyer_id, item_id, i_current_price, u_balance, buyer_credit);
            throw new UserAbortException(msg);
        }

        // Set item_purchase_id
        updated = this.getPreparedStatement(conn, insertPurchase, ip_id, ib_id, item_id, seller_id, currentTime).executeUpdate();
        assert(updated == 1);
       
        // Update item status to close
        updated = this.getPreparedStatement(conn, updateItem, currentTime, item_id, seller_id).executeUpdate();
        assert(updated == 1) :
            String.format("Failed to update %s for Seller #%d's Item #%d",
                          AuctionMarkConstants.TABLENAME_ITEM, seller_id, item_id);
       
        // And update this the USERACT_ITEM record to link it to the new ITEM_PURCHASE record
        // If we don't have a record to update, just go ahead and create it
        updated = this.getPreparedStatement(conn, updateUserItem, ip_id, ib_id, item_id, seller_id,
                                                                  ib_buyer_id, item_id, seller_id).executeUpdate();
        if (updated == 0) {
            updated = this.getPreparedStatement(conn, insertUserItem, ib_buyer_id, item_id, seller_id,
                                                                      ip_id, ib_id, item_id, seller_id,
                                                                      currentTime).executeUpdate();
        }
        assert(updated == 1) :
            String.format("Failed to update %s for Buyer #%d's Item #%d",
                          AuctionMarkConstants.TABLENAME_USERACCT_ITEM, ib_buyer_id, item_id);
       
        // Decrement the buyer's account
        updated = this.getPreparedStatement(conn, updateUserBalance, -1*(i_current_price) + buyer_credit, ib_buyer_id).executeUpdate();
        assert(updated == 1) :
            String.format("Failed to update %s for Buyer #%d",
                          AuctionMarkConstants.TABLENAME_USERACCT, ib_buyer_id);
       
        // And credit the seller's account
        this.getPreparedStatement(conn, updateUserBalance, i_current_price, seller_id).executeUpdate();
        assert(updated == 1) :
            String.format("Failed to update %s for Seller #%d",
                          AuctionMarkConstants.TABLENAME_USERACCT, seller_id);
       
        // Return a tuple of the item that we just updated
        return new Object[] {
            // ITEM ID
            item_id,
            // SELLER ID
            seller_id,
            // ITEM_NAME
            null,
            // CURRENT PRICE
            i_current_price,
            // NUM BIDS
            i_num_bids,
            // END DATE
            i_end_date,
            // STATUS
            i_status.ordinal(),
            // PURCHASE ID
            ip_id,
            // BID ID
            ib_id,
            // BUYER ID
View Full Code Here


        int col = 1;
        double i_initial_price = results.getDouble(col++);
        double i_current_price = results.getDouble(col++);
        long i_num_bids = results.getLong(col++);
        Timestamp i_end_date = results.getTimestamp(col++);
        ItemStatus i_status = ItemStatus.get(results.getLong(col++));
        results.close();
        long newBidId = 0;
        long newBidMaxBuyerId = buyer_id;
       
//        if (i_end_date.compareTo(currentTime) < 0 || i_status != ItemStatus.OPEN) {
//            if (debug)
//                LOG.debug(String.format("The auction for item %d has ended [status=%s]\nCurrentTime:\t%s\nActualEndDate:\t%s\nEstimatedEndDate:\t%s",
//                                        item_id, i_status, currentTime, i_end_date, estimatedEndDate));
//            throw new UserAbortException("Unable to bid on item: Auction has ended");
//        }
       
        // If we existing bids, then we need to figure out whether we are the new highest
        // bidder or if the existing one just has their max_bid bumped up
        if (i_num_bids > 0) {
            // Get the next ITEM_BID id for this item
            if (debug) LOG.debug("Retrieving ITEM_MAX_BID information for " + ItemId.toString(item_id));
            stmt = this.getPreparedStatement(conn, getMaxBidId, item_id, seller_id);
            results = stmt.executeQuery();
            boolean advanceRow = results.next();
            assert (advanceRow);
            newBidId = results.getLong(1) + 1;
            results.close();
           
            // Get the current max bid record for this item
            stmt = this.getPreparedStatement(conn, getItemMaxBid, item_id, seller_id);
            results = stmt.executeQuery();
            advanceRow = results.next();
            assert (advanceRow);
            col = 1;
            long currentBidId = results.getLong(col++);
            double currentBidAmount = results.getDouble(col++);
            double currentBidMax = results.getDouble(col++);
            long currentBuyerId = results.getLong(col++);
            results.close();
           
            boolean updateMaxBid = false;
            assert((int)currentBidAmount == (int)i_current_price) :
                String.format("%.2f == %.2f", currentBidAmount, i_current_price);
           
            // Check whether this bidder is already the max bidder
            // This means we just need to increase their current max bid amount without
            // changing the current auction price
            if (buyer_id == currentBuyerId) {
                if (newBid < currentBidMax) {
                    String msg = String.format("%s is already the highest bidder for Item %d but is trying to " +
                                               "set a new max bid %.2f that is less than current max bid %.2f",
                                               buyer_id, item_id, newBid, currentBidMax);
                    if (debug) LOG.debug(msg);
                    throw new UserAbortException(msg);
                }
                this.getPreparedStatement(conn, updateBid, i_current_price,
                                                           newBid,
                                                           currentTime,
                                                           currentBidId,
                                                           item_id,
                                                           seller_id).executeUpdate();
                if (debug) LOG.debug(String.format("Increasing the max bid the highest bidder %s from %.2f to %.2f for Item %d",
                                                   buyer_id, currentBidMax, newBid, item_id));
            }
            // Otherwise check whether this new bidder's max bid is greater than the current max
            else {
                // The new maxBid trumps the existing guy, so our the buyer_id for this txn becomes the new
                // winning bidder at this time. The new current price is one step above the previous
                // max bid amount
                if (newBid > currentBidMax) {
                    i_current_price = Math.min(newBid, currentBidMax + (i_initial_price * AuctionMarkConstants.ITEM_BID_PERCENT_STEP));
                    assert(i_current_price > currentBidMax);
                    // Defer the update to ITEM_MAX_BID until after we insert our new ITEM_BID record
                    updateMaxBid = true;
                }
                // The current max bidder is still the current one
                // We just need to bump up their bid amount to be at least the bidder's amount
                // Make sure that we don't go over the the currentMaxBidMax, otherwise this would mean
                // that we caused the user to bid more than they wanted.
                else {
                    newBidMaxBuyerId = currentBuyerId;
                    i_current_price = Math.min(currentBidMax, newBid + (i_initial_price * AuctionMarkConstants.ITEM_BID_PERCENT_STEP));
                    assert(i_current_price >= newBid) : String.format("%.2f > %.2f", i_current_price, newBid);
                    this.getPreparedStatement(conn, updateBid, i_current_price,
                                                               i_current_price,
                                                               currentTime,
                                                               currentBidId,
                                                               item_id,
                                                               seller_id).executeUpdate();
                    if (debug) LOG.debug(String.format("Keeping the existing highest bidder of Item %d as %s but updating current price from %.2f to %.2f",
                                                       item_id, buyer_id, currentBidAmount, i_current_price));
                }
           
                // Always insert an new ITEM_BID record even if BuyerId doesn't become
                // the new highest bidder. We also want to insert a new record even if
                // the BuyerId already has ITEM_BID record, because we want to maintain
                // the history of all the bid attempts
                this.getPreparedStatement(conn, insertItemBid, newBidId,
                                                               item_id,
                                                               seller_id,
                                                               buyer_id,
                                                               i_current_price,
                                                               newBid,
                                                               currentTime,
                                                               currentTime).executeUpdate();
                this.getPreparedStatement(conn, updateItem, i_current_price,
                                                            currentTime,
                                                            item_id,
                                                            seller_id).executeUpdate();
               
                // This has to be done after we insert the ITEM_BID record to make sure
                // that the HSQLDB test cases work
                if (updateMaxBid) {
                    this.getPreparedStatement(conn, updateItemMaxBid, newBidId,
                                                                      item_id,
                                                                      seller_id,
                                                                      currentTime,
                                                                      item_id,
                                                                      seller_id).executeUpdate();
                    if (debug) LOG.debug(String.format("Changing new highest bidder of Item %d to %s [newMaxBid=%.2f > currentMaxBid=%.2f]",
                                         item_id, UserId.toString(buyer_id), newBid, currentBidMax));
                }
            }
        }
        // There is no existing max bid record, therefore we can just insert ourselves
        else {
            this.getPreparedStatement(conn, insertItemBid, newBidId,
                                                           item_id,
                                                           seller_id,
                                                           buyer_id,
                                                           i_initial_price,
                                                           newBid,
                                                           currentTime,
                                                           currentTime).executeUpdate();
            this.getPreparedStatement(conn, insertItemMaxBid, item_id,
                                                              seller_id,
                                                              newBidId,
                                                              item_id,
                                                              seller_id,
                                                              currentTime,
                                                              currentTime).executeUpdate();
            this.getPreparedStatement(conn, updateItem, i_current_price,
                                                        currentTime,
                                                        item_id,
                                                        seller_id).execute();
            if (debug) LOG.debug(String.format("Creating the first bid record for Item %d and setting %s as highest bidder at %.2f",
                                               item_id, buyer_id, i_current_price));
        }
       
        // Return back information about the current state of the item auction
        return new Object[] {
            // ITEM_ID
            item_id,
            // SELLER_ID
            seller_id,
            // ITEM_NAME
            null, // ignore
            // CURRENT PRICE
            i_current_price,
            // NUM BIDS
            i_num_bids + 1,
            // END DATE
            i_end_date,
            // STATUS
            i_status.ordinal(),
            // MAX BID ID
            newBidId,
            // MAX BID BUYER_ID
            newBidMaxBuyerId,
        };
View Full Code Here

                long sellerId = dueItemsTable.getLong(col++);
                String i_name = dueItemsTable.getString(col++);
                double currentPrice = dueItemsTable.getDouble(col++);
                long numBids = dueItemsTable.getLong(col++);
                Timestamp endDate = dueItemsTable.getTimestamp(col++);
                ItemStatus itemStatus = ItemStatus.get(dueItemsTable.getLong(col++));
                Long bidId = null;
                Long buyerId = null;
               
                if (debug)
                    LOG.debug(String.format("Getting max bid for itemId=%d / sellerId=%d", itemId, sellerId));
                assert(itemStatus == ItemStatus.OPEN);
               
                // Has bid on this item - set status to WAITING_FOR_PURCHASE
                // We'll also insert a new USER_ITEM record as needed
                // We have to do this extra step because H-Store doesn't have good support in the
                // query optimizer for LEFT OUTER JOINs
                if (numBids > 0) {
                    waiting_ctr++;
                   
                    param = 1;
                    maxBidStmt.setLong(param++, itemId);
                    maxBidStmt.setLong(param++, sellerId);
                    maxBidResults = maxBidStmt.executeQuery();
                    adv = maxBidResults.next();
                    assert(adv);
                   
                    col = 1;
                    bidId = maxBidResults.getLong(col++);
                    buyerId = maxBidResults.getLong(col++);
                    updated = this.getPreparedStatement(conn, insertUserItem, buyerId, itemId, sellerId, currentTime).executeUpdate();
                    assert(updated == 1);
                    itemStatus = ItemStatus.WAITING_FOR_PURCHASE;
                    maxBidResults.close();
                }
                // No bid on this item - set status to CLOSED
                else {
                    closed_ctr++;
                    itemStatus = ItemStatus.CLOSED;
                }
               
                // Update Status!
                updated = this.getPreparedStatement(conn, updateItemStatus, itemStatus.ordinal(), currentTime, itemId, sellerId).executeUpdate();
                if (debug)
                    LOG.debug(String.format("Updated Status for Item %d => %s", itemId, itemStatus));
               
                Object row[] = new Object[] {
                        itemId,               // i_id
                        sellerId,             // i_u_id
                        i_name,               // i_name
                        currentPrice,         // i_current_price
                        numBids,              // i_num_bids
                        endDate,              // i_end_date
                        itemStatus.ordinal(), // i_status
                        bidId,                // imb_ib_id
                        buyerId               // ib_buyer_id
                };
                output_rows.add(row);
            } // WHILE
View Full Code Here

        Timestamp i_end_date = SQLUtil.getTimestamp(row[col++]);// i_end_date
        if (i_end_date == null) throw new RuntimeException("DJELLEL IS THE MAN! --> " + row[col-1] + " / " + row[col-1].getClass());
       
        Integer temp = SQLUtil.getInteger(row[col++]);
        if (temp == null) throw new RuntimeException("DJELLEL IS STILL THE MAN! --> " + row[col-1] + " / " + row[col-1].getClass());
        ItemStatus i_status = ItemStatus.get(temp); // i_status
       
        ItemInfo itemInfo = new ItemInfo(i_id, i_current_price, i_end_date, (int)i_num_bids);
        itemInfo.status = i_status;
       
        UserId sellerId = new UserId(i_u_id);
        assert (i_id.getSellerId().equals(sellerId));
        
        ItemStatus qtype = profile.addItemToProperQueue(itemInfo, false);
   
        return (i_id);
    }
View Full Code Here

                return (null);
            }
        }
       
        long remaining = itemInfo.endDate.getTime() - baseTime.getTime();
        ItemStatus new_status = (itemInfo.status != null ? itemInfo.status : ItemStatus.OPEN);
        // Already ended
        if (remaining <= AuctionMarkConstants.ITEM_ALREADY_ENDED) {
            if (itemInfo.numBids > 0 && itemInfo.status != ItemStatus.CLOSED) {
                new_status = ItemStatus.WAITING_FOR_PURCHASE;
            } else {
                new_status = ItemStatus.CLOSED;
            }
        }
        // About to end soon
        else if (remaining < AuctionMarkConstants.ITEM_ENDING_SOON) {
            new_status = ItemStatus.ENDING_SOON;
        }
       
        if (new_status != itemInfo.status) {
            if (itemInfo.status != null)
                assert(new_status.ordinal() > itemInfo.status.ordinal()) :
                    "Trying to improperly move " + itemInfo + " from " + itemInfo.status + " to " + new_status;
           
            switch (new_status) {
                case OPEN:
                    this.addItem(this.items_available, itemInfo);
View Full Code Here

TOP

Related Classes of com.oltpbenchmark.benchmarks.auctionmark.util.ItemStatus

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.