Example #1
0
        uint SelectSecondaryKeyIndex(SecondaryKeyInfo info, RelationVersionInfo prevVersion)
        {
            uint index = 0;

            if (prevVersion != null)
            {
                if (prevVersion._secondaryKeysNames.TryGetValue(info.Name, out index))
                {
                    var prevFields = prevVersion.GetSecondaryKeyFields(index);
                    var currFields = GetSecondaryKeyFields(info);
                    if (SecondaryIndexHasSameDefinition(currFields, prevFields))
                    {
                        return(index); //existing
                    }
                }
                while (prevVersion._secondaryKeys.ContainsKey(index) || _secondaryKeys.ContainsKey(index))
                {
                    index++;
                }
            }
            else
            {
                while (_secondaryKeys.ContainsKey(index))
                {
                    index++;
                }
            }
            return(index); //use fresh one
        }
Example #2
0
        void CreateSecondaryKeyInfo(List <Tuple <int, IList <SecondaryKeyAttribute> > > attributes,
                                    Dictionary <uint, TableFieldInfo> primaryKeyFields,
                                    RelationVersionInfo prevVersion)
        {
            _secondaryKeys      = new Dictionary <uint, SecondaryKeyInfo>();
            _secondaryKeysNames = new Dictionary <string, uint>();
            var skIndexNames = attributes.SelectMany(t => t.Item2).Select(a => a.Name).Distinct();

            foreach (var indexName in skIndexNames)
            {
                var indexFields = new List <Tuple <int, SecondaryKeyAttribute> >(); //fieldIndex, attribute
                foreach (var kv in attributes)
                {
                    var attr = kv.Item2.FirstOrDefault(a => a.Name == indexName);
                    if (attr == null)
                    {
                        continue;
                    }
                    indexFields.Add(Tuple.Create(kv.Item1, attr));
                }
                var orderedAttrs = indexFields.OrderBy(a => a.Item2.Order).ToList();
                var info         = new SecondaryKeyInfo
                {
                    Name   = indexName,
                    Fields = new List <FieldId>()
                };
                var usedPKFields = new Dictionary <uint, object>();
                foreach (var attr in orderedAttrs)
                {
                    for (uint i = 1; i <= attr.Item2.IncludePrimaryKeyOrder; i++)
                    {
                        usedPKFields.Add(i, null);
                        var pi = _primaryKeyFields.IndexOf(primaryKeyFields[i]);
                        info.Fields.Add(new FieldId(true, (uint)pi));
                    }
                    if (attr.Item1 < 0)
                    {
                        var pkOrder = (uint)-attr.Item1;
                        usedPKFields.Add(pkOrder, null);
                        var pi = _primaryKeyFields.IndexOf(primaryKeyFields[pkOrder]);
                        info.Fields.Add(new FieldId(true, (uint)pi));
                    }
                    else
                    {
                        info.Fields.Add(new FieldId(false, (uint)attr.Item1));
                    }
                }
                //fill all not present parts of primary key
                foreach (var pk in primaryKeyFields)
                {
                    if (!usedPKFields.ContainsKey(pk.Key))
                    {
                        info.Fields.Add(new FieldId(true, (uint)_primaryKeyFields.IndexOf(primaryKeyFields[pk.Key])));
                    }
                }
                var skIndex = SelectSecondaryKeyIndex(info, prevVersion);
                _secondaryKeysNames[indexName] = skIndex;
                _secondaryKeys[skIndex]        = info;
            }
        }
        uint SelectSecondaryKeyIndex(SecondaryKeyInfo info)
        {
            var index = 0u;

            while (_secondaryKeys.ContainsKey(index))
            {
                index++;
            }
            return(index); //use fresh one
        }
Example #4
0
        IReadOnlyList <TableFieldInfo> GetSecondaryKeyFields(SecondaryKeyInfo info)
        {
            var fields = new List <TableFieldInfo>();

            foreach (var field in info.Fields)
            {
                fields.Add(field.IsFromPrimaryKey
                    ? _primaryKeyFields[(int)field.Index]
                    : _secondaryKeyFields[(int)field.Index]);
            }
            return(fields);
        }
