Beispiel #1
0
        private static void PopulateResult(
            string fieldName, IFieldBridge fieldBridge, Attributes.Store fieldStore,
            string[] fields, object[] result, Document document
            )
        {
            int matchingPosition = GetFieldPosition(fields, fieldName);

            if (matchingPosition != -1)
            {
                //TODO make use of an isTwoWay() method
                if (fieldStore != Attributes.Store.No && fieldBridge is ITwoWayFieldBridge)
                {
                    result[matchingPosition] = ((ITwoWayFieldBridge)fieldBridge).Get(fieldName, document);
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info("Field " + fieldName + " projected as " + result[matchingPosition]);
                    }
                }
                else
                {
                    if (fieldStore == Attributes.Store.No)
                    {
                        throw new SearchException("Projecting an unstored field: " + fieldName);
                    }

                    throw new SearchException("IFieldBridge is not a ITwoWayFieldBridge: " + fieldBridge.GetType());
                }
            }
        }
        public FieldMapping(string name, IFieldBridge bridge, IGetter getter) : base(getter)
        {
            this.Name = name;
            this.Bridge = bridge;

            this.Store = Attributes.Store.No;
            this.Index = Attributes.Index.Tokenized;
        }
        public ClassBridgeMapping(string name, IFieldBridge bridge)
        {
            this.Name   = name;
            this.Bridge = bridge;

            this.Store = Attributes.Store.No;
            this.Index = Attributes.Index.Tokenized;
        }
        public FieldMapping(string name, IFieldBridge bridge, IGetter getter) : base(getter)
        {
            this.Name   = name;
            this.Bridge = bridge;

            this.Store = Attributes.Store.No;
            this.Index = Attributes.Index.Tokenized;
        }
        public ClassBridgeMapping(string name, IFieldBridge bridge)
        {
            this.Name = name;
            this.Bridge = bridge;

            this.Store = Attributes.Store.No;
            this.Index = Attributes.Index.Tokenized;
        }
        public static IFieldBridge ExtractType(IClassBridgeDefinition cb)
        {
            IFieldBridge bridge = null;

            if (cb != null)
            {
                System.Type impl = cb.Impl;

                if (impl != null)
                {
                    try
                    {
                        object instance = Activator.CreateInstance(impl);
                        if (instance is IFieldBridge)
                        {
                            bridge = (IFieldBridge)instance;
                        }

                        if (cb.Parameters.Count > 0 && instance is IParameterizedBridge)
                        {
                            // Already converted the parameters by this stage
                            ((IParameterizedBridge)instance).SetParameterValues(cb.Parameters);
                        }
                    }
                    catch (Exception e)
                    {
                        // TODO add classname
                        throw new HibernateException("Unable to instantiate IFieldBridge for " + cb.Name, e);
                    }
                }
            }
            // TODO add classname
            if (bridge == null)
            {
                throw new HibernateException("Unable to guess IFieldBridge ");
            }

            return(bridge);
        }
Beispiel #7
0
        private void BuildProperty(
            DocumentMapping documentMapping, MemberInfo member, Analyzer parentAnalyzer,
            bool isRoot, string path, BuildContext context
            )
        {
            IFieldBridge bridge = null;

            var analyzer = GetAnalyzer(member) ?? parentAnalyzer;
            var boost    = GetBoost(member);

            var getter = GetGetterFast(documentMapping.MappedClass, member);

            var documentIdAttribute = AttributeUtil.GetAttribute <DocumentIdAttribute>(member);

            if (documentIdAttribute != null)
            {
                string documentIdName = documentIdAttribute.Name ?? member.Name;
                bridge = GetFieldBridge(member);

                if (isRoot)
                {
                    if (!(bridge is ITwoWayFieldBridge))
                    {
                        throw new SearchException("Bridge for document id does not implement TwoWayFieldBridge: " + member.Name);
                    }

                    documentMapping.DocumentId = new DocumentIdMapping(
                        documentIdName, member.Name, (ITwoWayFieldBridge)bridge, getter
                        )
                    {
                        Boost = boost
                    };
                }
                else
                {
                    // Components should index their document id
                    documentMapping.Fields.Add(new FieldMapping(
                                                   GetAttributeName(member, documentIdName),
                                                   bridge, getter
                                                   )
                    {
                        Store = Attributes.Store.Yes,
                        Index = Attributes.Index.UnTokenized,
                        Boost = boost
                    });
                }
            }

            var fieldAttributes = AttributeUtil.GetFields(member);

            if (fieldAttributes.Length > 0)
            {
                if (bridge == null)
                {
                    bridge = GetFieldBridge(member);
                }

                foreach (var fieldAttribute in fieldAttributes)
                {
                    var fieldAnalyzer = GetAnalyzerByType(fieldAttribute.Analyzer) ?? analyzer;
                    var field         = new FieldMapping(
                        GetAttributeName(member, fieldAttribute.Name),
                        bridge, getter
                        )
                    {
                        Store    = fieldAttribute.Store,
                        Index    = fieldAttribute.Index,
                        Analyzer = fieldAnalyzer
                    };

                    documentMapping.Fields.Add(field);
                }
            }

            var embeddedAttribute = AttributeUtil.GetAttribute <IndexedEmbeddedAttribute>(member);

            if (embeddedAttribute != null)
            {
                int oldMaxLevel    = maxLevel;
                int potentialLevel = embeddedAttribute.Depth + level;
                if (potentialLevel < 0)
                {
                    potentialLevel = int.MaxValue;
                }

                maxLevel = potentialLevel > maxLevel ? maxLevel : potentialLevel;
                level++;

                System.Type elementType = embeddedAttribute.TargetElement ?? GetMemberTypeOrGenericArguments(member);

                var localPrefix = embeddedAttribute.Prefix == "." ? member.Name + "." : embeddedAttribute.Prefix;

                if (maxLevel == int.MaxValue && context.Processed.Contains(elementType))
                {
                    throw new SearchException(
                              string.Format(
                                  "Circular reference, Duplicate use of {0} in root entity {1}#{2}",
                                  elementType.FullName,
                                  context.Root.MappedClass.FullName,
                                  path + localPrefix));
                }


                if (level <= maxLevel)
                {
                    context.Processed.Add(elementType); // push
                    var embedded = new EmbeddedMapping(new DocumentMapping(elementType)
                    {
                        Boost    = GetBoost(member),
                        Analyzer = GetAnalyzer(member) ?? parentAnalyzer
                    }, getter)
                    {
                        Prefix = localPrefix
                    };

                    BuildClass(embedded.Class, false, path + localPrefix, context);

                    /**
                     * We will only index the "expected" type but that's OK, HQL cannot do downcasting either
                     */
                    // ayende: because we have to deal with generic collections here, we aren't
                    // actually using the element type to determine what the value is, since that
                    // was resolved to the element type of the possible collection
                    Type actualFieldType = GetMemberTypeOrGenericCollectionType(member);
                    embedded.IsCollection = typeof(IEnumerable).IsAssignableFrom(actualFieldType);

                    documentMapping.Embedded.Add(embedded);
                    context.Processed.Remove(actualFieldType); // pop
                }
                else if (logger.IsDebugEnabled)
                {
                    logger.Debug("Depth reached, ignoring " + path + localPrefix);
                }

                level--;
                maxLevel = oldMaxLevel; // set back the old max level
            }

            if (AttributeUtil.HasAttribute <ContainedInAttribute>(member))
            {
                documentMapping.ContainedIn.Add(new ContainedInMapping(getter));
            }
        }
