--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.impl.store.access.heap.HeapRowLocation\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.impl.store.access.heap;\r
+\r
+import org.apache.derby.iapi.error.StandardException;\r
+\r
+import org.apache.derby.iapi.services.cache.ClassSize;\r
+\r
+import org.apache.derby.iapi.services.io.ArrayInputStream;\r
+import org.apache.derby.iapi.services.io.CompressedNumber;\r
+import org.apache.derby.iapi.services.io.StoredFormatIds;\r
+\r
+import org.apache.derby.iapi.services.sanity.SanityManager;\r
+\r
+import org.apache.derby.iapi.store.raw.RecordHandle;\r
+import org.apache.derby.iapi.store.raw.ContainerHandle;\r
+\r
+import org.apache.derby.iapi.types.DataType;\r
+import org.apache.derby.iapi.types.DataValueDescriptor;\r
+import org.apache.derby.iapi.types.RowLocation;\r
+\r
+import java.io.ObjectOutput;\r
+import java.io.ObjectInput;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * A heap row location represents the location of a row in the heap.\r
+ * <P>\r
+ * It is implementad as a wrapper around a raw store record handle.\r
+ * \r
+ * @format_id ACCESS_HEAP_ROW_LOCATION_V1_ID\r
+ *\r
+ * @purpose Object used to store the location of a row within a Heap table. \r
+ * One of these is stored in every row of a btree secondary index \r
+ * built on a heap base table.\r
+ *\r
+ * @upgrade The type of the btree determines the type of rowlocation stored.\r
+ * In current btree implementations only one type of rowlocation can\r
+ * be stored per tree, and it's type is stored in the format id \r
+ * array stored in the Conglomerate object.\r
+ *\r
+ * @disk_layout \r
+ * page number(CompressedNumber.writeLong())\r
+ * record id(CompressedNumber.writeInt())\r
+ **/\r
+\r
+public class HeapRowLocation extends DataType implements RowLocation\r
+{\r
+ /**\r
+ The HeapRowLocation simply maintains a raw store record handle.\r
+ **/\r
+ private long pageno;\r
+ private int recid;\r
+ private RecordHandle rh;\r
+\r
+ private static final int BASE_MEMORY_USAGE = ClassSize.estimateBaseFromCatalog( HeapRowLocation.class);\r
+ private static final int RECORD_HANDLE_MEMORY_USAGE\r
+ = ClassSize.estimateBaseFromCatalog( org.apache.derby.impl.store.raw.data.RecordId.class);\r
+\r
+ public int estimateMemoryUsage()\r
+ {\r
+ int sz = BASE_MEMORY_USAGE;\r
+\r
+ if( null != rh)\r
+ sz += RECORD_HANDLE_MEMORY_USAGE;\r
+ return sz;\r
+ } // end of estimateMemoryUsage\r
+\r
+ public String getTypeName() {\r
+ return "RowLocation";\r
+ }\r
+\r
+ public void setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber,\r
+ boolean isNullable) {\r
+ }\r
+\r
+ public DataValueDescriptor getNewNull() {\r
+ return new HeapRowLocation();\r
+ }\r
+\r
+ public Object getObject() {\r
+ return null;\r
+ }\r
+\r
+ /*\r
+ ** Methods of CloneableObject.\r
+ */\r
+ public Object cloneObject()\r
+ {\r
+ return getClone();\r
+ \r
+ }\r
+\r
+ public DataValueDescriptor getClone() {\r
+ return new HeapRowLocation(this);\r
+ }\r
+\r
+ /**\r
+ * Recycle this HeapRowLocation object.\r
+ *\r
+ * @return this object reset to its initial state\r
+ */\r
+ public DataValueDescriptor recycle() {\r
+ pageno = 0L;\r
+ recid = 0;\r
+ rh = null;\r
+ return this;\r
+ }\r
+\r
+ public int getLength() {\r
+ return 10;\r
+ }\r
+\r
+ public String getString() {\r
+ return toString();\r
+ }\r
+\r
+ /*\r
+ ** Methods of Orderable (from RowLocation)\r
+ **\r
+ ** see description in\r
+ ** protocol/Database/Storage/Access/Interface/Orderable.java \r
+ **\r
+ */\r
+\r
+ public boolean compare(int op,\r
+ DataValueDescriptor other,\r
+ boolean orderedNulls,\r
+ boolean unknownRV)\r
+ {\r
+ // HeapRowLocation should not be null, ignore orderedNulls\r
+ int result = compare(other);\r
+\r
+ switch(op)\r
+ {\r
+ case ORDER_OP_LESSTHAN:\r
+ return (result < 0); // this < other\r
+ case ORDER_OP_EQUALS:\r
+ return (result == 0); // this == other\r
+ case ORDER_OP_LESSOREQUALS:\r
+ return (result <= 0); // this <= other\r
+ default:\r
+\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.THROWASSERT("Unexpected operation");\r
+ return false;\r
+ }\r
+ }\r
+\r
+ public int compare(DataValueDescriptor other)\r
+ {\r
+ // REVISIT: do we need this check?\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.ASSERT(other instanceof HeapRowLocation);\r
+\r
+ HeapRowLocation arg = (HeapRowLocation) other;\r
+ \r
+ // XXX (nat) assumption is that these HeapRowLocations are\r
+ // never null. However, if they ever become null, need\r
+ // to add null comparison logic.\r
+ //\r
+ // RESOLVE - change these to be state based\r
+ /*\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.ASSERT(getRecordHandle() != null);\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.ASSERT(arg.getRecordHandle() != null);\r
+ */\r
+\r
+ long myPage = this.pageno;\r
+ long otherPage = arg.pageno;\r
+\r
+ if (myPage < otherPage)\r
+ return -1;\r
+ else if (myPage > otherPage)\r
+ return 1;\r
+\r
+ int myRecordId = this.recid;\r
+ int otherRecordId = arg.recid;\r
+\r
+ if (myRecordId == otherRecordId)\r
+ return 0;\r
+ else if (myRecordId < otherRecordId)\r
+ return -1;\r
+ else\r
+ return 1;\r
+ }\r
+\r
+ /*\r
+ ** Methods of HeapRowLocation\r
+ */\r
+\r
+ HeapRowLocation(RecordHandle rh)\r
+ {\r
+ setFrom(rh);\r
+ }\r
+\r
+ public HeapRowLocation()\r
+ {\r
+ this.pageno = 0; \r
+ this.recid = RecordHandle.INVALID_RECORD_HANDLE;\r
+ }\r
+\r
+ /* For cloning */\r
+ private HeapRowLocation(HeapRowLocation other)\r
+ {\r
+ this.pageno = other.pageno;\r
+ this.recid = other.recid;\r
+ this.rh = other.rh;\r
+ }\r
+\r
+ public RecordHandle getRecordHandle(ContainerHandle ch)\r
+ throws StandardException\r
+ {\r
+ if (rh != null)\r
+ return rh;\r
+\r
+ return rh = ch.makeRecordHandle(this.pageno, this.recid);\r
+ }\r
+\r
+ void setFrom(RecordHandle rh)\r
+ {\r
+ this.pageno = rh.getPageNumber();\r
+ this.recid = rh.getId();\r
+ this.rh = rh;\r
+ }\r
+\r
+ //public void setFrom(long pageno, int recid)\r
+ //{\r
+ // this.pageno = pageno;\r
+ // this.recid = recid;\r
+ //}\r
+\r
+ /*\r
+ * InternalRowLocation interface\r
+ */\r
+\r
+ /**\r
+ * Return a RecordHandle built from current RowLocation.\r
+ * <p>\r
+ * Build a RecordHandle from the current RowLocation. The main client\r
+ * of this interface is row level locking secondary indexes which read\r
+ * the RowLocation field from a secondary index row, and then need a\r
+ * RecordHandle built from this RowLocation.\r
+ * <p>\r
+ * The interface is not as generic as one may have wanted in order to\r
+ * store as compressed a version of a RowLocation as possible. So \r
+ * if an implementation of a RowLocation does not have the segmentid, \r
+ * and containerid stored, use the input parameters instead. If the\r
+ * RowLocation does have the values stored use them and ignore the\r
+ * input parameters.\r
+ * <p>\r
+ * Example:\r
+ * <p>\r
+ * The HeapRowLocation implementation of RowLocation generated by the \r
+ * Heap class, only stores the page and record id. The B2I conglomerate\r
+ * implements a secondary index on top of a Heap class. B2I knows the\r
+ * segmentid and containerid of it's base table, and knows that it can\r
+ * find an InternalRowLocation in a particular column of it's rows. It\r
+ * uses InternalRowLocation.getRecordHandle() to build a RecordHandle\r
+ * from the InternalRowLocation, and uses it to set a row lock on that\r
+ * row in the btree.\r
+ *\r
+ * @return The newly allocated RecordHandle.\r
+ *\r
+ * @param segmentid The segment id to store in RecordHandle.\r
+ * @param containerid The segment id to store in RecordHandle.\r
+ *\r
+ * @exception StandardException Standard exception policy.\r
+ **/\r
+ /*public RecordHandle getRecordHandle(\r
+ TransactionManager tran,\r
+ long segmentid,\r
+ long containerid)\r
+ throws StandardException\r
+ {\r
+ return(\r
+ this.getRecordHandle(\r
+ tran.getRawStoreXact(), segmentid, containerid));\r
+ }\r
+*/\r
+\r
+ /*\r
+ * Storable interface, implies Externalizable, TypedFormat\r
+ */\r
+\r
+ /**\r
+ Return my format identifier.\r
+\r
+ @see org.apache.derby.iapi.services.io.TypedFormat#getTypeFormatId\r
+ */\r
+ public int getTypeFormatId() {\r
+ return StoredFormatIds.ACCESS_HEAP_ROW_LOCATION_V1_ID;\r
+ }\r
+\r
+ public boolean isNull()\r
+ {\r
+ return false;\r
+ }\r
+\r
+ public void writeExternal(ObjectOutput out) \r
+ throws IOException\r
+ {\r
+ // Write the page number, compressed\r
+ CompressedNumber.writeLong(out, this.pageno);\r
+\r
+ // Write the record id\r
+ CompressedNumber.writeInt(out, this.recid);\r
+ }\r
+\r
+ /**\r
+ @exception java.lang.ClassNotFoundException A class needed to read the\r
+ stored form of this object could not be found.\r
+ @see java.io.Externalizable#readExternal\r
+ */\r
+ public void readExternal(ObjectInput in) \r
+ throws IOException, ClassNotFoundException\r
+ {\r
+ this.pageno = CompressedNumber.readLong(in);\r
+\r
+ this.recid = CompressedNumber.readInt(in);\r
+\r
+ rh = null;\r
+ }\r
+ public void readExternalFromArray(ArrayInputStream in) \r
+ throws IOException, ClassNotFoundException\r
+ {\r
+ this.pageno = in.readCompressedLong();\r
+\r
+ this.recid = in.readCompressedInt();\r
+\r
+ rh = null;\r
+ }\r
+\r
+ public void restoreToNull()\r
+ {\r
+ if (SanityManager.DEBUG) \r
+ SanityManager.THROWASSERT("HeapRowLocation is never null");\r
+ }\r
+ protected void setFrom(DataValueDescriptor theValue) {\r
+ if (SanityManager.DEBUG)\r
+ SanityManager.THROWASSERT("SHOULD NOT BE CALLED");\r
+ }\r
+ /*\r
+ ** Methods of Object\r
+ */\r
+\r
+ /**\r
+ Implement value equality.\r
+ <BR>\r
+ MT - Thread safe\r
+ */\r
+ public boolean equals(Object ref) \r
+ {\r
+\r
+ if ((ref instanceof HeapRowLocation))\r
+ {\r
+ HeapRowLocation other = (HeapRowLocation) ref;\r
+\r
+ return(\r
+ (this.pageno == other.pageno) && (this.recid == other.recid));\r
+ }\r
+ else\r
+ {\r
+ return false;\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ Return a hashcode based on value.\r
+ <BR>\r
+ MT - thread safe\r
+ */\r
+ public int hashCode() \r
+ {\r
+ return ((int) this.pageno) ^ this.recid;\r
+ }\r
+\r
+ /*\r
+ * Standard toString() method.\r
+ */\r
+ public String toString()\r
+ {\r
+ String string = \r
+ "(" + this.pageno + "," + this.recid + ")";\r
+ return(string);\r
+ }\r
+}\r