bool EfficientContains(ContainsInfo contains, int check_id) { contains.is_sorted = false; int bloom_size = 3000000; if (!contains.small && contains.bloom_filter == null && contains.check_list == null) { int max = Int32.MinValue; bool is_sorted = true; int curr = Int32.MinValue; var ids = contains.ids; for (int i = 0; i < ids.Count(); i++) { if (max < ids[i]) { max = ids[i]; } if (is_sorted && curr > ids[i]) { is_sorted = false; } curr = ids[i]; } contains.max = max; if (ids.Count() > 100000) { contains.small = false; int max_size = 300000000; if (max < max_size) { List <bool> check_list = new List <bool>(max + 1); contains.check_list = check_list; for (int i = 0; i < max + 1; i++) { check_list.Add(false); } foreach (var id in ids) { check_list[id] = true; } } else { List <bool> bloom_filter = new List <bool>(bloom_size); contains.bloom_filter = bloom_filter; for (int i = 0; i < bloom_size; i++) { bloom_filter.Add(false); } foreach (var id in ids) { bloom_filter[id % bloom_size] = true; } if (!is_sorted) { contains.ids = ids.OrderBy(f => f).ToList(); contains.is_sorted = true; } } } else { contains.small = true; if (!is_sorted) { contains.ids = ids.OrderBy(f => f).ToList(); contains.is_sorted = true; } } } if (check_id > contains.max) { return(false); } if (contains.small) { return(contains.ids.BinarySearch(check_id) >= 0); } if (contains.check_list != null) { return(contains.check_list[check_id]); } else { if (contains.bloom_filter[check_id % bloom_size] && contains.ids.BinarySearch(check_id) >= 0) { return(true); } else { return(false); } } }
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); } } } }