2020-09-10 21:28:19 +00:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-09-27 20:17:14 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/bufferedwriter"
|
2020-09-10 21:28:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/netstorage"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmselect/searchutils"
|
2021-02-03 18:12:17 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2020-09-10 21:28:19 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/storage"
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MetricsFindHandler implements /metrics/find handler.
|
|
|
|
//
|
|
|
|
// See https://graphite-api.readthedocs.io/en/latest/api.html#metrics-find
|
|
|
|
func MetricsFindHandler(startTime time.Time, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
deadline := searchutils.GetDeadlineForQuery(r, startTime)
|
|
|
|
format := r.FormValue("format")
|
|
|
|
if format == "" {
|
|
|
|
format = "treejson"
|
|
|
|
}
|
|
|
|
switch format {
|
|
|
|
case "treejson", "completer":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf(`unexpected "format" query arg: %q; expecting "treejson" or "completer"`, format)
|
|
|
|
}
|
|
|
|
query := r.FormValue("query")
|
|
|
|
if len(query) == 0 {
|
|
|
|
return fmt.Errorf("expecting non-empty `query` arg")
|
|
|
|
}
|
|
|
|
delimiter := r.FormValue("delimiter")
|
|
|
|
if delimiter == "" {
|
|
|
|
delimiter = "."
|
|
|
|
}
|
|
|
|
if len(delimiter) > 1 {
|
|
|
|
return fmt.Errorf("`delimiter` query arg must contain only a single char")
|
|
|
|
}
|
|
|
|
if searchutils.GetBool(r, "automatic_variants") {
|
|
|
|
// See https://github.com/graphite-project/graphite-web/blob/bb9feb0e6815faa73f538af6ed35adea0fb273fd/webapp/graphite/metrics/views.py#L152
|
|
|
|
query = addAutomaticVariants(query, delimiter)
|
|
|
|
}
|
|
|
|
if format == "completer" {
|
|
|
|
// See https://github.com/graphite-project/graphite-web/blob/bb9feb0e6815faa73f538af6ed35adea0fb273fd/webapp/graphite/metrics/views.py#L148
|
|
|
|
query = strings.ReplaceAll(query, "..", ".*")
|
|
|
|
if !strings.HasSuffix(query, "*") {
|
|
|
|
query += "*"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
leavesOnly := searchutils.GetBool(r, "leavesOnly")
|
|
|
|
wildcards := searchutils.GetBool(r, "wildcards")
|
|
|
|
label := r.FormValue("label")
|
|
|
|
if label == "__name__" {
|
|
|
|
label = ""
|
|
|
|
}
|
|
|
|
jsonp := r.FormValue("jsonp")
|
|
|
|
from, err := searchutils.GetTime(r, "from", 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ct := startTime.UnixNano() / 1e6
|
|
|
|
until, err := searchutils.GetTime(r, "until", ct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tr := storage.TimeRange{
|
|
|
|
MinTimestamp: from,
|
|
|
|
MaxTimestamp: until,
|
|
|
|
}
|
2021-03-18 14:12:02 +00:00
|
|
|
paths, err := metricsFind(tr, label, "", query, delimiter[0], false, deadline)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if leavesOnly {
|
|
|
|
paths = filterLeaves(paths, delimiter)
|
|
|
|
}
|
2020-09-29 08:00:41 +00:00
|
|
|
paths = deduplicatePaths(paths, delimiter)
|
2020-09-10 21:28:19 +00:00
|
|
|
sortPaths(paths, delimiter)
|
2020-11-16 12:49:46 +00:00
|
|
|
contentType := getContentType(jsonp)
|
2020-09-10 21:28:19 +00:00
|
|
|
w.Header().Set("Content-Type", contentType)
|
2020-09-27 20:17:14 +00:00
|
|
|
bw := bufferedwriter.Get(w)
|
|
|
|
defer bufferedwriter.Put(bw)
|
|
|
|
WriteMetricsFindResponse(bw, paths, delimiter, format, wildcards, jsonp)
|
|
|
|
if err := bw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-10 21:28:19 +00:00
|
|
|
metricsFindDuration.UpdateDuration(startTime)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-29 08:00:41 +00:00
|
|
|
func deduplicatePaths(paths []string, delimiter string) []string {
|
|
|
|
if len(paths) == 0 {
|
|
|
|
return nil
|
2020-09-18 07:59:11 +00:00
|
|
|
}
|
2020-09-29 08:00:41 +00:00
|
|
|
sort.Strings(paths)
|
|
|
|
dst := paths[:1]
|
|
|
|
for _, path := range paths[1:] {
|
|
|
|
prevPath := dst[len(dst)-1]
|
|
|
|
if path == prevPath {
|
|
|
|
// Skip duplicate path.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
dst = append(dst, path)
|
|
|
|
}
|
2020-09-18 07:59:11 +00:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2020-09-10 21:28:19 +00:00
|
|
|
// MetricsExpandHandler implements /metrics/expand handler.
|
|
|
|
//
|
|
|
|
// See https://graphite-api.readthedocs.io/en/latest/api.html#metrics-expand
|
|
|
|
func MetricsExpandHandler(startTime time.Time, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
deadline := searchutils.GetDeadlineForQuery(r, startTime)
|
|
|
|
queries := r.Form["query"]
|
|
|
|
if len(queries) == 0 {
|
|
|
|
return fmt.Errorf("missing `query` arg")
|
|
|
|
}
|
|
|
|
groupByExpr := searchutils.GetBool(r, "groupByExpr")
|
|
|
|
leavesOnly := searchutils.GetBool(r, "leavesOnly")
|
|
|
|
label := r.FormValue("label")
|
|
|
|
if label == "__name__" {
|
|
|
|
label = ""
|
|
|
|
}
|
|
|
|
delimiter := r.FormValue("delimiter")
|
|
|
|
if delimiter == "" {
|
|
|
|
delimiter = "."
|
|
|
|
}
|
|
|
|
if len(delimiter) > 1 {
|
|
|
|
return fmt.Errorf("`delimiter` query arg must contain only a single char")
|
|
|
|
}
|
|
|
|
jsonp := r.FormValue("jsonp")
|
|
|
|
from, err := searchutils.GetTime(r, "from", 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ct := startTime.UnixNano() / 1e6
|
|
|
|
until, err := searchutils.GetTime(r, "until", ct)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tr := storage.TimeRange{
|
|
|
|
MinTimestamp: from,
|
|
|
|
MaxTimestamp: until,
|
|
|
|
}
|
|
|
|
m := make(map[string][]string, len(queries))
|
|
|
|
for _, query := range queries {
|
2021-03-18 14:12:02 +00:00
|
|
|
paths, err := metricsFind(tr, label, "", query, delimiter[0], true, deadline)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if leavesOnly {
|
|
|
|
paths = filterLeaves(paths, delimiter)
|
|
|
|
}
|
|
|
|
m[query] = paths
|
|
|
|
}
|
2020-11-16 12:49:46 +00:00
|
|
|
contentType := getContentType(jsonp)
|
2020-09-10 21:28:19 +00:00
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
if groupByExpr {
|
|
|
|
for _, paths := range m {
|
|
|
|
sortPaths(paths, delimiter)
|
|
|
|
}
|
|
|
|
WriteMetricsExpandResponseByQuery(w, m, jsonp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
paths := m[queries[0]]
|
|
|
|
if len(m) > 1 {
|
|
|
|
pathsSet := make(map[string]struct{})
|
|
|
|
for _, paths := range m {
|
|
|
|
for _, path := range paths {
|
|
|
|
pathsSet[path] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
paths = make([]string, 0, len(pathsSet))
|
|
|
|
for path := range pathsSet {
|
|
|
|
paths = append(paths, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sortPaths(paths, delimiter)
|
2020-09-27 20:17:14 +00:00
|
|
|
bw := bufferedwriter.Get(w)
|
|
|
|
defer bufferedwriter.Put(bw)
|
|
|
|
WriteMetricsExpandResponseFlat(bw, paths, jsonp)
|
|
|
|
if err := bw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-10 21:28:19 +00:00
|
|
|
metricsExpandDuration.UpdateDuration(startTime)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MetricsIndexHandler implements /metrics/index.json handler.
|
|
|
|
//
|
|
|
|
// See https://graphite-api.readthedocs.io/en/latest/api.html#metrics-index-json
|
|
|
|
func MetricsIndexHandler(startTime time.Time, w http.ResponseWriter, r *http.Request) error {
|
|
|
|
deadline := searchutils.GetDeadlineForQuery(r, startTime)
|
|
|
|
jsonp := r.FormValue("jsonp")
|
2022-05-31 23:29:19 +00:00
|
|
|
metricNames, err := netstorage.GetLabelValues(nil, "__name__", deadline)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`cannot obtain metric names: %w`, err)
|
|
|
|
}
|
2020-11-16 12:49:46 +00:00
|
|
|
contentType := getContentType(jsonp)
|
2020-09-10 21:28:19 +00:00
|
|
|
w.Header().Set("Content-Type", contentType)
|
2020-09-27 20:17:14 +00:00
|
|
|
bw := bufferedwriter.Get(w)
|
|
|
|
defer bufferedwriter.Put(bw)
|
|
|
|
WriteMetricsIndexResponse(bw, metricNames, jsonp)
|
|
|
|
if err := bw.Flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-09-10 21:28:19 +00:00
|
|
|
metricsIndexDuration.UpdateDuration(startTime)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-18 14:15:27 +00:00
|
|
|
// metricsFind searches for label values that match the given qHead and qTail.
|
|
|
|
func metricsFind(tr storage.TimeRange, label, qHead, qTail string, delimiter byte, isExpand bool, deadline searchutils.Deadline) ([]string, error) {
|
|
|
|
n := strings.IndexAny(qTail, "*{[")
|
2021-03-18 14:12:02 +00:00
|
|
|
if n < 0 {
|
2021-03-18 14:15:27 +00:00
|
|
|
query := qHead + qTail
|
2022-05-31 23:29:19 +00:00
|
|
|
suffixes, err := netstorage.GetTagValueSuffixes(nil, tr, label, query, delimiter, deadline)
|
2021-03-18 14:12:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
if len(suffixes) == 0 {
|
2021-03-18 14:12:02 +00:00
|
|
|
return nil, nil
|
2020-09-18 07:59:11 +00:00
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
if len(query) > 0 && query[len(query)-1] == delimiter {
|
|
|
|
return []string{query}, nil
|
|
|
|
}
|
|
|
|
results := make([]string, 0, len(suffixes))
|
|
|
|
for _, suffix := range suffixes {
|
|
|
|
if len(suffix) == 0 || len(suffix) == 1 && suffix[0] == delimiter {
|
|
|
|
results = append(results, query+suffix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results, nil
|
2021-03-18 14:12:02 +00:00
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
if n == len(qTail)-1 && strings.HasSuffix(qTail, "*") {
|
|
|
|
query := qHead + qTail[:len(qTail)-1]
|
2022-05-31 23:29:19 +00:00
|
|
|
suffixes, err := netstorage.GetTagValueSuffixes(nil, tr, label, query, delimiter, deadline)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 07:59:11 +00:00
|
|
|
if len(suffixes) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
results := make([]string, 0, len(suffixes))
|
|
|
|
for _, suffix := range suffixes {
|
2021-03-18 14:15:27 +00:00
|
|
|
results = append(results, query+suffix)
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
qHead += qTail[:n]
|
|
|
|
paths, err := metricsFind(tr, label, qHead, "*", delimiter, isExpand, deadline)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
suffix := qTail[n:]
|
|
|
|
qTail = ""
|
2020-09-10 21:28:19 +00:00
|
|
|
if m := strings.IndexByte(suffix, delimiter); m >= 0 {
|
2021-03-18 14:15:27 +00:00
|
|
|
qTail = suffix[m+1:]
|
2020-09-10 21:28:19 +00:00
|
|
|
suffix = suffix[:m+1]
|
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
qPrefix := qHead + suffix
|
2020-09-18 07:59:11 +00:00
|
|
|
rePrefix, err := getRegexpForQuery(qPrefix, delimiter)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
2020-09-18 07:59:11 +00:00
|
|
|
return nil, fmt.Errorf("cannot convert query %q to regexp: %w", qPrefix, err)
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
2020-09-18 07:59:11 +00:00
|
|
|
results := make([]string, 0, len(paths))
|
2020-09-10 21:28:19 +00:00
|
|
|
for _, path := range paths {
|
2020-09-18 07:59:11 +00:00
|
|
|
if !rePrefix.MatchString(path) {
|
|
|
|
continue
|
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
if qTail == "" {
|
2020-09-18 07:59:11 +00:00
|
|
|
results = append(results, path)
|
2020-09-10 21:28:19 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-18 14:15:27 +00:00
|
|
|
fullPaths, err := metricsFind(tr, label, path, qTail, delimiter, isExpand, deadline)
|
2020-09-10 21:28:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 07:59:11 +00:00
|
|
|
if isExpand {
|
|
|
|
results = append(results, fullPaths...)
|
|
|
|
} else {
|
|
|
|
for _, fullPath := range fullPaths {
|
|
|
|
results = append(results, qPrefix+fullPath[len(path):])
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
metricsFindDuration = metrics.NewSummary(`vm_request_duration_seconds{path="/metrics/find"}`)
|
|
|
|
metricsExpandDuration = metrics.NewSummary(`vm_request_duration_seconds{path="/metrics/expand"}`)
|
2020-09-10 22:58:16 +00:00
|
|
|
metricsIndexDuration = metrics.NewSummary(`vm_request_duration_seconds{path="/metrics/index.json"}`)
|
2020-09-10 21:28:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func addAutomaticVariants(query, delimiter string) string {
|
|
|
|
// See https://github.com/graphite-project/graphite-web/blob/bb9feb0e6815faa73f538af6ed35adea0fb273fd/webapp/graphite/metrics/views.py#L152
|
|
|
|
parts := strings.Split(query, delimiter)
|
|
|
|
for i, part := range parts {
|
|
|
|
if strings.Contains(part, ",") && !strings.Contains(part, "{") {
|
|
|
|
parts[i] = "{" + part + "}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return strings.Join(parts, delimiter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterLeaves(paths []string, delimiter string) []string {
|
|
|
|
leaves := paths[:0]
|
|
|
|
for _, path := range paths {
|
|
|
|
if !strings.HasSuffix(path, delimiter) {
|
|
|
|
leaves = append(leaves, path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return leaves
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortPaths(paths []string, delimiter string) {
|
|
|
|
sort.Slice(paths, func(i, j int) bool {
|
|
|
|
a, b := paths[i], paths[j]
|
|
|
|
isNodeA := strings.HasSuffix(a, delimiter)
|
|
|
|
isNodeB := strings.HasSuffix(b, delimiter)
|
|
|
|
if isNodeA == isNodeB {
|
|
|
|
return a < b
|
|
|
|
}
|
|
|
|
return isNodeA
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRegexpForQuery(query string, delimiter byte) (*regexp.Regexp, error) {
|
|
|
|
regexpCacheLock.Lock()
|
|
|
|
defer regexpCacheLock.Unlock()
|
|
|
|
|
|
|
|
k := regexpCacheKey{
|
|
|
|
query: query,
|
|
|
|
delimiter: delimiter,
|
|
|
|
}
|
|
|
|
if re := regexpCache[k]; re != nil {
|
|
|
|
return re.re, re.err
|
|
|
|
}
|
2021-02-03 18:12:17 +00:00
|
|
|
rs, tail := getRegexpStringForQuery(query, delimiter, false)
|
|
|
|
if len(tail) > 0 {
|
|
|
|
return nil, fmt.Errorf("unexpected tail left after parsing query %q; tail: %q", query, tail)
|
|
|
|
}
|
2020-12-11 15:03:12 +00:00
|
|
|
re, err := regexp.Compile(rs)
|
|
|
|
regexpCache[k] = ®expCacheEntry{
|
|
|
|
re: re,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
if len(regexpCache) >= maxRegexpCacheSize {
|
|
|
|
for k := range regexpCache {
|
|
|
|
if len(regexpCache) < maxRegexpCacheSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
delete(regexpCache, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return re, err
|
|
|
|
}
|
|
|
|
|
2021-02-03 18:12:17 +00:00
|
|
|
func getRegexpStringForQuery(query string, delimiter byte, isSubquery bool) (string, string) {
|
2020-12-11 15:03:12 +00:00
|
|
|
var a []string
|
2021-02-03 18:12:17 +00:00
|
|
|
var tail string
|
2020-09-18 07:59:11 +00:00
|
|
|
quotedDelimiter := regexp.QuoteMeta(string([]byte{delimiter}))
|
2021-02-03 18:12:17 +00:00
|
|
|
for {
|
|
|
|
n := strings.IndexAny(query, "*{[,}")
|
|
|
|
if n < 0 {
|
|
|
|
a = append(a, regexp.QuoteMeta(query))
|
|
|
|
tail = ""
|
|
|
|
goto end
|
|
|
|
}
|
|
|
|
a = append(a, regexp.QuoteMeta(query[:n]))
|
|
|
|
query = query[n:]
|
|
|
|
switch query[0] {
|
|
|
|
case ',', '}':
|
2020-12-11 15:03:12 +00:00
|
|
|
if isSubquery {
|
2021-02-03 18:12:17 +00:00
|
|
|
tail = query
|
|
|
|
goto end
|
2020-12-11 15:03:12 +00:00
|
|
|
}
|
2021-02-03 18:12:17 +00:00
|
|
|
a = append(a, regexp.QuoteMeta(query[:1]))
|
|
|
|
query = query[1:]
|
|
|
|
case '*':
|
|
|
|
a = append(a, "[^"+quotedDelimiter+"]*")
|
|
|
|
query = query[1:]
|
|
|
|
case '{':
|
|
|
|
var opts []string
|
|
|
|
for {
|
|
|
|
var x string
|
|
|
|
x, tail = getRegexpStringForQuery(query[1:], delimiter, true)
|
|
|
|
opts = append(opts, x)
|
|
|
|
if len(tail) == 0 {
|
|
|
|
a = append(a, regexp.QuoteMeta("{"))
|
|
|
|
a = append(a, strings.Join(opts, ","))
|
|
|
|
goto end
|
|
|
|
}
|
|
|
|
if tail[0] == ',' {
|
|
|
|
query = tail
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tail[0] == '}' {
|
|
|
|
a = append(a, "(?:"+strings.Join(opts, "|")+")")
|
|
|
|
query = tail[1:]
|
|
|
|
break
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
2021-02-03 18:12:17 +00:00
|
|
|
logger.Panicf("BUG: unexpected first char at tail %q; want `.` or `}`", tail)
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
|
|
|
case '[':
|
2021-02-03 18:12:17 +00:00
|
|
|
n := strings.IndexByte(query, ']')
|
|
|
|
if n < 0 {
|
|
|
|
a = append(a, regexp.QuoteMeta(query))
|
|
|
|
tail = ""
|
|
|
|
goto end
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
2021-02-03 18:12:17 +00:00
|
|
|
a = append(a, query[:n+1])
|
|
|
|
query = query[n+1:]
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-03 18:12:17 +00:00
|
|
|
end:
|
2020-09-10 21:28:19 +00:00
|
|
|
s := strings.Join(a, "")
|
2020-12-11 15:03:12 +00:00
|
|
|
if isSubquery {
|
2021-02-03 18:12:17 +00:00
|
|
|
return s, tail
|
2020-12-11 15:03:12 +00:00
|
|
|
}
|
2020-09-18 07:59:11 +00:00
|
|
|
if !strings.HasSuffix(s, quotedDelimiter) {
|
|
|
|
s += quotedDelimiter + "?"
|
|
|
|
}
|
2021-02-03 18:12:17 +00:00
|
|
|
s = "^" + s + "$"
|
|
|
|
return s, tail
|
2020-09-10 21:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type regexpCacheEntry struct {
|
|
|
|
re *regexp.Regexp
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type regexpCacheKey struct {
|
|
|
|
query string
|
|
|
|
delimiter byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var regexpCache = make(map[regexpCacheKey]*regexpCacheEntry)
|
|
|
|
var regexpCacheLock sync.Mutex
|
|
|
|
|
|
|
|
const maxRegexpCacheSize = 10000
|
2020-11-16 12:49:46 +00:00
|
|
|
|
|
|
|
func getContentType(jsonp string) string {
|
|
|
|
if jsonp == "" {
|
2021-11-09 16:03:50 +00:00
|
|
|
return "application/json"
|
2020-11-16 12:49:46 +00:00
|
|
|
}
|
|
|
|
return "text/javascript; charset=utf-8"
|
|
|
|
}
|