* fix: use keyed fields in struct literals - Replace unsafe reflect.StringHeader/SliceHeader with safe unsafe.String/Slice (weed/query/sqltypes/unsafe.go) - Add field names to Type_ScalarType struct literals (weed/mq/schema/schema_builder.go) - Add Duration field name to FlexibleDuration struct literals across test files - Add field names to bson.D struct literals (weed/filer/mongodb/mongodb_store_kv.go) Fixes go vet warnings about unkeyed struct literals. * fix: remove unreachable code - Remove unreachable return statements after infinite for loops - Remove unreachable code after if/else blocks where all paths return - Simplify recursive logic by removing unnecessary for loop (inode_to_path.go) - Fix Type_ScalarType literal to use enum value directly (schema_builder.go) - Call onCompletionFn on stream error (subscribe_session.go) Files fixed: - weed/query/sqltypes/unsafe.go - weed/mq/schema/schema_builder.go - weed/mq/client/sub_client/connect_to_sub_coordinator.go - weed/filer/redis3/ItemList.go - weed/mq/client/agent_client/subscribe_session.go - weed/mq/broker/broker_grpc_pub_balancer.go - weed/mount/inode_to_path.go - weed/util/skiplist/name_list.go * fix: avoid copying lock values in protobuf messages - Use proto.Merge() instead of direct assignment to avoid copying sync.Mutex in S3ApiConfiguration (iamapi_server.go) - Add explicit comments noting that channel-received values are already copies before taking addresses (volume_grpc_client_to_master.go) The protobuf messages contain sync.Mutex fields from the message state, which should not be copied. Using proto.Merge() properly merges messages without copying the embedded mutex. * fix: correct byte array size for uint32 bit shift operations The generateAccountId() function only needs 4 bytes to create a uint32 value. Changed from allocating 8 bytes to 4 bytes to match the actual usage. This fixes go vet warning about shifting 8-bit values (bytes) by more than 8 bits. * fix: ensure context cancellation on all error paths In broker_client_subscribe.go, ensure subscriberCancel() is called on all error return paths: - When stream creation fails - When partition assignment fails - When sending initialization message fails This prevents context leaks when an error occurs during subscriber creation. * fix: ensure subscriberCancel called for CreateFreshSubscriber stream.Send error Ensure subscriberCancel() is called when stream.Send fails in CreateFreshSubscriber. * ci: add go vet step to prevent future lint regressions - Add go vet step to GitHub Actions workflow - Filter known protobuf lock warnings (MessageState sync.Mutex) These are expected in generated protobuf code and are safe - Prevents accumulation of go vet errors in future PRs - Step runs before build to catch issues early * fix: resolve remaining syntax and logic errors in vet fixes - Fixed syntax errors in filer_sync.go caused by missing closing braces - Added missing closing brace for if block and function - Synchronized fixes to match previous commits on branch * fix: add missing return statements to daemon functions - Add 'return false' after infinite loops in filer_backup.go and filer_meta_backup.go - Satisfies declared bool return type signatures - Maintains consistency with other daemon functions (runMaster, runFilerSynchronize, runWorker) - While unreachable, explicitly declares the return satisfies function signature contract * fix: add nil check for onCompletionFn in SubscribeMessageRecord - Check if onCompletionFn is not nil before calling it - Prevents potential panic if nil function is passed - Matches pattern used in other callback functions * docs: clarify unreachable return statements in daemon functions - Add comments documenting that return statements satisfy function signature - Explains that these returns follow infinite loops and are unreachable - Improves code clarity for future maintainers
335 lines
7.0 KiB
Go
335 lines
7.0 KiB
Go
package skiplist
|
|
|
|
import (
|
|
"bytes"
|
|
)
|
|
|
|
type NameList struct {
|
|
skipList *SkipList
|
|
batchSize int
|
|
}
|
|
|
|
func newNameList(store ListStore, batchSize int) *NameList {
|
|
return &NameList{
|
|
skipList: New(store),
|
|
batchSize: batchSize,
|
|
}
|
|
}
|
|
|
|
/*
|
|
Be reluctant to create new nodes. Try to fit into either previous node or next node.
|
|
Prefer to add to previous node.
|
|
|
|
There are multiple cases after finding the name for greater or equal node
|
|
|
|
1. found and node.Key == name
|
|
The node contains a batch with leading key the same as the name
|
|
nothing to do
|
|
|
|
2. no such node found or node.Key > name
|
|
|
|
if no such node found
|
|
prevNode = list.LargestNode
|
|
|
|
// case 2.1
|
|
if previousNode contains name
|
|
nothing to do
|
|
|
|
// prefer to add to previous node
|
|
if prevNode != nil {
|
|
// case 2.2
|
|
if prevNode has capacity
|
|
prevNode.add name, and save
|
|
return
|
|
// case 2.3
|
|
split prevNode by name
|
|
}
|
|
|
|
// case 2.4
|
|
// merge into next node. Avoid too many nodes if adding data in reverse order.
|
|
if nextNode is not nil and nextNode has capacity
|
|
delete nextNode.Key
|
|
nextNode.Key = name
|
|
nextNode.batch.add name
|
|
insert nodeNode.Key
|
|
return
|
|
|
|
// case 2.5
|
|
if prevNode is nil
|
|
insert new node with key = name, value = batch{name}
|
|
return
|
|
*/
|
|
func (nl *NameList) WriteName(name string) error {
|
|
|
|
lookupKey := []byte(name)
|
|
prevNode, nextNode, found, err := nl.skipList.FindGreaterOrEqual(lookupKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// case 1: the name already exists as one leading key in the batch
|
|
if found && bytes.Compare(nextNode.Key, lookupKey) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if !found {
|
|
prevNode, err = nl.skipList.GetLargestNode()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if nextNode != nil && prevNode == nil {
|
|
prevNode, err = nl.skipList.LoadElement(nextNode.Prev)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if prevNode != nil {
|
|
prevNameBatch := LoadNameBatch(prevNode.Value)
|
|
// case 2.1
|
|
if prevNameBatch.ContainsName(name) {
|
|
return nil
|
|
}
|
|
|
|
// case 2.2
|
|
if len(prevNameBatch.names) < nl.batchSize {
|
|
prevNameBatch.WriteName(name)
|
|
return nl.skipList.ChangeValue(prevNode, prevNameBatch.ToBytes())
|
|
}
|
|
|
|
// case 2.3
|
|
x, y := prevNameBatch.SplitBy(name)
|
|
addToX := len(x.names) <= len(y.names)
|
|
if len(x.names) != len(prevNameBatch.names) {
|
|
if addToX {
|
|
x.WriteName(name)
|
|
}
|
|
if x.key == prevNameBatch.key {
|
|
if err := nl.skipList.ChangeValue(prevNode, x.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if _, err := nl.skipList.InsertByKey([]byte(x.key), 0, x.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if len(y.names) != len(prevNameBatch.names) {
|
|
if !addToX {
|
|
y.WriteName(name)
|
|
}
|
|
if y.key == prevNameBatch.key {
|
|
if err := nl.skipList.ChangeValue(prevNode, y.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if _, err := nl.skipList.InsertByKey([]byte(y.key), 0, y.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
|
|
}
|
|
|
|
// case 2.4
|
|
if nextNode != nil {
|
|
nextNameBatch := LoadNameBatch(nextNode.Value)
|
|
if len(nextNameBatch.names) < nl.batchSize {
|
|
if _, err := nl.skipList.DeleteByKey(nextNode.Key); err != nil {
|
|
return err
|
|
}
|
|
nextNameBatch.WriteName(name)
|
|
if _, err := nl.skipList.InsertByKey([]byte(nextNameBatch.key), 0, nextNameBatch.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// case 2.5
|
|
// now prevNode is nil
|
|
newNameBatch := NewNameBatch()
|
|
newNameBatch.WriteName(name)
|
|
if _, err := nl.skipList.InsertByKey([]byte(newNameBatch.key), 0, newNameBatch.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
// case 1: exists in nextNode
|
|
|
|
if nextNode != nil && nextNode.Key == name {
|
|
remove from nextNode, update nextNode
|
|
// TODO: merge with prevNode if possible?
|
|
return
|
|
}
|
|
|
|
if nextNode is nil
|
|
|
|
prevNode = list.Largestnode
|
|
|
|
if prevNode == nil and nextNode.Prev != nil
|
|
|
|
prevNode = load(nextNode.Prev)
|
|
|
|
// case 2: does not exist
|
|
// case 2.1
|
|
|
|
if prevNode == nil {
|
|
return
|
|
}
|
|
|
|
// case 2.2
|
|
|
|
if prevNameBatch does not contain name {
|
|
return
|
|
}
|
|
|
|
// case 3
|
|
delete from prevNameBatch
|
|
if prevNameBatch + nextNode < capacityList
|
|
|
|
// case 3.1
|
|
merge
|
|
|
|
else
|
|
|
|
// case 3.2
|
|
update prevNode
|
|
*/
|
|
func (nl *NameList) DeleteName(name string) error {
|
|
lookupKey := []byte(name)
|
|
prevNode, nextNode, found, err := nl.skipList.FindGreaterOrEqual(lookupKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// case 1
|
|
var nextNameBatch *NameBatch
|
|
if nextNode != nil {
|
|
nextNameBatch = LoadNameBatch(nextNode.Value)
|
|
}
|
|
if found && bytes.Compare(nextNode.Key, lookupKey) == 0 {
|
|
if _, err := nl.skipList.DeleteByKey(nextNode.Key); err != nil {
|
|
return err
|
|
}
|
|
nextNameBatch.DeleteName(name)
|
|
if len(nextNameBatch.names) > 0 {
|
|
if _, err := nl.skipList.InsertByKey([]byte(nextNameBatch.key), 0, nextNameBatch.ToBytes()); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if !found {
|
|
prevNode, err = nl.skipList.GetLargestNode()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if nextNode != nil && prevNode == nil {
|
|
prevNode, err = nl.skipList.LoadElement(nextNode.Prev)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// case 2
|
|
if prevNode == nil {
|
|
// case 2.1
|
|
return nil
|
|
}
|
|
prevNameBatch := LoadNameBatch(prevNode.Value)
|
|
if !prevNameBatch.ContainsName(name) {
|
|
// case 2.2
|
|
return nil
|
|
}
|
|
|
|
// case 3
|
|
prevNameBatch.DeleteName(name)
|
|
if len(prevNameBatch.names) == 0 {
|
|
if _, err := nl.skipList.DeleteByKey(prevNode.Key); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
if nextNameBatch != nil && len(nextNameBatch.names)+len(prevNameBatch.names) < nl.batchSize {
|
|
// case 3.1 merge nextNode and prevNode
|
|
if _, err := nl.skipList.DeleteByKey(nextNode.Key); err != nil {
|
|
return err
|
|
}
|
|
for nextName := range nextNameBatch.names {
|
|
prevNameBatch.WriteName(nextName)
|
|
}
|
|
return nl.skipList.ChangeValue(prevNode, prevNameBatch.ToBytes())
|
|
} else {
|
|
// case 3.2 update prevNode
|
|
return nl.skipList.ChangeValue(prevNode, prevNameBatch.ToBytes())
|
|
}
|
|
}
|
|
|
|
func (nl *NameList) ListNames(startFrom string, visitNamesFn func(name string) bool) error {
|
|
lookupKey := []byte(startFrom)
|
|
prevNode, nextNode, found, err := nl.skipList.FindGreaterOrEqual(lookupKey)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if found && bytes.Compare(nextNode.Key, lookupKey) == 0 {
|
|
prevNode = nil
|
|
}
|
|
if !found {
|
|
prevNode, err = nl.skipList.GetLargestNode()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if prevNode != nil {
|
|
prevNameBatch := LoadNameBatch(prevNode.Value)
|
|
if !prevNameBatch.ListNames(startFrom, visitNamesFn) {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
for nextNode != nil {
|
|
nextNameBatch := LoadNameBatch(nextNode.Value)
|
|
if !nextNameBatch.ListNames(startFrom, visitNamesFn) {
|
|
return nil
|
|
}
|
|
nextNode, err = nl.skipList.LoadElement(nextNode.Next[0])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (nl *NameList) RemoteAllListElement() error {
|
|
|
|
t := nl.skipList
|
|
|
|
nodeRef := t.StartLevels[0]
|
|
for nodeRef != nil {
|
|
node, err := t.LoadElement(nodeRef)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if node == nil {
|
|
return nil
|
|
}
|
|
if err := t.DeleteElement(node); err != nil {
|
|
return err
|
|
}
|
|
nodeRef = node.Next[0]
|
|
}
|
|
return nil
|
|
|
|
}
|