Skip to content

Commit

Permalink
Merge pull request #6587 from influxdata/jw-validate-fields
Browse files Browse the repository at this point in the history
Fix for merge values
  • Loading branch information
jwilder committed May 10, 2016
2 parents 9b86bfe + 8839cab commit d8490f1
Show file tree
Hide file tree
Showing 3 changed files with 255 additions and 63 deletions.
10 changes: 9 additions & 1 deletion services/meta/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,11 +694,19 @@ func (c *Client) DropShard(id uint64) error {

// CreateShardGroup creates a shard group on a database and policy for a given timestamp.
func (c *Client) CreateShardGroup(database, policy string, timestamp time.Time) (*ShardGroupInfo, error) {
// Check under a read-lock
c.mu.RLock()
if sg, _ := c.cacheData.ShardGroupByTimestamp(database, policy, timestamp); sg != nil {
c.mu.RUnlock()
return sg, nil
}
c.mu.RUnlock()

c.mu.Lock()
defer c.mu.Unlock()

// Check again under the write lock
data := c.cacheData.Clone()

if sg, _ := data.ShardGroupByTimestamp(database, policy, timestamp); sg != nil {
return sg, nil
}
Expand Down
227 changes: 165 additions & 62 deletions tsdb/engine/tsm1/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,45 @@ func (a Values) Merge(b Values) Values {
return a
}

var i, j int
for ; i < len(a) && j < len(b); i++ {
av, bv := a[i].UnixNano(), b[j].UnixNano()
for i := 0; i < len(a) && len(b) > 0; i++ {
av, bv := a[i].UnixNano(), b[0].UnixNano()
// Value in a is greater than B, we need to merge
if av > bv {
a[i], b[j] = b[j], a[i]
// Save value in a
temp := a[i]

// Overwrite a with b
a[i] = b[0]

// Slide all values of b down 1
copy(b, b[1:])
b = b[:len(b)-1]

// See where value we save from a should be inserted in b to keep b sorted
k := sort.Search(len(b), func(i int) bool { return b[i].UnixNano() >= temp.UnixNano() })

if k == len(b) {
// Last position?
b = append(b, temp)
} else if b[k].UnixNano() != temp.UnixNano() {
// Save the last element, since it will get overwritten
last := b[len(b)-1]
// Somewhere in the middle of b, insert it only if it's not a duplicate
copy(b[k+1:], b[k:])
// Add the last vale to the end
b = append(b, last)
b[k] = temp
}
} else if av == bv {
a[i] = b[j]
j++
// Value in a an b are the same, use b
a[i] = b[0]
b = b[1:]
}
}

sort.Sort(b[j:])

if i >= len(a) {
if j+1 < len(b) && b[j].UnixNano() == b[j+1].UnixNano() {
j++
}
return append(a, b[j:]...)
if len(b) > 0 {
return append(a, b...)
}

return a
}

Expand Down Expand Up @@ -442,24 +461,45 @@ func (a FloatValues) Merge(b FloatValues) FloatValues {
return a
}

var i, j int
for ; i < len(a) && j < len(b); i++ {
av, bv := a[i].UnixNano(), b[j].UnixNano()
for i := 0; i < len(a) && len(b) > 0; i++ {
av, bv := a[i].UnixNano(), b[0].UnixNano()
// Value in a is greater than B, we need to merge
if av > bv {
a[i], b[j] = b[j], a[i]
// Save value in a
temp := a[i]

// Overwrite a with b
a[i] = b[0]

// Slide all values of b down 1
copy(b, b[1:])
b = b[:len(b)-1]

// See where value we save from a should be inserted in b to keep b sorted
k := sort.Search(len(b), func(i int) bool { return b[i].UnixNano() >= temp.UnixNano() })

if k == len(b) {
// Last position?
b = append(b, temp)
} else if b[k].UnixNano() != temp.UnixNano() {
// Save the last element, since it will get overwritten
last := b[len(b)-1]
// Somewhere in the middle of b, insert it only if it's not a duplicate
copy(b[k+1:], b[k:])
// Add the last vale to the end
b = append(b, last)
b[k] = temp
}
} else if av == bv {
a[i] = b[j]
j++
// Value in a an b are the same, use b
a[i] = b[0]
b = b[1:]
}
}

if i >= len(a) {
if j+1 < len(b) && b[j].UnixNano() == b[j+1].UnixNano() {
j++
}
return append(a, b[j:]...)
if len(b) > 0 {
return append(a, b...)
}

return a
}

Expand Down Expand Up @@ -655,24 +695,45 @@ func (a BooleanValues) Merge(b BooleanValues) BooleanValues {
return a
}

var i, j int
for ; i < len(a) && j < len(b); i++ {
av, bv := a[i].UnixNano(), b[j].UnixNano()
for i := 0; i < len(a) && len(b) > 0; i++ {
av, bv := a[i].UnixNano(), b[0].UnixNano()
// Value in a is greater than B, we need to merge
if av > bv {
a[i], b[j] = b[j], a[i]
// Save value in a
temp := a[i]

// Overwrite a with b
a[i] = b[0]

// Slide all values of b down 1
copy(b, b[1:])
b = b[:len(b)-1]

// See where value we save from a should be inserted in b to keep b sorted
k := sort.Search(len(b), func(i int) bool { return b[i].UnixNano() >= temp.UnixNano() })

if k == len(b) {
// Last position?
b = append(b, temp)
} else if b[k].UnixNano() != temp.UnixNano() {
// Save the last element, since it will get overwritten
last := b[len(b)-1]
// Somewhere in the middle of b, insert it only if it's not a duplicate
copy(b[k+1:], b[k:])
// Add the last vale to the end
b = append(b, last)
b[k] = temp
}
} else if av == bv {
a[i] = b[j]
j++
// Value in a an b are the same, use b
a[i] = b[0]
b = b[1:]
}
}

if i >= len(a) {
if j+1 < len(b) && b[j].UnixNano() == b[j+1].UnixNano() {
j++
}
return append(a, b[j:]...)
if len(b) > 0 {
return append(a, b...)
}

return a
}

Expand Down Expand Up @@ -828,24 +889,45 @@ func (a IntegerValues) Merge(b IntegerValues) IntegerValues {
return a
}

var i, j int
for ; i < len(a) && j < len(b); i++ {
av, bv := a[i].UnixNano(), b[j].UnixNano()
for i := 0; i < len(a) && len(b) > 0; i++ {
av, bv := a[i].UnixNano(), b[0].UnixNano()
// Value in a is greater than B, we need to merge
if av > bv {
a[i], b[j] = b[j], a[i]
// Save value in a
temp := a[i]

// Overwrite a with b
a[i] = b[0]

// Slide all values of b down 1
copy(b, b[1:])
b = b[:len(b)-1]

// See where value we save from a should be inserted in b to keep b sorted
k := sort.Search(len(b), func(i int) bool { return b[i].UnixNano() >= temp.UnixNano() })

if k == len(b) {
// Last position?
b = append(b, temp)
} else if b[k].UnixNano() != temp.UnixNano() {
// Save the last element, since it will get overwritten
last := b[len(b)-1]
// Somewhere in the middle of b, insert it only if it's not a duplicate
copy(b[k+1:], b[k:])
// Add the last vale to the end
b = append(b, last)
b[k] = temp
}
} else if av == bv {
a[i] = b[j]
j++
// Value in a an b are the same, use b
a[i] = b[0]
b = b[1:]
}
}

if i >= len(a) {
if j+1 < len(b) && b[j].UnixNano() == b[j+1].UnixNano() {
j++
}
return append(a, b[j:]...)
if len(b) > 0 {
return append(a, b...)
}

return a
}

Expand Down Expand Up @@ -1003,24 +1085,45 @@ func (a StringValues) Merge(b StringValues) StringValues {
return a
}

var i, j int
for ; i < len(a) && j < len(b); i++ {
av, bv := a[i].UnixNano(), b[j].UnixNano()
for i := 0; i < len(a) && len(b) > 0; i++ {
av, bv := a[i].UnixNano(), b[0].UnixNano()
// Value in a is greater than B, we need to merge
if av > bv {
a[i], b[j] = b[j], a[i]
// Save value in a
temp := a[i]

// Overwrite a with b
a[i] = b[0]

// Slide all values of b down 1
copy(b, b[1:])
b = b[:len(b)-1]

// See where value we save from a should be inserted in b to keep b sorted
k := sort.Search(len(b), func(i int) bool { return b[i].UnixNano() >= temp.UnixNano() })

if k == len(b) {
// Last position?
b = append(b, temp)
} else if b[k].UnixNano() != temp.UnixNano() {
// Save the last element, since it will get overwritten
last := b[len(b)-1]
// Somewhere in the middle of b, insert it only if it's not a duplicate
copy(b[k+1:], b[k:])
// Add the last vale to the end
b = append(b, last)
b[k] = temp
}
} else if av == bv {
a[i] = b[j]
j++
// Value in a an b are the same, use b
a[i] = b[0]
b = b[1:]
}
}

if i >= len(a) {
if j+1 < len(b) && b[j].UnixNano() == b[j+1].UnixNano() {
j++
}
return append(a, b[j:]...)
if len(b) > 0 {
return append(a, b...)
}

return a
}

Expand Down
Loading

0 comments on commit d8490f1

Please sign in to comment.