Address comments

This commit is contained in:
ngaut
2015-12-22 13:50:57 +08:00
parent 4659488476
commit f91303cfd4
2 changed files with 6 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import (
)
// SegmentMap is used for handle a big map slice by slice
// It's not thread safe
type SegmentMap struct {
size int
maps []map[string]interface{}
@ -48,17 +49,17 @@ func (sm *SegmentMap) Get(key []byte) (interface{}, bool) {
// GetSegment gets the map specific by index
func (sm *SegmentMap) GetSegment(index int) (map[string]interface{}, error) {
if index >= len(sm.maps) {
return nil, errors.Errorf("index out of bound")
return nil, errors.Errorf("index out of bound: %d", index)
}
return sm.maps[index], nil
}
// Set if empty, return whether already exists
func (sm *SegmentMap) Set(key []byte, value interface{}, force bool) (exist bool) {
func (sm *SegmentMap) Set(key []byte, value interface{}, force bool) bool {
idx := int(crc32.ChecksumIEEE(key)) % sm.size
k := string(key)
_, exist = sm.maps[idx][k]
_, exist := sm.maps[idx][k]
if exist && !force {
return exist
}

View File

@ -29,14 +29,14 @@ func (s *testSegmentMapSuite) TestSegment(c *C) {
k := []byte("k")
v := []byte("v")
val, exist := m.Get(k)
c.Assert(exist, Equals, false)
c.Assert(exist, IsFalse)
exist = m.Set(k, v, false)
c.Assert(exist, IsFalse)
val, exist = m.Get(k)
c.Assert(v, DeepEquals, val.([]byte))
c.Assert(exist, Equals, true)
c.Assert(exist, IsTrue)
m0, err := m.GetSegment(0)
c.Assert(err, IsNil)