query

package
v0.0.0-...-f8ab5ae Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2021 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	QueryTypeOrder = map[QueryType]int{
		CaseQuery:     1,
		LanguageQuery: 2,
		RepoQuery:     3,
		FileQuery:     4,
		TokenQuery:    6,
		RegexQuery:    7,
		TreeQuery:     8,
	}
)

Functions

func GetModifierName

func GetModifierName(qtype QueryType) string

func UpdateStats

func UpdateStats(query *Query)

用深度优先的遍历顺序给 query 逐级更新 ID 和统计信息等

Types

type Query

type Query struct {
	// 表达式类型
	Type QueryType

	// 在树状结构中的唯一标识 ID,不同的查询树重新构建 ID
	ID int

	// 当 QueryType 为简单文本查询时,存储需要匹配的字符串
	// 其他类型下,该字段为空
	Token string

	// 表达式基础上取反操作
	Negate bool

	// 当 QueryType 为正则表达式的时候,这里存储正则表达式原始字符串
	RegexString string
	// 当 QueryType 为正则表达式的时候,这里存储所有解析出来的 token
	RegexTokens []string

	// 当 QueryType 为查询树时,SubQueries 存储解析出的递归子查询表达式
	SubQueries []*Query
	Or         bool // 如果为 true,则 SubQueries 之间取或,否则取和

	// 当 token 或者 regex 类型的 symbol
	IsSymbol bool

	// FileQuery
	FileRegexString string

	// RepoQuery
	RepoRegexString string

	// CaseQuery
	Case bool

	// LanguageQuery
	Language string

	// 子节点的统计和调试信息
	NumNodes     int // 树中一共多少节点(包含根节点)
	MaxDepth     int // 子节点最大深度,叶子节点深度为 0
	RootDistance int // 该节点到 root 的距离
	// contains filtered or unexported fields
}

查询表达式 保存了从表达式解析出来的树状结构

func Copy

func Copy(query *Query) *Query

深度拷贝所有子节点

func Flatten

func Flatten(query *Query) *Query

对子查询和上级 AND/OR 一致的情况,做扁平化 比如

(a AND (b AND c)) -> (a AND b AND c)
(a OR (b OR c)) -> (a OR b OR c)

另外,对只有一个子查询的情况,做级别提升

((a)) -> (a)

func Parse

func Parse(pattern string) (*Query, error)

从 pattern 中解析 Query

func Sort

func Sort(q *Query) *Query

对 query 的各级进行排序,排序规则参见 internalSort 函数中注释

func (*Query) GetTokens

func (query *Query) GetTokens() []string

从查询表达式树中获得所有的 tokens,包括了正则表达式中拆分出来的单串 返回结果做了去重

func (*Query) String

func (query *Query) String() string

返回 query 的字符串表征 该字符串和 query 本身是一一对应的,也就是说 1、两个表征字符串相同的 query 是完全相同的 2、实际相同的两个 query 的表征字符串完全相等 这里所说的“实际相同”值得是做过归一化的相同,比如

(a AND b) == (b AND a),表征字符串为 "(a AND b)"
(a AND (b AND c)) == (a AND b AND c),表征字符串为 "(a AND b AND c)"

注意复杂运算下相同的两个逻辑相等的 query 可能有不同的字符串表征,比如

(a AND (b OR c)) 表征字符串为 "(a AND (b OR c))"
((a AND b) OR (a AND c)) 表征字符串为 "((a AND b) OR (a AND c))"

type QueryType

type QueryType int
const (
	UnknownQueryType QueryType = iota
	TokenQuery                 // 简单文本查询
	RegexQuery                 // 正则表达式查询
	TreeQuery                  // 复杂的查询树
	// 修饰词
	FileQuery     // 文件名查询
	RepoQuery     // 仓库名查询
	CaseQuery     // 大小写区分
	LanguageQuery // 编程语言查询
)

Jump to

Keyboard shortcuts

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