public ReturnTuple<Integer> newCustomer(int id, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting newCustomer to master"); return this.getMaster().newCustomer(id, timestamp); } else { timestamp.stamp(); int rid = Integer.parseInt( String.valueOf(id) + String.valueOf(Calendar.getInstance().get(Calendar.MILLISECOND)) + String.valueOf(Math.round(Math.random() * 100 + 1))); NewCustomerWithIdRMICommand nc = new NewCustomerWithIdRMICommand(carGroup, flightGroup, roomGroup, id, rid); nc.setTimestampObject(timestamp); ReturnTuple<Integer> result = null; try { overseer.validTransaction(id); lm.Lock(id, Customer.getKey(rid), nc.getRequiredLock()); nc.execute(); result = new ReturnTuple<Integer>(rid, nc.success.timestamp); overseer.addCommandToTransaction(id, nc); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Deletes customer from the database. public ReturnTuple<Boolean> deleteCustomer(int id, int customerID, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting deleteCustomer to master"); return this.getMaster().deleteCustomer(id, customerID, timestamp); } else { timestamp.stamp(); DeleteCustomerRMICommand dc = new DeleteCustomerRMICommand(carGroup, flightGroup, roomGroup, id, customerID); dc.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Customer.getKey(customerID), dc.getRequiredLock()); dc.execute(); result = dc.success; if (result.result) overseer.addCommandToTransaction(id, dc); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Returns price of cars at this location public ReturnTuple<Integer> queryCarsPrice(int id, String location, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting queryCarsPrice to master"); return this.getMaster().queryCarsPrice(id, location, timestamp); } else { QueryCarsPriceRMICommand qcp = new QueryCarsPriceRMICommand(roomGroup, id, location); qcp.setTimestampObject(timestamp); ReturnTuple<Integer> result = null; try { overseer.validTransaction(id); lm.Lock(id, Car.getKey(location), qcp.getRequiredLock()); qcp.execute(); result = qcp.price; } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Create a new car location or add cars to an existing location // NOTE: if price <= 0 and the location already exists, it maintains its current price public ReturnTuple<Boolean> addCars( int id, String location, int count, int price, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting addCars to master"); return this.getMaster().addCars(id, location, count, price, timestamp); } else { timestamp.stamp(); AddCarsRMICommand ac = new AddCarsRMICommand(carGroup, id, location, count, price); ac.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Car.getKey(location), ac.getRequiredLock()); ac.execute(); result = ac.success; if (result.result) overseer.addCommandToTransaction(id, ac); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Returns the number of empty seats on this flight public ReturnTuple<Integer> queryFlight(int id, int flightNum, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting queryFlight to master"); return this.getMaster().queryFlight(id, flightNum, timestamp); } else { timestamp.stamp(); QueryFlightRMICommand qf = new QueryFlightRMICommand(flightGroup, id, flightNum); qf.setTimestampObject(timestamp); ReturnTuple<Integer> result = null; try { overseer.validTransaction(id); lm.Lock(id, Flight.getKey(flightNum), qf.getRequiredLock()); qf.execute(); result = qf.numSeats; } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Delete rooms from a location public ReturnTuple<Boolean> deleteRooms(int id, String location, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting deleteRooms to master"); return this.getMaster().deleteRooms(id, location, timestamp); } else { timestamp.stamp(); DeleteRoomsRMICommand dr = new DeleteRoomsRMICommand(roomGroup, id, location); dr.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Hotel.getKey(location), dr.getRequiredLock()); dr.execute(); result = dr.success; if (result.result) overseer.addCommandToTransaction(id, dr); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// Create a new flight, or add seats to existing flight // NOTE: if flightPrice <= 0 and the flight already exists, it maintains its current price public ReturnTuple<Boolean> addFlight( int id, int flightNum, int flightSeats, int flightPrice, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting addFlight to master"); return this.getMaster().addFlight(id, flightNum, flightSeats, flightPrice, timestamp); } else { timestamp.stamp(); AddFlightRMICommand af = new AddFlightRMICommand(flightGroup, id, flightNum, flightSeats, flightPrice); af.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Flight.getKey(flightNum), af.getRequiredLock()); af.execute(); result = af.success; if (result.result) overseer.addCommandToTransaction(id, af); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// see DbFile.java for javadocs public DbFileIterator iterator(TransactionId tid) { try { return new HeapFileIterator(tid, this); } catch (DbException dbe) { dbe.printStackTrace(); } catch (TransactionAbortedException transe) { transe.printStackTrace(); } catch (NoSuchElementException nosuche) { nosuche.printStackTrace(); } return null; }
/** * Create a new TableStats object, that keeps track of statistics on each column of a table * * @param tableid The table over which to compute statistics * @param ioCostPerPage The cost per page of IO. This doesn't differentiate between * sequential-scan IO and disk seeks. */ public TableStats(int tableid, int ioCostPerPage) { // For this function, we use the DbFile for the table in question, // then scan through its tuples and calculate the values that you // to build the histograms. // TODO: Fill out the rest of the constructor. // Feel free to change anything already written, it's only a guideline this.ioCostPerPage = ioCostPerPage; DbFile file = Database.getCatalog().getDbFile(tableid); tupleDesc = file.getTupleDesc(); numPages = ((HeapFile) file).numPages(); numTuples = 0; int numFields = tupleDesc.numFields(); // TODO: what goes here? statistics = new ArrayList<Object>(); for (int i = 0; i < numFields; i++) { if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) { statistics.add(new IntStatistics(NUM_HIST_BINS)); } else { statistics.add(new StringHistogram(NUM_HIST_BINS)); } } final DbFileIterator iter = file.iterator(null); try { iter.open(); while (iter.hasNext()) { Tuple t = iter.next(); numTuples++; // TODO: and here? for (int i = 0; i < numFields; i++) { if (Type.INT_TYPE.equals(tupleDesc.getFieldType(i))) { ((IntStatistics) statistics.get(i)).addValue(((IntField) t.getField(i)).getValue()); } else { ((StringHistogram) statistics.get(i)) .addValue(((StringField) t.getField(i)).getValue()); } } } iter.close(); } catch (DbException e) { e.printStackTrace(); } catch (TransactionAbortedException e) { e.printStackTrace(); } }
/* reserve an itinerary */ public ReturnTuple<Boolean> itinerary( int id, int customer, Vector flightNumbers, String location, boolean Car, boolean Room, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting itinerary to master"); return this.getMaster() .itinerary(id, customer, flightNumbers, location, Car, Room, timestamp); } else { timestamp.stamp(); ItineraryRMICommand i = new ItineraryRMICommand( carGroup, flightGroup, roomGroup, id, customer, flightNumbers, location, Car, Room); i.setTimestampObject(timestamp); ReturnTuple<Boolean> result = null; try { overseer.validTransaction(id); lm.Lock(id, Customer.getKey(customer), i.getRequiredLock()); lm.Lock(id, ResImpl.Car.getKey(location), i.getRequiredLock()); lm.Lock(id, Hotel.getKey(location), i.getRequiredLock()); for (Object flightNo : flightNumbers) { int flightNum = Integer.parseInt((String) flightNo); lm.Lock(id, Flight.getKey(flightNum), i.getRequiredLock()); } i.execute(); result = i.success; if (result.result && !i.error()) overseer.addCommandToTransaction(id, i); } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }
// return a bill public ReturnTuple<String> queryCustomerInfo(int id, int customerID, Timestamp timestamp) throws RemoteException, TransactionAbortedException, InvalidTransactionException { if (!isMaster) { System.out.println("Slave retransmitting queryCustomerInfo to master"); return this.getMaster().queryCustomerInfo(id, customerID, timestamp); } else { timestamp.stamp(); QueryCustomerInfoRMICommand qci = new QueryCustomerInfoRMICommand(carGroup, flightGroup, roomGroup, id, customerID); qci.setTimestampObject(timestamp); ReturnTuple<String> result = null; try { Vector<Integer> flightNos; Vector<String> locations; overseer.validTransaction(id); lm.Lock(id, Customer.getKey(customerID), qci.getRequiredLock()); qci.execute(); flightNos = qci.getCustomerFlightReservations(); for (int flightNo : flightNos) { lm.Lock(id, Flight.getKey(flightNo), qci.getRequiredLock()); } locations = qci.getCustomerRoomReservations(); for (String location : locations) { lm.Lock(id, Hotel.getKey(location), qci.getRequiredLock()); } locations = qci.getCustomerCarReservations(); for (String location : locations) { lm.Lock(id, Car.getKey(location), qci.getRequiredLock()); } qci.execute(); result = qci.customerInfo; } catch (DeadlockException d) { timestamp.stamp(); overseer.abort(id); timestamp.stamp(); throw new TransactionAbortedException(timestamp); } catch (TransactionAbortedException tae) { tae.t = timestamp; throw tae; } catch (InvalidTransactionException ite) { ite.t = timestamp; throw ite; } result.timestamp.stamp(); return result; } }