Exemplo n.º 1
0
        public int GetIndex(IEnumerable <AttributeMember> members)
        {
            int           index = 0;
            IndexTreeNode cn    = null;

            foreach (AttributeMember m in members)
            {
                if (index == 0)
                {
                    foreach (IndexTreeNode n in IndexTree.Nodes)
                    {
                        if (n.Member == m)
                        {
                            cn = n;
                            break;
                        }
                        else
                        {
                            cn = null;
                        }
                    }
                    index++;
                }
                else if (cn != null)
                {
                    foreach (IndexTreeNode n in cn.Children)
                    {
                        if (n.Member == m)
                        {
                            cn = n;
                            break;
                        }
                        else
                        {
                            cn = null;
                        }
                    }
                    index++;
                }
            }

            if (cn != null)
            {
                return(cn.Index);
            }
            else
            {
                return(-1);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="skipTotal">Tells GetValue should return a total place holder if tuple is a total</param>
        /// <param name="members"></param>
        /// <returns></returns>
        public ValueInfo GetValue(bool skipTotal, params AttributeMember[][] members)
        {
            int           index   = 0;
            Measure       measure = null;
            IndexTreeNode cn      = null;

            foreach (AttributeMember[] curr in members)
            {
                foreach (AttributeMember m in curr)
                {
                    // return a place holder for total
                    if (m.IsTotal)
                    {
                        if (!skipTotal)
                        {
                            return(new ValueInfo(null, measure, true, members));
                        }
                    }
                    // measure
                    else if (m.LinkedMeasure != null)
                    {
                        measure = m.LinkedMeasure;
                    }
                    // index == 0 -> first level, start searching
                    else if (index == 0)
                    {
                        foreach (IndexTreeNode n in IndexTree.Nodes)
                        {
                            if (n.Member == m)
                            {
                                cn = n;
                                break;
                            }
                            else
                            {
                                cn = null;
                            }
                        }
                        index++;
                    }
                    // keep searching
                    // cn.Index = rowIndex of the aggregation table
                    else if (cn != null)
                    {
                        foreach (IndexTreeNode n in cn.Children)
                        {
                            if (n.Member == m)
                            {
                                cn = n;
                                break;
                            }
                            else
                            {
                                cn = null;
                            }
                        }
                        index++;
                    }
                }
            }

            if (cn != null)
            {
                return(new ValueInfo(this[measure].Values[cn.Index], measure));
            }
            else
            {
                return(new ValueInfo(null, measure));
            }


            /*
             * Search by scan - do not delete this code
             *
             * Measure measure = null;
             * int index = Tuples.FindIndex(curr =>
             * {
             *  foreach (Member m in members1)
             *  {
             *      if (m.LinkedMeasure != null)
             *          measure = m.LinkedMeasure;
             *      else if (!curr.Contains(m))
             *          return false;
             *  }
             *
             *  foreach (Member m in members2)
             *  {
             *      if (m.LinkedMeasure != null)
             *          measure = m.LinkedMeasure;
             *      else if (!curr.Contains(m))
             *          return false;
             *  }
             *
             *  return true;
             * });
             *
             * if (index > -1)
             *  return this[measure].Values[index];
             * else
             *  return null;
             */
        }
Exemplo n.º 3
0
        public Aggregation Query()
        {
            DateTime t = DateTime.Now;

            int         attrLen = Attributes.Length;
            int         ceiling = attrLen - 1;
            int         mLen    = Measures.Length;
            Aggregation agg     = new Aggregation(Attributes, Measures);

            IDataReader reader = null;

            try
            {
                int rowIndex = 0;
                reader = ExecuteReader(GetSql(null));
                while (reader.Read())
                {
                    AttributeMember[] tuple = new AttributeMember[attrLen];
                    for (int i = 0; i < attrLen; i++)
                    {
                        tuple[i] = (AttributeMember)Attributes[i].Levels[1].Members[reader[i]];
                    }
                    if (rowIndex > 0)
                    {
                        AddTotals(tuple, ref rowIndex);
                    }
                    agg.Tuples.Add(tuple);

                    int index = attrLen;
                    for (int i = 0; i < mLen; i++)
                    {
                        agg.Values[i].Values.Add(reader[index]);
                        index++;
                    }
                    rowIndex++;
                }
                AddTotals(new AttributeMember[attrLen], ref rowIndex);
                BuildIndexTree();

                agg.QueryTime = (DateTime.Now - t).TotalMilliseconds;
                return(agg);
            }
            finally
            {
                CloseReader(reader);
            }

            void AddTotals(AttributeMember[] tuple, ref int ri)
            {
                AttributeMember[] last = agg.Tuples.Last();
                for (int i = tuple.Length - 2; i >= 0; i--)
                {
                    if (tuple[i] != last[i])
                    {
                        AttributeMember[] total = (AttributeMember[])last.Clone();
                        for (int j = i + 1; j < attrLen; j++)
                        {
                            Attribute attr = last[j].Attribute;
                            //Member tm = new Member(attr.Dimension.AttributeAllMemberName);
                            //tm.IsTotal = true;
                            //tm.Attribute = attr;
                            total[j] = (AttributeMember)attr.AllMember;
                        }
                        agg.Tuples.Add(total);
                        agg.TotalIndices.Add(ri);

                        int index = attrLen;
                        for (int j = 0; j < mLen; j++)
                        {
                            agg.Values[j].Values.Add(0);
                            index++;
                        }
                        ri++;
                    }
                }
            }

            void BuildIndexTree()
            {
                IndexTreeNode[] currNodes = new IndexTreeNode[attrLen];
                for (int i = 0; i < agg.Tuples.Count; i++)
                {
                    bool changed = false;
                    for (int j = 0; j < attrLen; j++)
                    {
                        AttributeMember member = agg.Tuples[i][j];

                        // Transfers flat columns to IndexTree structure
                        // currNode == 1 => first pass
                        // currNode.Member != member => current member is different from the previous row
                        // changed => any previous columns is different from the previous row
                        IndexTreeNode currNode = currNodes[j];
                        if (currNode == null || currNode.Member != member || changed)
                        {
                            changed = true;
                            // Only leaf nodes hold row index
                            IndexTreeNode n = new IndexTreeNode(member);
                            if (j == 0)
                            {
                                agg.IndexTree.Nodes.Add(n);
                                if (attrLen == 1)
                                {
                                    n.Index = i;
                                }
                            }
                            else
                            {
                                currNodes[j - 1].Children.Add(n);
                                if (j == ceiling)
                                {
                                    n.Index = i;
                                }
                            }
                            currNodes[j] = n;
                        }
                    }
                }
            }
        }