*** empty log message ***
[IRC.git] / Robust / Transactions / dstm2src / benchmark / FinancialTransaction.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package dstm2.benchmark;
7
8 import TransactionalIO.benchmarks.benchmark;
9 import TransactionalIO.core.Defaults;
10 import TransactionalIO.core.TransactionalFile;
11 import dstm2.AtomicArray;
12 import dstm2.atomic;
13 import dstm2.Thread;
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;
20
21
22 /**
23  *
24  * @author navid
25  */
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);
30      
31      LockedFTrHolder[] hlm;
32      
33      
34      
35      
36  /*    String buyer1 = new String();
37      int soldshare1 = 0;
38      String seller1 = new String();
39      
40      String buyer2 = new String();
41      int soldshare2 = 0;
42      String seller2 = new String();
43      
44      String buyer3 = new String();
45      int soldshare3 = 0;
46      String seller3 = new String();
47      
48      String buyer4 = new String();
49      int soldshare4 = 0;
50      String seller4 = new String();
51      
52      String buyer5 = new String();
53      int soldshare5 = 0;
54      String seller5 = new String();
55      
56      int lockedcounter = 1;*/
57      AtomicArray<FTrHolder> financialTransactionKeeper;
58
59     protected void init() {
60       /*  hlm = new LockedFTrHolder[20];
61         for (int i=0; i<20; i++){
62             hlm[i] = new LockedFTrHolder();
63             hlm[i].counter =1;
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;
70             }
71                 
72         }*/
73         
74         
75         
76         
77         RootHolder ck =  factory2.create();
78         ck.setFinancialTransactionKeeper(new AtomicArray<FTrHolder>(FTrHolder.class, 20));
79         
80
81         financialTransactionKeeper = ck.getFinancialTransactionKeeper();
82         for (int i=0; i<20; i++){ 
83            
84             FTrHolder f1 = factory3.create();
85             f1.setCounter(1);
86             f1.setFinancialTransactionKeeper(new AtomicArray<FinancialTransactionDS>(FinancialTransactionDS.class, 5));
87             for (int j=0; j<5; j++)
88             {
89                 FinancialTransactionDS ftk = factory.create();
90                 ftk.setBuyer("");
91                 ftk.setSeller("");
92                 ftk.setSoldShare(0);
93                 AtomicArray<FinancialTransactionDS> tmp = f1.getFinancialTransactionKeeper();
94                 tmp.set(j, ftk);
95             }
96           
97             
98             financialTransactionKeeper.set(i, f1);
99        }
100  
101     }
102
103
104     protected void execute(Vector arguments) {
105         try {
106
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);
115             
116       
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();
121             
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();
126             
127             
128             file.seek(offset1towrite);
129             file.write(st.getBytes());
130             file.seek(offset2towrite);
131             file.write(st2.getBytes());
132             
133         //    RandomAccessFile file2 = (RandomAccessFile) benchmark.m.get("8");
134            TransactionalFile file2 = (TransactionalFile) benchmark.m.get("6");
135             
136             String towrite = oldowner + " " + stocktrade.toString() + " " + newowner + " processed\n";
137             file2.write(towrite.getBytes());
138             /*switch(lockedcounter){
139                 case 1:
140                       seller1 = oldowner;
141                       soldshare1 = stocktrade.intValue();
142                       buyer1 = newowner;
143                       lockedcounter = 2;
144                       break;
145                 case 2:
146                       seller2 = oldowner;
147                       soldshare2 = stocktrade.intValue();
148                       buyer2 = newowner;
149                       lockedcounter = 3;
150                       break;
151                 case 3:
152                       seller3 = oldowner;
153                       soldshare3 = stocktrade.intValue();
154                       buyer3 = newowner;
155                       lockedcounter = 4;
156                       break;
157                 case 4:
158                       seller4 = oldowner;
159                       soldshare4 = stocktrade.intValue();
160                       buyer4 = newowner;
161                       lockedcounter = 5;
162                       break;
163                 case 5:    
164                       seller5 = oldowner;
165                       soldshare5 = stocktrade.intValue();
166                       buyer5 = newowner;
167                       lockedcounter = 1;
168                       break;
169             }*/
170                int i; 
171                for (i=0;i<benchmark.stocks.length; i++){
172                    if (benchmark.stocks[i].equalsIgnoreCase(nameofstock))
173                        break;
174                }
175          
176              /*  switch(financialTransactionKeeper.get(i).getCounter()){
177                 case 1:
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);
182                       break;
183                 case 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);
188                       break;
189                 case 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);
194                       break;
195                 case 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);
200                       break;
201                 case 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);
206                       break;
207
208             }*/
209                
210             /* switch(hlm[i].counter){
211                 case 1:
212                       hlm[i].lk[0].seller = oldowner;
213                       hlm[i].lk[0].soldshare = stocktrade.intValue();
214                       hlm[i].lk[0].buyer = newowner;
215                       hlm[i].counter = 2;
216                       break;
217                 case 2:
218                       hlm[i].lk[1].seller = oldowner;
219                       hlm[i].lk[1].soldshare = stocktrade.intValue();
220                       hlm[i].lk[1].buyer = newowner;
221                       hlm[i].counter = 3;
222                       break;
223                 case 3:
224                       hlm[i].lk[2].seller = oldowner;
225                       hlm[i].lk[2].soldshare = stocktrade.intValue();
226                       hlm[i].lk[2].buyer = newowner;
227                       hlm[i].counter = 4;
228                       break;
229                 case 4:
230                       hlm[i].lk[3].seller = oldowner;
231                       hlm[i].lk[3].soldshare = stocktrade.intValue();
232                       hlm[i].lk[3].buyer = newowner;
233                       hlm[i].counter = 5;
234                       break;
235                 case 5:    
236                       hlm[i].lk[4].seller = oldowner;
237                       hlm[i].lk[4].soldshare = stocktrade.intValue();
238                       hlm[i].lk[4].buyer = newowner;
239                       hlm[i].counter = 1;
240                       break;
241             }*/
242             
243         } catch (IOException ex) {
244             Logger.getLogger(FinancialTransaction.class.getName()).log(Level.SEVERE, null, ex);
245         }
246         }
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());
252             e.printStackTrace();
253         }*/
254        /* int oldbalance = getOldBalance();
255        int newnumber =  oldbalance - stocktrade.intValue();
256        updateFile(newnumber);
257    //     System.out.println("offset: " + offset1 + " for: " + oldowner + " old balance: "+ oldbalance);
258         
259         if (oldowner.equals("Smith")){
260             System.out.println("offset: " + offset1 + " for: " + oldowner + " old balance: "+ oldbalance);
261             System.out.println("trade money: " + stocktrade);
262         }
263        // System.out.println("old number: " + oldbalance);
264         
265         
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);
272         }*/
273         
274           
275             
276       //  }
277
278    // }
279
280     private Vector computeandupdate(boolean type, Integer stocktrade, String origstockname ){
281       // try{ 
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];
287          
288         // int counter =0;
289          boolean flag = false;
290          data[0] = 'a';
291          int counter = 0;
292          while (data[0] != '\n') {
293                 int res;
294                 res = file.read(data);
295                 if (res == -1) {
296                     flag = true;
297                     break;
298                 }
299         }
300
301         while(true){ 
302             char[] stname = new char[10];
303             data[0] = 'a'; 
304             int ol=0;
305             while (data[0] != ' ') {
306                 int res;
307                 res = file.read(data);
308                 if (res == -1) {
309                     flag = true;
310                     break;
311                 }
312                 stname[ol] = (char)data[0];
313                 ol++;
314             }
315             String stockname = String.copyValueOf(stname, 0, ol-1);
316             if (stockname.equalsIgnoreCase(origstockname))   { 
317                 break;
318             }
319             else while (data[0] != '\n')
320                 file.read(data);
321         }
322         
323          
324          
325          
326         data[0] = 'a';    
327         while ((char) data[0] != ':') {
328             int res;
329             res = file.read(data);
330             if (res == -1) {
331                 flag = true;
332                 break;
333             }
334         }
335         int res = file.read(data);
336         long offsetofnumber = file.getFilePointer();
337         do {
338             res = file.read(data);
339             if (res == -1) {
340                 flag = true;
341                 break;
342             }
343             balance[counter] = (char) data[0];
344             counter++;
345         } while (Character.isDigit((char) data[0]) || (char)data[0] == '-');
346         
347         int oldbalance = Integer.parseInt(String.valueOf(balance, 0, counter - 1));
348             
349         //    return oldnumber;
350             
351
352          int newnumber;
353           if (type){
354              newnumber = oldbalance - stocktrade.intValue();
355           }
356           else 
357               newnumber = oldbalance + stocktrade.intValue();
358
359             
360          //////   file.seek(offsetofnumber);
361             
362             
363             String st = new String();
364             st = String.valueOf(newnumber);
365             if (String.valueOf(newnumber).length() < counter - 1){
366              
367                 for (int i=0; i<counter-String.valueOf(newnumber).length(); i++){
368                     st += (new String(" "));
369                     //file.write((new String(" ")).getBytes());
370                 }
371             }
372        //     st += new String("\n");
373             //file.write((new String("\n")).getBytes());
374             v.add(Long.valueOf(offsetofnumber));
375             v.add(st);
376             return v;
377 /*            } catch (IOException ex) {
378                
379                 Logger.getLogger(FinancialTransaction.class.getName()).log(Level.SEVERE, null, ex);
380                    return null;
381             }*/
382     }
383
384     protected  void printResults() {
385       
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++)
390         {
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());
394         }
395         System.out.println("----------------------------------------------");
396       }
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++)
401             {
402                  
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);
406             }
407             System.out.println("----------------------------------------------");
408         }*/
409         
410       /*  System.out.print(finance1.getSeller() + " ");
411         System.out.print(finance1.getBuyer() + " ");
412         System.out.println(finance1.getSoldShare());
413         
414         System.out.print(finance2.getSeller() + " ");
415         System.out.print(finance2.getBuyer()+ " ");
416         System.out.println(finance2.getSoldShare());
417         
418         System.out.print(finance3.getSeller() + " ");
419         System.out.print(finance3.getBuyer()+ " ");
420         System.out.println(finance3.getSoldShare());
421         
422         System.out.print(finance4.getSeller() + " ");
423         System.out.print(finance4.getBuyer()+ " ");
424         System.out.println(finance4.getSoldShare());
425         
426         System.out.print(finance5.getSeller() + " ");
427         System.out.print(finance5.getBuyer()+ " ");
428         System.out.println(finance5.getSoldShare());*/
429       
430         /*System.out.print(buyer1 + " ");
431         System.out.print(soldshare1 + " ");
432         System.out.println(seller1);
433         
434         System.out.print(buyer2 + " ");
435         System.out.print(soldshare2 + " ");
436         System.out.println(seller2);
437         
438         System.out.print(buyer3 + " ");
439         System.out.print(soldshare3 + " ");
440         System.out.println(seller3);
441         
442         System.out.print(buyer4 + " ");
443         System.out.print(soldshare4 + " ");
444         System.out.println(seller4);
445         
446         System.out.print(buyer5 + " ");
447         System.out.print(soldshare5 + " ");
448         System.out.println(seller5);*/
449         
450         //System.out.println("----------------------------------------------");
451     }
452     
453
454     
455     
456       @atomic public interface FinancialTransactionDS{
457         String getSeller();
458         void setSeller(String value);
459         int getSoldShare();
460         void setSoldShare(int value);
461         String getBuyer();
462         void setBuyer(String value);  
463       }
464       
465       @atomic public interface FTrHolder{
466           AtomicArray<FinancialTransactionDS> getFinancialTransactionKeeper();
467           void setFinancialTransactionKeeper(AtomicArray<FinancialTransactionDS> arr);
468           int getCounter();
469           void setCounter(int value);
470       }
471       
472       @atomic public interface RootHolder{
473           AtomicArray<FTrHolder> getFinancialTransactionKeeper();
474           void setFinancialTransactionKeeper(AtomicArray<FTrHolder> arr);
475         //  int getCounter();
476         //  void setCounter(int value);
477       }
478       
479       class LockedFinancialTransactionDS{
480           public String seller;
481           public String buyer;
482           public int soldshare;          
483       }
484       
485       class LockedFTrHolder{
486           public LockedFinancialTransactionDS[] lk = new LockedFinancialTransactionDS[5];
487           public int counter;
488       }
489       
490       
491       
492       
493       /*    private int getOldBalance(){
494          
495          byte[] data = new byte[1];
496          char[] balance = new char[20];
497          
498         // int counter =0;
499          boolean flag = false;
500          data[0] = 'a';
501          counter = 0;
502          while (data[0] != '\n') {
503                 int res;
504                 res = file.read(data);
505                 if (res == -1) {
506                     flag = true;
507                     break;
508                 }
509             }
510         while ((char) data[0] != ':') {
511             int res;
512             res = file.read(data);
513             if (res == -1) {
514                 flag = true;
515                 break;
516             }
517         }
518         int res = file.read(data);
519         offsetofnumber = file.getFilePointer();
520         do {
521             res = file.read(data);
522             if (res == -1) {
523                 flag = true;
524                 break;
525             }
526             balance[counter] = (char) data[0];
527             counter++;
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);
532             return oldnumber;
533             
534
535     }
536     
537     private void updateFile(int newnumber){
538         try {
539             
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){
544              
545                 for (int i=0; i<counter-String.valueOf(newnumber).length(); i++){
546                   
547                     file.write((new String(" ")).getBytes());
548                 }
549             }
550             file.write((new String("\n")).getBytes());
551    
552             } catch (IOException ex) {
553                 Logger.getLogger(FinancialTransaction.class.getName()).log(Level.SEVERE, null, ex);
554             }
555     }*/
556
557 }