mirror of
https://github.com/peco/peco.git
synced 2026-09-10 07:16:29 -04:00
We don't need this globally unique ID stuff
This wasn't a distributed system, oops.
This commit is contained in:
parent
d7c6aad237
commit
ca7dc1b6a5
|
|
@ -206,6 +206,8 @@ func doSelectAll(i *Input, _ termbox.Event) {
|
|||
for x := 0; x < b.Size(); x++ {
|
||||
if l, err := b.LineAt(x); err == nil {
|
||||
i.selection.Add(l)
|
||||
} else {
|
||||
i.selection.Remove(l)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -295,6 +297,8 @@ func doInvertSelection(i *Input, _ termbox.Event) {
|
|||
for x := 0; x < b.Size(); x++ {
|
||||
if l, err := b.LineAt(x); err == nil {
|
||||
i.selection.Add(l)
|
||||
} else {
|
||||
i.selection.Remove(l)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -153,10 +153,9 @@ func (rlb *RawLineBuffer) Append(l Line) (Line, error) {
|
|||
tracer.Printf("RawLineBuffer.Append: %s", l.DisplayString())
|
||||
if rlb.capacity > 0 && len(rlb.lines) > rlb.capacity {
|
||||
diff := len(rlb.lines) - rlb.capacity
|
||||
// TODO Notify that we're invalidating these lines
|
||||
|
||||
// Golang's version of array realloc
|
||||
rlb.lines = append(append([]Line(nil), rlb.lines[diff:]...), l)
|
||||
rlb.lines = rlb.lines[diff:rlb.capacity:rlb.capacity]
|
||||
} else {
|
||||
rlb.lines = append(rlb.lines, l)
|
||||
}
|
||||
|
|
|
|||
66
line.go
66
line.go
|
|
@ -3,68 +3,32 @@ package peco
|
|||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/btree"
|
||||
)
|
||||
|
||||
const (
|
||||
epochOffset = 946684800
|
||||
hostIDBits = 16
|
||||
serialBits = 12
|
||||
serialShift = 16
|
||||
timeBits = 36
|
||||
timeShift = 28
|
||||
)
|
||||
|
||||
type idGen struct {
|
||||
seed uint64
|
||||
mutex *sync.Mutex
|
||||
serialID int64
|
||||
timeID int64
|
||||
timeout int64
|
||||
genCh chan uint64
|
||||
}
|
||||
|
||||
func newIDGen(seed uint64) *idGen {
|
||||
func newIDGen() *idGen {
|
||||
ch := make(chan uint64)
|
||||
go func() {
|
||||
var i uint64
|
||||
for ; ; i++ {
|
||||
ch <- i
|
||||
if i >= uint64(1<<63)-1 {
|
||||
i = 0
|
||||
}
|
||||
}
|
||||
}()
|
||||
return &idGen{
|
||||
seed: seed,
|
||||
mutex: &sync.Mutex{},
|
||||
serialID: 1,
|
||||
timeID: time.Now().Unix(),
|
||||
genCh: ch,
|
||||
}
|
||||
}
|
||||
|
||||
func (ig *idGen) create() uint64 {
|
||||
ig.mutex.Lock()
|
||||
defer ig.mutex.Unlock()
|
||||
|
||||
timeID := time.Now().Unix()
|
||||
serialID := ig.serialID
|
||||
if ig.timeID == 0 {
|
||||
ig.timeID = timeID
|
||||
}
|
||||
|
||||
if ig.timeID == timeID {
|
||||
serialID++
|
||||
} else {
|
||||
serialID = 1
|
||||
}
|
||||
|
||||
if serialID >= (1<<serialBits)-1 {
|
||||
// Overflow:/ we recieved more than serialBits
|
||||
panic("Serial bits overflowed")
|
||||
}
|
||||
|
||||
ig.serialID = serialID
|
||||
ig.timeID = timeID
|
||||
|
||||
timeBits := (timeID - epochOffset) << timeShift
|
||||
serialBits := serialID << serialShift
|
||||
|
||||
id := uint64(timeBits) | uint64(serialBits) | ig.seed
|
||||
|
||||
return id
|
||||
return <-ig.genCh
|
||||
}
|
||||
|
||||
// Global var used to strips ansi sequences
|
||||
|
|
@ -116,7 +80,7 @@ type RawLine struct {
|
|||
dirty bool
|
||||
}
|
||||
|
||||
var idGenerator = newIDGen(uint64(time.Now().Unix()))
|
||||
var idGenerator = newIDGen()
|
||||
|
||||
func NewRawLine(v string, enableSep bool) *RawLine {
|
||||
id := idGenerator.create()
|
||||
|
|
|
|||
20
peco_test.go
20
peco_test.go
|
|
@ -1,6 +1,10 @@
|
|||
package peco
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type interceptorArgs []interface{}
|
||||
type interceptor struct {
|
||||
|
|
@ -35,3 +39,17 @@ func (i *interceptor) record(name string, args []interface{}) {
|
|||
events[name] = append(v, interceptorArgs(args))
|
||||
}
|
||||
|
||||
func TestIDGen(t *testing.T) {
|
||||
lines := []*RawLine{}
|
||||
for i := 0; i < 1000000; i++ {
|
||||
lines = append(lines, NewRawLine(fmt.Sprintf("%d", i), false))
|
||||
}
|
||||
|
||||
sel := NewSelection()
|
||||
for _, l := range lines {
|
||||
if sel.Has(l) {
|
||||
t.Errorf("Collision detected %d", l.ID())
|
||||
}
|
||||
sel.Add(l)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue