aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Lu <chris.lu@gmail.com>2021-10-03 01:07:35 -0700
committerChris Lu <chris.lu@gmail.com>2021-10-03 01:07:35 -0700
commit4f50f8c2ca7243a358e7ee9dab40a5a42fc462d1 (patch)
tree98e8a25911cdd57f2f70d80f7ee50ef40761b23f
parent69b84bb771e816e2914daee8635379b1150e0de7 (diff)
downloadseaweedfs-4f50f8c2ca7243a358e7ee9dab40a5a42fc462d1.tar.xz
seaweedfs-4f50f8c2ca7243a358e7ee9dab40a5a42fc462d1.zip
insert key and value
-rw-r--r--weed/util/skiplist/serde.go12
-rw-r--r--weed/util/skiplist/skiplist.go41
-rw-r--r--weed/util/skiplist/skiplist.pb.go97
-rw-r--r--weed/util/skiplist/skiplist.proto7
-rw-r--r--weed/util/skiplist/skiplist_test.go50
5 files changed, 84 insertions, 123 deletions
diff --git a/weed/util/skiplist/serde.go b/weed/util/skiplist/serde.go
index 2337b4b19..135d3b0b5 100644
--- a/weed/util/skiplist/serde.go
+++ b/weed/util/skiplist/serde.go
@@ -3,16 +3,10 @@ package skiplist
import "bytes"
func compareElement(a *SkipListElement, key []byte) int {
- if len(a.Values) == 0 {
+ if len(a.Key) == 0 {
return -1
}
- if bytes.Compare(a.Values[0], key) < 0 {
- return -1
- }
- if bytes.Compare(a.Values[len(a.Values)-1], key) > 0 {
- return 1
- }
- return 0
+ return bytes.Compare(a.Key, key)
}
var (
@@ -25,7 +19,7 @@ func (node *SkipListElement) Reference() *SkipListElementReference {
}
return &SkipListElementReference{
ElementPointer: node.Id,
- Key: node.Values[0],
+ Key: node.Key,
}
}
func (node *SkipListElement) Save() {
diff --git a/weed/util/skiplist/skiplist.go b/weed/util/skiplist/skiplist.go
index a47cf4608..19fa556ae 100644
--- a/weed/util/skiplist/skiplist.go
+++ b/weed/util/skiplist/skiplist.go
@@ -15,11 +15,11 @@ const (
)
type SkipList struct {
- startLevels [maxLevel]*SkipListElementReference
- endLevels [maxLevel]*SkipListElementReference
- maxNewLevel int
- maxLevel int
- elementCount int
+ startLevels [maxLevel]*SkipListElementReference
+ endLevels [maxLevel]*SkipListElementReference
+ maxNewLevel int
+ maxLevel int
+ // elementCount int
}
// NewSeedEps returns a new empty, initialized Skiplist.
@@ -32,9 +32,9 @@ func NewSeed(seed int64) *SkipList {
//fmt.Printf("SkipList seed: %v\n", seed)
list := &SkipList{
- maxNewLevel: maxLevel,
- maxLevel: 0,
- elementCount: 0,
+ maxNewLevel: maxLevel,
+ maxLevel: 0,
+ // elementCount: 0,
}
return list
@@ -193,7 +193,7 @@ func (t *SkipList) Delete(key []byte) {
nextNextNode.Prev = currentNode.Reference()
nextNextNode.Save()
}
- t.elementCount--
+ // t.elementCount--
nextNode.DeleteSelf()
}
@@ -230,7 +230,7 @@ func (t *SkipList) Delete(key []byte) {
// Insert inserts the given ListElement into the skiplist.
// Insert runs in approx. O(log(n))
-func (t *SkipList) Insert(key []byte) {
+func (t *SkipList) Insert(key, value []byte) {
if t == nil || key == nil {
return
@@ -245,13 +245,14 @@ func (t *SkipList) Insert(key []byte) {
}
elem := &SkipListElement{
- Id: rand.Int63(),
- Next: make([]*SkipListElementReference, t.maxNewLevel, t.maxNewLevel),
- Level: int32(level),
- Values: [][]byte{key},
+ Id: rand.Int63(),
+ Next: make([]*SkipListElementReference, t.maxNewLevel, t.maxNewLevel),
+ Level: int32(level),
+ Key: key,
+ Value: value,
}
- t.elementCount++
+ // t.elementCount++
newFirst := true
newLast := true
@@ -371,11 +372,6 @@ func (t *SkipList) Insert(key []byte) {
}
-// GetValue extracts the ListElement value from a skiplist node.
-func (e *SkipListElement) GetValue() []byte {
- return e.Values[0]
-}
-
// GetSmallestNode returns the very first/smallest node in the skiplist.
// GetSmallestNode runs in O(1)
func (t *SkipList) GetSmallestNode() *SkipListElement {
@@ -406,11 +402,6 @@ func (t *SkipList) Prev(e *SkipListElement) *SkipListElement {
return e.Prev.Load()
}
-// GetNodeCount returns the number of nodes currently in the skiplist.
-func (t *SkipList) GetNodeCount() int {
- return t.elementCount
-}
-
// String returns a string format of the skiplist. Useful to get a graphical overview and/or debugging.
func (t *SkipList) println() {
diff --git a/weed/util/skiplist/skiplist.pb.go b/weed/util/skiplist/skiplist.pb.go
index 63b6c74a3..82afec453 100644
--- a/weed/util/skiplist/skiplist.pb.go
+++ b/weed/util/skiplist/skiplist.pb.go
@@ -30,12 +30,10 @@ type SkipListProto struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
- StartLevels []*SkipListElementReference `protobuf:"bytes,1,rep,name=start_levels,json=startLevels,proto3" json:"start_levels,omitempty"`
- EndLevels []*SkipListElementReference `protobuf:"bytes,2,rep,name=end_levels,json=endLevels,proto3" json:"end_levels,omitempty"`
- MaxNewLevel int32 `protobuf:"varint,3,opt,name=max_new_level,json=maxNewLevel,proto3" json:"max_new_level,omitempty"`
- MaxLevel int32 `protobuf:"varint,4,opt,name=max_level,json=maxLevel,proto3" json:"max_level,omitempty"`
- ElementCount int64 `protobuf:"varint,5,opt,name=element_count,json=elementCount,proto3" json:"element_count,omitempty"`
- Eps float64 `protobuf:"fixed64,7,opt,name=eps,proto3" json:"eps,omitempty"`
+ StartLevels []*SkipListElementReference `protobuf:"bytes,1,rep,name=start_levels,json=startLevels,proto3" json:"start_levels,omitempty"`
+ EndLevels []*SkipListElementReference `protobuf:"bytes,2,rep,name=end_levels,json=endLevels,proto3" json:"end_levels,omitempty"`
+ MaxNewLevel int32 `protobuf:"varint,3,opt,name=max_new_level,json=maxNewLevel,proto3" json:"max_new_level,omitempty"`
+ MaxLevel int32 `protobuf:"varint,4,opt,name=max_level,json=maxLevel,proto3" json:"max_level,omitempty"`
}
func (x *SkipListProto) Reset() {
@@ -98,20 +96,6 @@ func (x *SkipListProto) GetMaxLevel() int32 {
return 0
}
-func (x *SkipListProto) GetElementCount() int64 {
- if x != nil {
- return x.ElementCount
- }
- return 0
-}
-
-func (x *SkipListProto) GetEps() float64 {
- if x != nil {
- return x.Eps
- }
- return 0
-}
-
type SkipListElementReference struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
@@ -172,11 +156,12 @@ type SkipListElement struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
- Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
- Next []*SkipListElementReference `protobuf:"bytes,2,rep,name=next,proto3" json:"next,omitempty"`
- Level int32 `protobuf:"varint,3,opt,name=level,proto3" json:"level,omitempty"`
- Values [][]byte `protobuf:"bytes,4,rep,name=values,proto3" json:"values,omitempty"`
- Prev *SkipListElementReference `protobuf:"bytes,5,opt,name=prev,proto3" json:"prev,omitempty"`
+ Id int64 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
+ Next []*SkipListElementReference `protobuf:"bytes,2,rep,name=next,proto3" json:"next,omitempty"`
+ Level int32 `protobuf:"varint,3,opt,name=level,proto3" json:"level,omitempty"`
+ Key []byte `protobuf:"bytes,4,opt,name=key,proto3" json:"key,omitempty"`
+ Value []byte `protobuf:"bytes,5,opt,name=value,proto3" json:"value,omitempty"`
+ Prev *SkipListElementReference `protobuf:"bytes,6,opt,name=prev,proto3" json:"prev,omitempty"`
}
func (x *SkipListElement) Reset() {
@@ -232,9 +217,16 @@ func (x *SkipListElement) GetLevel() int32 {
return 0
}
-func (x *SkipListElement) GetValues() [][]byte {
+func (x *SkipListElement) GetKey() []byte {
+ if x != nil {
+ return x.Key
+ }
+ return nil
+}
+
+func (x *SkipListElement) GetValue() []byte {
if x != nil {
- return x.Values
+ return x.Value
}
return nil
}
@@ -250,7 +242,7 @@ var File_skiplist_proto protoreflect.FileDescriptor
var file_skiplist_proto_rawDesc = []byte{
0x0a, 0x0e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f,
- 0x12, 0x08, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x22, 0x91, 0x02, 0x0a, 0x0d, 0x53,
+ 0x12, 0x08, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x22, 0xda, 0x01, 0x0a, 0x0d, 0x53,
0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x45, 0x0a, 0x0c,
0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x73, 0x18, 0x01, 0x20, 0x03,
0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b,
@@ -264,32 +256,29 @@ var file_skiplist_proto_rawDesc = []byte{
0x77, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x0b, 0x6d,
0x61, 0x78, 0x4e, 0x65, 0x77, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x1b, 0x0a, 0x09, 0x6d, 0x61,
0x78, 0x5f, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x6d,
- 0x61, 0x78, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x23, 0x0a, 0x0d, 0x65, 0x6c, 0x65, 0x6d, 0x65,
- 0x6e, 0x74, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c,
- 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x43, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x10, 0x0a, 0x03,
- 0x65, 0x70, 0x73, 0x18, 0x07, 0x20, 0x01, 0x28, 0x01, 0x52, 0x03, 0x65, 0x70, 0x73, 0x22, 0x55,
- 0x0a, 0x18, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e,
- 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x65, 0x6c,
- 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x5f, 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20,
- 0x01, 0x28, 0x03, 0x52, 0x0e, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x50, 0x6f, 0x69, 0x6e,
- 0x74, 0x65, 0x72, 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c,
- 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0xbf, 0x01, 0x0a, 0x0f, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69,
- 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18,
- 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x36, 0x0a, 0x04, 0x6e, 0x65, 0x78,
- 0x74, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69,
- 0x73, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65,
- 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x52, 0x04, 0x6e, 0x65, 0x78,
- 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05,
- 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65,
- 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x0c, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x12,
- 0x36, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x76, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x22, 0x2e,
- 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73,
- 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63,
- 0x65, 0x52, 0x04, 0x70, 0x72, 0x65, 0x76, 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75,
- 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63, 0x68, 0x72, 0x69, 0x73, 0x6c, 0x75, 0x73, 0x66, 0x2f,
- 0x73, 0x65, 0x61, 0x77, 0x65, 0x65, 0x64, 0x66, 0x73, 0x2f, 0x77, 0x65, 0x65, 0x64, 0x2f, 0x75,
- 0x74, 0x69, 0x6c, 0x2f, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72,
- 0x6f, 0x74, 0x6f, 0x33,
+ 0x61, 0x78, 0x4c, 0x65, 0x76, 0x65, 0x6c, 0x22, 0x55, 0x0a, 0x18, 0x53, 0x6b, 0x69, 0x70, 0x4c,
+ 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65,
+ 0x6e, 0x63, 0x65, 0x12, 0x27, 0x0a, 0x0f, 0x65, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x5f, 0x70,
+ 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0e, 0x65, 0x6c,
+ 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x50, 0x6f, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x12, 0x10, 0x0a, 0x03,
+ 0x6b, 0x65, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b, 0x65, 0x79, 0x22, 0xcf,
+ 0x01, 0x0a, 0x0f, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65,
+ 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02,
+ 0x69, 0x64, 0x12, 0x36, 0x0a, 0x04, 0x6e, 0x65, 0x78, 0x74, 0x18, 0x02, 0x20, 0x03, 0x28, 0x0b,
+ 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70,
+ 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x66, 0x65, 0x72,
+ 0x65, 0x6e, 0x63, 0x65, 0x52, 0x04, 0x6e, 0x65, 0x78, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x65,
+ 0x76, 0x65, 0x6c, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x65, 0x76, 0x65, 0x6c,
+ 0x12, 0x10, 0x0a, 0x03, 0x6b, 0x65, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x52, 0x03, 0x6b,
+ 0x65, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28,
+ 0x0c, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x12, 0x36, 0x0a, 0x04, 0x70, 0x72, 0x65, 0x76,
+ 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x22, 0x2e, 0x73, 0x6b, 0x69, 0x70, 0x6c, 0x69, 0x73,
+ 0x74, 0x2e, 0x53, 0x6b, 0x69, 0x70, 0x4c, 0x69, 0x73, 0x74, 0x45, 0x6c, 0x65, 0x6d, 0x65, 0x6e,
+ 0x74, 0x52, 0x65, 0x66, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x65, 0x52, 0x04, 0x70, 0x72, 0x65, 0x76,
+ 0x42, 0x33, 0x5a, 0x31, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x63,
+ 0x68, 0x72, 0x69, 0x73, 0x6c, 0x75, 0x73, 0x66, 0x2f, 0x73, 0x65, 0x61, 0x77, 0x65, 0x65, 0x64,
+ 0x66, 0x73, 0x2f, 0x77, 0x65, 0x65, 0x64, 0x2f, 0x75, 0x74, 0x69, 0x6c, 0x2f, 0x73, 0x6b, 0x69,
+ 0x70, 0x6c, 0x69, 0x73, 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
diff --git a/weed/util/skiplist/skiplist.proto b/weed/util/skiplist/skiplist.proto
index ce84ed996..bfb190b33 100644
--- a/weed/util/skiplist/skiplist.proto
+++ b/weed/util/skiplist/skiplist.proto
@@ -9,8 +9,6 @@ message SkipListProto {
repeated SkipListElementReference end_levels = 2;
int32 max_new_level = 3;
int32 max_level = 4;
- int64 element_count = 5;
- double eps = 7;
}
message SkipListElementReference {
@@ -22,6 +20,7 @@ message SkipListElement {
int64 id = 1;
repeated SkipListElementReference next = 2;
int32 level = 3;
- repeated bytes values = 4;
- SkipListElementReference prev = 5;
+ bytes key = 4;
+ bytes value = 5;
+ SkipListElementReference prev = 6;
}
diff --git a/weed/util/skiplist/skiplist_test.go b/weed/util/skiplist/skiplist_test.go
index 60bd5f923..811fd5be9 100644
--- a/weed/util/skiplist/skiplist_test.go
+++ b/weed/util/skiplist/skiplist_test.go
@@ -18,7 +18,7 @@ func TestInsertAndFind(t *testing.T) {
var list *SkipList
var listPointer *SkipList
- listPointer.Insert(k0)
+ listPointer.Insert(k0, k0)
if _, ok := listPointer.Find(k0); ok {
t.Fail()
}
@@ -34,7 +34,7 @@ func TestInsertAndFind(t *testing.T) {
// Test at the beginning of the list.
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(maxN-i))
- list.Insert(key)
+ list.Insert(key, key)
}
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(maxN-i))
@@ -48,7 +48,7 @@ func TestInsertAndFind(t *testing.T) {
// Test at the end of the list.
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i))
- list.Insert(key)
+ list.Insert(key, key)
}
for i := 0; i < maxN; i++ {
key := []byte(strconv.Itoa(i))
@@ -62,17 +62,17 @@ func TestInsertAndFind(t *testing.T) {
rList := rand.Perm(maxN)
for _, e := range rList {
key := []byte(strconv.Itoa(e))
- println("insert", e)
- list.Insert(key)
+ // println("insert", e)
+ list.Insert(key, key)
}
for _, e := range rList {
key := []byte(strconv.Itoa(e))
- println("find", e)
+ // println("find", e)
if _, ok := list.Find(key); !ok {
t.Fail()
}
}
- println("print list")
+ // println("print list")
list.println()
}
@@ -97,7 +97,7 @@ func TestDelete(t *testing.T) {
t.Fail()
}
- list.Insert(k0)
+ list.Insert(k0, k0)
list.Delete(k0)
if !list.IsEmpty() {
t.Fail()
@@ -105,7 +105,7 @@ func TestDelete(t *testing.T) {
// Delete elements at the beginning of the list.
for i := 0; i < maxN; i++ {
- list.Insert(Element(i))
+ list.Insert(Element(i), Element(i))
}
for i := 0; i < maxN; i++ {
list.Delete(Element(i))
@@ -117,7 +117,7 @@ func TestDelete(t *testing.T) {
list = New()
// Delete elements at the end of the list.
for i := 0; i < maxN; i++ {
- list.Insert(Element(i))
+ list.Insert(Element(i), Element(i))
}
for i := 0; i < maxN; i++ {
list.Delete(Element(maxN - i - 1))
@@ -130,7 +130,7 @@ func TestDelete(t *testing.T) {
// Delete elements at random positions in the list.
rList := rand.Perm(maxN)
for _, e := range rList {
- list.Insert(Element(e))
+ list.Insert(Element(e), Element(e))
}
for _, e := range rList {
list.Delete(Element(e))
@@ -144,7 +144,7 @@ func TestNext(t *testing.T) {
list := New()
for i := 0; i < maxN; i++ {
- list.Insert(Element(i))
+ list.Insert(Element(i), Element(i))
}
smallest := list.GetSmallestNode()
@@ -155,7 +155,7 @@ func TestNext(t *testing.T) {
for node != largest {
node = list.Next(node)
// Must always be incrementing here!
- if bytes.Compare(node.Values[0], lastNode.Values[0]) <= 0 {
+ if bytes.Compare(node.Key, lastNode.Key) <= 0 {
t.Fail()
}
// Next.Prev must always point to itself!
@@ -174,7 +174,7 @@ func TestPrev(t *testing.T) {
list := New()
for i := 0; i < maxN; i++ {
- list.Insert(Element(i))
+ list.Insert(Element(i), Element(i))
}
smallest := list.GetSmallestNode()
@@ -185,7 +185,7 @@ func TestPrev(t *testing.T) {
for node != smallest {
node = list.Prev(node)
// Must always be incrementing here!
- if bytes.Compare(node.Values[0], lastNode.Values[0]) >= 0 {
+ if bytes.Compare(node.Key, lastNode.Key) >= 0 {
t.Fail()
}
// Next.Prev must always point to itself!
@@ -200,18 +200,6 @@ func TestPrev(t *testing.T) {
}
}
-func TestGetNodeCount(t *testing.T) {
- list := New()
-
- for i := 0; i < maxN; i++ {
- list.Insert(Element(i))
- }
-
- if list.GetNodeCount() != maxN {
- t.Fail()
- }
-}
-
func TestFindGreaterOrEqual(t *testing.T) {
maxNumber := maxN * 100
@@ -227,21 +215,21 @@ func TestFindGreaterOrEqual(t *testing.T) {
list = New()
for i := 0; i < maxN; i++ {
- list.Insert(Element(rand.Intn(maxNumber)))
+ list.Insert(Element(rand.Intn(maxNumber)), Element(i))
}
for i := 0; i < maxN; i++ {
key := Element(rand.Intn(maxNumber))
if v, ok := list.FindGreaterOrEqual(key); ok {
// if f is v should be bigger than the element before
- if bytes.Compare(v.Prev.Key, key) >= 0 {
+ if v.Prev != nil && bytes.Compare(v.Prev.Key, key) >= 0 {
fmt.Printf("PrevV: %s\n key: %s\n\n", string(v.Prev.Key), string(key))
t.Fail()
}
// v should be bigger or equal to f
// If we compare directly, we get an equal key with a difference on the 10th decimal point, which fails.
- if bytes.Compare(v.Values[0], key) < 0 {
- fmt.Printf("v: %s\n key: %s\n\n", string(v.Values[0]), string(key))
+ if bytes.Compare(v.Key, key) < 0 {
+ fmt.Printf("v: %s\n key: %s\n\n", string(v.Key), string(key))
t.Fail()
}
} else {