예제 #1
0
        public TableSchema DefineIndex(string name, SchemaIndexDef index)
        {
            index.NameAsSlice = Slice.From(StorageEnvironment.LabelsContext, name, ByteStringType.Immutable);
            _indexes[name]    = index;

            return(this);
        }
예제 #2
0
        public TableSchema DefineIndex(SchemaIndexDef index)
        {
            if (!index.Name.HasValue || SliceComparer.Equals(Slices.Empty, index.Name))
            {
                throw new ArgumentException("Index name must be non-empty", nameof(index));
            }

            _indexes[index.Name] = index;

            return(this);
        }
예제 #3
0
        public static TableSchema ReadFrom(ByteStringContext context, byte *location, int size)
        {
            var input  = new TableValueReader(location, size);
            var schema = new TableSchema();

            // Since there might not be a primary key, we have a moving index to deserialize the schema
            int currentIndex = 0;

            int   currentSize;
            byte *currentPtr = input.Read(currentIndex++, out currentSize);

            bool hasPrimaryKey = Convert.ToBoolean(*currentPtr);

            if (hasPrimaryKey)
            {
                currentPtr = input.Read(currentIndex++, out currentSize);
                var pk = SchemaIndexDef.ReadFrom(context, currentPtr, currentSize);
                schema.DefineKey(pk);
            }

            // Read common schema indexes
            currentPtr = input.Read(currentIndex++, out currentSize);
            int indexCount = *(int *)currentPtr;

            while (indexCount > 0)
            {
                currentPtr = input.Read(currentIndex++, out currentSize);
                var indexSchemaDef = SchemaIndexDef.ReadFrom(context, currentPtr, currentSize);
                schema.DefineIndex(indexSchemaDef);

                indexCount--;
            }

            // Read fixed size schema indexes
            currentPtr = input.Read(currentIndex++, out currentSize);
            indexCount = *(int *)currentPtr;

            while (indexCount > 0)
            {
                currentPtr = input.Read(currentIndex++, out currentSize);
                var fixedIndexSchemaDef = FixedSizeSchemaIndexDef.ReadFrom(context, currentPtr, currentSize);
                schema.DefineFixedSizeIndex(fixedIndexSchemaDef);

                indexCount--;
            }

            return(schema);
        }
예제 #4
0
            public void Validate(SchemaIndexDef actual)
            {
                if (actual == null)
                {
                    throw new ArgumentNullException(nameof(actual), "Expected an index but received null");
                }

                if (!SliceComparer.Equals(Name, actual.Name))
                {
                    throw new ArgumentException(
                              $"Expected index to have Name='{Name}', got Name='{actual.Name}' instead",
                              nameof(actual));
                }

                if (Type != actual.Type)
                {
                    throw new ArgumentException(
                              $"Expected index {Name} to have Type='{Type}', got Type='{actual.Type}' instead",
                              nameof(actual));
                }

                if (StartIndex != actual.StartIndex)
                {
                    throw new ArgumentException(
                              $"Expected index {Name} to have StartIndex='{StartIndex}', got StartIndex='{actual.StartIndex}' instead",
                              nameof(actual));
                }

                if (Count != actual.Count)
                {
                    throw new ArgumentException(
                              $"Expected index {Name} to have Count='{Count}', got Count='{actual.Count}' instead",
                              nameof(actual));
                }

                if (IsGlobal != actual.IsGlobal)
                {
                    throw new ArgumentException(
                              $"Expected index {Name} to have IsGlobal='{IsGlobal}', got IsGlobal='{actual.IsGlobal}' instead",
                              nameof(actual));
                }
            }
예제 #5
0
        public TableSchema DefineKey(SchemaIndexDef pk)
        {
            _pk = pk;
            if (pk.IsGlobal && string.IsNullOrWhiteSpace(pk.Name))
            {
                throw new ArgumentException("When specifying global PK the name must be specified", nameof(pk));
            }

            if (string.IsNullOrWhiteSpace(_pk.Name))
            {
                _pk.Name = "PK";
            }
            _pk.NameAsSlice = Slice.From(StorageEnvironment.LabelsContext, _pk.Name, ByteStringType.Immutable);

            if (_pk.Count > 1)
            {
                throw new InvalidOperationException("Primary key must be a single field");
            }

            return(this);
        }
예제 #6
0
        public TableSchema DefineKey(SchemaIndexDef index)
        {
            bool hasEmptyName = !index.Name.HasValue || SliceComparer.Equals(Slices.Empty, index.Name);

            if (index.IsGlobal && hasEmptyName)
            {
                throw new ArgumentException("Name must be non empty for global index as primary key", nameof(index));
            }

            if (hasEmptyName)
            {
                index.Name = PkSlice;
            }

            if (index.Count > 1)
            {
                throw new InvalidOperationException("Primary key must be a single field");
            }

            _primaryKey = index;

            return(this);
        }
예제 #7
0
            public static SchemaIndexDef ReadFrom(ByteStringContext context, byte *location, int size)
            {
                var input    = new TableValueReader(location, size);
                var indexDef = new SchemaIndexDef();

                int   currentSize;
                byte *currentPtr = input.Read(0, out currentSize);

                indexDef.Type = (TableIndexType)(*(ulong *)currentPtr);

                currentPtr          = input.Read(1, out currentSize);
                indexDef.StartIndex = *(int *)currentPtr;

                currentPtr     = input.Read(2, out currentSize);
                indexDef.Count = *(int *)currentPtr;

                currentPtr        = input.Read(3, out currentSize);
                indexDef.IsGlobal = Convert.ToBoolean(*currentPtr);

                currentPtr = input.Read(4, out currentSize);
                Slice.From(context, currentPtr, currentSize, ByteStringType.Immutable, out indexDef.Name);

                return(indexDef);
            }