Skip to content

Commit

Permalink
Merge pull request #35 from shengyanli1982/dev
Browse files Browse the repository at this point in the history
Optimize code for efficient execution.
  • Loading branch information
shengyanli1982 authored Dec 5, 2024
2 parents 95cab1f + 643e944 commit 513d7af
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 342 deletions.
90 changes: 19 additions & 71 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,122 +2,70 @@ package law

import lf "github.com/shengyanli1982/law/internal/lockfree"

// DefaultBufferSize 是默认的缓冲区大小
// DefaultBufferSize 默认缓冲区大小
// DefaultBufferSize is the default buffer size
const DefaultBufferSize = 2048

// Config 是配置结构体,包含了日志器、缓冲区大小和回调函数
// Config is a structure that contains a logger, buffer size, and a callback function
// Config 配置结构体
// Config is the configuration structure
type Config struct {
// buffSize 是缓冲区的大小
// buffSize is the size of the buffer
buffSize int

// callback 是回调函数,用于处理特定事件
// callback is a callback function for handling specific events
callback Callback

// queue 是一个队列,它用于存储即将处理的事件。
// queue is a queue that is used to store events that are about to be processed.
queue Queue
buffSize int // 缓冲区大小 / buffer size
callback Callback // 回调函数 / callback function
queue Queue // 队列实现 / queue implementation
}

// NewConfig 是一个构造函数,用于创建一个新的 Config 实例
// NewConfig is a constructor function for creating a new Config instance
// NewConfig 创建新的配置实例
// NewConfig creates a new configuration instance
func NewConfig() *Config {
// 返回一个新的 Config 实例
// Return a new Config instance
return &Config{
// 设置缓冲区大小为默认值
// Set the buffer size to the default value
buffSize: DefaultBufferSize,

// 创建一个空的回调函数
// Create an empty callback function
callback: newEmptyCallback(),

// 创建一个无锁队列
// Create an unlocked queue
queue: lf.NewLockFreeQueue(),
queue: lf.NewLockFreeQueue(),
}
}

// DefaultConfig 是一个函数,返回一个新的默认配置实例
// DefaultConfig is a function that returns a new default configuration instance
// DefaultConfig 返回默认配置
// DefaultConfig returns the default configuration
func DefaultConfig() *Config {
// 调用 NewConfig 函数创建一个新的配置实例
// Call the NewConfig function to create a new configuration instance
return NewConfig()
}

// WithBufferSize 是 Config 结构体的一个方法,用于设置缓冲区大小
// WithBufferSize is a method of the Config structure, used to set the buffer size
// WithBufferSize 设置缓冲区大小
// WithBufferSize sets the buffer size
func (c *Config) WithBufferSize(size int) *Config {
// 设置缓冲区大小
// Set the buffer size
c.buffSize = size

// 返回配置实例,以便进行链式调用
// Return the configuration instance for chaining
return c
}

// WithCallback 是 Config 结构体的一个方法,用于设置回调函数
// WithCallback is a method of the Config structure, used to set the callback function
// WithCallback 设置回调函数
// WithCallback sets the callback function
func (c *Config) WithCallback(cb Callback) *Config {
// 设置回调函数
// Set the callback function
c.callback = cb

// 返回配置实例,以便进行链式调用
// Return the configuration instance for chaining
return c
}

// WithQueue 是 Config 结构体的一个方法,用于设置队列
// WithQueue is a method of the Config structure, used to set the queue
// WithQueue 设置队列实现
// WithQueue sets the queue implementation
func (c *Config) WithQueue(q Queue) *Config {
// 设置队列
// Set the queue
c.queue = q

// 返回配置实例,以便进行链式调用
// Return the configuration instance for chaining
return c
}

