@@ -43,6 +43,8 @@ this software and associated documentation files (the "Software"), to deal in
4343import java .util .Map ;
4444import java .util .concurrent .atomic .AtomicInteger ;
4545
46+ import static su .interference .persistent .Table .SYSTEM_PKG_PREFIX ;
47+
4648/**
4749 * @author Yuriy Glotanov
4850 * @since 1.0
@@ -64,7 +66,6 @@ public class DataChunk implements Chunk {
6466 private Object entity ;
6567 private Object undoentity ;
6668 private Map <Integer , DataChunk > ics = new HashMap <>();
67- private DataChunk source ;
6869 private UndoChunk uc ;
6970 private FrameData uframe ;
7071 private boolean terminate ;
@@ -199,9 +200,6 @@ public Comparable getId (Field idfield, Session s) throws InvocationTargetExcept
199200 final Id a = f [i ].getAnnotation (Id .class );
200201 if (a != null ) {
201202 if (sa != null ) {
202- //Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
203- //Object v = z.invoke(entity, null);
204- //id = (Comparable) v;
205203 id = (Comparable ) f [i ].get (entity );
206204 } else {
207205 Method z = c .getMethod ("get" + f [i ].getName ().substring (0 , 1 ).toUpperCase () + f [i ].getName ().substring (1 , f [i ].getName ().length ()), new Class <?>[]{Session .class });
@@ -212,9 +210,6 @@ public Comparable getId (Field idfield, Session s) throws InvocationTargetExcept
212210 }
213211 } else {
214212 if (sa != null ) {
215- //Method z = c.getMethod("get"+f[i].getName().substring(0,1).toUpperCase()+f[i].getName().substring(1,f[i].getName().length()), null);
216- //Object v = z.invoke(entity, null);
217- //id = (Comparable) v;
218213 id = (Comparable ) idfield .get (entity );
219214 } else {
220215 Method z = c .getMethod ("get" + idfield .getName ().substring (0 , 1 ).toUpperCase () + idfield .getName ().substring (1 , idfield .getName ().length ()), new Class <?>[]{Session .class });
@@ -357,7 +352,6 @@ public DataChunk (byte[] b, Table t, RowHeader h, DataChunk source) {
357352 this .chunk = b ;
358353 this .header = h ;
359354 this .state = INIT_STATE ;
360- this .source = source ;
361355 this .t = t ;
362356 }
363357
@@ -490,15 +484,13 @@ public Object getEntity () {
490484 if (t .isIndex ()) {
491485 final ResultSetEntity rsa = (ResultSetEntity ) ((Table ) this .t ).getTableClass ().getAnnotation (ResultSetEntity .class );
492486 final DataChunk dc = rsa == null ? (DataChunk ) Instance .getInstance ().getChunkByPointer (this .getHeader ().getFramePtr (), this .getHeader ().getFramePtrRowId ().getRowPointer ()) : this ;
493- dc .setIc (this );
494- ((IndexChunk )o ).setDataChunk (dc );
495487 if (dc == null ) {
496- // todo during rframe.IndexFrame.init system directory not yet contains replicated FrameData objects
497- // final long allocId = Instance.getInstance().getFrameById(this.header.getRowID().getFileId()+this.header.getRowID().getFramePointer()).getAllocId();
498- // final long allocId2 = Instance.getInstance().getFrameById(this.getHeader().getFramePtr()).getAllocId();
499- // logger.error("null datachunk found for indexframe allocId = " + allocId + " indexptr allocId = " + allocId2);
488+ // todo during rframe.IndexFrame.init system directory not yet contains replicated FrameData objects
500489 logger .error ("null datachunk found" );
501490 }
491+ dc .setIc (this );
492+ ((IndexChunk )o ).setFramePtrRowId (this .getHeader ().getFramePtrRowId ());
493+ ((IndexChunk )o ).setDataChunk (dc );
502494 }
503495 if (!t .isNoTran ()) {
504496 ((EntityContainer ) o ).setRowId (header .getRowID ());
@@ -629,19 +621,15 @@ public DataChunk cloneEntity(Session s) throws InternalException {
629621 return dc_ ;
630622 }
631623
632- public DataChunk restore (Frame b , Session s ) throws Exception {
633- if (source == null ) {
634- throw new InternalException ();
635- }
636- if (!(this .getHeader ().getRowID ().getFileId () == source .getHeader ().getRowID ().getFileId () && this .getHeader ().getRowID ().getFramePointer () == source .getHeader ().getRowID ().getFramePointer ())) {
637- final long srcFrameId = source .getHeader ().getRowID ().getFileId () + source .getHeader ().getRowID ().getFramePointer ();
624+ public DataChunk restore (UndoChunk uc ) throws Exception {
625+ // second, since we are using a dirty hack with changing the header in a source chunk for rollback his,
626+ // we need to first delete the source chunk in the target block to prevent inconsistency within ChunkMap / chunk headers
627+ if (!(this .getHeader ().getRowID ().getFileId () == uc .getFile () && this .getHeader ().getRowID ().getFramePointer () == uc .getFrame ())) {
628+ final long srcFrameId = uc .getFile () + uc .getFrame ();
638629 final Frame srcFrame = Instance .getInstance ().getFrameById (srcFrameId ).getFrame ();
639- //todo ???
640- srcFrame .removeChunk (source .getHeader ().getRowID ().getRowPointer (), null , false );
630+ srcFrame .removeChunk (uc .getPtr (), null , true );
641631 }
642- source .setHeader (this .getHeader ());
643- source .updateEntity (this .getEntity ());
644- return source ;
632+ return this ;
645633 }
646634
647635 private byte [] getBytes (Field f , Object o ) throws IllegalAccessException , UnsupportedEncodingException , ClassNotFoundException , InternalException , InstantiationException {
@@ -666,15 +654,11 @@ public void setHeader(Header header) {
666654
667655 //lock mechanism
668656
669- private synchronized DataChunk insertUC (FrameData cb , UndoChunk uc , Session s , LLT llt ) throws Exception {
657+ private synchronized DataChunk insertUC (UndoChunk uc , Session s , LLT llt ) throws Exception {
670658 final WaitFrame ubw = s .getTransaction ().getAvailableFrame (uc , true );
671659 final FrameData ub = ubw .getBd ();
672660 if (ub == null ) {
673- //s.getTransaction().createUndoFrames(s);
674- //ub = s.getTransaction().getAvailableFrame(uc, true);
675- //if (ub == null) {
676- throw new InternalException ();
677- //}
661+ throw new InternalException ();
678662 }
679663
680664 final DataChunk dc = new DataChunk (uc , s );
@@ -685,10 +669,8 @@ private synchronized DataChunk insertUC (FrameData cb, UndoChunk uc, Session s,
685669 s .getTransaction ().setNewLB (ub , nb , false );
686670 nb .getDataFrame ().insertChunk (dc , s , true , llt );
687671 dc .uframe = nb ;
688- //s.getTransaction().storeFrame(cb, nb, 0, s, llt);
689672 } else {
690673 dc .uframe = ub ;
691- //s.getTransaction().storeFrame(cb, ub, 0, s, llt);
692674 }
693675 ubw .release ();
694676 return dc ;
@@ -739,25 +721,11 @@ public synchronized DataChunk lock (Session s, LLT llt) throws Exception {
739721 this .getHeader ().setTran (s .getTransaction ());
740722 ((EntityContainer )o ).setTran (s .getTransaction ());
741723
742- final FrameData bd = Instance .getInstance ().getFrameById (this .getHeader ().getRowID ().getFileId () + this .getHeader ().getRowID ().getFramePointer ());
743724 final DataChunk cc = this .cloneEntity (s );
744725 cc .getHeader ().setTran (s .getTransaction ());
745726 final RowId rw = this .getHeader ().getRowID ();
746727 uc = new UndoChunk (cc , s .getTransaction (), rw .getFileId (), rw .getFramePointer (), rw .getRowPointer ());
747- return insertUC (bd , uc , s , llt );
748- }
749-
750- protected synchronized void undo (Session s , LLT llt ) throws Exception {
751- if (s .getTransaction ().getTransId () != this .getHeader ().getTran ().getTransId ()) {
752- throw new InternalException ();
753- } else {
754- final FrameData bd = Instance .getInstance ().getFrameById (this .getHeader ().getRowID ().getFileId () + this .getHeader ().getRowID ().getFramePointer ());
755- final DataChunk cc = this .cloneEntity (s );
756- cc .getHeader ().setTran (s .getTransaction ());
757- final RowId rw = this .getHeader ().getRowID ();
758- uc = new UndoChunk (cc , s .getTransaction (), rw .getFileId (), rw .getFramePointer (), rw .getRowPointer ());
759- insertUC (bd , uc , s , llt );
760- }
728+ return insertUC (uc , s , llt );
761729 }
762730
763731 public FrameData getUframe () {
@@ -777,8 +745,48 @@ public void setIc(DataChunk ic) {
777745 this .ics .put (t_ .getObjectId (), ic );
778746 }
779747
780- public Map <Integer , DataChunk > getIcs () {
781- return this .ics ;
748+ public void clearIcs () {
749+ this .ics .clear ();
750+ }
751+
752+ protected void cleanUpIcs () {
753+ for (Map .Entry <Integer , DataChunk > entry : this .ics .entrySet ()) {
754+ final IndexChunk ic = (IndexChunk ) entry .getValue ().getEntity ();
755+ ic .setDataChunk (null );
756+ }
757+ }
758+
759+ public DataChunk getIc (IndexDescript ids , Session s ) throws Exception {
760+ final Table ixt = Instance .getInstance ().getTableByName (SYSTEM_PKG_PREFIX + ids .getName ());
761+ final DataChunk ic = this .ics .get (ixt .getObjectId ());
762+ if (ic == null ) {
763+ final ValueSet key = this .getValueByColumnName (ids .getColumns (), s );
764+ final DataChunk ic_ = ixt .getObjectByKey (key , s );
765+ if (ic_ != null ) {
766+ this .ics .put (ixt .getObjectId (), ic_ );
767+ return ic_ ;
768+ } else {
769+ throw new RuntimeException ("Unable to retrieve index chunk: " + ids .getName ());
770+ }
771+ }
772+ return ic ;
773+ }
774+
775+ public ValueSet getValueByColumnName (String [] columns , Session s ) throws Exception {
776+ final Object o = this .getEntity ();
777+ final Class c = o .getClass ();
778+ final SystemEntity sa = (SystemEntity )c .getAnnotation (SystemEntity .class );
779+ final List <Object > res = new ArrayList <>();
780+ for (String name : columns ) {
781+ if (sa != null ) {
782+ final Method z = c .getMethod ("get" + name .substring (0 , 1 ).toUpperCase () + name .substring (1 , name .length ()), null );
783+ res .add (z .invoke (o , null ));
784+ } else {
785+ final Method z = c .getMethod ("get" + name .substring (0 , 1 ).toUpperCase () + name .substring (1 , name .length ()), new Class <?>[]{Session .class });
786+ res .add (z .invoke (o , new Object []{s }));
787+ }
788+ }
789+ return new ValueSet (res .toArray (new Object []{}));
782790 }
783791
784792 private byte [] getBytesFromHexString (String s ) {
0 commit comments