Example #5
0
        public static RelationVersionInfo Load(AbstractBufferedReader reader, IFieldHandlerFactory fieldHandlerFactory, string relationName)
        {
            var pkCount     = reader.ReadVUInt32();
            var primaryKeys = new List <TableFieldInfo>((int)pkCount);

            for (var i = 0u; i < pkCount; i++)
            {
                primaryKeys.Add(TableFieldInfo.Load(reader, fieldHandlerFactory, relationName, FieldHandlerOptions.Orderable));
            }
            var skFieldCount       = reader.ReadVUInt32();
            var secondaryKeyFields = new TableFieldInfo[skFieldCount];

            for (var i = 0; i < skFieldCount; i++)
            {
                secondaryKeyFields[i] = TableFieldInfo.Load(reader, fieldHandlerFactory, relationName,
                                                            FieldHandlerOptions.Orderable);
            }
            var skCount            = reader.ReadVUInt32();
            var secondaryKeys      = new Dictionary <uint, SecondaryKeyInfo>((int)skCount);
            var secondaryKeysNames = new Dictionary <string, uint>((int)skCount);

            for (var i = 0; i < skCount; i++)
            {
                var skIndex = reader.ReadVUInt32();
                var info    = new SecondaryKeyInfo();
                info.Index = reader.ReadVUInt32();
                info.Name  = reader.ReadString();
                var cnt = reader.ReadVUInt32();
                info.Fields = new List <FieldId>((int)cnt);
                for (var j = 0; j < cnt; j++)
                {
                    var fromPrimary = reader.ReadBool();
                    var index       = reader.ReadVUInt32();
                    info.Fields.Add(new FieldId(fromPrimary, index));
                }
                secondaryKeys.Add(skIndex, info);
                secondaryKeysNames.Add(info.Name, skIndex);
            }

            var fieldCount = reader.ReadVUInt32();
            var fieldInfos = new TableFieldInfo[fieldCount];

            for (int i = 0; i < fieldCount; i++)
            {
                fieldInfos[i] = TableFieldInfo.Load(reader, fieldHandlerFactory, relationName, FieldHandlerOptions.None);
            }

            return(new RelationVersionInfo(primaryKeys, secondaryKeys, secondaryKeysNames, fieldInfos));
        }
Example #6
0
 internal static bool Equal(RelationVersionInfo a, RelationVersionInfo b)
 {
     //PKs
     if (a._primaryKeyFields.Count != b._primaryKeyFields.Count)
     {
         return(false);
     }
     for (int i = 0; i < a._primaryKeyFields.Count; i++)
     {
         if (!TableFieldInfo.Equal(a._primaryKeyFields[i], b._primaryKeyFields[i]))
         {
             return(false);
         }
     }
     //SKs
     if (a._secondaryKeys.Count != b._secondaryKeys.Count)
     {
         return(false);
     }
     foreach (var key in a._secondaryKeys)
     {
         SecondaryKeyInfo bInfo;
         if (!b._secondaryKeys.TryGetValue(key.Key, out bInfo))
         {
             return(false);
         }
         if (!SecondaryKeyInfo.Equal(key.Value, bInfo))
         {
             return(false);
         }
     }
     //Fields
     if (a._fields.Length != b._fields.Length)
     {
         return(false);
     }
     for (int i = 0; i < a._fields.Length; i++)
     {
         if (!TableFieldInfo.Equal(a._fields[i], b._fields[i]))
         {
             return(false);
         }
     }
     return(true);
 }
Example #7
0
 public static bool Equal(SecondaryKeyInfo a, SecondaryKeyInfo b)
 {
     if (a.Name != b.Name)
     {
         return(false);
     }
     if (a.Fields.Count != b.Fields.Count)
     {
         return(false);
     }
     for (int i = 0; i < a.Fields.Count; i++)
     {
         if (!a.Fields[i].Equals(b.Fields[i]))
         {
             return(false);
         }
     }
     return(true);
 }