mirror of
				https://github.com/TeaOSLab/EdgeNode.git
				synced 2025-11-04 07:40:56 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -75,7 +75,22 @@ func (this *Table[T]) Set(key string, value T) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.WriteTx(func(tx *Tx[T]) error {
 | 
			
		||||
		return this.set(tx, key, valueBytes, value)
 | 
			
		||||
		return this.set(tx, key, valueBytes, value, false)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Table[T]) Insert(key string, value T) error {
 | 
			
		||||
	if len(key) > KeyMaxLength {
 | 
			
		||||
		return ErrKeyTooLong
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	valueBytes, err := this.encoder.Encode(value)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.WriteTx(func(tx *Tx[T]) error {
 | 
			
		||||
		return this.set(tx, key, valueBytes, value, true)
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -283,7 +298,7 @@ func (this *Table[T]) deleteKeys(tx *Tx[T], key ...string) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Table[T]) set(tx *Tx[T], key string, valueBytes []byte, value T) error {
 | 
			
		||||
func (this *Table[T]) set(tx *Tx[T], key string, valueBytes []byte, value T, insertOnly bool) error {
 | 
			
		||||
	var keyBytes = this.FullKey(key)
 | 
			
		||||
 | 
			
		||||
	var batch = tx.batch
 | 
			
		||||
@@ -292,6 +307,8 @@ func (this *Table[T]) set(tx *Tx[T], key string, valueBytes []byte, value T) err
 | 
			
		||||
	var oldValue T
 | 
			
		||||
	var oldFound bool
 | 
			
		||||
	var countFields = len(this.fieldNames)
 | 
			
		||||
 | 
			
		||||
	if !insertOnly {
 | 
			
		||||
		if countFields > 0 {
 | 
			
		||||
			oldValueBytes, closer, getErr := batch.Get(keyBytes)
 | 
			
		||||
			if getErr != nil {
 | 
			
		||||
@@ -311,6 +328,7 @@ func (this *Table[T]) set(tx *Tx[T], key string, valueBytes []byte, value T) err
 | 
			
		||||
				oldFound = true
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	setErr := batch.Set(keyBytes, valueBytes, DefaultWriteOptions)
 | 
			
		||||
	if setErr != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,24 @@ func (this *Tx[T]) Set(key string, value T) error {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.table.set(this, key, valueBytes, value)
 | 
			
		||||
	return this.table.set(this, key, valueBytes, value, false)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Tx[T]) Insert(key string, value T) error {
 | 
			
		||||
	if this.readOnly {
 | 
			
		||||
		return errors.New("can not set value in readonly transaction")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if len(key) > KeyMaxLength {
 | 
			
		||||
		return ErrKeyTooLong
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	valueBytes, err := this.table.encoder.Encode(value)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return this.table.set(this, key, valueBytes, value, true)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *Tx[T]) Get(key string) (value T, err error) {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										17
									
								
								internal/utils/mem/system_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								internal/utils/mem/system_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,17 @@
 | 
			
		||||
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
 | 
			
		||||
 | 
			
		||||
package memutils_test
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeNode/internal/utils/mem"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestSystemMemoryGB(t *testing.T) {
 | 
			
		||||
	t.Log(memutils.SystemMemoryGB())
 | 
			
		||||
	t.Log(memutils.SystemMemoryGB())
 | 
			
		||||
	t.Log(memutils.SystemMemoryGB())
 | 
			
		||||
	t.Log(memutils.SystemMemoryBytes())
 | 
			
		||||
	t.Log(memutils.SystemMemoryBytes())
 | 
			
		||||
	t.Log(memutils.SystemMemoryBytes()>>30, "GB")
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user