View Javadoc

1   /*
2    *   Copyright 2004 The Apache Software Foundation
3    *
4    *   Licensed under the Apache License, Version 2.0 (the "License");
5    *   you may not use this file except in compliance with the License.
6    *   You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *   Unless required by applicable law or agreed to in writing, software
11   *   distributed under the License is distributed on an "AS IS" BASIS,
12   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *   See the License for the specific language governing permissions and
14   *   limitations under the License.
15   *
16   */
17  package org.apache.ldap.server.partition.impl.btree;
18  
19  
20  import java.math.BigInteger;
21  
22  import javax.naming.directory.Attributes;
23  
24  
25  /***
26   * An index key value pair based on a tuple which can optionally reference the
27   * indexed entry if one has been resusitated.
28   * 
29   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
30   * @version $Rev: 264732 $
31   */
32  public class IndexRecord
33  {
34      /*** The underlying BTree Tuple */
35      private final Tuple tuple = new Tuple();
36      /*** The referenced entry if resusitated */
37      private Attributes entry = null;
38  
39  
40      /***
41       * Sets the key value tuple represented by this IndexRecord optionally 
42       * setting the entry if one was resusitated from the master table.
43       *
44       * @param tuple the tuple for the IndexRecord
45       * @param entry the resusitated entry if any
46       */
47      public void setTuple( Tuple tuple, Attributes entry )
48      {
49          this.tuple.setKey( tuple.getKey() );
50          this.tuple.setValue( tuple.getValue() );
51          this.entry = entry;
52      }
53  
54  
55      /***
56       * Sets the key value tuple but swapping the key and the value and 
57       * optionally adding the entry if one was resusitated.
58       *
59       * @param tuple the tuple for the IndexRecord
60       * @param entry the resusitated entry if any
61       */
62      public void setSwapped( Tuple tuple, Attributes entry )
63      {
64          this.tuple.setKey( tuple.getValue() );
65          this.tuple.setValue( tuple.getKey() );
66          this.entry = entry;
67      }
68  
69  
70      /***
71       * Gets the entry id for this IndexRecord. 
72       *
73       * @return the id of the entry indexed
74       */
75      public BigInteger getEntryId()
76      {
77          return ( BigInteger ) tuple.getValue();
78      }
79  
80  
81      /***
82       * Gets the index key ( the attribute's value ) for this IndexRecord.
83       *
84       * @return the key of the entry indexed
85       */
86      public Object getIndexKey()
87      {
88          return tuple.getKey();
89      }
90  
91  
92      /***
93       * Sets the entry id.
94       *
95       * @param id the id of the entry
96       */
97      public void setEntryId( BigInteger id )
98      {
99          tuple.setValue( id );
100     }
101 
102 
103     /***
104      * Sets the index key.
105      *
106      * @param key the key of the IndexRecord
107      */
108     public void setIndexKey( Object key )
109     {
110         tuple.setKey( key );
111     }
112 
113 
114     /***
115      * Gets the entry of this IndexRecord if one was resusitated form the master
116      * table.
117      * 
118      * @return the entry's attributes
119      */
120     public Attributes getAttributes()
121     {
122         if( entry == null )
123         {
124             return null;
125         }
126 
127         return ( Attributes ) entry.clone();
128     }
129 
130 
131     /***
132      * Sets the entry's attributes.
133      * 
134      * @param entry the entry's attributes
135      */
136     public void setAttributes( Attributes entry )
137     {
138         this.entry = entry;
139     }
140 
141 
142     /***
143      * Clears the tuple key, the tuple value and the entry fields.
144      */
145     public void clear()
146     {
147         entry = null;
148         tuple.setKey( null );
149         tuple.setValue( null );
150     }
151     
152  
153     /***
154      * Utility method used to copy the contents of one IndexRecord into this
155      * IndexRecord.
156      * 
157      * @param record the record whose contents we copy
158      */   
159     public void copy( IndexRecord record )
160     {
161         entry = record.getAttributes();
162         tuple.setKey( record.getIndexKey() );
163         tuple.setValue( record.getEntryId() );
164     }
165 }