2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package dstm2.benchmark;
8 import TransactionalIO.benchmarks.benchmark;
9 import TransactionalIO.core.Defaults;
10 import TransactionalIO.core.TransactionalFile;
11 import dstm2.AtomicArray;
14 import dstm2.factory.Factory;
15 import java.io.IOException;
16 import java.io.RandomAccessFile;
17 import java.util.Vector;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
26 public class FinancialTransaction extends CustomBenchmark{
27 private static Factory<FinancialTransactionDS> factory = Thread.makeFactory(FinancialTransactionDS.class);
28 private static Factory<RootHolder> factory2 = Thread.makeFactory(RootHolder.class);
29 private static Factory<FTrHolder> factory3 = Thread.makeFactory(FTrHolder.class);
31 LockedFTrHolder[] hlm;
36 /* String buyer1 = new String();
38 String seller1 = new String();
40 String buyer2 = new String();
42 String seller2 = new String();
44 String buyer3 = new String();
46 String seller3 = new String();
48 String buyer4 = new String();
50 String seller4 = new String();
52 String buyer5 = new String();
54 String seller5 = new String();
56 int lockedcounter = 1;*/
57 AtomicArray<FTrHolder> financialTransactionKeeper;
59 protected void init() {
60 /* hlm = new LockedFTrHolder[20];
61 for (int i=0; i<20; i++){
62 hlm[i] = new LockedFTrHolder();
64 hlm[i].lk = new LockedFinancialTransactionDS[5];
65 for (int j=0; j<5; j++){
66 hlm[i].lk[j] = new LockedFinancialTransactionDS();
67 hlm[i].lk[j].buyer = "";
68 hlm[i].lk[j].seller = "";
69 hlm[i].lk[j].soldshare = 0;
77 RootHolder ck = factory2.create();
78 ck.setFinancialTransactionKeeper(new AtomicArray<FTrHolder>(FTrHolder.class, 20));
81 financialTransactionKeeper = ck.getFinancialTransactionKeeper();
82 for (int i=0; i<20; i++){
84 FTrHolder f1 = factory3.create();
86 f1.setFinancialTransactionKeeper(new AtomicArray<FinancialTransactionDS>(FinancialTransactionDS.class, 5));
87 for (int j=0; j<5; j++)
89 FinancialTransactionDS ftk = factory.create();
93 AtomicArray<FinancialTransactionDS> tmp = f1.getFinancialTransactionKeeper();
98 financialTransactionKeeper.set(i, f1);
104 protected void execute(Vector arguments) {
107 TransactionalFile file = (TransactionalFile) benchmark.m.get("5");
108 // RandomAccessFile file = (RandomAccessFile) benchmark.m.get("7");
109 String oldowner = (String) arguments.get(0);
110 Integer stocktrade = (Integer) arguments.get(1);
111 String newowner = (String) arguments.get(2);
112 String nameofstock = (String) arguments.get(3);
113 Integer offset1 = (Integer) benchmark.m4.get(oldowner);
114 Integer offset2 = (Integer) benchmark.m4.get(newowner);
117 file.seek(offset1 * Defaults.FILEFRAGMENTSIZE);
118 Vector v = computeandupdate(true, stocktrade, nameofstock);
119 String st = (String)(v.get(1));
120 long offset1towrite = ((Long)(v.get(0))).longValue();
122 file.seek(offset2 * Defaults.FILEFRAGMENTSIZE);
123 v = computeandupdate(false, stocktrade, nameofstock);
124 String st2 = (String)(v.get(1));
125 long offset2towrite = ((Long)(v.get(0))).longValue();
128 file.seek(offset1towrite);
129 file.write(st.getBytes());
130 file.seek(offset2towrite);
131 file.write(st2.getBytes());
133 // RandomAccessFile file2 = (RandomAccessFile) benchmark.m.get("8");
134 TransactionalFile file2 = (TransactionalFile) benchmark.m.get("6");
136 String towrite = oldowner + " " + stocktrade.toString() + " " + newowner + " processed\n";
137 file2.write(towrite.getBytes());
138 /*switch(lockedcounter){
141 soldshare1 = stocktrade.intValue();
147 soldshare2 = stocktrade.intValue();
153 soldshare3 = stocktrade.intValue();
159 soldshare4 = stocktrade.intValue();
165 soldshare5 = stocktrade.intValue();
171 for (i=0;i<benchmark.stocks.length; i++){
172 if (benchmark.stocks[i].equalsIgnoreCase(nameofstock))
176 /* switch(financialTransactionKeeper.get(i).getCounter()){
178 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(0).setSeller(oldowner);
179 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(0).setSoldShare(stocktrade.intValue());
180 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(0).setBuyer(newowner);
181 financialTransactionKeeper.get(i).setCounter(2);
184 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(1).setSeller(oldowner);
185 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(1).setSoldShare(stocktrade.intValue());
186 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(1).setBuyer(newowner);
187 financialTransactionKeeper.get(i).setCounter(3);
190 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(2).setSeller(oldowner);
191 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(2).setSoldShare(stocktrade.intValue());
192 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(2).setBuyer(newowner);
193 financialTransactionKeeper.get(i).setCounter(4);
196 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(3).setSeller(oldowner);
197 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(3).setSoldShare(stocktrade.intValue());
198 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(3).setBuyer(newowner);
199 financialTransactionKeeper.get(i).setCounter(5);
202 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(4).setSeller(oldowner);
203 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(4).setSoldShare(stocktrade.intValue());
204 financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(4).setBuyer(newowner);
205 financialTransactionKeeper.get(i).setCounter(1);
210 /* switch(hlm[i].counter){
212 hlm[i].lk[0].seller = oldowner;
213 hlm[i].lk[0].soldshare = stocktrade.intValue();
214 hlm[i].lk[0].buyer = newowner;
218 hlm[i].lk[1].seller = oldowner;
219 hlm[i].lk[1].soldshare = stocktrade.intValue();
220 hlm[i].lk[1].buyer = newowner;
224 hlm[i].lk[2].seller = oldowner;
225 hlm[i].lk[2].soldshare = stocktrade.intValue();
226 hlm[i].lk[2].buyer = newowner;
230 hlm[i].lk[3].seller = oldowner;
231 hlm[i].lk[3].soldshare = stocktrade.intValue();
232 hlm[i].lk[3].buyer = newowner;
236 hlm[i].lk[4].seller = oldowner;
237 hlm[i].lk[4].soldshare = stocktrade.intValue();
238 hlm[i].lk[4].buyer = newowner;
243 } catch (IOException ex) {
244 Logger.getLogger(FinancialTransaction.class.getName()).log(Level.SEVERE, null, ex);
247 /*catch (NullPointerException e){
248 System.out.println("file?? " + file);
249 System.out.println("offset?? " + offset1);
250 System.out.println(oldowner);
251 System.out.println(Thread.currentThread());
254 /* int oldbalance = getOldBalance();
255 int newnumber = oldbalance - stocktrade.intValue();
256 updateFile(newnumber);
257 // System.out.println("offset: " + offset1 + " for: " + oldowner + " old balance: "+ oldbalance);
259 if (oldowner.equals("Smith")){
260 System.out.println("offset: " + offset1 + " for: " + oldowner + " old balance: "+ oldbalance);
261 System.out.println("trade money: " + stocktrade);
263 // System.out.println("old number: " + oldbalance);
266 oldbalance = getOldBalance();
267 newnumber = oldbalance + stocktrade.intValue();
268 updateFile(newnumber);*/
269 /* if (newowner.equals("Smith")){
270 System.out.println("offset: " + offset2 + " for: " + newowner + " old balance: "+ oldbalance);
271 System.out.println("trade money: " + stocktrade);
280 private Vector computeandupdate(boolean type, Integer stocktrade, String origstockname ){
282 // RandomAccessFile file = (RandomAccessFile) benchmark.m.get("7");
283 TransactionalFile file = (TransactionalFile)benchmark.m.get("5");
284 Vector v = new Vector();
285 byte[] data = new byte[1];
286 char[] balance = new char[20];
289 boolean flag = false;
292 while (data[0] != '\n') {
294 res = file.read(data);
302 char[] stname = new char[10];
305 while (data[0] != ' ') {
307 res = file.read(data);
312 stname[ol] = (char)data[0];
315 String stockname = String.copyValueOf(stname, 0, ol-1);
316 if (stockname.equalsIgnoreCase(origstockname)) {
319 else while (data[0] != '\n')
327 while ((char) data[0] != ':') {
329 res = file.read(data);
335 int res = file.read(data);
336 long offsetofnumber = file.getFilePointer();
338 res = file.read(data);
343 balance[counter] = (char) data[0];
345 } while (Character.isDigit((char) data[0]) || (char)data[0] == '-');
347 int oldbalance = Integer.parseInt(String.valueOf(balance, 0, counter - 1));
354 newnumber = oldbalance - stocktrade.intValue();
357 newnumber = oldbalance + stocktrade.intValue();
360 ////// file.seek(offsetofnumber);
363 String st = new String();
364 st = String.valueOf(newnumber);
365 if (String.valueOf(newnumber).length() < counter - 1){
367 for (int i=0; i<counter-String.valueOf(newnumber).length(); i++){
368 st += (new String(" "));
369 //file.write((new String(" ")).getBytes());
372 // st += new String("\n");
373 //file.write((new String("\n")).getBytes());
374 v.add(Long.valueOf(offsetofnumber));
377 /* } catch (IOException ex) {
379 Logger.getLogger(FinancialTransaction.class.getName()).log(Level.SEVERE, null, ex);
384 protected void printResults() {
386 for (int i=0; i<20; i++){
387 System.out.println("----------------------------------------------");
388 System.out.println(benchmark.stocks[i]);
389 for (int j=0; j<5; j++)
391 System.out.print(financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(j).getSeller() + " ");
392 System.out.print(financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(j).getBuyer() + " ");
393 System.out.println(financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(j).getSoldShare());
395 System.out.println("----------------------------------------------");
397 /* for (int i=0; i<20; i++){
398 System.out.println("----------------------------------------------");
399 System.out.println(benchmark.stocks[i]);
400 for (int j=0; j<5; j++)
403 System.out.print(hlm[i].lk[j].seller + " ");
404 System.out.print(hlm[i].lk[j].buyer + " ");
405 System.out.println(hlm[i].lk[j].soldshare);
407 System.out.println("----------------------------------------------");
410 /* System.out.print(finance1.getSeller() + " ");
411 System.out.print(finance1.getBuyer() + " ");
412 System.out.println(finance1.getSoldShare());
414 System.out.print(finance2.getSeller() + " ");
415 System.out.print(finance2.getBuyer()+ " ");
416 System.out.println(finance2.getSoldShare());
418 System.out.print(finance3.getSeller() + " ");
419 System.out.print(finance3.getBuyer()+ " ");
420 System.out.println(finance3.getSoldShare());
422 System.out.print(finance4.getSeller() + " ");
423 System.out.print(finance4.getBuyer()+ " ");
424 System.out.println(finance4.getSoldShare());
426 System.out.print(finance5.getSeller() + " ");
427 System.out.print(finance5.getBuyer()+ " ");
428 System.out.println(finance5.getSoldShare());*/
430 /*System.out.print(buyer1 + " ");
431 System.out.print(soldshare1 + " ");
432 System.out.println(seller1);
434 System.out.print(buyer2 + " ");
435 System.out.print(soldshare2 + " ");
436 System.out.println(seller2);
438 System.out.print(buyer3 + " ");
439 System.out.print(soldshare3 + " ");
440 System.out.println(seller3);
442 System.out.print(buyer4 + " ");
443 System.out.print(soldshare4 + " ");
444 System.out.println(seller4);
446 System.out.print(buyer5 + " ");
447 System.out.print(soldshare5 + " ");
448 System.out.println(seller5);*/
450 //System.out.println("----------------------------------------------");
456 @atomic public interface FinancialTransactionDS{
458 void setSeller(String value);
460 void setSoldShare(int value);
462 void setBuyer(String value);
465 @atomic public interface FTrHolder{
466 AtomicArray<FinancialTransactionDS> getFinancialTransactionKeeper();
467 void setFinancialTransactionKeeper(AtomicArray<FinancialTransactionDS> arr);
469 void setCounter(int value);
472 @atomic public interface RootHolder{
473 AtomicArray<FTrHolder> getFinancialTransactionKeeper();
474 void setFinancialTransactionKeeper(AtomicArray<FTrHolder> arr);
476 // void setCounter(int value);
479 class LockedFinancialTransactionDS{
480 public String seller;
482 public int soldshare;
485 class LockedFTrHolder{
486 public LockedFinancialTransactionDS[] lk = new LockedFinancialTransactionDS[5];
493 /* private int getOldBalance(){
495 byte[] data = new byte[1];
496 char[] balance = new char[20];
499 boolean flag = false;
502 while (data[0] != '\n') {
504 res = file.read(data);
510 while ((char) data[0] != ':') {
512 res = file.read(data);
518 int res = file.read(data);
519 offsetofnumber = file.getFilePointer();
521 res = file.read(data);
526 balance[counter] = (char) data[0];
528 } while (Character.isDigit((char) data[0]) || (char)data[0] == '-');
529 // System.out.println((char)data[0]);
530 int oldnumber = Integer.parseInt(String.valueOf(balance, 0, counter - 1));
531 // System.out.println(oldnumber);
537 private void updateFile(int newnumber){
540 file.seek(offsetofnumber);
541 // System.out.println(String.valueOf(newnumber));
542 file.write(String.valueOf(newnumber).getBytes());
543 if (String.valueOf(newnumber).length() < counter - 1){
545 for (int i=0; i<counter-String.valueOf(newnumber).length(); i++){
547 file.write((new String(" ")).getBytes());
550 file.write((new String("\n")).getBytes());
552 } catch (IOException ex) {
553 Logger.getLogger(FinancialTransaction.class.getName()).log(Level.SEVERE, null, ex);