// isConfigValid 是一个函数,用于检查配置是否有效。如果配置无效,它将使用默认值进行修复。
// isConfigValid is a function to check if the configuration is valid. If the configuration is invalid, it will fix it with default values.
// isConfigValid 验证并修正配置
// isConfigValid validates and corrects the configuration
func isConfigValid(conf *Config) *Config {
// 如果配置不为空
// If the configuration is not null
if conf != nil {

// 如果缓冲区大小小于或等于0,将其设置为默认缓冲区大小
// If the buffer size is less than or equal to 0, set it to the default buffer size
if conf.buffSize <= 0 {
conf.buffSize = DefaultBufferSize
}

// 如果回调函数为空,创建一个新的空回调函数
// If the callback function is null, create a new empty callback function
if conf.callback == nil {
conf.callback = newEmptyCallback()
}

// 如果队列为空,创建一个新的无锁队列
// If the queue is null, create a new unlocked queue
if conf.queue == nil {
conf.queue = lf.NewLockFreeQueue()
}

} else {
// 如果配置为空,使用默认配置
// If the configuration is null, use the default configuration
conf = DefaultConfig()
}

// 返回修复后的配置
// Return the fixed configuration
return conf
}
48 changes: 22 additions & 26 deletions interface.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,47 @@
package law

// Writer 是一个接口,定义了写操作的行为。
// Writer is an interface that defines the behavior of write operations.
// Writer 定义了写入器接口
// Writer defines the writer interface
type Writer interface {
// Write 方法接受一个字节切片,返回写入的字节数和可能的错误
// The Write method accepts a byte slice and returns the number of bytes written and a possible error.
// Write 写入数据,返回写入的字节数和可能的错误
// Write writes data and returns the number of bytes written and any error
Write([]byte) (int, error)

// Stop 方法用于停止写操作。
// The Stop method is used to stop write operations.
// Stop 停止写入器
// Stop stops the writer
Stop()
}

// Callback 是一个接口,定义了队列操作和写操作的回调函数。
// Callback is an interface that defines callback functions for queue operations and write operations.
// Callback 定义了回调接口
// Callback defines the callback interface
type Callback interface {
// OnWriteFailed 是一个方法,当写操作失败时会被调用。
// 它接受两个参数:一个字节切片(表示写入内容)和一个错误(表示失败的原因)。
// OnWriteFailed is a method that is called when a write operation fails.
// It takes two parameters: a byte slice (indicating the content to be written) and an error (indicating the reason for the failure).
// OnWriteFailed 当写入失败时被调用
// OnWriteFailed is called when writing fails
OnWriteFailed(content []byte, reason error)
}

// emptyCallback 是一个实现了 Callback 接口的结构体,但所有方法的实现都为空。
// emptyCallback is a struct that implements the Callback interface, but all method implementations are empty.
// emptyCallback 空回调实现
// emptyCallback is an empty callback implementation
type emptyCallback struct{}

// OnWriteFailed 是 emptyCallback 结构体实现 Callback 接口的方法,但此方法没有任何实现。
// OnWriteFailed is a method of the emptyCallback struct that implements the Callback interface, but this method has no implementation.
// OnWriteFailed 空回调的写入失败处理方法(无操作)
// OnWriteFailed handles write failures for empty callback (no-op)
func (c *emptyCallback) OnWriteFailed([]byte, error) {}

// newEmptyCallback 是一个构造函数,用于创建一个新的 emptyCallback 实例。
// newEmptyCallback is a constructor function for creating a new emptyCallback instance.
// newEmptyCallback 创建新的空回调实例
// newEmptyCallback creates a new empty callback instance
func newEmptyCallback() Callback {
// 返回一个新的 emptyCallback 实例。
// Return a new emptyCallback instance.
return &emptyCallback{}
}

// Queue 是一个接口,定义了队列的基本操作:Push 和 Pop。
// Queue is an interface that defines the basic operations of a queue: Push and Pop.
// Queue 定义了队列接口
// Queue defines the queue interface
type Queue interface {
// Push 方法用于将值添加到队列中。
// The Push method is used to add a value to the queue.
// Push 将值推入队列
// Push pushes a value into the queue
Push(value interface{})

// Pop 方法用于从队列中取出一个值。
// The Pop method is used to take a value out of the queue.
// Pop 从队列中取出值
// Pop retrieves a value from the queue
Pop() interface{}
}
Loading

0 comments on commit 513d7af

Please sign in to comment.