Beispiel #1
0
 public void LookupCompareFields(int bTreeDatabaseNumber)
 {
     using (SessionNoServer session = new SessionNoServer(systemDir))
     {
         Person personPrior = null, person;
         session.BeginRead();
         BTreeSet <Person>         bTree = (BTreeSet <Person>)session.Open(Oid.Encode((uint)bTreeDatabaseNumber, 1, 1));
         BTreeSetIterator <Person> itr   = bTree.Iterator();
         int ct = 0;
         while ((person = itr.Next()) != null)
         {
             if (personPrior != null)
             {
                 Assert.LessOrEqual(personPrior.FirstName, person.FirstName);
             }
             ct++;
             personPrior = person;
         }
         int ct2 = 0;
         personPrior = null;
         foreach (Person pers in (IEnumerable <Person>)bTree)
         {
             if (personPrior != null)
             {
                 Assert.LessOrEqual(personPrior.FirstName, pers.FirstName);
             }
             ct2++;
             personPrior = pers;
         }
         session.Commit();
         Assert.AreEqual(ct, ct2);
     }
 }
        static IEnumerable <Key> Equal <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp)
#if WINDOWS_PHONE
            where Key : new()
#endif
        {
            SessionBase          session   = sourceCollection.Session;
            CompareByField <Key> comparer  = sourceCollection.Comparer as CompareByField <Key>;
            Expression           leftSide  = binExp.Left;
            Expression           rightSide = binExp.Right;
            object rightValue = GetRightValue(leftSide, rightSide);

            if (leftSide.NodeType == ExpressionType.Parameter)
            {
                Key key = (Key)rightValue;
                if (key != null)
                {
                    BTreeSetIterator <Key> itr = sourceCollection.Iterator();
                    itr.GoTo(key);
                    while (sourceCollection.Comparer.Compare(itr.Current(), key) == 0)
                    {
                        yield return(itr.Next());
                    }
                }
            }
            else
            {
                if (comparer != null)
                {
                    MemberExpression returnedEx = null;
#if WINDOWS_PHONE || WINDOWS_UWP
                    Key key = (Key)Activator.CreateInstance(typeof(Key));
#else
                    Key key = (Key)FormatterServices.GetUninitializedObject(typeof(Key));
#endif
                    if (comparer.FieldsToCompare == null)
                    {
                        comparer.SetupFieldsToCompare();
                    }
                    DataMember dataMember = comparer.FieldsToCompare[0];
                    if (rightValue != null && HasIndexablePropertyOnLeft <Key>(leftSide, sourceCollection, dataMember, out returnedEx))
                    {
                        MemberExpression propExp  = (MemberExpression)returnedEx;
                        MemberInfo       property = propExp.Member;
                        dataMember.SetMemberValueWithPossibleConvert(key, rightValue);
                        BTreeSetIterator <Key> itr = sourceCollection.Iterator();
                        itr.GoTo(key);
                        Key current = itr.Current();
                        while (current != null && comparer.CompareField(dataMember, key, current, 0) == 0)
                        {
                            yield return(current);

                            current = itr.Next();
                        }
                    }
                }
            }
        }
Beispiel #3
0
        static void createDocumentInvertedIndex(SessionBase session, Database db, BTreeSet <Document> documentSet)
        {
            UInt32    dbNum            = db.DatabaseNumber;
            Document  doc              = null;
            Document  inputDoc         = new Document(db.Id);
            Placement wordPlacement    = new Placement(inputDoc.DatabaseNumber, 20000, 1, 25000, 65000, true, false, 1, false);
            Placement wordHitPlacement = new Placement(inputDoc.DatabaseNumber, 40000, 1, 25000, 65500, true, false, 1, false);
            //session.SetTraceDbActivity(db.DatabaseNumber);
            BTreeSetIterator <Document> iterator = documentSet.Iterator();

            iterator.GoTo(inputDoc);
            inputDoc = iterator.Current();
            while (inputDoc != null && inputDoc.Page.Database.DatabaseNumber == dbNum)
            {
                doc = (Document)session.Open(inputDoc.Page.Database, inputDoc.Id); // if matching database is availeble, use it to speed up lookup
                DocumentText    docText    = doc.Content;
                string          text       = docText.Text.ToLower();
                MatchCollection tagMatches = Regex.Matches(text, "[a-z][a-z.$]+");
                UInt64          wordCt     = 0;
                WordHit         wordHit;
                Word            word;
                if (++s_docCountIndexed % 50000 == 0)
                {
                    Console.WriteLine(DateTime.Now.ToString() + ", done indexing article: " + s_docCountIndexed);
                }
                BTreeSetOidShort <Word> wordSet = doc.WordSet;
                foreach (Match m in tagMatches)
                {
                    word = new Word(m.Value);
                    if (wordSet.TryGetKey(word, ref word))
                    {
                        //wordHit = doc.WordHit[word]; // to costly to add tight now - figure out a better way ?
                        //wordHit.Add(wordCt);
                    }
                    else
                    {
                        word = new Word(m.Value);
                        word.Persist(wordPlacement, session);
                        wordSet.Add(word);
                        wordHit = new WordHit(doc, wordCt++, session);
                        //wordHit.Persist(wordHitPlacement, session);
                        doc.WordHit.ValuePlacement = wordHitPlacement;
                        doc.WordHit.AddFast(word, wordHit);
                    }
                }
                inputDoc = iterator.Next();
            }
            session.FlushUpdates(db);
            session.ClearCachedObjects(db); // free up memory for objects we no longer need to have cached
            Console.WriteLine(DateTime.Now.ToString() + ", done indexing article: " + s_docCountIndexed + " Database: " + dbNum + " is completed.");
        }
        static IEnumerable <Key> And <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp)