Beispiel #8
0
        private static void PopulateResult(
            string fieldName, IFieldBridge fieldBridge, Attributes.Store fieldStore,
            string[] fields, object[] result, Document document
        )
        {
            int matchingPosition = GetFieldPosition(fields, fieldName);
            if (matchingPosition != -1)
            {
                //TODO make use of an isTwoWay() method
                if (fieldStore != Attributes.Store.No && fieldBridge is ITwoWayFieldBridge)
                {
                    result[matchingPosition] = ((ITwoWayFieldBridge)fieldBridge).Get(fieldName, document);
                    if (logger.IsInfoEnabled)
                    {
                        logger.Info("Field " + fieldName + " projected as " + result[matchingPosition]);
                    }
                }
                else
                {
                    if (fieldStore == Attributes.Store.No)
                    {
                        throw new SearchException("Projecting an unstored field: " + fieldName);
                    }

                    throw new SearchException("IFieldBridge is not a ITwoWayFieldBridge: " + fieldBridge.GetType());
                }
            }
        }
        public static IFieldBridge GuessType(
            string fieldName,
            System.Type fieldType,
            IFieldBridgeDefinition fieldBridgeDefinition,
            IDateBridgeDefinition dateBridgeDefinition
            )
        {
            IFieldBridge bridge = null;

            if (fieldBridgeDefinition != null)
            {
                System.Type impl = fieldBridgeDefinition.Impl;
                try
                {
                    object instance = Activator.CreateInstance(impl);
                    if (instance is IFieldBridge)
                    {
                        bridge = (IFieldBridge)instance;
                    }
                    else if (instance is ITwoWayStringBridge)
                    {
                        bridge = new TwoWayString2FieldBridgeAdaptor((ITwoWayStringBridge)instance);
                    }
                    else if (instance is IStringBridge)
                    {
                        bridge = new String2FieldBridgeAdaptor((IStringBridge)instance);
                    }

                    if (fieldBridgeDefinition.Parameters.Count > 0 && instance is IParameterizedBridge)
                    {
                        ((IParameterizedBridge)instance).SetParameterValues(fieldBridgeDefinition.Parameters);
                    }
                }
                catch (Exception e)
                {
                    // TODO add classname
                    throw new HibernateException("Unable to instantiate IFieldBridge for " + fieldName, e);
                }
            }
            else if (dateBridgeDefinition != null)
            {
                bridge = GetDateField(dateBridgeDefinition.Resolution);
            }
            else
            {
                // find in built-ins
                System.Type returnType = fieldType;
                if (IsNullable(returnType))
                {
                    returnType = returnType.GetGenericArguments()[0];
                }

                builtInBridges.TryGetValue(returnType.Name, out bridge);
                if (bridge == null && returnType.IsEnum)
                {
                    bridge = new TwoWayString2FieldBridgeAdaptor(new EnumBridge(returnType));
                }
            }

            // TODO add classname
            if (bridge == null)
            {
                throw new HibernateException("Unable to guess IFieldBridge for " + fieldName);
            }

            return(bridge);
        }