Exemplo n.º 1
0
        public JoinSetEnumerator(Storage storage, PersistentEnumerator left, PersistentEnumerator right)

        {
            this.storage = storage;
            i1           = left;
            i2           = right;
        }
        public virtual int IndexOf(Key key)
        {
            PersistentEnumerator iterator = (PersistentEnumerator)GetEnumerator(null, key, IterationOrder.DescentOrder);
            int i;

            for (i = -1; iterator.MoveNext(); i++)
            {
                ;
            }
            return(i);
        }
        protected virtual IEnumerable <Item> sortResult(IEnumerable <Item> iterable)
        {
            PersistentEnumerator src = (PersistentEnumerator)iterable.GetEnumerator();
            OidEnumerable        dst = new OidEnumerable(db);

            while (src.MoveNext())
            {
                dst.add(src.CurrentOid);
            }
            dst.sort();
            return(dst);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Constructor of bitmap
        /// </summary>
        /// <param name="sto">storage of persistent object selected by this bitmap</param>
        /// <param name="i ">enumerator through persistent object which is used to initialize bitmap</param>
        public Bitmap(Storage sto, IEnumerator i)
        {
            storage = sto;
            n_bits  = sto.MaxOid;
            int[] bm = new int[(n_bits + 31) >> 5];
            PersistentEnumerator pi = (PersistentEnumerator)i;

            while (pi.MoveNext())
            {
                int oid = pi.CurrentOid;
                bm[oid >> 5] |= 1 << (oid & 31);
            }
            bitmap = bm;
        }
    static public void Main(string[] args)
    {
        Storage db = StorageFactory.Instance.CreateStorage();

        db.Open("testbitmap.dbs", pagePoolSize);

        City city = (City)db.Root;

        if (city == null)
        {
            city = new City();
#if USE_GENERICS
            city.byLocation = db.CreateSpatialIndexR2 <Restaurant>();
            city.byKitchen  = db.CreateFieldIndex <string, Restaurant>("kitchen", false, true, true);
            city.byAvgPrice = db.CreateFieldIndex <int, Restaurant>("avgPrice", false, true, true);
            city.byRating   = db.CreateFieldIndex <int, Restaurant>("rating", false, true, true);
#else
            city.byLocation = db.CreateSpatialIndexR2();
            city.byKitchen  = db.CreateFieldIndex(typeof(Restaurant), "kitchen", false, true, true);
            city.byAvgPrice = db.CreateFieldIndex(typeof(Restaurant), "avgPrice", false, true, true);
            city.byRating   = db.CreateFieldIndex(typeof(Restaurant), "rating", false, true, true);
#endif
            db.Root = city;
        }
        DateTime start = DateTime.Now;
        Random   rnd   = new Random(2013);
        for (int i = 0; i < nRecords; i++)
        {
            Restaurant rest = new Restaurant();
            rest.lat      = 55 + (float)rnd.NextDouble();
            rest.lng      = 37 + (float)rnd.NextDouble();
            rest.kitchen  = kitchens[rnd.Next(kitchens.Length)];
            rest.avgPrice = rnd.Next(1000);
            rest.rating   = rnd.Next(10);
            city.byLocation.Put(new RectangleR2(rest.lat, rest.lng, rest.lat, rest.lng), rest);
            city.byKitchen.Put(rest);
            city.byAvgPrice.Put(rest);
            city.byRating.Put(rest);
        }
        db.Commit();
        Console.WriteLine("Elapsed time for inserting " + nRecords + " records: " + (DateTime.Now - start));

        start = DateTime.Now;
        long total = 0;
        for (int i = 0; i < nSearches; i++)
        {
            double lat       = 55 + rnd.NextDouble();
            double lng       = 37 + rnd.NextDouble();
            String kitchen   = kitchens[rnd.Next(kitchens.Length)];
            int    minPrice  = rnd.Next(1000);
            int    maxPrice  = minPrice + rnd.Next(1000);
            int    minRating = rnd.Next(10);
            Bitmap bitmap    = db.CreateBitmap(city.byKitchen.GetEnumerator(kitchen, kitchen));
            bitmap.And(db.CreateBitmap(city.byAvgPrice.GetEnumerator(minPrice, maxPrice)));
            bitmap.And(db.CreateBitmap(city.byRating.GetEnumerator(new Key(minRating), null)));
            PersistentEnumerator enumerator = (PersistentEnumerator)city.byLocation.Neighbors(lat, lng).GetEnumerator();

            int nAlternatives = 0;
            while (enumerator.MoveNext())
            {
                int oid = enumerator.CurrentOid;
                if (bitmap.Contains(oid))
                {
                    Restaurant rest = (Restaurant)db.GetObjectByOID(oid);
                    total += 1;
                    if (++nAlternatives == 10)
                    {
                        break;
                    }
                }
            }
        }
        Console.WriteLine("Elapsed time for " + nSearches + " searches of " + total + " variants among " + nRecords + " records: "
                          + (DateTime.Now - start) + " milliseconds");
        db.Close();
    }
        protected IEnumerable <Item> evaluate(Predicate predicate, bool sortNeeded)
        {
            IEnumerable <Item> iterator = null;

            if (predicate is Predicate.Compare)
            {
                Predicate.Compare cmp = (Predicate.Compare)predicate;
                int id;
                if (!db.name2id.TryGetValue(cmp.name, out id))
                {
                    return(getEmptyResultSet());
                }
                Index  index = (Index)db.storage.GetObjectByOID(id);
                Object value = cmp.value;
                switch (cmp.oper)
                {
                case Predicate.Compare.Operation.Equals:
                {
                    Key key = createKey(value, true);
                    return(new Item.ItemEnumerable(index.Range(key, key)));
                }

                case Predicate.Compare.Operation.LessThan:
                    return(new Item.ItemEnumerable(index.Range(null, createKey(value, false))));

                case Predicate.Compare.Operation.LessOrEquals:
                    iterator = new Item.ItemEnumerable(index.Range(null, createKey(value, true)));
                    break;

                case Predicate.Compare.Operation.GreaterThan:
                    iterator = new Item.ItemEnumerable(index.Range(createKey(value, false), null));
                    break;

                case Predicate.Compare.Operation.GreaterOrEquals:
                    iterator = new Item.ItemEnumerable(index.Range(createKey(value, true), null));
                    break;

                case Predicate.Compare.Operation.StartsWith:
                    iterator = new Item.ItemEnumerable(index.StartsWith(((String)value).ToLower()));
                    break;

                case Predicate.Compare.Operation.IsPrefixOf:
                    iterator = new ArrayEnumerable(index.PrefixSearch(((String)value).ToLower()));
                    break;

                case Predicate.Compare.Operation.InArray:
                {
                    OidEnumerable dst = new OidEnumerable(db);
                    if (value is String[])
                    {
                        String[] arr = (String[])value;
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Key key = new Key(arr[i].ToLower());
                            PersistentEnumerator src = (PersistentEnumerator)index.GetEnumerator(key, key);
                            while (src.MoveNext())
                            {
                                dst.add(src.CurrentOid);
                            }
                        }
                    }
                    else if (value is double[])
                    {
                        double[] arr = (double[])value;
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Key key = new Key(arr[i]);
                            PersistentEnumerator src = (PersistentEnumerator)index.GetEnumerator(key, key);
                            while (src.MoveNext())
                            {
                                dst.add(src.CurrentOid);
                            }
                        }
                    }
                    else
                    {
                        Item[] arr = (Item[])value;
                        for (int i = 0; i < arr.Length; i++)
                        {
                            Key key = new Key(arr[i]);
                            PersistentEnumerator src = (PersistentEnumerator)index.GetEnumerator(key, key);
                            while (src.MoveNext())
                            {
                                dst.add(src.CurrentOid);
                            }
                        }
                    }
                    dst.uniq();
                    return(dst);
                }
                }
            }
            else if (predicate is Predicate.And)
            {
                Predicate.And and = (Predicate.And)predicate;
                return(new JoinEnumerable(db, evaluate(and.left, true), evaluate(and.right, true)));
            }
            else if (predicate is Predicate.Or)
            {
                Predicate.Or or = (Predicate.Or)predicate;
                return(new MergeEnumerable(db, evaluate(or.left, true), evaluate(or.right, true)));
            }
            else if (predicate is Predicate.Between)
            {
                Predicate.Between between = (Predicate.Between)predicate;
                int id;
                if (!db.name2id.TryGetValue(between.name, out id))
                {
                    return(getEmptyResultSet());
                }
                Index index = (Index)db.storage.GetObjectByOID(id);
                iterator = new Item.ItemEnumerable(index.Range(createKey(between.from, true), createKey(between.till, true)));
            }
            else if (predicate is Predicate.Match)
            {
                Predicate.Match match = (Predicate.Match)predicate;
                iterator = new FullTextSearchResultEnumerable(db.root.fullTextIndex.Search(match.query, db.language, match.maxResults, match.timeLimit));
            }
            else if (predicate is Predicate.In)
            {
                Predicate.In isIn = (Predicate.In)predicate;
                int          id;
                if (!db.name2id.TryGetValue(isIn.name, out id))
                {
                    return(getEmptyResultSet());
                }
                Index         index = (Index)db.storage.GetObjectByOID(id);
                OidEnumerable dst   = new OidEnumerable(db);
                foreach (Item item in evaluate(isIn.subquery, false))
                {
                    Key key = new Key(item);
                    PersistentEnumerator src = (PersistentEnumerator)index.GetEnumerator(key, key);
                    while (src.MoveNext())
                    {
                        dst.add(src.CurrentOid);
                    }
                }
                dst.uniq();
                return(dst);
            }
            else
            {
                return(null);
            }
            return(sortNeeded ? sortResult(iterator) : iterator);
        }