use of org.voltdb.VoltTable in project voltdb by VoltDB.
the class BidGenerator method getMaxBidId.
/**
* Find the current highest bid id in the bids table. We'll start generating
* new bids at this number plus one.
* @param client A connection to the database
* @return current highest bid id
*/
private static long getMaxBidId(Client client) {
long currentMaxBidId = 0;
try {
VoltTable vt = client.callProcedure("@AdHoc", "select max(id) from bids").getResults()[0];
vt.advanceRow();
currentMaxBidId = vt.getLong(0);
if (vt.wasNull()) {
currentMaxBidId = 0;
}
} catch (IOException | ProcCallException e) {
e.printStackTrace();
}
return currentMaxBidId;
}
use of org.voltdb.VoltTable in project voltdb by VoltDB.
the class GetHighestBidForLocation method run.
public long run(long deviceId, GeographyPointValue point) {
long matchingBidId = -1;
// Find a matching bid for this location and the current time.
voltQueueSQL(getHighestBidStmt, point);
VoltTable vt = voltExecuteSQL()[0];
if (vt.getRowCount() > 0) {
// We found a match!
vt.advanceRow();
matchingBidId = vt.getLong(0);
long advertiserId = vt.getLong(1);
double bidAmount = vt.getDouble(2);
voltQueueSQL(insertRequestStmt, deviceId, matchingBidId, advertiserId, bidAmount);
} else {
// No applicable bids. Insert a row containing
// null values to show that a device appeared
// but no ad was served.
voltQueueSQL(insertRequestStmt, deviceId, null, null, null);
}
// Update ad_requests with the result of the request.
voltExecuteSQL(true);
return matchingBidId;
}
use of org.voltdb.VoltTable in project voltdb by VoltDB.
the class Client method runStatusLoop.
/**
* Loop until all auctions have closed, printing status for each auction at
* specific intervals.
*/
static void runStatusLoop() {
// create a second client handle for this second thread
org.voltdb.client.Client client = null;
try {
ClientConfig config = new ClientConfig("program", "pass");
client = ClientFactory.createClient(config);
client.createConnection("localhost");
} catch (java.io.IOException e) {
e.printStackTrace();
System.exit(-1);
}
boolean auctionsOver = false;
while (auctionsOver == false) {
try {
// assume auctions have ended. if any open auctions are found, then set this false
auctionsOver = true;
// print out the status header
System.out.printf("\nAUCTION STATUS AS OF: %tT\n", new Date());
System.out.printf("+-----------------------------------------------------" + "-----------------------------------------------+\n");
System.out.printf("| %-20s | %-16s | %-16s | %5s | %8s | %9s | %-6s |\n", "ITEM", "BIDDER", "SELLER", "BIDS", "PRICE", "END", "STATUS");
System.out.printf("+-----------------------------------------------------" + "-----------------------------------------------+\n");
// loop over all auction ids, printing a row of status for each one
for (int auctionId : allAuctionIds) {
VoltTable[] statusResultSet = client.callProcedure("AuctionStatus", auctionId).getResults();
if (statusResultSet.length != 1)
throw new Exception("AuctionStatus returned no results");
VoltTable statusTable = statusResultSet[0];
VoltTableRow row = statusTable.fetchRow(0);
System.out.printf("| %-20s | %-16s | %-16s | %5d | %8.2f | %9tT | %-6s |\n", row.getString("item"), row.getString("bidder"), row.getString("seller"), row.getLong("bidcount"), row.getDouble("price"), row.getTimestampAsTimestamp("endtime").asApproximateJavaDate(), row.getString("status"));
if (row.getString("status").equals("OPEN"))
auctionsOver = false;
}
// print the status footer
System.out.printf("+-----------------------------------------------------" + "-----------------------------------------------+\n");
// wait for next status update
Thread.sleep(1000 * statusPeriodInSeconds);
} catch (Exception e) {
e.printStackTrace();
}
}
try {
client.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of org.voltdb.VoltTable in project voltdb by VoltDB.
the class Client method doBid.
/**
* Attempt to make a single bid on an auction.
*
* @param auctionId The id of the auction to bid on.
* @param userId The id of the user making the bid.
* @return A status code which can be found at the top of this class.
*/
static int doBid(org.voltdb.client.Client client, int auctionId, int userId) {
///////////////////////////////////////
// get the current bid price of an item
///////////////////////////////////////
VoltTable infoResult = null;
try {
VoltTable[] infoResultSet = client.callProcedure("GetAuctionInfo", auctionId).getResults();
if (infoResultSet.length != 1)
throw new Exception("GetAuctionInfo returned no results");
infoResult = infoResultSet[0];
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
double oldBidAmount = infoResult.fetchRow(0).getDouble("BIDPRICE");
////////////////////////////////////////
// make a new bid on the exact same item
////////////////////////////////////////
double newBidAmount = getRandomNewBidAmount(oldBidAmount);
VoltTable bidResult = null;
try {
VoltTable[] bidResultSet = client.callProcedure("BidOnAuction", auctionId, userId, newBidAmount, nextBidId++).getResults();
if (bidResultSet.length != 1)
throw new Exception("BidOnAuction returned no results");
bidResult = bidResultSet[0];
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
// we're primarily interested in POST_CLOSE_BID
return (int) bidResult.asScalarLong();
}
use of org.voltdb.VoltTable in project voltdb by VoltDB.
the class Client method main.
/**
* Load the initial data, then run the simulation.
*
* @param args This program uses no command line arguments.
*/
public static void main(String[] args) {
System.out.println("***************************************");
System.out.println("* Welcome to Bobbi's Awesome Auctions *");
System.out.println("* *");
System.out.println("* Connecting to Server... *");
// connect to VoltDB server
org.voltdb.client.Client client = null;
ClientConfig config = new ClientConfig("program", "pass");
client = ClientFactory.createClient(config);
int sleep = 1000;
while (true) {
try {
client.createConnection("localhost");
break;
} catch (Exception e) {
System.out.println("Connection failed - retrying in " + (sleep / 1000) + " second(s).");
try {
Thread.sleep(sleep);
} catch (Exception tie) {
}
if (sleep < 8000)
sleep += sleep;
}
}
System.out.println("* Connected *");
System.out.println("* Running loader *");
System.out.println("***************************************\n");
// get itemId and userId from database
try {
VoltTable modCount = client.callProcedure("@AdHoc", "SELECT ITEMID FROM ITEM").getResults()[0];
allAuctionIds = new ArrayList<Integer>();
userIds = new ArrayList<Integer>();
while (modCount.advanceRow()) {
Integer i = (Integer) modCount.get(0, VoltType.INTEGER);
allAuctionIds.add(i);
}
modCount = client.callProcedure("@AdHoc", "SELECT USERID FROM USER").getResults()[0];
while (modCount.advanceRow()) {
Integer i = (Integer) modCount.get(0, VoltType.INTEGER);
userIds.add(i);
}
activeAuctionIds.addAll(allAuctionIds);
} catch (Exception e) {
e.printStackTrace();
}
// since we create 1 bid per auction
nextBidId = allAuctionIds.size() + 1;
System.out.println("***************************************");
System.out.println("* Finished running loader *");
System.out.println("* Running auctions *");
System.out.println("***************************************");
// create and start a thread that prints auction status every
// 10 seconds, ending when all auctions have closed
Thread statusThread = new Thread(new Runnable() {
public void run() {
runStatusLoop();
}
});
statusThread.start();
// runloop executes bids until all auctions have ended
runBidLoop(client);
// wait for the status-printing thread to finish
try {
statusThread.join();
} catch (Exception e) {
}
// print out a joke with dramatic pauses
System.out.println("\n***************************************");
System.out.println("* Complete... *");
System.out.println("* *");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
System.out.println("* Where do ghosts shop? *");
System.out.println("* *");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
System.out.println("* In Boo-tiques! *");
System.out.println("***************************************");
try {
client.close();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Aggregations