2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package com.solidosystems.tuplesoup.core;
8 import TransactionalIO.core.TransactionalFile;
9 import dstm2.AtomicSuperClass;
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;
24 public class TableIndexEntryTransactional implements AtomicSuperClass, Comparable<TableIndexEntryTransactional>{
26 static Factory<TableIndexEntryTSInf> factory = Thread.makeFactory(TableIndexEntryTSInf.class);
28 TableIndexEntryTSInf atomicfields;
29 public @atomic interface TableIndexEntryTSInf{
31 Integer getLocation();
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);
44 public TableIndexEntryTransactional(String id,int rowsize,int location,long position){
45 atomicfields = factory.create();
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);
53 public String getId(){
54 return atomicfields.getId();
56 public void setPosition(long position){
57 this.atomicfields.setPosition(position);
59 public long getPosition(){
60 return atomicfields.getPosition();
62 public void setLocation(int location){
63 this.atomicfields.setLocation(location);
65 public int getLocation(){
66 return atomicfields.getLocation();
69 public int getRowSize(){
70 return atomicfields.getRowsize().intValue();
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;
80 public boolean equals(Object obj){
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())){
90 }catch(ClassCastException e){}
95 if(atomicfields.getSize()<0) calcSize();
96 return atomicfields.getSize().intValue();
98 public void setSize(int size){
99 this.atomicfields.setSize(size);
101 private void calcSize(){
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());
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));
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));
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());
145 protected static TableIndexEntryTransactional readData(TransactionalFile in) throws IOException{
146 long pre=in.getFilePointer();
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());
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));
163 protected static TableIndexEntryTransactional readData(DataInputStream in) throws IOException{
165 int num=in.readShort();
166 StringBuilder buf=new StringBuilder(num);
167 for(int i=0;i<num;i++){
168 buf.append(in.readChar());
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);
178 protected static long scanForOffset(String id,DataInputStream in) throws IOException{
180 int scanhash=id.hashCode();
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();
190 }catch(EOFException e){}
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);
201 StringBuilder buf=new StringBuilder(num);
202 for(int i=0;i<num;i++){
203 buf.append(in.readChar());
205 String readid=buf.toString();
206 if(!readid.equals(id)){
210 int rowsize=in.readInt();
211 int location=in.readByte();
212 long position=in.readLong();
213 TableIndexEntryTransactional tmp=new TableIndexEntryTransactional(id,rowsize,location,position);
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);
224 StringBuilder buf=new StringBuilder(num);
225 for(int i=0;i<num;i++){
226 buf.append(in.readChar());
228 String readid=buf.toString();
229 if(!readid.equals(id)){
233 int rowsize=in.readInt();
234 int location=in.readByte();
235 long position=in.readLong();
236 TableIndexEntryTransactional tmp=new TableIndexEntryTransactional(id,rowsize,location,position);