data

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	StatusPending  = "pending"
	StatusEnabled  = "enabled"
	StatusDisabled = "disabled"
)

Variables

This section is empty.

Functions

func TokensToTSVector

func TokensToTSVector(tokens []Token) []string

TokensToTSVector takes a list of tokens, de-duplicates them, and returns a Postgres tsvector string.

Types

type Comments

type Comments struct {
	ID       int      `json:"id" db:"id"`
	FromID   int      `json:"from_id" db:"from_id"`
	ToID     null.Int `json:"to_id" db:"to_id"`
	Comments string   `json:"comments" db:"comments"`
}

type Data

type Data struct {
	Langs LangMap
	// contains filtered or unexported fields
}

Data represents the dictionary search interface.

func New

func New(q *Queries, langs LangMap) *Data

New returns an instance of the search interface.

func (*Data) ApproveSubmission

func (d *Data) ApproveSubmission(id int) error

ApproveSubmission approves a pending submission (entry, relations, related entries).

func (*Data) DeleteComments

func (d *Data) DeleteComments(id int) error

DeleteComments deletes a change suggestion from the public.

func (*Data) DeleteEntry

func (d *Data) DeleteEntry(id int) error

DeleteEntry deletes a dictionary entry by its id.

func (*Data) DeleteRelation

func (s *Data) DeleteRelation(fromID, relID int) error

DeleteRelation deletes a dictionary entry by its id.

func (*Data) GetComments

func (d *Data) GetComments() ([]Comments, error)

GetComments retrieves change submissions.

func (*Data) GetEntry

func (d *Data) GetEntry(id int) (Entry, error)

GetEntry returns an entry by its id.

func (*Data) GetGlossaryWords

func (d *Data) GetGlossaryWords(lang, initial string, offset, limit int) ([]GlossaryWord, int, error)

GetGlossaryWords gets words ordered by weight for a language to build a glossary.

func (*Data) GetInitials

func (d *Data) GetInitials(lang string) ([]string, error)

GetInitials gets the list of all unique initials (first character) across all the words for a given language.

func (*Data) GetParentEntries

func (d *Data) GetParentEntries(id int) ([]Entry, error)

GetParentEntries returns the parent entries of an entry by its id.

func (*Data) GetPendingEntries

func (d *Data) GetPendingEntries(lang string, tags pq.StringArray, offset, limit int) ([]Entry, int, error)

GetPendingEntries fetches entries based on the given condition.

func (*Data) GetStats

func (d *Data) GetStats() (Stats, error)

GetStats returns DB stats.

func (*Data) InsertComments

func (d *Data) InsertComments(fromGUID, toGUID, comments string) error

InsertComments inserts a change suggestion from the public.

func (*Data) InsertEntry

func (d *Data) InsertEntry(e Entry) (int, error)

InsertEntry inserts a new non-unique (content+lang) dictionary entry and returns its id.

func (*Data) InsertRelation

func (d *Data) InsertRelation(fromID, toID int, r Relation) (int, error)

InsertRelation adds a non-unique relation between to entries.

func (*Data) InsertSubmissionEntry

func (d *Data) InsertSubmissionEntry(e Entry) (int, error)

InsertSubmissionEntry checks if a given content+lang exists and returns the existing ID. If it doesn't exist, a new entry is inserted and its ID is returned. This is used for accepting public submissions which are conntected to existing entries (if they exist).

func (*Data) InsertSubmissionRelation

func (d *Data) InsertSubmissionRelation(fromID, toID int, r Relation) (int, error)

InsertRelation adds a relation between to entries only if a from_id+to_id+types relation doesn't already exist.

func (*Data) RejectSubmission

func (d *Data) RejectSubmission(id int) error

RejectSubmission rejects a pending submission and deletes related pending entries.

func (*Data) ReorderRelations

func (d *Data) ReorderRelations(ids []int) error

ReorderRelations updates the weights of the given relation IDs in the given order.

func (*Data) Search

func (d *Data) Search(q Query) ([]Entry, int, error)

Search returns the entries filtered and paginated by a given Query along with the total number of matches in the database.

func (*Data) SearchAndLoadRelations

func (d *Data) SearchAndLoadRelations(e []Entry, q Query) error

SearchAndLoadRelations loads related entries into the given Entries.

func (*Data) UpdateEntry

func (d *Data) UpdateEntry(id int, e Entry) error

UpdateEntry updates a dictionary entry.

func (*Data) UpdateRelation

func (d *Data) UpdateRelation(id int, r Relation) error

UpdateRelation updates a relation's properties.

type Entry

