github.com/balzaczyy/golucene@v0.0.0-20151210033525-d0be9ee89713/core/util/filename.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"regexp"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  // index/IndexFileNames.java
    12  
    13  const (
    14  	SEGMENTS = "segments"
    15  )
    16  
    17  func FileNameFromGeneration(base, ext string, gen int64) string {
    18  	// log.Printf("Filename from generation: %v, %v, %v", base, ext, gen)
    19  	switch {
    20  	case gen == -1:
    21  		return ""
    22  	case gen == 0:
    23  		return SegmentFileName(base, "", ext)
    24  	default:
    25  		// assert gen > 0
    26  		// The '6' part in the length is: 1 for '.', 1 for '_' and 4 as estimate
    27  		// to the gen length as string (hopefully an upper limit so SB won't
    28  		// expand in the middle.
    29  		var buffer bytes.Buffer
    30  		fmt.Fprintf(&buffer, "%v_%v", base, strconv.FormatInt(gen, 36))
    31  		if len(ext) > 0 {
    32  			buffer.WriteString(".")
    33  			buffer.WriteString(ext)
    34  		}
    35  		return buffer.String()
    36  	}
    37  }
    38  
    39  func SegmentFileName(name, suffix, ext string) string {
    40  	if len(ext) > 0 || len(suffix) > 0 {
    41  		// assert ext[0] != '.'
    42  		var buffer bytes.Buffer
    43  		buffer.WriteString(name)
    44  		if len(suffix) > 0 {
    45  			buffer.WriteString("_")
    46  			buffer.WriteString(suffix)
    47  		}
    48  		if len(ext) > 0 {
    49  			buffer.WriteString(".")
    50  			buffer.WriteString(ext)
    51  		}
    52  		return buffer.String()
    53  	}
    54  	return name
    55  }
    56  
    57  func indexOfSegmentName(filename string) int {
    58  	// If it is a .del file, there's an '_' after the first character
    59  	if idx := strings.Index(filename[1:], "_"); idx >= 0 {
    60  		return idx + 1
    61  	}
    62  	// If it's not, strip everything that's before the '.'
    63  	return strings.Index(filename, ".")
    64  }
    65  
    66  func StripSegmentName(filename string) string {
    67  	if idx := indexOfSegmentName(filename); idx != -1 {
    68  		return filename[idx:]
    69  	}
    70  	return filename
    71  }
    72  
    73  func ParseSegmentName(filename string) string {
    74  	if idx := indexOfSegmentName(filename); idx != -1 {
    75  		return filename[0:idx]
    76  	}
    77  	return filename
    78  }
    79  
    80  func StripExtension(filename string) string {
    81  	if idx := strings.Index(filename, "."); idx != -1 {
    82  		return filename[0:idx]
    83  	}
    84  	return filename
    85  }
    86  
    87  /* Returns the generation from this file name, or 0 if there is no generation. */
    88  func ParseGeneration(filename string) int64 {
    89  	assert(strings.HasPrefix(filename, "_"))
    90  	parts := strings.Split(StripExtension(filename)[1:], "_")
    91  	// 4 cases:
    92  	// segment.ext
    93  	// segment_gen.ext
    94  	// segment_codec_suffix.ext
    95  	// segment_gen_codec_suffix.ext
    96  	if n := len(parts); n == 2 || n == 4 {
    97  		v, err := strconv.ParseInt(parts[1], 36, 64)
    98  		assert(err == nil)
    99  		return v
   100  	}
   101  	return 0
   102  }
   103  
   104  /*
   105  All files created by codecs must match this pattern (checked in SegmentInfo)
   106  */
   107  var CODEC_FILE_PATTERN = regexp.MustCompile("_[a-z0-9]+(_.*)?\\..*")