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