type Entry struct {
	ID        int            `json:"id,omitempty" db:"id"`
	GUID      string         `json:"guid" db:"guid"`
	Weight    float64        `json:"weight" db:"weight"`
	Initial   string         `json:"initial" db:"initial"`
	Lang      string         `json:"lang" db:"lang"`
	Content   string         `json:"content" db:"content"`
	Tokens    string         `json:"tokens" db:"tokens"`
	Tags      pq.StringArray `json:"tags" db:"tags"`
	Phones    pq.StringArray `json:"phones" db:"phones"`
	Notes     string         `json:"notes" db:"notes"`
	Status    string         `json:"status" db:"status"`
	Relations []Entry        `json:"relations,omitempty" db:"relations"`
	Total     int            `json:"-" db:"total"`
	CreatedAt null.Time      `json:"created_at" db:"created_at"`
	UpdatedAt null.Time      `json:"updated_at" db:"updated_at"`

	// Non-public fields for scanning relationship data and populating Relation.
	FromID            int            `json:"-" db:"from_id"`
	RelationID        int            `json:"-" db:"relation_id"`
	RelationTypes     pq.StringArray `json:"-" db:"relation_types"`
	RelationTags      pq.StringArray `json:"-" db:"relation_tags"`
	RelationNotes     string         `json:"-" db:"relation_notes"`
	RelationWeight    float64        `json:"-" db:"relation_weight"`
	RelationStatus    string         `json:"-" db:"relation_status"`
	RelationCreatedAt null.Time      `json:"-" db:"relation_created_at"`
	RelationUpdatedAt null.Time      `json:"-" db:"relation_updated_at"`

	// RelationEntry encompasses an Entry with added fields that
	// describes its relationship to other []Entry. This is only populated in
	// []Entry in the Relations list.
	Relation *Relation `json:"relation,omitempty"`
}

Entry represents a dictionary entry.

type GlossaryWord

type GlossaryWord struct {
	ID      int    `json:"id,omitempty" db:"id"`
	Content string `json:"content" db:"content"`
	Total   int    `json:"-" db:"total"`
}

GlossaryWord to read glosary content from db.

type Lang

type Lang struct {
	Name          string            `json:"name"`
	Types         map[string]string `json:"types"`
	TokenizerName string            `json:"tokenizer"`
	TokenizerType string            `json:"tokenizer_type"`
	Tokenizer     Tokenizer         `json:"-"`
}

Lang represents a language's configuration.

type LangMap

type LangMap map[string]Lang

LangMap represents a map of language controllers indexed by the language key.

type Queries

type Queries struct {
	Search             *sqlx.Stmt `query:"search"`
	SearchRelations    *sqlx.Stmt `query:"search-relations"`
	GetEntry           *sqlx.Stmt `query:"get-entry"`
	GetParentRelations *sqlx.Stmt `query:"get-parent-relations"`
	GetInitials        *sqlx.Stmt `query:"get-initials"`
	GetGlossaryWords   *sqlx.Stmt `query:"get-glossary-words"`
	InsertEntry        *sqlx.Stmt `query:"insert-entry"`
	UpdateEntry        *sqlx.Stmt `query:"update-entry"`
	InsertRelation     *sqlx.Stmt `query:"insert-relation"`
	UpdateRelation     *sqlx.Stmt `query:"update-relation"`
	ReorderRelations   *sqlx.Stmt `query:"reorder-relations"`
	DeleteEntry        *sqlx.Stmt `query:"delete-entry"`
	DeleteRelation     *sqlx.Stmt `query:"delete-relation"`
	GetStats           *sqlx.Stmt `query:"get-stats"`

	GetPendingEntries        *sqlx.Stmt `query:"get-pending-entries"`
	InsertSubmissionEntry    *sqlx.Stmt `query:"insert-submission-entry"`
	InsertSubmissionRelation *sqlx.Stmt `query:"insert-submission-relation"`
	InsertComments           *sqlx.Stmt `query:"insert-comments"`
	GetComments              *sqlx.Stmt `query:"get-comments"`
	DeleteComments           *sqlx.Stmt `query:"delete-comments"`
	ApproveSubmission        *sqlx.Stmt `query:"approve-submission"`
	RejectSubmission         *sqlx.Stmt `query:"reject-submission"`
}

Queries contains prepared DB queries.

type Query

type Query struct {
	Query    string   `json:"query"`
	FromLang string   `json:"from_lang"`
	ToLang   string   `json:"to_lang"`
	Types    []string `json:"types"`
	Tags     []string `json:"tags"`
	Status   string   `json:"status"`
	Offset   int      `json:"offset"`
	Limit    int      `json:"limit"`
}

Query represents the parameters of a single search query.

type Relation

type Relation struct {
	ID        int            `json:"id,omitempty"`
	Types     pq.StringArray `json:"types"`
	Tags      pq.StringArray `json:"tags"`
	Notes     string         `json:"notes"`
	Weight    float64        `json:"weight"`
	Status    string         `json:"status"`
	CreatedAt null.Time      `json:"created_at"`
	UpdatedAt null.Time      `json:"updated_at"`
}

Relation represents the relationship between two IDs.

type Stats

type Stats struct {
	Entries   int            `json:"entries"`
	Relations int            `json:"relations"`
	Languages map[string]int `json:"languages"`
}

Stats contains database statistics.

type Token

type Token struct {
	Token  string
	Weight int
}

Token represents a Postgres tsvector token.

type Tokenizer

type Tokenizer interface {
	// Tokenize takes a string and tokenizes it into a list of tsvector tokens
	// that can be stored in the database for fulltext search.
	ToTokens(s string, lang string) ([]string, error)

	// ToTSQuery takes a search string and returns a Postgres tsquery string,
	// for example 'fat & cat`.
	ToQuery(s string, lang string) (string, error)
}

Tokenizer represents a function that takes a string and returns a list of Postgres tsvector tokens.

Jump to

Keyboard shortcuts

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