github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/cmd/camtool/splits.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     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 main
    18  
    19  import (
    20  	"bufio"
    21  	"fmt"
    22  	"io"
    23  	"log"
    24  	"os"
    25  	"strings"
    26  
    27  	"camlistore.org/pkg/rollsum"
    28  )
    29  
    30  type span struct {
    31  	from, to int64
    32  	bits     int
    33  	children []span
    34  }
    35  
    36  func showSplits(file string) {
    37  	f, err := os.Open(file)
    38  	if err != nil {
    39  		panic(err.Error())
    40  	}
    41  	bufr := bufio.NewReader(f)
    42  
    43  	spans := []span{}
    44  	rs := rollsum.New()
    45  	n := int64(0)
    46  	last := n
    47  
    48  	for {
    49  		c, err := bufr.ReadByte()
    50  		if err != nil {
    51  			if err == io.EOF {
    52  				if n != last {
    53  					spans = append(spans, span{from: last, to: n})
    54  				}
    55  				break
    56  			}
    57  			panic(err.Error())
    58  		}
    59  		n++
    60  		rs.Roll(c)
    61  		if rs.OnSplit() {
    62  			bits := rs.Bits()
    63  			sliceFrom := len(spans)
    64  			for sliceFrom > 0 && spans[sliceFrom-1].bits < bits {
    65  				sliceFrom--
    66  			}
    67  			nCopy := len(spans) - sliceFrom
    68  			var children []span
    69  			if nCopy > 0 {
    70  				children = make([]span, nCopy)
    71  				nCopied := copy(children, spans[sliceFrom:])
    72  				if nCopied != nCopy {
    73  					panic("n wrong")
    74  				}
    75  				spans = spans[:sliceFrom]
    76  			}
    77  			spans = append(spans, span{from: last, to: n, bits: bits, children: children})
    78  
    79  			log.Printf("split at %d (after %d), bits=%d", n, n-last, bits)
    80  			last = n
    81  		}
    82  	}
    83  
    84  	var dumpSpans func(s []span, indent int)
    85  	dumpSpans = func(s []span, indent int) {
    86  		in := strings.Repeat(" ", indent)
    87  		for _, sp := range s {
    88  			fmt.Printf("%sfrom=%d, to=%d (len %d) bits=%d\n", in, sp.from, sp.to, sp.to-sp.from, sp.bits)
    89  			if len(sp.children) > 0 {
    90  				dumpSpans(sp.children, indent+4)
    91  			}
    92  		}
    93  	}
    94  	dumpSpans(spans, 0)
    95  	fmt.Printf("\n\nNOTE NOTE NOTE: the camdebug tool hasn't been updated to use the splitting policy from pkg/schema/filewriter.go.")
    96  }