private static Pair <QueryPlanIndexItem, IndexMultiKey> PlanIndex(bool unique, IList <IndexedPropDesc> hashProps, IList <IndexedPropDesc> btreeProps, bool mustCoerce) { // not resolved as full match and not resolved as unique index match, allocate var indexPropKey = new IndexMultiKey(unique, hashProps, btreeProps); var indexedPropDescs = hashProps.ToArray(); var indexProps = IndexedPropDesc.GetIndexProperties(indexedPropDescs); var indexCoercionTypes = IndexedPropDesc.GetCoercionTypes(indexedPropDescs); if (!mustCoerce) { indexCoercionTypes = null; } var rangePropDescs = btreeProps.ToArray(); var rangeProps = IndexedPropDesc.GetIndexProperties(rangePropDescs); var rangeCoercionTypes = IndexedPropDesc.GetCoercionTypes(rangePropDescs); var indexItem = new QueryPlanIndexItem(indexProps, indexCoercionTypes, rangeProps, rangeCoercionTypes, unique); return(new Pair <QueryPlanIndexItem, IndexMultiKey>(indexItem, indexPropKey)); }
public static EventTableCreateIndexDesc FromMultiKey(IndexMultiKey multiKey) { return(new EventTableCreateIndexDesc( multiKey.HashIndexedProps, multiKey.RangeIndexedProps, multiKey.IsUnique)); }
public static IndexMultiKey FindByIndexHint( IDictionary <IndexMultiKey, EventTableIndexEntryBase> indexCandidates, IList <IndexHintInstruction> instructions) { foreach (IndexHintInstruction instruction in instructions) { if (instruction is IndexHintInstructionIndexName name) { string indexName = name.IndexName; IndexMultiKey found = FindExplicitIndexByName(indexCandidates, indexName); if (found != null) { return(found); } } if (instruction is IndexHintInstructionExplicit) { IndexMultiKey found = FindExplicitIndexAnyName(indexCandidates); if (found != null) { return(found); } } if (instruction is IndexHintInstructionBust) { throw new EPException("Failed to plan index access, index hint busted out"); } } return(null); }
private static bool IndexMatchesProvided( IndexMultiKey indexDesc, IList <IndexedPropDesc> hashPropsProvided, IList <IndexedPropDesc> rangePropsProvided) { IndexedPropDesc[] hashPropIndexedList = indexDesc.HashIndexedProps; foreach (IndexedPropDesc hashPropIndexed in hashPropIndexedList) { bool foundHashProp = IndexHashIsProvided(hashPropIndexed, hashPropsProvided); if (!foundHashProp) { return(false); } } IndexedPropDesc[] rangePropIndexedList = indexDesc.RangeIndexedProps; foreach (IndexedPropDesc rangePropIndexed in rangePropIndexedList) { bool foundRangeProp = IndexHashIsProvided(rangePropIndexed, rangePropsProvided); if (!foundRangeProp) { return(false); } } return(true); }
public bool Equals(IndexMultiKey other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } if (other.IsUnique.Equals(IsUnique) && Collections.AreEqual(other.HashIndexedProps, HashIndexedProps) && Collections.AreEqual(other.RangeIndexedProps, RangeIndexedProps)) { if (AdvancedIndexDesc == null) { return(other.AdvancedIndexDesc == null); } else { return(other.AdvancedIndexDesc != null && AdvancedIndexDesc.EqualsAdvancedIndex(other.AdvancedIndexDesc)); } } return(false); }
private static Pair <IndexMultiKey, EventTableIndexEntryBase> GetPair( IDictionary <IndexMultiKey, EventTableIndexEntryBase> tableIndexesRefCount, IndexMultiKey indexMultiKey) { EventTableIndexEntryBase indexFound = tableIndexesRefCount.Get(indexMultiKey); return(new Pair <IndexMultiKey, EventTableIndexEntryBase>(indexMultiKey, indexFound)); }
public bool RemoveIndexReference(IndexMultiKey index, string referringStatementName) { var entry = _indexes.Get(index); if (entry == null) { return(false); } return(entry.RemoveReferringStatement(referringStatementName)); }
public void AddIndexReference(IndexMultiKey indexMultiKey, string statementName) { EventTableIndexMetadataEntry entry = _indexes.Get(indexMultiKey); if (entry == null) { return; } entry.AddReferringStatement(statementName); }
public EventTable GetIndexByDesc(IndexMultiKey indexKey) { var entry = _tableIndexesRefCount.Get(indexKey); if (entry == null) { return(null); } return(entry.Table); }
private static bool IsExactMatch(IndexMultiKey existing, bool unique, IList <IndexedPropDesc> hashProps, IList <IndexedPropDesc> btreeProps) { if (existing.IsUnique != unique) { return(false); } var keyPropCompare = IndexedPropDesc.Compare(existing.HashIndexedProps, hashProps); return(keyPropCompare && IndexedPropDesc.Compare(existing.RangeIndexedProps, btreeProps)); }
/// <summary> /// Given an index with a defined set of hash(equals) and range(btree) props and uniqueness flag, /// and given a list of indexable properties and accessors for both hash and range, /// return the ordered keys and coercion information. /// </summary> /// <param name="indexMultiKey">index definition</param> /// <param name="hashIndexPropsProvided">hash indexable properties</param> /// <param name="hashJoinedProps">keys for hash indexable properties</param> /// <param name="rangeIndexPropsProvided">btree indexable properties</param> /// <param name="rangeJoinedProps">keys for btree indexable properties</param> /// <returns>ordered set of key information</returns> public static IndexKeyInfo CompileIndexKeyInfo( IndexMultiKey indexMultiKey, string[] hashIndexPropsProvided, SubordPropHashKey[] hashJoinedProps, string[] rangeIndexPropsProvided, SubordPropRangeKey[] rangeJoinedProps) { // map the order of indexed columns (key) to the key information available var indexedKeyProps = indexMultiKey.HashIndexedProps; var isCoerceHash = false; var hashesDesc = new SubordPropHashKey[indexedKeyProps.Length]; var hashPropCoercionTypes = new Type[indexedKeyProps.Length]; for (var i = 0; i < indexedKeyProps.Length; i++) { var indexField = indexedKeyProps[i].IndexPropName; var index = CollectionUtil.FindItem(hashIndexPropsProvided, indexField); if (index == -1) { throw new IllegalStateException("Could not find index property for lookup '" + indexedKeyProps[i]); } hashesDesc[i] = hashJoinedProps[index]; hashPropCoercionTypes[i] = indexedKeyProps[i].CoercionType; var evaluatorHashkey = hashesDesc[i].HashKey.KeyExpr.ExprEvaluator; if (evaluatorHashkey != null && TypeHelper.GetBoxedType(indexedKeyProps[i].CoercionType) != TypeHelper.GetBoxedType(evaluatorHashkey.ReturnType)) // we allow null evaluator { isCoerceHash = true; } } // map the order of range columns (range) to the range information available indexedKeyProps = indexMultiKey.RangeIndexedProps; var rangesDesc = new SubordPropRangeKey[indexedKeyProps.Length]; var rangePropCoercionTypes = new Type[indexedKeyProps.Length]; var isCoerceRange = false; for (var i = 0; i < indexedKeyProps.Length; i++) { var indexField = indexedKeyProps[i].IndexPropName; var index = CollectionUtil.FindItem(rangeIndexPropsProvided, indexField); if (index == -1) { throw new IllegalStateException("Could not find range property for lookup '" + indexedKeyProps[i]); } rangesDesc[i] = rangeJoinedProps[index]; rangePropCoercionTypes[i] = rangeJoinedProps[index].CoercionType; if (TypeHelper.GetBoxedType(indexedKeyProps[i].CoercionType) != TypeHelper.GetBoxedType(rangePropCoercionTypes[i])) { isCoerceRange = true; } } return(new IndexKeyInfo(hashesDesc, new CoercionDesc(isCoerceHash, hashPropCoercionTypes), rangesDesc, new CoercionDesc(isCoerceRange, rangePropCoercionTypes))); }
public void AddIndexNonExplicit(IndexMultiKey indexMultiKey, string statementName, QueryPlanIndexItem queryPlanIndexItem) { if (_indexes.ContainsKey(indexMultiKey)) { return; } var entry = new EventTableIndexMetadataEntry(null, false, queryPlanIndexItem, null); entry.AddReferringStatement(statementName); _indexes.Put(indexMultiKey, entry); }
public SubordinateQueryIndexDesc( IndexKeyInfo indexKeyInfo, string indexName, IndexMultiKey indexMultiKey, QueryPlanIndexItem queryPlanIndexItem) { IndexKeyInfo = indexKeyInfo; IndexName = indexName; IndexMultiKey = indexMultiKey; QueryPlanIndexItem = queryPlanIndexItem; }
public void RemoveIndex(IndexMultiKey index) { var entry = _tableIndexesRefCount.Pluck(index); if (entry != null) { _tables.Remove(entry.Table); if (entry.OptionalIndexName != null) { _explicitIndexes.Remove(entry.OptionalIndexName); } } }
public bool Equals(IndexMultiKey other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } return(other.IsUnique.Equals(IsUnique) && Collections.AreEqual(other.HashIndexedProps, HashIndexedProps) && Collections.AreEqual(other.RangeIndexedProps, RangeIndexedProps)); }
/// <summary> /// The RemoveIndex /// </summary> /// <param name="index">The <see cref="IndexMultiKey"/></param> public void RemoveIndex(IndexMultiKey index) { EventTableIndexRepositoryEntry entry = _tableIndexesRefCount.Delete(index); if (entry != null) { _tables.Remove(entry.Table); if (entry.OptionalIndexName != null) { _explicitIndexes.Remove(entry.OptionalIndexName); } entry.Table.Destroy(); } }
public void AddIndexExplicit(bool isPrimary, IndexMultiKey indexMultiKey, string explicitIndexName, QueryPlanIndexItem explicitIndexDesc, string statementName) { if (GetIndexByName(explicitIndexName) != null) { throw new ExprValidationException("An index by name '" + explicitIndexName + "' already exists"); } if (_indexes.ContainsKey(indexMultiKey)) { throw new ExprValidationException("An index for the same columns already exists"); } EventTableIndexMetadataEntry entry = new EventTableIndexMetadataEntry(explicitIndexName, isPrimary, explicitIndexDesc, explicitIndexName); entry.AddReferringStatement(statementName); _indexes.Put(indexMultiKey, entry); }
public static Pair <IndexMultiKey, EventTableIndexEntryBase> FindIndexBestAvailable( IDictionary <IndexMultiKey, EventTableIndexEntryBase> tablesAvailable, ISet <string> keyPropertyNames, ISet <string> rangePropertyNames, IList <IndexHintInstruction> optionalIndexHintInstructions) { if (keyPropertyNames.IsEmpty() && rangePropertyNames.IsEmpty()) { return(null); } // determine candidates var hashProps = new List <IndexedPropDesc>(); foreach (string keyPropertyName in keyPropertyNames) { hashProps.Add(new IndexedPropDesc(keyPropertyName, null)); } var rangeProps = new List <IndexedPropDesc>(); foreach (string rangePropertyName in rangePropertyNames) { rangeProps.Add(new IndexedPropDesc(rangePropertyName, null)); } var indexCandidates = FindCandidates(tablesAvailable, hashProps, rangeProps); // handle hint if (optionalIndexHintInstructions != null) { IndexMultiKey found = FindByIndexHint(indexCandidates, optionalIndexHintInstructions); if (found != null) { return(GetPair(tablesAvailable, found)); } } // no candidates if (indexCandidates == null || indexCandidates.IsEmpty()) { if (Log.IsDebugEnabled) { Log.Debug("No index found."); } return(null); } return(GetBestCandidate(indexCandidates)); }
/// <summary> /// The AddIndex /// </summary> /// <param name="unique">The <see cref="bool"/></param> /// <param name="hashProps">The <see cref="IList{IndexedPropDesc}"/></param> /// <param name="btreeProps">The <see cref="IList{IndexedPropDesc}"/></param> /// <param name="advancedIndexProvisionDesc">The <see cref="EventAdvancedIndexProvisionDesc"/></param> /// <param name="prefilledEvents">The <see cref="IEnumerable{EventBean}"/></param> /// <param name="indexedType">The <see cref="EventType"/></param> /// <param name="indexName">The <see cref="string"/></param> /// <param name="mustCoerce">The <see cref="bool"/></param> /// <param name="agentInstanceContext">The <see cref="AgentInstanceContext"/></param> /// <param name="optionalSerde">The <see cref="object"/></param> /// <returns>The <see cref="Pair{IndexMultiKey, EventTableAndNamePair}"/></returns> private Pair <IndexMultiKey, EventTableAndNamePair> AddIndex( bool unique, IList <IndexedPropDesc> hashProps, IList <IndexedPropDesc> btreeProps, EventAdvancedIndexProvisionDesc advancedIndexProvisionDesc, IEnumerable <EventBean> prefilledEvents, EventType indexedType, string indexName, bool mustCoerce, AgentInstanceContext agentInstanceContext, object optionalSerde) { // not resolved as full match and not resolved as unique index match, allocate var indexPropKey = new IndexMultiKey(unique, hashProps, btreeProps, advancedIndexProvisionDesc == null ? null : advancedIndexProvisionDesc.IndexDesc); var indexedPropDescs = hashProps.ToArray(); var indexProps = IndexedPropDesc.GetIndexProperties(indexedPropDescs); var indexCoercionTypes = IndexedPropDesc.GetCoercionTypes(indexedPropDescs); if (!mustCoerce) { indexCoercionTypes = null; } var rangePropDescs = btreeProps.ToArray(); var rangeProps = IndexedPropDesc.GetIndexProperties(rangePropDescs); var rangeCoercionTypes = IndexedPropDesc.GetCoercionTypes(rangePropDescs); var indexItem = new QueryPlanIndexItem(indexProps, indexCoercionTypes, rangeProps, rangeCoercionTypes, unique, advancedIndexProvisionDesc); var table = EventTableUtil.BuildIndex(agentInstanceContext, 0, indexItem, indexedType, true, unique, indexName, optionalSerde, false); // fill table since its new var events = new EventBean[1]; foreach (EventBean prefilledEvent in prefilledEvents) { events[0] = prefilledEvent; table.Add(events, agentInstanceContext); } // add table _tables.Add(table); // add index, reference counted _tableIndexesRefCount.Put(indexPropKey, new EventTableIndexRepositoryEntry(indexName, table)); return(new Pair <IndexMultiKey, EventTableAndNamePair>(indexPropKey, new EventTableAndNamePair(table, indexName))); }
public static IndexMultiKey FindIndexConsiderTyping( IDictionary <IndexMultiKey, EventTableIndexEntryBase> tableIndexesRefCount, IList <IndexedPropDesc> hashProps, IList <IndexedPropDesc> btreeProps, IList <IndexHintInstruction> optionalIndexHintInstructions) { if (hashProps.IsEmpty() && btreeProps.IsEmpty()) { throw new ArgumentException("Invalid zero element list for hash and btree columns"); } var indexCandidates = FindCandidates(tableIndexesRefCount, hashProps, btreeProps); // if there are hints, follow these if (optionalIndexHintInstructions != null) { IndexMultiKey found = FindByIndexHint(indexCandidates, optionalIndexHintInstructions); if (found != null) { return(found); } } // Get an existing table, if any, matching the exact requirement, prefer unique IndexMultiKey indexPropKeyMatch = FindExactMatchNameAndType(tableIndexesRefCount.Keys, true, hashProps, btreeProps, null); if (indexPropKeyMatch == null) { indexPropKeyMatch = FindExactMatchNameAndType(tableIndexesRefCount.Keys, false, hashProps, btreeProps, null); } if (indexPropKeyMatch != null) { return(indexPropKeyMatch); } if (indexCandidates.IsEmpty()) { return(null); } return(GetBestCandidate(indexCandidates).First); }
private static bool IsExactMatch( IndexMultiKey existing, bool unique, IList <IndexedPropDesc> hashProps, IList <IndexedPropDesc> btreeProps, AdvancedIndexDesc advancedIndexDesc) { if (existing.IsUnique != unique) { return(false); } if (!IndexedPropDesc.Compare(existing.HashIndexedProps, hashProps)) { return(false); } if (!IndexedPropDesc.Compare(existing.RangeIndexedProps, btreeProps)) { return(false); } if (existing.AdvancedIndexDesc == null) { return(advancedIndexDesc == null); } return(advancedIndexDesc != null && existing.AdvancedIndexDesc.EqualsAdvancedIndex(advancedIndexDesc)); }
public void RemoveIndex(IndexMultiKey imk) { _indexes.Remove(imk); }
public void AddIndex(IndexMultiKey indexMultiKey, EventTableIndexRepositoryEntry entry) { _tableIndexesRefCount.Put(indexMultiKey, entry); _tables.Add(entry.Table); }