github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/docs/storage/commit_log/simkafka/kafka.go (about)

     1  // Copyright 2017 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package simkafka is a toy simulation of a Kafka commit log.
    16  package simkafka
    17  
    18  import (
    19  	"fmt"
    20  	"sync"
    21  )
    22  
    23  type commitLog []string
    24  
    25  const showCount = 10
    26  
    27  func (c commitLog) String() string {
    28  	result := ""
    29  	l := len(c)
    30  	start := l - showCount
    31  	if start < 0 {
    32  		start = 0
    33  	} else if start > 0 {
    34  		result += "... "
    35  	}
    36  	for i := start; i < l; i++ {
    37  		result += fmt.Sprintf("| %d:%s ", i, c[i])
    38  	}
    39  	result += "|"
    40  	return result
    41  }
    42  
    43  var mu sync.RWMutex
    44  var topics = make(map[string]commitLog)
    45  
    46  // Status reports the current status of the simulated Kafka instance
    47  func Status() string {
    48  	mu.RLock()
    49  	defer mu.RUnlock()
    50  	result := ""
    51  	for key, commit := range topics {
    52  		result += fmt.Sprintf("%s: %s\n", key, commit)
    53  	}
    54  	return result
    55  }
    56  
    57  // Read returns a value for a topic at a specific offset.
    58  func Read(which string, offset int) string {
    59  	mu.RLock()
    60  	defer mu.RUnlock()
    61  	topic, ok := topics[which]
    62  	if !ok {
    63  		return ""
    64  	}
    65  	if offset >= len(topic) {
    66  		return ""
    67  	}
    68  	return topic[offset]
    69  }
    70  
    71  // ReadLast returns the latest value for a topic, and its offset
    72  func ReadLast(which string) (string, int) {
    73  	mu.RLock()
    74  	defer mu.RUnlock()
    75  	topic, ok := topics[which]
    76  	if !ok {
    77  		return "", -1
    78  	}
    79  	offset := len(topic) - 1
    80  	return topic[offset], offset
    81  }
    82  
    83  // ReadMultiple reads values for a topic, starting at the given offset, up to the
    84  // given maximum number of results.
    85  func ReadMultiple(which string, offset, max int) []string {
    86  	mu.RLock()
    87  	defer mu.RUnlock()
    88  	topic, ok := topics[which]
    89  	if !ok {
    90  		return nil
    91  	}
    92  	if offset > len(topic) {
    93  		return nil
    94  	}
    95  	if offset+max > len(topic) {
    96  		max = len(topic) - offset
    97  	}
    98  	return topic[offset : offset+max]
    99  }
   100  
   101  // Append adds a value to the end of a topic, and returns the offset of the added value.
   102  func Append(which string, what string) int {
   103  	mu.Lock()
   104  	defer mu.Unlock()
   105  	topics[which] = append(topics[which], what)
   106  	return len(topics[which]) - 1
   107  }