eaca765b2d4631f68792960dafccb00f696a4d74
[IRC.git] /
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5
6 package com.solidosystems.tuplesoup.core;
7
8 import TransactionalIO.core.TransactionalFile;
9 import dstm2.AtomicSuperClass;
10 import dstm2.atomic;
11 import dstm2.Thread;
12 import dstm2.factory.Factory;
13 import java.io.ByteArrayOutputStream;
14 import java.io.DataInputStream;
15 import java.io.DataOutputStream;
16 import java.io.EOFException;
17 import java.io.IOException;
18 import java.io.RandomAccessFile;
19
20 /**
21  *
22  * @author navid
23  */
24 public class TableIndexEntryTransactional implements AtomicSuperClass, Comparable<TableIndexEntryTransactional>{
25      
26     static Factory<TableIndexEntryTSInf> factory = Thread.makeFactory(TableIndexEntryTSInf.class);
27     
28     TableIndexEntryTSInf atomicfields;
29     public @atomic interface TableIndexEntryTSInf{
30         String getId();
31         Integer getLocation();
32         Long getPosition();
33         Integer getSize();
34         Integer getRowsize();
35         
36         void setId(String val);
37         void setLocation(Integer val);
38         void setPosition(Long val);
39         void setSize(Integer val);
40         void setRowsize(Integer val);
41     }
42        
43          
44     public TableIndexEntryTransactional(String id,int rowsize,int location,long position){
45         atomicfields = factory.create();
46         
47         this.atomicfields.setId(id);
48         this.atomicfields.setLocation(location);
49         this.atomicfields.setPosition(position);
50         this.atomicfields.setRowsize(rowsize);
51         this.atomicfields.setSize(-1);
52     }
53     public String getId(){
54         return atomicfields.getId();
55     }
56     public void setPosition(long position){
57         this.atomicfields.setPosition(position);
58     }
59     public long getPosition(){
60         return atomicfields.getPosition();
61     }
62     public void setLocation(int location){
63         this.atomicfields.setLocation(location);
64     }
65     public int getLocation(){
66         return atomicfields.getLocation();
67     }
68     
69     public int getRowSize(){
70         return atomicfields.getRowsize().intValue();
71     }
72     
73     public int compareTo(TableIndexEntryTransactional obj) throws ClassCastException{
74         TableIndexEntryTransactional ent=(TableIndexEntryTransactional)obj;
75         if(atomicfields.getPosition()<ent.atomicfields.getPosition()) return -1;
76         if(atomicfields.getPosition()==ent.atomicfields.getPosition()) return 0;
77         return 1;
78     }
79     
80     public boolean equals(Object obj){
81         try{
82             TableIndexEntryTransactional ent=(TableIndexEntryTransactional)obj;
83             if(ent.atomicfields.getLocation()==atomicfields.getLocation()){
84                 if(ent.atomicfields.getPosition()==atomicfields.getPosition()){
85                     if(ent.atomicfields.getId().equals(atomicfields.getId())){
86                         return true;
87                     }
88                 }
89             }
90         }catch(ClassCastException e){}
91         return false;
92     }
93     
94     public int getSize(){
95         if(atomicfields.getSize()<0) calcSize();
96         return atomicfields.getSize().intValue();
97     }
98     public void setSize(int size){
99         this.atomicfields.setSize(size);
100     }
101     private void calcSize(){
102         try{
103             ByteArrayOutputStream bout=new ByteArrayOutputStream();
104             DataOutputStream dout=new DataOutputStream(bout);
105             dout.writeInt(atomicfields.getId().hashCode());
106             dout.writeShort(atomicfields.getId().length());
107             dout.writeChars(atomicfields.getId());
108             dout.writeInt(atomicfields.getRowsize().intValue());
109             dout.writeByte(atomicfields.getLocation());
110             dout.writeLong(atomicfields.getPosition());
111             setSize(bout.size());
112             dout.close();
113             bout.close();
114         }catch(Exception e){
115             e.printStackTrace();
116         }
117     }
118     
119     protected void writeData(TransactionalFile out) throws IOException{
120         long pre=out.getFilePointer();
121         out.writeInt(atomicfields.getId().hashCode());
122         out.writeShort(atomicfields.getId().length());
123         out.writeChars(atomicfields.getId());
124         out.writeInt(atomicfields.getRowsize().intValue());
125         out.writeByte(atomicfields.getLocation());
126         out.writeLong(atomicfields.getPosition());
127         setSize((int)(out.getFilePointer()-pre));
128     }
129     protected void updateData(TransactionalFile out) throws IOException{
130         long pre=out.getFilePointer();
131         out.skipBytes(4+2+atomicfields.getId().length()*2);
132         out.writeInt(atomicfields.getRowsize().intValue());
133         out.writeByte(atomicfields.getLocation());
134         out.writeLong(atomicfields.getPosition());
135         setSize((int)(out.getFilePointer()-pre));
136     }
137     protected void writeData(DataOutputStream out) throws IOException{
138         out.writeInt(atomicfields.getId().hashCode());
139         out.writeShort(atomicfields.getId().length());
140         out.writeChars(atomicfields.getId());
141         out.writeInt(atomicfields.getRowsize().intValue());
142         out.writeByte(atomicfields.getLocation());
143         out.writeLong(atomicfields.getPosition());
144     }
145     protected static TableIndexEntryTransactional readData(TransactionalFile in) throws IOException{
146         long pre=in.getFilePointer();
147         in.readInt();
148         int num=in.readShort();
149         //System.out.println("num= " + num);
150         StringBuilder buf=new StringBuilder(num);
151         for(int i=0;i<num;i++){
152             buf.append(in.readChar());
153         }
154         String id=buf.toString();
155         int rowsize=in.readInt();
156         int location=in.readByte();
157         long position=in.readLong();
158         TableIndexEntryTransactional tmp=new TableIndexEntryTransactional(id,rowsize,location,position);
159         tmp.setSize((int)(in.getFilePointer()-pre));
160         return tmp;
161     }
162     
163     protected static TableIndexEntryTransactional readData(DataInputStream in) throws IOException{
164         in.readInt();
165         int num=in.readShort();
166         StringBuilder buf=new StringBuilder(num);
167         for(int i=0;i<num;i++){
168             buf.append(in.readChar());
169         }
170         String id=buf.toString();
171         int rowsize=in.readInt();
172         int location=in.readByte();
173         long position=in.readLong();
174         TableIndexEntryTransactional tmp=new TableIndexEntryTransactional(id,rowsize,location,position);
175         return tmp;
176     }
177     
178     protected static long scanForOffset(String id,DataInputStream in) throws IOException{
179         long offset=0;
180         int scanhash=id.hashCode();
181         try{
182             int datahash=in.readInt();
183             while(scanhash!=datahash){
184                 int num=in.readShort();
185                 in.skipBytes(1+4+8+num*2);
186                 offset+=4+4+1+2+8+num*2;
187                 datahash=in.readInt();
188             }
189             return offset;
190         }catch(EOFException e){}
191         return -1;
192     }
193     protected static TableIndexEntryTransactional lookForData(String id,DataInputStream in) throws IOException{
194         int scanhash=id.hashCode();
195         int datahash=in.readInt();
196         int num=in.readShort();
197         if(scanhash!=datahash){
198             in.skipBytes(4+1+8+num*2);
199             return null;
200         }
201         StringBuilder buf=new StringBuilder(num);
202         for(int i=0;i<num;i++){
203             buf.append(in.readChar());
204         }
205         String readid=buf.toString();
206         if(!readid.equals(id)){
207             in.skipBytes(4+1+8);
208             return null;
209         }
210         int rowsize=in.readInt();
211         int location=in.readByte();
212         long position=in.readLong();
213         TableIndexEntryTransactional tmp=new TableIndexEntryTransactional(id,rowsize,location,position);
214         return tmp;
215     }
216     protected static TableIndexEntryTransactional lookForData(String id,TransactionalFile in) throws IOException{
217         int scanhash=id.hashCode();
218         int datahash=in.readInt();
219         int num=in.readShort();
220         if(scanhash!=datahash){
221             in.skipBytes(4+1+8+num*2);
222             return null;
223         }
224         StringBuilder buf=new StringBuilder(num);
225         for(int i=0;i<num;i++){
226             buf.append(in.readChar());
227         }
228         String readid=buf.toString();
229         if(!readid.equals(id)){
230             in.skipBytes(4+1+8);
231             return null;
232         }
233         int rowsize=in.readInt();
234         int location=in.readByte();
235         long position=in.readLong();
236         TableIndexEntryTransactional tmp=new TableIndexEntryTransactional(id,rowsize,location,position);
237         return tmp;
238     }
239 }