github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/utils/segutils.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package utils
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/gob"
    22  	"fmt"
    23  	"math/rand"
    24  
    25  	"github.com/cespare/xxhash"
    26  	"github.com/rogpeppe/fastuuid"
    27  )
    28  
    29  var UUID_GENERATOR *fastuuid.Generator
    30  
    31  var MAX_SHARDS = int(1)
    32  
    33  var single_whitespace = []byte(" ")
    34  
    35  func init() {
    36  	var err error
    37  	UUID_GENERATOR, err = fastuuid.NewGenerator()
    38  	if err != nil {
    39  		panic(err)
    40  	}
    41  }
    42  
    43  func EncodeGOB(node any) ([]byte, error) {
    44  	var buf bytes.Buffer
    45  	e := gob.NewEncoder(&buf)
    46  	err := e.Encode(node)
    47  	if err != nil {
    48  		return nil, err
    49  	}
    50  	return buf.Bytes(), nil
    51  }
    52  
    53  func DecodeGOB(raw []byte, node any) error {
    54  	buf := bytes.NewBuffer(raw)
    55  	d := gob.NewDecoder(buf)
    56  	err := d.Decode(node)
    57  	if err != nil {
    58  		return err
    59  	}
    60  	return nil
    61  }
    62  
    63  func CreateStreamId(indexName string, orgId uint64) string {
    64  	// todo this still has a issue of having 50 shards per index, we need to cap it somehow
    65  	return fmt.Sprintf("%d-%v-%v", rand.Intn(MAX_SHARDS), orgId, xxhash.Sum64String(indexName))
    66  }
    67  
    68  func CreateStreamIdForMetrics(mname *string) string {
    69  	return fmt.Sprintf("%d", xxhash.Sum64String(*mname)%uint64(MAX_SHARDS))
    70  }
    71  
    72  func CreateUniqueIndentifier() string {
    73  	return UUID_GENERATOR.Hex128()
    74  }
    75  
    76  func Max(x, y uint64) uint64 {
    77  	if x < y {
    78  		return y
    79  	}
    80  	return x
    81  }
    82  
    83  func Min(x, y uint64) uint64 {
    84  	if x > y {
    85  		return y
    86  	}
    87  	return x
    88  }
    89  
    90  func MaxInt64(x, y int64) int64 {
    91  	if x < y {
    92  		return y
    93  	}
    94  	return x
    95  }
    96  
    97  func MinInt64(x, y int64) int64 {
    98  	if x > y {
    99  		return y
   100  	}
   101  	return x
   102  }
   103  
   104  func HashString(x string) string {
   105  	return fmt.Sprintf("%d", xxhash.Sum64String(x))
   106  }
   107  
   108  // we are assumung that needleLen and haystackLen are both non zero
   109  func IsSubWordPresent(haystack []byte, needle []byte) bool {
   110  	needleLen := len(needle)
   111  	haystackLen := len(haystack)
   112  
   113  	if needleLen > haystackLen {
   114  		return false
   115  	} else if needleLen == haystackLen {
   116  		return bytes.Equal(needle, haystack)
   117  	}
   118  
   119  	for i := 0; i < haystackLen-needleLen+1; i += 1 {
   120  		if haystack[i] == needle[0] {
   121  			for j := needleLen - 1; j >= 1; j -= 1 {
   122  				if haystack[i+j] != needle[j] {
   123  					break
   124  				}
   125  				if j == 1 {
   126  					// haystack[i:i+needleLen-1] was matched
   127  					// we need to check if haystack[i - 1] is a whitespace and if haystack[i + needleLen] is a whitespace
   128  					if i-1 >= 0 && haystack[i-1] != single_whitespace[0] {
   129  						break
   130  					}
   131  					if i+needleLen < haystackLen && haystack[i+needleLen] != single_whitespace[0] {
   132  						break
   133  					}
   134  					return true
   135  				}
   136  			}
   137  		}
   138  	}
   139  
   140  	return false
   141  }