now it will sort recursively, and the sorting test case is better....

This commit is contained in:
evan-gu 2013-09-08 23:13:28 -04:00
parent 38489bd846
commit 643a92a490
5 changed files with 74 additions and 19 deletions

View File

@ -36,17 +36,17 @@ type KeyValuePair struct {
KVPairs []KeyValuePair `json:"kvs,omitempty"`
}
// interfaces for sort
func (e Event) Len() int {
return len(e.KVPairs)
// interfaces for sorting
func (k KeyValuePair) Len() int {
return len(k.KVPairs)
}
func (e Event) Less(i, j int) bool {
return e.KVPairs[i].Key < e.KVPairs[j].Key
func (k KeyValuePair) Less(i, j int) bool {
return k.KVPairs[i].Key < k.KVPairs[j].Key
}
func (e Event) Swap(i, j int) {
e.KVPairs[i], e.KVPairs[j] = e.KVPairs[j], e.KVPairs[i]
func (k KeyValuePair) Swap(i, j int) {
k.KVPairs[i], k.KVPairs[j] = k.KVPairs[j], k.KVPairs[i]
}
func newEvent(action string, key string, index uint64, term uint64) *Event {

View File

@ -26,7 +26,7 @@ func New() *FileSystem {
}
func (fs *FileSystem) Get(nodePath string, recusive, sorted bool, index uint64, term uint64) (*Event, error) {
func (fs *FileSystem) Get(nodePath string, recursive, sorted bool, index uint64, term uint64) (*Event, error) {
n, err := fs.InternalGet(nodePath, index, term)
if err != nil {
@ -51,16 +51,22 @@ func (fs *FileSystem) Get(nodePath string, recusive, sorted bool, index uint64,
continue
}
e.KVPairs[i] = child.Pair(recusive)
e.KVPairs[i] = child.Pair(recursive, sorted)
i++
}
// eliminate hidden nodes
e.KVPairs = e.KVPairs[:i]
if sorted {
sort.Sort(e)
rootPairs := KeyValuePair{
KVPairs: e.KVPairs,
}
if sorted {
sort.Sort(rootPairs)
}
} else { // node is file
e.Value = n.Value
}

View File

@ -1,7 +1,8 @@
package fileSystem
import (
"store"
"math/rand"
"strconv"
"testing"
"time"
)
@ -408,16 +409,17 @@ func TestSort(t *testing.T) {
fs := New()
// simulating random creation
keys := store.GenKeys(100, 5)
keys := GenKeys(80, 4)
//t.Log(keys)
i := uint64(1)
for _, k := range keys {
_, err := fs.Create(k, "bar", Permanent, i, 1)
if err != nil {
t.Fatalf("create node[%s] failed", k, err.Error())
//t.Logf("create node[%s] failed %s", k, err.Error())
} else {
i++
}
i++
}
e, err := fs.Get("/foo", true, true, i, 1)
@ -426,9 +428,53 @@ func TestSort(t *testing.T) {
}
for i, k := range e.KVPairs[:len(e.KVPairs)-1] {
//t.Log("root:")
//t.Log(k)
if k.Key >= e.KVPairs[i+1].Key {
t.Fatalf("sort failed, [%s] should be placed after [%s]", k.Key, e.KVPairs[i+1].Key)
}
if k.Dir {
recursiveTestSort(k, t)
}
}
if k := e.KVPairs[len(e.KVPairs)-1]; k.Dir {
recursiveTestSort(k, t)
}
}
func recursiveTestSort(k KeyValuePair, t *testing.T) {
//t.Log("recursive in")
//t.Log(k)
for i, v := range k.KVPairs[:len(k.KVPairs)-1] {
if v.Key >= k.KVPairs[i+1].Key {
t.Fatalf("sort failed, [%s] should be placed after [%s]", v.Key, k.KVPairs[i+1].Key)
}
if v.Dir {
recursiveTestSort(v, t)
}
}
if v := k.KVPairs[len(k.KVPairs)-1]; v.Dir {
recursiveTestSort(v, t)
}
}
// GenKeys randomly generate num of keys with max depth
func GenKeys(num int, depth int) []string {
keys := make([]string, num)
for i := 0; i < num; i++ {
keys[i] = "/foo"
depth := rand.Intn(depth) + 1
for j := 0; j < depth; j++ {
keys[i] += "/" + strconv.Itoa(rand.Int()%20)
}
}
return keys
}

View File

@ -3,6 +3,7 @@ package fileSystem
import (
"fmt"
"path"
"sort"
"sync"
"time"
@ -279,7 +280,7 @@ func (n *Node) IsHidden() bool {
return false
}
func (n *Node) Pair(recurisive bool) KeyValuePair {
func (n *Node) Pair(recurisive, sorted bool) KeyValuePair {
if n.IsDir() {
pair := KeyValuePair{
@ -303,14 +304,16 @@ func (n *Node) Pair(recurisive bool) KeyValuePair {
continue
}
pair.KVPairs[i] = child.Pair(recurisive)
pair.KVPairs[i] = child.Pair(recurisive, sorted)
i++
}
// eliminate hidden nodes
pair.KVPairs = pair.KVPairs[:i]
if sorted {
sort.Sort(pair)
}
return pair
}

View File

@ -14,7 +14,7 @@ func GenKeys(num int, depth int) []string {
depth := rand.Intn(depth) + 1
for j := 0; j < depth; j++ {
keys[i] += "/" + strconv.Itoa(rand.Int())
keys[i] += "/" + strconv.Itoa(rand.Int()%20)
}
}
return keys