コード例 #1
0
ファイル: OrderBy.cs プロジェクト: ren85/linqdb
        Dictionary <int, int> OrderValuesByDescendingWithNegatives(OrderByInfo order_info, OperResult res_set, int?take, int?skip, bool all, ReadOptions ro)
        {
            var pos_res = OrderValuesByDescending(order_info, res_set, take, skip, all, ro);

            int?take_left = pos_res.Take == null ? null : (pos_res.Take - pos_res.Data.Count);

            if (take_left == null || take_left > 0)
            {
                order_info.ColumnNumber *= -1;
                var neg_res = OrderValues(order_info, res_set, take_left, pos_res.Skip, all, ro);
                order_info.ColumnNumber *= -1;
                var pos_count = pos_res.Data.Count();
                foreach (var pv in neg_res.Data)
                {
                    pos_res.Data[pv.Key] = pv.Value + pos_count;
                }
            }
            return(pos_res.Data);
        }
コード例 #2
0
ファイル: OrderBy.cs プロジェクト: ren85/linqdb
        Dictionary <int, int> OrderSomeValuesWithIndex(OrderByInfo order_info, OperResult res_set, int?take, int?skip, bool desc, Dictionary <long, byte[]> cache, ReadOptions ro, TableInfo table_info)
        {
            var skey   = MakeSnapshotKey(table_info.TableNumber, table_info.ColumnNumbers[order_info.ColumnName]);
            var snapid = leveld_db.Get(skey, null, ro);

            if (snapid == null)
            {
                return(null);
            }
            var snapshot_id = Encoding.UTF8.GetString(snapid);

            if (string.IsNullOrEmpty(table_info.Name) || string.IsNullOrEmpty(order_info.ColumnName))
            {
                throw new LinqDbException("Linqdb: bad indexes.");
            }
            if (!indexes.ContainsKey(table_info.Name + "|" + order_info.ColumnName + "|" + snapshot_id))
            {
                return(null);
            }

            var index = indexes[table_info.Name + "|" + order_info.ColumnName + "|" + snapshot_id];

            skey   = MakeSnapshotKey(table_info.TableNumber, table_info.ColumnNumbers["Id"]);
            snapid = leveld_db.Get(skey, null, ro);
            var id_snapshot_id = Encoding.UTF8.GetString(snapid);
            var ids_index      = indexes[table_info.Name + "|Id|" + id_snapshot_id];

            if (index.IndexType == IndexType.GroupOnly)
            {
                return(null);
            }


            Dictionary <int, int> res = new Dictionary <int, int>();
            var matched = MatchesFromIndex(res_set.ResIds, index, ids_index, table_info.Columns[order_info.ColumnName]);

            if (matched.Item3 != null)
            {
                res_set.ResIds = matched.Item3;
            }

            if (table_info.Columns[order_info.ColumnName] == LinqDbTypes.int_)
            {
                var int_vals = matched.Item1.OrderBy(f => f.Value).ToList();

                if (!desc)
                {
                    if (skip == null)
                    {
                        skip = 0;
                    }
                    int icount = 0;
                    for (int i = 0; i < int_vals.Count(); i++)
                    {
                        if (skip > 0)
                        {
                            skip--;
                            continue;
                        }
                        if (take == null || take > 0)
                        {
                            if (take > 0)
                            {
                                take--;
                            }
                            res[int_vals[i].Key] = icount;
                            icount++;
                        }
                        else if (take == 0)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (skip == null)
                    {
                        skip = 0;
                    }
                    int icount = 0;
                    for (int i = int_vals.Count() - 1; i >= 0; i--)
                    {
                        if (skip > 0)
                        {
                            skip--;
                            continue;
                        }
                        if (take == null || take > 0)
                        {
                            if (take > 0)
                            {
                                take--;
                            }
                            res[int_vals[i].Key] = icount;
                            icount++;
                        }
                        else if (take == 0)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                var double_vals = matched.Item2.OrderBy(f => f.Value).ToList();

                if (!desc)
                {
                    if (skip == null)
                    {
                        skip = 0;
                    }
                    int icount = 0;
                    for (int i = 0; i < double_vals.Count(); i++)
                    {
                        if (skip > 0)
                        {
                            skip--;
                            continue;
                        }
                        if (take == null || take > 0)
                        {
                            if (take > 0)
                            {
                                take--;
                            }
                            res[double_vals[i].Key] = icount;
                            icount++;
                        }
                        else if (take == 0)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    if (skip == null)
                    {
                        skip = 0;
                    }
                    int icount = 0;
                    for (int i = double_vals.Count() - 1; i >= 0; i--)
                    {
                        if (skip > 0)
                        {
                            skip--;
                            continue;
                        }
                        if (take == null || take > 0)
                        {
                            if (take > 0)
                            {
                                take--;
                            }
                            res[double_vals[i].Key] = icount;
                            icount++;
                        }
                        else if (take == 0)
                        {
                            break;
                        }
                    }
                }
            }

            return(res);
        }
コード例 #3
0
ファイル: OrderBy.cs プロジェクト: ren85/linqdb
        OrderedTmpResult OrderValuesByDescending(OrderByInfo order_info, OperResult res_set, int?take, int?skip, bool all, ReadOptions ro)
        {
            OrderedTmpResult res = new OrderedTmpResult();

            res.Data = new Dictionary <int, int>();
            res.Take = take;
            res.Skip = skip;
            var key = MakeIndexSearchKey(new IndexKeyInfo()
            {
                TableNumber  = order_info.TableNumber,
                ColumnNumber = order_info.ColumnNumber,
                ColumnType   = order_info.ColumnType,
                Val          = PostEverything
            });
            int count = 0;

            using (var it = leveld_db.NewIterator(null, ro))
            {
                it.Seek(key);
                if (!it.Valid())
                {
                    return(res);
                }
                var k    = it.Key();
                var info = GetIndexKey(k);
                if (info.NotKey || info.TableNumber != order_info.TableNumber || info.ColumnNumber != order_info.ColumnNumber)
                {
                    it.Prev();
                    if (!it.Valid())
                    {
                        return(res);
                    }
                    k = it.Key();
                    if (k == null)
                    {
                        return(res);
                    }
                    info = GetIndexKey(k);
                }
                var contains = new ContainsInfo()
                {
                    ids = res_set.ResIds
                };
                bool has = false;
                if (!info.NotKey && info.TableNumber == order_info.TableNumber && info.ColumnNumber == order_info.ColumnNumber)
                {
                    if (!all)
                    {
                        has = EfficientContains(contains, info.Id);
                        if (contains.is_sorted)
                        {
                            res_set.ResIds = contains.ids;
                        }
                    }
                    if (all || has)
                    {
                        if (res.Skip == null || res.Skip == 0)
                        {
                            res.Data[info.Id] = count;
                            count++;
                            if (res.Take != null && count == res.Take)
                            {
                                return(res);
                            }
                        }
                        else
                        {
                            res.Skip--;
                        }
                    }
                }
                else
                {
                    return(res);
                }
                while (true)
                {
                    it.Prev();
                    if (!it.Valid())
                    {
                        return(res);
                    }

                    k = it.Key();
                    if (k == null)
                    {
                        return(res);
                    }
                    info = GetIndexKey(k);
                    if (!info.NotKey && info.TableNumber == order_info.TableNumber && info.ColumnNumber == order_info.ColumnNumber)
                    {
                        if (!all)
                        {
                            has = EfficientContains(contains, info.Id);
                        }
                        if (all || has)
                        {
                            if (res.Skip == null || res.Skip == 0)
                            {
                                res.Data[info.Id] = count;
                                count++;
                                if (res.Take != null && count == res.Take)
                                {
                                    return(res);
                                }
                            }
                            else
                            {
                                res.Skip--;
                            }
                        }
                    }
                    else
                    {
                        return(res);
                    }
                }
            }
        }
コード例 #4
0
ファイル: OrderBy.cs プロジェクト: ren85/linqdb
        Dictionary <int, int> OrderSomeValues(OrderByInfo order_info, OperResult res_set, int?take, int?skip, bool desc, Dictionary <long, byte[]> cache, ReadOptions ro, TableInfo table_info)
        {
            if (order_info.ColumnName != "Id")
            {
                var resindex = OrderSomeValuesWithIndex(order_info, res_set, take, skip, desc, cache, ro, table_info);
                if (resindex != null)
                {
                    return(resindex);
                }
            }
            else
            {
                return(OrderSomeValuesById(order_info, res_set, take, skip, desc, cache, ro, table_info));
            }

            Dictionary <int, int> res = new Dictionary <int, int>();
            List <KeyValuePair <object, int> > tmp_res = new List <KeyValuePair <object, int> >(res_set.ResIds.Count());

            foreach (var r in res_set.ResIds)
            {
                if (order_info.ColumnName == "Id")
                {
                    tmp_res.Add(new KeyValuePair <object, int>(r, r));
                }
                else
                {
                    byte[] val = null;
                    //if (!GetFromCache(order_info.ColumnNumber, r, cache, out val))
                    //{
                    if (order_info.ColumnType == LinqDbTypes.string_)
                    {
                        var key = MakeStringValueKey(new IndexKeyInfo()
                        {
                            TableNumber  = order_info.TableNumber,
                            ColumnNumber = order_info.ColumnNumber,
                            Id           = r
                        });
                        val = leveld_db.Get(key, null, ro);
                    }
                    else
                    {
                        var key = MakeValueKey(new IndexKeyInfo()
                        {
                            TableNumber  = order_info.TableNumber,
                            ColumnNumber = order_info.ColumnNumber,
                            Id           = r
                        });
                        val = leveld_db.Get(key, null, ro);
                    }
                    //}

                    if (order_info.ColumnType != LinqDbTypes.string_ && ValsEqual(val, NullConstant) ||
                        order_info.ColumnType == LinqDbTypes.string_ && ValsEqual(val, BinaryOrStringValuePrefix))
                    {
                        tmp_res.Add(new KeyValuePair <object, int>(null, r));
                    }
                    else
                    {
                        if (order_info.ColumnType == LinqDbTypes.int_)
                        {
                            tmp_res.Add(new KeyValuePair <object, int>(BitConverter.ToInt32(val.MyReverseWithCopy(), 0), r));
                        }
                        else if (order_info.ColumnType == LinqDbTypes.double_ || order_info.ColumnType == LinqDbTypes.DateTime_)
                        {
                            tmp_res.Add(new KeyValuePair <object, int>(BitConverter.ToDouble(val.MyReverseWithCopy(), 0), r));
                        }
                        else if (order_info.ColumnType == LinqDbTypes.string_)
                        {
                            tmp_res.Add(new KeyValuePair <object, int>(Encoding.Unicode.GetString(val), r));
                        }
                    }
                }
            }

            List <KeyValuePair <object, int> > t = null;

            if (!desc)
            {
                t = tmp_res.OrderBy(f => f.Key).ToList();
            }
            else
            {
                t = tmp_res.OrderByDescending(f => f.Key).ToList();
            }
            if (skip != null)
            {
                t = t.Skip((int)skip).ToList();
            }
            if (take != null)
            {
                t = t.Take((int)take).ToList();
            }

            int count = 0;

            foreach (var v in t)
            {
                res[v.Value] = count;
                count++;
            }
            return(res);
        }
コード例 #5
0
ファイル: OrderBy.cs プロジェクト: ren85/linqdb
        Dictionary <int, int> OrderSomeValuesById(OrderByInfo order_info, OperResult res_set, int?take, int?skip, bool desc, Dictionary <long, byte[]> cache, ReadOptions ro, TableInfo table_info)
        {
            Dictionary <int, int> res = new Dictionary <int, int>();
            var ids = res_set.ResIds;

            if (!IsSorted(ids))
            {
                ids = ids.OrderBy(f => f).ToList();
            }

            if (!desc)
            {
                if (skip == null)
                {
                    skip = 0;
                }
                int count = 0;
                for (int i = 0; i < ids.Count(); i++)
                {
                    if (skip > 0)
                    {
                        skip--;
                        continue;
                    }
                    if (take == null || take > 0)
                    {
                        if (take > 0)
                        {
                            take--;
                        }
                        res[ids[i]] = count;
                        count++;
                    }
                    else if (take == 0)
                    {
                        break;
                    }
                }
            }
            else
            {
                if (skip == null)
                {
                    skip = 0;
                }
                int count = 0;
                for (int i = ids.Count() - 1; i >= 0; i--)
                {
                    if (skip > 0)
                    {
                        skip--;
                        continue;
                    }
                    if (take == null || take > 0)
                    {
                        if (take > 0)
                        {
                            take--;
                        }
                        res[ids[i]] = count;
                        count++;
                    }
                    else if (take == 0)
                    {
                        break;
                    }
                }
            }
            return(res);
        }