2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
23 #ifndef _H_APPLEDL_DBINDEX
24 #define _H_APPLEDL_DBINDEX
26 #include "MetaRecord.h"
35 typedef constVector
<uint32
> DbOffsetVector
;
37 typedef DbOffsetVector::const_iterator DbIndexIterator
;
40 // An object that represents a key being used as part of a query.
45 friend class DbConstIndex
;
46 friend class DbKeyComparator
;
49 DbQueryKey(const DbConstIndex
&index
);
51 enum { kQueryValue
= 0 };
54 WriteSection mKeyData
;
56 const DbConstIndex
&mIndex
;
57 const ReadSection
&mTableSection
;
62 // An object which performs comparison between keys, either stored
63 // in a database or provided as part of a query.
69 DbKeyComparator(const DbQueryKey
&key
) : mKey(key
) {}
71 int operator () (uint32 keyOffset1
, uint32 keyOffset2
) const;
74 const DbQueryKey
&mKey
;
78 // A key as stored in an index.
83 DbIndexKey(const ReadSection
&key
, const Range
&keyRange
, const DbIndex
&index
)
84 : mKeySection(key
), mKeyRange(keyRange
), mIndex(index
) {}
86 bool operator < (const DbIndexKey
&other
) const;
88 uint32
keySize() const { return mKeyRange
.mSize
; }
89 const uint8
*keyData() const { return mKeySection
.range(mKeyRange
); }
92 // the key data, expressed as a subsection of a read section
93 const ReadSection
&mKeySection
;
96 // the index that knows how to interpret the key data
97 const DbIndex
&mIndex
;
100 // Base class containing stuff shared between const and mutable indexes.
104 friend class DbIndexKey
;
107 uint32
indexId() const { return mIndexId
; }
109 // append an attribute to the index key
110 void appendAttribute(uint32 attributeId
);
113 DbIndex(const MetaRecord
&metaRecord
, uint32 indexId
, bool isUniqueIndex
);
115 // meta record for table associated with this index
116 const MetaRecord
&mMetaRecord
;
118 // vector of indexed attributes
119 typedef vector
<const MetaAttribute
*> AttributeVector
;
120 AttributeVector mAttributes
;
128 class DbConstIndex
: public DbIndex
130 friend class DbMutableIndex
;
131 friend class DbQueryKey
;
132 friend class DbKeyComparator
;
135 DbConstIndex(const Table
&table
, uint32 indexId
, bool isUniqueIndex
);
136 DbConstIndex(const Table
&table
, const ReadSection
&indexSection
);
138 const Table
&table() const { return mTable
; }
140 // check if this index can be used for a given query, and if so, generate
141 // the appropriate index key from the query
142 bool matchesQuery(const CSSM_QUERY
&query
, DbQueryKey
*&queryKey
) const;
144 // perform a query on the index
145 void performQuery(const DbQueryKey
&queryKey
,
146 DbIndexIterator
&begin
, DbIndexIterator
&end
) const;
148 // given an iterator as returned by performQuery(), return the read section for the record
149 ReadSection
getRecordSection(DbIndexIterator iter
) const;
152 // sorted vector of offsets to index key data
153 DbOffsetVector mKeyOffsetVector
;
155 // vector, in same order as key vector, of corresponding record numbers
156 DbOffsetVector mRecordNumberVector
;
161 // A memory-resident index that can be modified, but not used for a query.
163 class DbMutableIndex
: public DbIndex
166 DbMutableIndex(const DbConstIndex
&index
);
167 DbMutableIndex(const MetaRecord
&metaRecord
, uint32 indexId
, bool isUniqueIndex
);
170 // insert a record into the index
171 void insertRecord(uint32 recordNumber
, const ReadSection
&packedRecord
);
173 // remove a record from the index
174 void removeRecord(uint32 recordNumber
);
177 uint32
writeIndex(WriteSection
&ws
, uint32 offset
);
180 // helper methods called by insertRecord()
181 void insertRecordSingle(uint32 recordOffset
, const ReadSection
&packedRecord
);
182 void insertRecordMulti(uint32 recordOffset
, const ReadSection
&packedRecord
,
183 uint32 attributeIndex
, WriteSection
&keyData
, uint32 keySize
);
185 // a single write section which stores generated index key data
186 WriteSection mIndexData
;
187 uint32 mIndexDataSize
;
189 // a map from index keys to record numbers
190 typedef multimap
<DbIndexKey
, uint32
> IndexMap
;
194 } // end namespace Security
196 #endif // _H_APPLEDL_DBINDEX