Giter Site home page Giter Site logo

go-matcher's Introduction

go-matcher - graphite glob/seriesByTag expressions batch match engine for Go

go-matcher is a graphite glob/seriesByTag expressions batch match engine for Go. It doesn't have constant time guarantees like the built-in regexp package, but it allows backtracking and is compatible with regexp package.

Basis of the engine

Contains 2 parts:

  • glob - graphite glob expressions match engine (no dot-separation)
  • gglob - graphite glob expressions match engine (dot-separated)
  • gtags - graphite tags (with seriesByTag) expressions match engine
  • expand - expand graphite glob expressions

Installing

This is a go-gettable library, so install is easy:

go get github.com/msaf1980/go-matcher/gglob
go get github.com/msaf1980/go-matcher/gtags

Usage

gglob

  w:= gglob.NewGlobMatcher()
  err = w.Adds([]string{"a.b", "d.*"})
  if err != nil {
    ...
  }
  
  // get matched globs (also contain normalized versions of globs)
  matchedGlobs := w.Match(path)
  
  // use preallocated slice (for better perfomance
  // var matchedGlobs []string
  matchedGlobs := matchedGlobs[:0]
  w.MatchB(path, &matchedGlobs)

With splitted path parts

  w:= gglob.NewGlobMatcher()
  err = w.Adds([]string{"a.b", "d.*"})
  if err != nil {
    ...
  }
  
  parts := wildcards.PathSplit(path)
  
  // get matched globs (also contain normalized versions of globs)
  matchedGlobs := w.MatchByParts(parts)
  
  // use preallocated slice (for better perfomance
  // var matchedGlobs []string
  matchedGlobs := matchedGlobs[:0]
  w.MatchByPartsB(parts, &matchedGlobs)

Get macthed globs index

  var buf strings.Builder
  buf.Grow(128)

  globs := []string{"a.b", "d.*"}
  w:= gglob.NewGlobMatcher()
  for i, glob := range globs {
    _, err = w.AddIndexed(glob, i, &buf)
    if err != nil {
      ...
    }
  }
    
  // get matched globs indexes
  matchedIndex := w.MatchIndexed(path)
  
  // use preallocated slice (for better perfomance)
  // var matchedIndex []int
  matchedIndex := matchedIndex[:0]
  w.MatchB(path, &matchedIndex)  
  
  // get first mached (with lowest index number)
  first := -1
  w.MatchFirst(path, &first)

Get macthed globs index with splitted path parts

  var buf strings.Builder
  buf.Grow(128)

  globs := []string{"a.b", "d.*"}
  w:= gglob.NewGlobMatcher()
  for i, glob := range globs {
    _, err = w.AddIndexed(glob, i, &buf)
    if err != nil {
      ...
    }
  }
  
  parts := wildcards.PathSplit(path)
  
  // get matched globs indexes
  matchedIndex := w.MatchIndexedByParts(parts)
  
  // use preallocated slice (for better perfomance)
  // var matchedIndex []int
  matchedIndex := matchedIndex[:0]
  w.MatchByPartsB(parts, &matchedIndex)  
  
  // get first mached (with lowest index number)
  first := -1
  w.MatchFirstByParts(parts, &first)

gtags

  w:= gtags.NewTagsMatcher()
  err = w.Adds([]string{`seriesByTag('__name__=a.b','b=d.*', '__name__=a.b','b=e')`})
  if err != nil {
    ...
  }
  
  tags, err := PathTags(path)
  if err != nil {
    ..
  }
  
  // get matched globs (also contain normalized versions of globs)
  matchedQueries := w.MatchByTags(tags)
  
  // use preallocated slice (for better perfomance
  // var matchedQueries []string
  matchedQueries := matchedQueries[:0]
  w.MatchByTagsB(tags, &matchedQueries)

Get macthed globs index

  var buf strings.Builder
  buf.Grow(128)

  queries := []string{`seriesByTag('__name__=a.b','b=d.*', '__name__=a.b','b=e')`}
  w:= gtags.NewTagsMatcher()
  for i, query := range queries {
    _, err = w.AddIndexed(glob, i, &buf)
    if err != nil {
      ...
    }
  }
  
  // get matched globs indexes
  matchedIndex := w.MatchIndexedByTags(path)
  
  // use preallocated slice (for better perfomance)
  // var matchedIndex []int
  matchedIndex := matchedIndex[:0]
  w.MatchByTagsB(path, &matchedIndex)  
  
  // get first mached (with lowest index number)
  first := -1
  w.MatchFirstByTags(path, &first)

expand

See documentation.

go-matcher's People

Contributors

msaf1980 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.