[#23] Support SQL-like queries for placement

JSON format is rather verbose an inconvenient to be
edited by hand. This commit implements SQL-like
language for representing placement policy.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2020-09-11 17:44:34 +03:00 committed by Alex Vanin
parent 246a15de35
commit 001a23eb31
7 changed files with 531 additions and 0 deletions

60
pkg/policy/grammar.go Normal file
View file

@ -0,0 +1,60 @@
package policy
import (
"github.com/alecthomas/participle"
)
var parser *participle.Parser
func init() {
p, err := participle.Build(&query{})
if err != nil {
panic(err)
}
parser = p
}
type query struct {
Replicas []*replicaStmt `@@+`
CBF uint32 `("CBF" @Int)?`
Selectors []*selectorStmt `@@*`
Filters []*filterStmt `@@*`
}
type replicaStmt struct {
Count int `"REP" @Int`
Selector string `("IN" @Ident)?`
}
type selectorStmt struct {
Count uint32 `"SELECT" @Int`
Clause string `"IN" @("SAME" | "DISTINCT")?`
Bucket string `@Ident`
Filter string `"FROM" @(Ident | "*")`
Name string `("AS" @Ident)?`
}
type filterStmt struct {
Value *orChain `"FILTER" @@`
Name string `"AS" @Ident`
}
type filterOrExpr struct {
Reference string `"@"@Ident`
Expr *simpleExpr `| @@`
}
type orChain struct {
Clauses []*andChain `@@ ("OR" @@)*`
}
type andChain struct {
Clauses []*filterOrExpr `@@ ("AND" @@)*`
}
type simpleExpr struct {
Key string `@Ident`
// We don't use literals here to improve error messages.
Op string `@Ident`
Value string `@(Ident | String | Int)`
}