github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/kvstore/table_field.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package kvstore
     4  
     5  import (
     6  	"errors"
     7  )
     8  
     9  func (this *Table[T]) AddField(fieldName string) error {
    10  	if !IsValidName(fieldName) {
    11  		return errors.New("invalid field name '" + fieldName + "'")
    12  	}
    13  
    14  	// check existence
    15  	for _, field := range this.fieldNames {
    16  		if field == fieldName {
    17  			return nil
    18  		}
    19  	}
    20  
    21  	this.fieldNames = append(this.fieldNames, fieldName)
    22  	return nil
    23  }
    24  
    25  func (this *Table[T]) AddFields(fieldName ...string) error {
    26  	for _, subFieldName := range fieldName {
    27  		err := this.AddField(subFieldName)
    28  		if err != nil {
    29  			return err
    30  		}
    31  	}
    32  	return nil
    33  }
    34  
    35  func (this *Table[T]) DropField(fieldName string) error {
    36  	this.mu.Lock()
    37  	defer this.mu.Unlock()
    38  
    39  	var start = this.FieldKey(fieldName + "$")
    40  	return this.db.store.rawDB.DeleteRange(start, append(start, 0xFF), DefaultWriteOptions)
    41  }