private void Init() { _start = 0; _end = 0; // for instance, only manage index for one field query using 'equal' if (_classInfo.HasIndex() && _query.HasCriteria() && ((IInternalConstraint)_query.GetCriteria()).CanUseIndex()) { var fields = _query.GetAllInvolvedFields(); if (fields.IsNullOrEmpty()) { _useIndex = false; } else { var fieldIds = GetAllInvolvedFieldIds(fields); _classInfoIndex = _classInfo.GetIndexForAttributeIds(fieldIds); if (_classInfoIndex != null) { _useIndex = true; } } } // Keep the detail _details = GetDetails(); }
/// <summary> /// Take the fields of the index and take value from the query /// </summary> /// <param name="index"> The index </param> /// <returns> The key of the index </returns> protected virtual IOdbComparable ComputeIndexKey(ClassInfoIndex index) { var attributesNames = ClassInfo.GetAttributeNames(index.AttributeIds); var constraint = Query.GetCriteria(); var values = ((IInternalConstraint)constraint).GetValues(); return(IndexTool.BuildIndexKey(index.Name, values, attributesNames)); }
protected override IOdbComparable ComputeIndexKey(ClassInfoIndex index) { var constraint = Query.GetCriteria(); var values = ((IInternalConstraint)constraint).GetValues(); // if values.hasOid() is true, this means that we are working of the full object, // the index key is then the oid and not the object itself return(values.HasOid() ? new SimpleCompareKey(values.GetOid()) : base.ComputeIndexKey(index)); }
public void RemoveIndex(ClassInfoIndex cii) { _indexes.Remove(cii); }
public ClassInfoIndex AddIndexOn(string name, string[] indexFields, bool acceptMultipleValuesForSameKey) { if (_indexes == null) _indexes = new OdbList<ClassInfoIndex>(); var cii = new ClassInfoIndex { ClassInfoId = _oidInfo.ID, Name = name, IsUnique = !acceptMultipleValuesForSameKey }; var attributeIds = new int[indexFields.Length]; for (var i = 0; i < indexFields.Length; i++) attributeIds[i] = GetAttributeId(indexFields[i]); cii.AttributeIds = attributeIds; _indexes.Add(cii); return cii; }
/// <summary> /// Execute query using index /// </summary> /// <param name="index"> </param> /// <param name="inMemory"> </param> /// <param name="returnObjects"> </param> /// <param name="queryResultAction"> </param> private IInternalObjectSet <T> ExecuteUsingIndex <T>(ClassInfoIndex index, bool inMemory, bool returnObjects, IMatchingObjectAction queryResultAction) { // Index that have not been used yet do not have persister! if (index.BTree.GetPersister() == null) { index.BTree.SetPersister(new LazyOdbBtreePersister(StorageEngine)); } var nbObjects = ClassInfo.NumberOfObjects; var btreeSize = index.BTree.GetSize(); // the two values should be equal if (nbObjects != btreeSize) { var classInfo = StorageEngine.GetSession().GetMetaModel().GetClassInfoFromId(index.ClassInfoId); throw new OdbRuntimeException( NDatabaseError.IndexIsCorrupted.AddParameter(index.Name).AddParameter(classInfo.FullClassName). AddParameter(nbObjects).AddParameter(btreeSize)); } if (OdbConfiguration.IsLoggingEnabled()) { DLogger.Debug(string.Format("GenericQueryExecutor: loading {0} instance(s) of {1}", nbObjects, ClassInfo.FullClassName)); } if (ExecuteStartAndEndOfQueryAction()) { queryResultAction.Start(); } PrepareQuery(); if (Query != null) { _queryHasOrderBy = Query.HasOrderBy(); } var tree = index.BTree; var isUnique = index.IsUnique; // Iterator iterator = new BTreeIterator(tree, // OrderByConstants.ORDER_BY_ASC); var key = ComputeIndexKey(index); IList list = null; // If index is unique, get the object if (isUnique) { var treeSingle = (IBTreeSingleValuePerKey)tree; var value = treeSingle.Search(key); if (value != null) { list = new List <object> { value } } ; } else { var treeMultiple = (IBTreeMultipleValuesPerKey)tree; list = treeMultiple.Search(key); } if (list != null) { foreach (OID oid in list) { // FIXME Why calling this method ObjectReader.GetObjectPositionFromItsOid(oid, true, true); _orderByKey = null; var objectMatches = MatchObjectWithOid(oid, returnObjects, inMemory); if (objectMatches) { queryResultAction.ObjectMatch(oid, GetCurrentObjectMetaRepresentation(), _orderByKey); } } queryResultAction.End(); return(queryResultAction.GetObjects <T>()); } if (ExecuteStartAndEndOfQueryAction()) { queryResultAction.End(); } return(queryResultAction.GetObjects <T>()); }