Skip to content

Commit

Permalink
Entry: Expose modified timestamp of the runtime entry (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodaine authored Nov 8, 2017
1 parent e94d1ca commit 426ae35
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 21 deletions.
15 changes: 10 additions & 5 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,22 @@ func (l *Loader) walkDirectoryCallback(path string, info os.FileInfo, err error)

key = strings.Replace(key, "/", ".", -1)
stringValue := string(contents)
entry := entry.New(stringValue, 0, false)
e := &entry.Entry{
StringValue: stringValue,
Uint64Value: 0,
Uint64Valid: false,
Modified: info.ModTime(),
}

uint64Value, err := strconv.ParseUint(strings.TrimSpace(stringValue), 10, 64)
if err == nil {
entry.Uint64Value = uint64Value
entry.Uint64Valid = true
e.Uint64Value = uint64Value
e.Uint64Valid = true
}

logger.Debugf("runtime: adding key=%s value=%s uint=%t", key,
stringValue, entry.Uint64Valid)
l.nextSnapshot.SetEntry(key, entry)
stringValue, e.Uint64Valid)
l.nextSnapshot.SetEntry(key, e)
}

return nil
Expand Down
3 changes: 3 additions & 0 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func TestSymlinkRefresher(t *testing.T) {
assert.Equal(uint64(7), snapshot.GetInteger("dir.file2", 7))
assert.Equal(uint64(34), snapshot.GetInteger("dir2.file3", 100))

info, _ := os.Stat(tempDir + "/testdir1/app/file1")
assert.Equal(info.ModTime(), snapshot.GetModified("file1"))

keys := snapshot.Keys()
sort.Strings(keys)
assert.EqualValues([]string{"dir.file2", "dir2.file3", "file1"}, keys)
Expand Down
9 changes: 3 additions & 6 deletions snapshot/entry/entry.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package entry

import "time"

// An individual snapshot entry. Optimized for integers by pre-converting them if possible.
type Entry struct {
StringValue string
Uint64Value uint64
Uint64Valid bool
}

func New(s string, ui uint64, v bool) (e *Entry) {
e = &Entry{s, ui, v}

return
Modified time.Time
}
10 changes: 9 additions & 1 deletion snapshot/iface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package snapshot

import "github.com/lyft/goruntime/snapshot/entry"
import (
"time"

"github.com/lyft/goruntime/snapshot/entry"
)

// Snapshot provides the currently loaded set of runtime values.
type IFace interface {
Expand Down Expand Up @@ -37,6 +41,10 @@ type IFace interface {
// @return uint64 the runtime value or the default value.
GetInteger(key string, defaultValue uint64) uint64

// GetModified returns the last modified timestamp for key. If key does not
// exist, the zero value for time.Time is returned.
GetModified(key string) time.Time

// Fetch all keys inside the snapshot.
// @return []string all of the keys.
Keys() []string
Expand Down
34 changes: 29 additions & 5 deletions snapshot/mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package snapshot

import "github.com/lyft/goruntime/snapshot/entry"
import (
"time"

"github.com/lyft/goruntime/snapshot/entry"
)

// Mock provides a Snapshot implementation for testing
type Mock struct {
Expand All @@ -18,28 +22,48 @@ func NewMock() (s *Mock) {

// SetEnabled overrides the entry for `key` to be enabled
func (m *Mock) SetEnabled(key string) *Mock {
m.Snapshot.entries[key] = entry.New(key, 0, true)
m.Snapshot.entries[key] = &entry.Entry{
StringValue: key,
Uint64Value: 0,
Uint64Valid: true,
Modified: time.Now(),
}

return m
}

// SetDisabled overrides the entry for `key` to be disabled
func (m *Mock) SetDisabled(key string) *Mock {
m.Snapshot.entries[key] = entry.New(key, 0, false)
m.Snapshot.entries[key] = &entry.Entry{
StringValue: key,
Uint64Value: 0,
Uint64Valid: false,
Modified: time.Now(),
}

return m
}

// Set set the entry for `key` to `val`
func (m *Mock) Set(key string, val string) *Mock {
m.Snapshot.entries[key] = entry.New(val, 0, false)
m.Snapshot.entries[key] = &entry.Entry{
StringValue: val,
Uint64Value: 0,
Uint64Valid: false,
Modified: time.Now(),
}

return m
}

// SetUInt64 set the entry for `key` to `val` as a uint64
func (m *Mock) SetUInt64(key string, val uint64) *Mock {
m.Snapshot.entries[key] = entry.New("", val, true)
m.Snapshot.entries[key] = &entry.Entry{
StringValue: "",
Uint64Value: val,
Uint64Valid: true,
Modified: time.Now(),
}

return m
}
Expand Down
12 changes: 8 additions & 4 deletions snapshot/nil.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package snapshot

import "github.com/lyft/goruntime/snapshot/entry"
import (
"time"

"github.com/lyft/goruntime/snapshot/entry"
)

// Implementation of Snapshot for the nilLoaderImpl.
type Nil struct{}
Expand All @@ -21,9 +25,9 @@ func (n *Nil) FeatureEnabledForID(key string, id uint64, defaultPercentage uint3

func (n *Nil) Get(key string) string { return "" }

func (n *Nil) GetInteger(key string, defaultValue uint64) uint64 {
return defaultValue
}
func (n *Nil) GetInteger(key string, defaultValue uint64) uint64 { return defaultValue }

func (n *Nil) GetModified(key string) time.Time { return time.Time{} }

func (n *Nil) Keys() []string {
return []string{}
Expand Down
10 changes: 10 additions & 0 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ func (s *Snapshot) GetInteger(key string, defaultValue uint64) uint64 {
}
}

// GetModified returns the last modified timestamp for key. If key does not
// exist, the zero value for time.Time is returned.
func (s *Snapshot) GetModified(key string) time.Time {
if e, ok := s.entries[key]; ok {
return e.Modified
}

return time.Time{}
}

func (s *Snapshot) Keys() []string {
ret := []string{}
for key, _ := range s.entries {
Expand Down
11 changes: 11 additions & 0 deletions snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/lyft/goruntime/snapshot/entry"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -48,3 +49,13 @@ func TestSnapshot_FeatureEnabledForIDDisabled(t *testing.T) {
assert.True(t, ss.FeatureEnabledForID(key, 1, 100))
assert.False(t, ss.FeatureEnabledForID(key, 1, 0))
}

func TestSnapshot_GetModified(t *testing.T) {
ss := NewMock()

assert.True(t, ss.GetModified("foo").IsZero())

now := time.Now()
ss.entries["foo"] = &entry.Entry{Modified: now}
assert.Equal(t, now, ss.GetModified("foo"))
}

0 comments on commit 426ae35

Please sign in to comment.