1818 */
1919package com .arcadedb .database ;
2020
21- import com .arcadedb .database .Record ;
2221import com .arcadedb .engine .LocalBucket ;
2322import com .arcadedb .engine .PageId ;
23+ import com .arcadedb .exception .DatabaseOperationException ;
2424import com .arcadedb .exception .RecordNotFoundException ;
2525import com .arcadedb .graph .Edge ;
2626import com .arcadedb .graph .Vertex ;
3434 * It represents the logical address of a record in the database. The record id is composed by the bucket id (the bucket containing the record) and the offset
3535 * as the absolute position of the record in the bucket.
3636 * <br>
37- * Immutable class.
37+ * Immutable class. The database is resolved from the thread-local context on demand, keeping this object lightweight (only bucketId + offset).
3838 */
3939public class RID implements Identifiable , Comparable <Object >, Serializable {
40- private transient final BasicDatabase database ;
41- protected final int bucketId ;
42- protected final long offset ;
43- private int cachedHashCode = 0 ; // BOOST PERFORMANCE BECAUSE RID.HASHCODE() IS ONE OF THE HOTSPOTS FOR ANY USE CASES
40+ protected final int bucketId ;
41+ protected final long offset ;
4442
4543 public RID (final int bucketId , final long offset ) {
46- this (null , bucketId , offset );
44+ this .bucketId = bucketId ;
45+ this .offset = offset ;
4746 }
4847
4948 public RID (final BasicDatabase database , final int bucketId , final long offset ) {
50- if (database == null )
51- // RETRIEVE THE DATABASE FROM THE THREAD LOCAL
52- this .database = DatabaseContext .INSTANCE .getActiveDatabase ();
53- else
54- this .database = database ;
55-
5649 this .bucketId = bucketId ;
5750 this .offset = offset ;
5851 }
5952
6053 public RID (final String value ) {
61- this (null , value );
62- }
63-
64- public RID (final BasicDatabase database , String value ) {
65- if (database == null )
66- // RETRIEVE THE DATABASE FROM THE THREAD LOCAL
67- this .database = DatabaseContext .INSTANCE .getActiveDatabase ();
68- else
69- this .database = database ;
70-
7154 if (!value .startsWith ("#" ))
7255 throw new IllegalArgumentException ("The RID '" + value + "' is not valid" );
7356
74- value = value .substring (1 );
75-
76- final List <String > parts = CodeUtils .split (value , ':' , 2 );
57+ final String stripped = value .substring (1 );
58+ final List <String > parts = CodeUtils .split (stripped , ':' , 2 );
7759 this .bucketId = Integer .parseInt (parts .getFirst ());
7860 this .offset = Long .parseLong (parts .get (1 ));
7961 }
8062
63+ public RID (final BasicDatabase database , final String value ) {
64+ this (value );
65+ }
66+
8167 public static boolean is (final Object value ) {
8268 if (value instanceof RID )
8369 return true ;
@@ -124,15 +110,15 @@ public Record getRecord() {
124110
125111 @ Override
126112 public Record getRecord (final boolean loadContent ) {
127- return database .lookupByRID (this , loadContent );
113+ return requireDatabase () .lookupByRID (this , loadContent );
128114 }
129115
130116 public Document asDocument () {
131117 return asDocument (true );
132118 }
133119
134120 public Document asDocument (final boolean loadContent ) {
135- return (Document ) database .lookupByRID (this , loadContent );
121+ return (Document ) requireDatabase () .lookupByRID (this , loadContent );
136122 }
137123
138124 public Vertex asVertex () {
@@ -141,7 +127,9 @@ public Vertex asVertex() {
141127
142128 public Vertex asVertex (final boolean loadContent ) {
143129 try {
144- return (Vertex ) database .lookupByRID (this , loadContent );
130+ return (Vertex ) requireDatabase ().lookupByRID (this , loadContent );
131+ } catch (final RecordNotFoundException e ) {
132+ throw e ;
145133 } catch (final Exception e ) {
146134 throw new RecordNotFoundException ("Record " + this + " not found" , this , e );
147135 }
@@ -152,7 +140,7 @@ public Edge asEdge() {
152140 }
153141
154142 public Edge asEdge (final boolean loadContent ) {
155- return (Edge ) database .lookupByRID (this , loadContent );
143+ return (Edge ) requireDatabase () .lookupByRID (this , loadContent );
156144 }
157145
158146 @ Override
@@ -164,14 +152,12 @@ public boolean equals(final Object obj) {
164152 return false ;
165153
166154 final RID o = ((Identifiable ) obj ).getIdentity ();
167- return bucketId == o .bucketId && offset == o .offset && Objects . equals ( database , o . getDatabase ()) ;
155+ return bucketId == o .bucketId && offset == o .offset ;
168156 }
169157
170158 @ Override
171159 public int hashCode () {
172- if (cachedHashCode == 0 )
173- cachedHashCode = Objects .hash (database , bucketId , offset );
174- return cachedHashCode ;
160+ return bucketId * 31 + Long .hashCode (offset );
175161 }
176162
177163 @ Override
@@ -180,21 +166,10 @@ public int compareTo(final Object o) {
180166 if (o instanceof RID iD )
181167 otherRID = iD ;
182168 else if (o instanceof String string )
183- otherRID = new RID (database , string );
169+ otherRID = new RID (string );
184170 else
185171 return -1 ;
186172
187- final BasicDatabase otherDb = otherRID .getDatabase ();
188- if (database != null ) {
189- if (otherDb != null ) {
190- final int res = database .getName ().compareTo (otherDb .getName ());
191- if (res != 0 )
192- return res ;
193- } else
194- return -1 ;
195- } else if (otherDb != null )
196- return 1 ;
197-
198173 if (bucketId > otherRID .bucketId )
199174 return 1 ;
200175 else if (bucketId < otherRID .bucketId )
@@ -208,16 +183,21 @@ else if (offset < otherRID.offset)
208183 return 0 ;
209184 }
210185
211- public BasicDatabase getDatabase () {
212- return database ;
213- }
214-
215186 public boolean isValid () {
216187 return bucketId > -1 && offset > -1 ;
217188 }
218189
219190 public PageId getPageId () {
220- return new PageId (database , bucketId ,
221- (int ) (getPosition () / ((LocalBucket ) database .getSchema ().getBucketById (bucketId )).getMaxRecordsInPage ()));
191+ final BasicDatabase db = requireDatabase ();
192+ return new PageId (db , bucketId ,
193+ (int ) (getPosition () / ((LocalBucket ) db .getSchema ().getBucketById (bucketId )).getMaxRecordsInPage ()));
194+ }
195+
196+ private BasicDatabase requireDatabase () {
197+ final BasicDatabase db = DatabaseContext .INSTANCE .getActiveDatabase ();
198+ if (db == null )
199+ throw new DatabaseOperationException (
200+ "No active database context for RID " + this + ". Use database.lookupByRID() instead or ensure a database context is active on the current thread" );
201+ return db ;
222202 }
223203}
0 commit comments