#if WINDOWS_PHONE
            where Key : new()
#endif
        {
            CompareByField <Key> comparer = sourceCollection.Comparer as CompareByField <Key>;
            int indexNumberOfFields       = comparer.FieldsToCompare.Length;

#if WINDOWS_PHONE || WINDOWS_UWP
            Key key = (Key)Activator.CreateInstance(typeof(Key));
#else
            Key key = (Key)FormatterServices.GetUninitializedObject(typeof(Key));
#endif
            while (binExp.NodeType == ExpressionType.AndAlso)
            {
                BinaryExpression leftExpr   = (BinaryExpression)binExp.Left;
                BinaryExpression rightExpr  = (BinaryExpression)binExp.Right;
                DataMember       dataMember = comparer.FieldsToCompare[--indexNumberOfFields];
                object           rightValue = GetRightValue(rightExpr.Left, rightExpr.Right);
                dataMember.SetMemberValueWithPossibleConvert(key, rightValue);
                binExp = leftExpr;
                if (binExp.NodeType == ExpressionType.Equal)
                {
                    dataMember = comparer.FieldsToCompare[--indexNumberOfFields];
                    rightValue = GetRightValue(binExp.Left, binExp.Right);
                    dataMember.SetMemberValueWithPossibleConvert(key, rightValue);
                }
            }
            BTreeSetIterator <Key> itr = sourceCollection.Iterator();
            itr.GoTo(key);
            Key current = itr.Current();
            while (current != null && comparer.Compare(key, current) == 0)
            {
                yield return(current);

                current = itr.Next();
            }
        }
        static IEnumerable <Key> LessThanOrEqual <Key>(BTreeBase <Key, Key> sourceCollection, BinaryExpression binExp)
#if WINDOWS_PHONE
            where Key : new()
#endif
        {
            SessionBase          session  = sourceCollection.Session;
            CompareByField <Key> comparer = sourceCollection.Comparer as CompareByField <Key>;

            Expression leftSide   = binExp.Left;
            Expression rightSide  = binExp.Right;
            object     rightValue = GetRightValue(leftSide, rightSide);

            if (leftSide.NodeType == ExpressionType.Parameter)
            {
                Key key = (Key)rightValue;
                if (key != null)
                {
                    BTreeSetIterator <Key> itr = sourceCollection.Iterator();
                    itr.GoTo(key);
                    while (itr.Current() != null)
                    {
                        yield return(itr.Current());

                        itr.Next();
                    }
                }
            }
            else
            {
                if (comparer != null)
                {
                    //if we were able to create a hash from the right side (likely)
                    MemberExpression returnedEx = null;
#if WINDOWS_PHONE || WINDOWS_UWP
                    Key key = (Key)Activator.CreateInstance(typeof(Key));
#else
                    Key key = (Key)FormatterServices.GetUninitializedObject(typeof(Key));
#endif
                    if (comparer.FieldsToCompare == null)
                    {
                        comparer.SetupFieldsToCompare();
                    }
                    DataMember dataMember = comparer.FieldsToCompare[0];
                    if (rightValue != null && HasIndexablePropertyOnLeft <Key>(leftSide, sourceCollection, dataMember, out returnedEx))
                    {
                        //cast to MemberExpression - it allows us to get the property
                        MemberExpression propExp  = (MemberExpression)returnedEx;
                        MemberInfo       property = propExp.Member;
                        foreach (DataMember member in comparer.FieldsToCompare.Skip(1))
                        {
                            if (member.GetTypeCode == TypeCode.String)
                            {
                                member.SetMemberValue(key, "");
                            }
                        }
                        dataMember.SetMemberValueWithPossibleConvert(key, rightValue);
                        BTreeSetIterator <Key> itr = sourceCollection.Iterator();
                        itr.GoTo(key);
                        while (itr.Current() != null)
                        {
                            yield return(itr.Current());

                            itr.Previous();
                        }
                    }
                    else if (leftSide.NodeType == ExpressionType.Call)
                    {
                        // don't know yet how to handle TODO

                        /*MethodCallExpression expression = leftSide as MethodCallExpression;
                         * Trace.Out.WriteLine("Method: " + expression.Method.Name);
                         * Trace.Out.WriteLine("Args: ");
                         * foreach (var exp in expression.Arguments)
                         * sourceCollection where */
                    }
                }
            }
        }