schema

package
v1.0.0-alpha.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrimaryKeyIndexName = "pkey"
	AutoPrimaryKeyF     = "id"
	PrimaryKeySchemaK   = "primary_key"
)
View Source
const (
	UserDefinedSchema = "user_defined_schema"
)

Variables

View Source
var (
	MsgFieldNameAsLanguageKeyword = "Invalid collection field name, It contains language keyword for fieldName = '%s'"
	MsgFieldNameInvalidPattern    = "" /* 131-byte string literal not displayed */
	LanguageKeywords              = set.New("abstract", "add", "alias", "and", "any", "args", "arguments", "array",
		"as", "as?", "ascending", "assert", "async", "await", "base", "bool", "boolean", "break", "by", "byte",
		"callable", "case", "catch", "chan", "char", "checked", "class", "clone", "const", "constructor", "continue",
		"debugger", "decimal", "declare", "def", "default", "defer", "del", "delegate", "delete", "descending", "die",
		"do", "double", "dynamic", "echo", "elif", "else", "elseif", "empty", "enddeclare", "endfor", "endforeach",
		"endif", "endswitch", "endwhile", "enum", "equals", "eval", "event", "except", "exception", "exit", "explicit",
		"export", "extends", "extern", "fallthrough", "false", "final", "finally", "fixed", "float", "fn", "for",
		"foreach", "from", "fun", "func", "function", "get", "global", "go", "goto", "group", "if", "implements",
		"implicit", "import", "in", "include", "include_once", "init", "instanceof", "insteadof", "int", "integer",
		"interface", "internal", "into", "is", "isset", "join", "lambda", "let", "list", "lock", "long", "managed",
		"map", "match", "module", "nameof", "namespace", "native", "new", "nint", "none", "nonlocal", "not", "notnull",
		"nuint", "null", "number", "object", "of", "on", "operator", "or", "orderby", "out", "override", "package",
		"params", "partial", "pass", "print", "private", "protected", "public", "raise", "range", "readonly", "record",
		"ref", "remove", "require", "require_once", "return", "sbyte", "sealed", "select", "set", "short", "sizeof",
		"stackalloc", "static", "strictfp", "string", "struct", "super", "switch", "symbol", "synchronized", "this",
		"throw", "throws", "trait", "transient", "true", "try", "type", "typealias", "typeof", "uint", "ulong",
		"unchecked", "unmanaged", "unsafe", "unset", "use", "ushort", "using", "val", "value", "var", "virtual", "void",
		"volatile", "when", "where", "while", "with", "xor", "yield")
	ValidFieldNamePattern = regexp.MustCompile(`^[a-zA-Z_$][a-zA-Z0-9_$]*$`)
)
View Source
var (
	ErrMissingField           = api.Errorf(api.Code_INVALID_ARGUMENT, "removing a field is a backward incompatible change")
	ErrModifiedPrimaryKey     = api.Errorf(api.Code_INVALID_ARGUMENT, "changing primary key is a backward incompatible change")
	ErrCollectionNameMismatch = api.Errorf(api.Code_INVALID_ARGUMENT, "mismatch in the collection name")
)
View Source
var FieldNames = [...]string{
	UnknownType:  "unknown",
	NullType:     "null",
	BoolType:     "bool",
	Int32Type:    "int32",
	Int64Type:    "int64",
	DoubleType:   "double",
	StringType:   "string",
	ByteType:     "byte",
	UUIDType:     "uuid",
	DateTimeType: "datetime",
	ArrayType:    "array",
	ObjectType:   "object",
}
View Source
var ReservedFields = [...]string{
	CreatedAt:     "created_at",
	UpdatedAt:     "updated_at",
	Metadata:      "metadata",
	IdToSearchKey: "_tigris_id",
}
View Source
var SupportedFieldProperties = set.New(
	"type",
	"format",
	"items",
	"maxLength",
	"description",
	"contentEncoding",
	"properties",
	"autoGenerate",
)

Functions

func ApplySchemaRules

func ApplySchemaRules(existing *DefaultCollection, current *Factory) error

ApplySchemaRules is to validate incoming collection request against the existing present collection. It performs following validations,

  • Primary Key Changed, or order of fields part of the primary key is changed
  • Collection name change
  • Type of existing field is changed
  • A validation on field property is also applied like for instance if existing field has some property but it is removed in the new schema
  • Removing a field
  • Any index exist on the collection will also have same checks like type, etc

func FacetableField

func FacetableField(field *Field) bool

func GetSearchDeltaFields

func GetSearchDeltaFields(existingFields []*Field, incomingFields []*Field) []tsApi.Field

func IndexableField

func IndexableField(field *Field) bool

func IsReservedField

func IsReservedField(name string) bool

func IsValidIndexType

func IsValidIndexType(t FieldType) bool

func PackSearchField

func PackSearchField(field *Field) bool

func ToSearchFieldType

func ToSearchFieldType(field *Field) string

Types

type DefaultCollection

type DefaultCollection struct {
	// Id is the dictionary encoded value for this collection.
	Id uint32
	// SchVer returns the schema version
	SchVer int
	// Name is the name of the collection.
	Name string
	// Fields are derived from the user schema.
	Fields []*Field
	// Indexes is a wrapper on the indexes part of this collection.
	Indexes *Indexes
	// Validator is used to validate the JSON document. As it is expensive to create this, it is only created once
	// during constructor of the collection.
	Validator *jsonschema.Schema
	// JSON schema
	Schema jsoniter.RawMessage

	// search schema
	Search *tsApi.CollectionSchema
}

