Browse Source

reduce one redis lookup on hot path

pull/2380/head
Chris Lu 3 years ago
parent
commit
f3d8232e14
  1. 31
      weed/filer/redis3/ItemList.go
  2. 3
      weed/util/skiplist/skiplist.go

31
weed/filer/redis3/ItemList.go

@ -96,22 +96,17 @@ func (nl *ItemList) WriteName(name string) error {
return nil
}
var prevNodeReference *skiplist.SkipListElementReference
if !found {
prevNode, err = nl.skipList.GetLargestNode()
if err != nil {
return err
}
prevNodeReference = nl.skipList.GetLargestNodeReference()
}
if nextNode != nil && prevNode == nil {
prevNode, err = nl.skipList.LoadElement(nextNode.Prev)
if err != nil {
return err
}
prevNodeReference = nextNode.Prev
}
if prevNode != nil {
alreadyContains, nodeSize, err := nl.canAddMember(prevNode.Reference(), name)
if prevNodeReference != nil {
alreadyContains, nodeSize, err := nl.canAddMember(prevNodeReference, name)
if err != nil {
return err
}
@ -122,11 +117,11 @@ func (nl *ItemList) WriteName(name string) error {
// case 2.2
if nodeSize < nl.batchSize {
return nl.NodeAddMember(prevNode.Reference(), name)
return nl.NodeAddMember(prevNodeReference, name)
}
// case 2.3
x := nl.NodeInnerPosition(prevNode.Reference(), name)
x := nl.NodeInnerPosition(prevNodeReference, name)
y := nodeSize - x
addToX := x <= y
// add to a new node
@ -138,12 +133,12 @@ func (nl *ItemList) WriteName(name string) error {
}
if addToX {
// collect names before name, add them to X
namesToX, err := nl.NodeRangeBeforeExclusive(prevNode.Reference(), name)
namesToX, err := nl.NodeRangeBeforeExclusive(prevNodeReference, name)
if err != nil {
return nil
}
// delete skiplist reference to old node
if _, err := nl.skipList.DeleteByKey(prevNode.Key); err != nil {
if _, err := nl.skipList.DeleteByKey(prevNodeReference.Key); err != nil {
return err
}
// add namesToY and name to a new X
@ -152,18 +147,18 @@ func (nl *ItemList) WriteName(name string) error {
return nil
}
// remove names less than name from current Y
if err := nl.NodeDeleteBeforeExclusive(prevNode.Reference(), name); err != nil {
if err := nl.NodeDeleteBeforeExclusive(prevNodeReference, name); err != nil {
return nil
}
// point skip list to current Y
if err := nl.ItemAdd(lookupKey, prevNode.Id); err != nil {
if err := nl.ItemAdd(lookupKey, prevNodeReference.ElementPointer); err != nil {
return nil
}
return nil
} else {
// collect names after name, add them to Y
namesToY, err := nl.NodeRangeAfterExclusive(prevNode.Reference(), name)
namesToY, err := nl.NodeRangeAfterExclusive(prevNodeReference, name)
if err != nil {
return nil
}
@ -173,7 +168,7 @@ func (nl *ItemList) WriteName(name string) error {
return nil
}
// remove names after name from current X
if err := nl.NodeDeleteAfterExclusive(prevNode.Reference(), name); err != nil {
if err := nl.NodeDeleteAfterExclusive(prevNodeReference, name); err != nil {
return nil
}
return nil

3
weed/util/skiplist/skiplist.go

@ -467,6 +467,9 @@ func (t *SkipList) GetSmallestNode() (*SkipListElement, error) {
func (t *SkipList) GetLargestNode() (*SkipListElement, error) {
return t.LoadElement(t.EndLevels[0])
}
func (t *SkipList) GetLargestNodeReference() (*SkipListElementReference) {
return t.EndLevels[0]
}
// Next returns the next element based on the given node.
// Next will loop around to the first node, if you call it on the last!

Loading…
Cancel
Save