-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from hashibuto/prepare_v2
update to v1
- Loading branch information
Showing
30 changed files
with
1,845 additions
and
790 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
v0.2.7 | ||
v1.0.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package ns | ||
|
||
import ( | ||
"sort" | ||
|
||
"github.com/hashibuto/nimble" | ||
) | ||
|
||
type BasicHistoryManager struct { | ||
index *nimble.IndexedDequeue | ||
maxKeep int | ||
prev string | ||
} | ||
|
||
type BasicHistoryIterator struct { | ||
iter *nimble.IndexedDequeIterator | ||
} | ||
|
||
func (bhi *BasicHistoryIterator) Forward() string { | ||
if bhi.iter == nil { | ||
return "" | ||
} | ||
|
||
bhi.iter.Next() | ||
return bhi.iter.Value() | ||
} | ||
|
||
func (bhi *BasicHistoryIterator) Backward() string { | ||
if bhi.iter == nil { | ||
return "" | ||
} | ||
|
||
bhi.iter.ReverseNext() | ||
return bhi.iter.Value() | ||
} | ||
|
||
func NewBasicHistoryManager(maxKeep int) *BasicHistoryManager { | ||
return &BasicHistoryManager{ | ||
index: nimble.NewIndexedDequeue(), | ||
maxKeep: maxKeep, | ||
} | ||
} | ||
|
||
func (h *BasicHistoryManager) Push(value string) { | ||
if value == h.prev { | ||
return | ||
} | ||
h.prev = value | ||
h.index.Push(value) | ||
if h.index.Size() > h.maxKeep { | ||
h.index.Pop() | ||
} | ||
} | ||
|
||
func (h *BasicHistoryManager) GetIterator() HistoryIterator { | ||
if h.index.Size() > 0 { | ||
return &BasicHistoryIterator{ | ||
iter: h.index.GetIter(), | ||
} | ||
} | ||
|
||
return &BasicHistoryIterator{} | ||
} | ||
|
||
func (h *BasicHistoryManager) Search(pattern string) []string { | ||
if h.index.Size() == 0 { | ||
return nil | ||
} | ||
|
||
if len(pattern) == 0 { | ||
return nil | ||
} | ||
|
||
links := h.index.Find(pattern) | ||
if len(links) == 0 { | ||
return nil | ||
} | ||
|
||
// sort from most recent to oldest | ||
sort.Slice(links, func(i, j int) bool { | ||
return links[i].CreatedAt.After(links[j].CreatedAt) | ||
}) | ||
|
||
strs := make([]string, len(links)) | ||
for i, link := range links { | ||
strs[i] = link.Value | ||
} | ||
|
||
return strs | ||
} | ||
|
||
func (h *BasicHistoryManager) Exit() { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ns | ||
|
||
import ( | ||
"os" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
type FileLock struct { | ||
filename string | ||
f *os.File | ||
} | ||
|
||
func NewFileLock(filename string) *FileLock { | ||
return &FileLock{ | ||
filename: filename, | ||
} | ||
} | ||
|
||
func (fl *FileLock) Lock() error { | ||
f, err := os.OpenFile(fl.filename, os.O_RDWR|os.O_CREATE, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fl.f = f | ||
|
||
err = unix.Flock(int(fl.f.Fd()), unix.LOCK_EX) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (fl *FileLock) Unlock() error { | ||
defer fl.f.Close() | ||
|
||
err := unix.Flock(int(fl.f.Fd()), unix.LOCK_UN) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ns | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFileLockSeq(t *testing.T) { | ||
l := NewFileLock("/tmp/test.lock") | ||
err := l.Lock() | ||
assert.NoError(t, err) | ||
err = l.Unlock() | ||
assert.NoError(t, err) | ||
|
||
err = l.Lock() | ||
assert.NoError(t, err) | ||
err = l.Unlock() | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestFileLockConcur(t *testing.T) { | ||
l := NewFileLock("/tmp/test.lock") | ||
err := l.Lock() | ||
assert.NoError(t, err) | ||
go func() { | ||
time.Sleep(1 * time.Second) | ||
err = l.Unlock() | ||
assert.NoError(t, err) | ||
}() | ||
|
||
l2 := NewFileLock("/tmp/test.lock") | ||
err = l2.Lock() | ||
assert.NoError(t, err) | ||
err = l2.Unlock() | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.