DefaultCollection is used to represent a collection. The tenant in the metadata package is responsible for creating the collection.

func NewDefaultCollection

func NewDefaultCollection(name string, id uint32, schVer int, fields []*Field, indexes *Indexes, schema jsoniter.RawMessage, searchCollectionName string) *DefaultCollection

func (*DefaultCollection) GetFields

func (d *DefaultCollection) GetFields() []*Field

func (*DefaultCollection) GetIndexes

func (d *DefaultCollection) GetIndexes() *Indexes

func (*DefaultCollection) GetName

func (d *DefaultCollection) GetName() string

func (*DefaultCollection) SearchCollectionName

func (d *DefaultCollection) SearchCollectionName() string

func (*DefaultCollection) Type

func (d *DefaultCollection) Type() string

func (*DefaultCollection) Validate

func (d *DefaultCollection) Validate(document interface{}) error

Validate expects an unmarshalled document which it will validate again the schema of this collection.

type Factory

type Factory struct {
	// Name is the collection name of this schema.
	Name string
	// Fields are derived from the user schema.
	Fields []*Field
	// Indexes is a wrapper on the indexes part of this collection. At this point the dictionary encoded value is not
	// set for these indexes which is set as part of collection creation.
	Indexes *Indexes
	// Schema is the raw JSON schema received as part of CreateOrUpdateCollection request. This is stored as-is in the
	// schema subspace.
	Schema jsoniter.RawMessage
}

Factory is used as an intermediate step so that collection can be initialized with properly encoded values.

func Build

func Build(collection string, reqSchema jsoniter.RawMessage) (*Factory, error)

Build is used to deserialize the user json schema into a schema factory.

type Field

type Field struct {
	FieldName       string
	DataType        FieldType
	MaxLength       *int32
	UniqueKeyField  *bool
	PrimaryKeyField *bool
	AutoGenerated   *bool
	Fields          []*Field
}

func (*Field) IsAutoGenerated

func (f *Field) IsAutoGenerated() bool

func (*Field) IsCompatible

func (f *Field) IsCompatible(f1 *Field) error

func (*Field) IsPrimaryKey

func (f *Field) IsPrimaryKey() bool

func (*Field) Name

func (f *Field) Name() string

func (*Field) Type

func (f *Field) Type() FieldType

type FieldBuilder

type FieldBuilder struct {
	FieldName   string
	Description string              `json:"description,omitempty"`
	Type        string              `json:"type,omitempty"`
	Format      string              `json:"format,omitempty"`
	Encoding    string              `json:"contentEncoding,omitempty"`
	MaxLength   *int32              `json:"maxLength,omitempty"`
	Auto        *bool               `json:"autoGenerate,omitempty"`
	Items       *FieldBuilder       `json:"items,omitempty"`
	Properties  jsoniter.RawMessage `json:"properties,omitempty"`
	Primary     *bool
	Fields      []*Field
}

func (*FieldBuilder) Build

func (f *FieldBuilder) Build(isArrayElement bool) (*Field, error)

func (*FieldBuilder) Validate

func (f *FieldBuilder) Validate(v []byte) error

type FieldSchemaValidator

type FieldSchemaValidator struct{}

func (*FieldSchemaValidator) Validate

func (v *FieldSchemaValidator) Validate(existing *DefaultCollection, current *Factory) error

type FieldType

type FieldType int
const (
	UnknownType FieldType = iota
	NullType
	BoolType
	Int32Type
	Int64Type
	DoubleType
	StringType
	// ByteType is a base64 encoded characters, this means if this type is used as key then we need to decode it
	// and then use it as key.
	ByteType
	UUIDType
	// DateTimeType is a valid date representation as defined by RFC 3339, see https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
	DateTimeType
	ArrayType
	ObjectType
)

func ToFieldType

func ToFieldType(jsonType string, encoding string, format string) FieldType

type Index

type Index struct {
	// Fields that are part of this index. An index can have a single or composite fields.
	Fields []*Field
	// Name is used by dictionary encoder for this index.
	Name string
	// Id is assigned to this index by the dictionary encoder.
	Id uint32
}

Index can be composite so it has a list of fields, each index has name and encoded id. The encoded is used for key building.

func (*Index) IsCompatible

func (i *Index) IsCompatible(i1 *Index) error

type IndexSchemaValidator

type IndexSchemaValidator struct{}

func (*IndexSchemaValidator) Validate

func (v *IndexSchemaValidator) Validate(existing *DefaultCollection, current *Factory) error

type Indexes

type Indexes struct {
	PrimaryKey *Index
}

Indexes is to wrap different index that a collection can have.

func (*Indexes) GetIndexes

func (i *Indexes) GetIndexes() []*Index

type JSONSchema

type JSONSchema struct {
	Name        string              `json:"title,omitempty"`
	Description string              `json:"description,omitempty"`
	Properties  jsoniter.RawMessage `json:"properties,omitempty"`
	PrimaryKeys []string            `json:"primary_key,omitempty"`
}

type ReservedField

type ReservedField uint8
const (
	CreatedAt ReservedField = iota
	UpdatedAt
	Metadata
	IdToSearchKey
)

type Validator

type Validator interface {
	Validate(existing *DefaultCollection, current *Factory) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL