Skip to content

Commit aa57988

Browse files
committed
internal/function: don't encode in base64 keys for uast cache and make uast cache size configurable through env var
Signed-off-by: Manuel Carmona <[email protected]>
1 parent 7fbe481 commit aa57988

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

docs/using-gitbase/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| `GITBASE_TRACE` | enable jaeger tracing, default disabled |
1414
| `GITBASE_READONLY` | allow read queries only, disabling creating and deleting indexes, default disabled |
1515
| `GITBASE_LANGUAGE_CACHE_SIZE` | size of the cache for the `language` UDF. The size is the maximum number of elements kept in the cache, 10000 by default |
16+
| `GITBASE_UAST_CACHE_SIZE` | size of the cache for the `uast` and `uast_mode` UDFs. The size is the maximum number of elements kept in the cache, 10000 by default |
1617

1718
### Jaeger tracing variables
1819

internal/function/uast.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"crypto/sha1"
55
"fmt"
66
"hash"
7+
"os"
8+
"strconv"
79
"strings"
810

911
lru "github.com/hashicorp/golang-lru"
@@ -16,13 +18,21 @@ import (
1618
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
1719
)
1820

19-
const defaultUASTCacheSize = 10000
21+
const (
22+
uastCacheSize = "GITBASE_UAST_CACHE_SIZE"
23+
defaultUASTCacheSize = 10000
24+
)
2025

2126
var uastCache *lru.Cache
2227

2328
func init() {
24-
var err error
25-
uastCache, err = lru.New(defaultUASTCacheSize)
29+
s := os.Getenv(uastCacheSize)
30+
size, err := strconv.Atoi(s)
31+
if err != nil || size <= 0 {
32+
size = defaultUASTCacheSize
33+
}
34+
35+
uastCache, err = lru.New(size)
2636
if err != nil {
2737
panic(fmt.Errorf("cannot initialize UAST cache: %s", err))
2838
}
@@ -246,7 +256,7 @@ func NewUAST(args ...sql.Expression) (sql.Expression, error) {
246256

247257
switch len(args) {
248258
default:
249-
return nil, sql.ErrInvalidArgumentNumber.New("1, 2, or 3", len(args))
259+
return nil, sql.ErrInvalidArgumentNumber.New("1, 2 or 3", len(args))
250260
case 3:
251261
xpath = args[2]
252262
fallthrough

internal/function/uast_utils.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package function
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"encoding/binary"
76
"fmt"
87
"hash"
@@ -64,7 +63,7 @@ func computeKey(h hash.Hash, mode, lang string, blob []byte) (string, error) {
6463
return "", err
6564
}
6665

67-
return base64.URLEncoding.EncodeToString(h.Sum(nil)), nil
66+
return string(h.Sum(nil)), nil
6867
}
6968

7069
func writeToHash(h hash.Hash, elements [][]byte) error {

0 commit comments

Comments
 (0)