@@ -316,7 +316,7 @@ func TestRandKeys(t *testing.T) {
316316 seed := uint64 (1234 )
317317 count := 10
318318 valueLen := 4
319- cfg := keyGenConfig {
319+ cfg := KeyGenConfig {
320320 PrefixAlphabetLen : 8 ,
321321 PrefixLenShared : 4 ,
322322 RoachKeyLen : 8 ,
@@ -344,7 +344,7 @@ func TestRandKeys(t *testing.T) {
344344 switch d .Cmd {
345345 case "rand-kvs" :
346346 var vals [][]byte
347- keys , vals = randomKVs (rng , count , cfg , valueLen )
347+ keys , vals = RandomKVs (rng , count , cfg , valueLen )
348348 for i , key := range keys {
349349 fmt .Fprintf (& buf , "%s = %X\n " , formatUserKey (key ), vals [i ])
350350 }
@@ -369,7 +369,7 @@ func TestCockroachDataColBlock(t *testing.T) {
369369 rng := rand .New (rand .NewPCG (0 , seed ))
370370
371371 for i := 0 ; i < 100 ; i ++ {
372- keyCfg := keyGenConfig {
372+ keyCfg := KeyGenConfig {
373373 PrefixAlphabetLen : 2 + rng .IntN (25 ),
374374 RoachKeyLen : 10 + int (rng .ExpFloat64 ()* 10 ),
375375 AvgKeysPerPrefix : 1 + int (rng .ExpFloat64 ()* []float64 {1 , 10 , 100 }[rng .IntN (3 )]),
@@ -400,7 +400,7 @@ func TestCockroachDataColBlock(t *testing.T) {
400400 }
401401}
402402
403- func testCockroachDataColBlock (t * testing.T , seed uint64 , keyCfg keyGenConfig ) {
403+ func testCockroachDataColBlock (t * testing.T , seed uint64 , keyCfg KeyGenConfig ) {
404404 const targetBlockSize = 32 << 10
405405 rng := rand .New (rand .NewPCG (0 , seed ))
406406 valueLen := 1 + rng .IntN (300 )
@@ -453,9 +453,9 @@ func testCockroachDataColBlock(t *testing.T, seed uint64, keyCfg keyGenConfig) {
453453// parameters. Returns the serialized block data and the keys and values
454454// written.
455455func generateDataBlock (
456- rng * rand.Rand , targetBlockSize int , cfg keyGenConfig , valueLen int ,
456+ rng * rand.Rand , targetBlockSize int , cfg KeyGenConfig , valueLen int ,
457457) (data []byte , keys [][]byte , values [][]byte ) {
458- keys , values = randomKVs (rng , targetBlockSize / valueLen , cfg , valueLen )
458+ keys , values = RandomKVs (rng , targetBlockSize / valueLen , cfg , valueLen )
459459
460460 var w colblk.DataBlockEncoder
461461 w .Init (& KeySchema )
@@ -478,80 +478,6 @@ func (g getInternalValuer) GetInternalValueForPrefixAndValueHandle(
478478 return g (handle )
479479}
480480
481- // keyGenConfig configures the shape of the random keys generated.
482- type keyGenConfig struct {
483- PrefixAlphabetLen int // Number of bytes in the alphabet used for the prefix.
484- PrefixLenShared int // Number of bytes shared by all key prefixes.
485- RoachKeyLen int // Number of bytes in the prefix (without the 0 sentinel byte).
486- AvgKeysPerPrefix int // Average number of keys (with varying suffixes) per prefix.
487- BaseWallTime uint64 // Smallest MVCC WallTime.
488- PercentLogical int // Percent of MVCC keys with non-zero MVCC logical time.
489- PercentEmptySuffix int // Percent of keys with empty suffix.
490- PercentLockSuffix int // Percent of keys with lock suffix.
491- }
492-
493- func (cfg keyGenConfig ) String () string {
494- return fmt .Sprintf (
495- "AlphaLen=%d,Prefix=%d,Shared=%d,KeysPerPrefix=%d%s" ,
496- cfg .PrefixAlphabetLen , cfg .RoachKeyLen , cfg .PrefixLenShared ,
497- cfg .AvgKeysPerPrefix ,
498- crstrings .If (cfg .PercentLogical != 0 , fmt .Sprintf (",Logical=%d" , cfg .PercentLogical )),
499- )
500- }
501-
502- // randomKVs constructs count random KVs with the provided parameters.
503- func randomKVs (rng * rand.Rand , count int , cfg keyGenConfig , valueLen int ) (keys , vals [][]byte ) {
504- g := makeCockroachKeyGen (rng , cfg )
505- sharedPrefix := make ([]byte , cfg .PrefixLenShared )
506- for i := 0 ; i < len (sharedPrefix ); i ++ {
507- sharedPrefix [i ] = byte (rng .IntN (cfg .PrefixAlphabetLen ) + 'a' )
508- }
509-
510- keys = make ([][]byte , 0 , count )
511- for len (keys ) < count {
512- roachKey := g .randRoachKey (sharedPrefix )
513- // We use the exponential distribution so that we occasionally have many
514- // suffixes
515- n := int (rng .ExpFloat64 () * float64 (cfg .AvgKeysPerPrefix ))
516- n = max (n , 1 )
517- for i := 0 ; i < n && len (keys ) < count ; i ++ {
518- if cfg .PercentEmptySuffix + cfg .PercentLockSuffix > 0 {
519- if r := rng .IntN (100 ); r < cfg .PercentEmptySuffix + cfg .PercentLockSuffix {
520- k := append (roachKey , 0 )
521- if r < cfg .PercentLockSuffix {
522- // Generate a lock key suffix.
523- for j := 0 ; j < engineKeyVersionLockTableLen ; j ++ {
524- k = append (k , byte (g .rng .IntN (g .cfg .PrefixAlphabetLen )+ 'a' ))
525- }
526- k = append (k , engineKeyVersionLockTableLen + 1 )
527- }
528- keys = append (keys , k )
529- continue
530- }
531- }
532-
533- wallTime , logicalTime := g .randTimestamp ()
534- k := makeMVCCKey (roachKey , wallTime , logicalTime )
535- keys = append (keys , k )
536- }
537- }
538- slices .SortFunc (keys , Compare )
539- vals = make ([][]byte , count )
540- for i := range vals {
541- v := make ([]byte , valueLen )
542- for j := range v {
543- v [j ] = byte (rng .Uint32 ())
544- }
545- vals [i ] = v
546- }
547- return keys , vals
548- }
549-
550- func makeMVCCKey (roachKey []byte , wallTime uint64 , logicalTime uint32 ) []byte {
551- k := slices .Grow (slices .Clip (roachKey ), MaxSuffixLen )
552- return EncodeTimestamp (k , wallTime , logicalTime )
553- }
554-
555481// randomQueryKeys returns a slice of count random query keys. Each key has a
556482// random prefix uniformly chosen from the distinct prefixes in writtenKeys and
557483// a random timestamp.
@@ -581,35 +507,6 @@ func randomQueryKeys(
581507 return result
582508}
583509
584- type cockroachKeyGen struct {
585- rng * rand.Rand
586- cfg keyGenConfig
587- }
588-
589- func makeCockroachKeyGen (rng * rand.Rand , cfg keyGenConfig ) cockroachKeyGen {
590- return cockroachKeyGen {
591- rng : rng ,
592- cfg : cfg ,
593- }
594- }
595-
596- func (g * cockroachKeyGen ) randRoachKey (blockPrefix []byte ) []byte {
597- roachKey := make ([]byte , 0 , g .cfg .RoachKeyLen + MaxSuffixLen )
598- roachKey = append (roachKey , blockPrefix ... )
599- for len (roachKey ) < g .cfg .RoachKeyLen {
600- roachKey = append (roachKey , byte (g .rng .IntN (g .cfg .PrefixAlphabetLen )+ 'a' ))
601- }
602- return roachKey
603- }
604-
605- func (g * cockroachKeyGen ) randTimestamp () (wallTime uint64 , logicalTime uint32 ) {
606- wallTime = g .cfg .BaseWallTime + g .rng .Uint64N (uint64 (time .Hour ))
607- if g .cfg .PercentLogical > 0 && g .rng .IntN (100 ) < g .cfg .PercentLogical {
608- logicalTime = g .rng .Uint32 ()
609- }
610- return wallTime , logicalTime
611- }
612-
613510// formatUserKey formats a user key in the format:
614511//
615512// <roach-key> [ @ <version-hex> ]
0 commit comments