Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ringbuffer readAt get wrong result after resize #64

Open
ans76 opened this issue Oct 24, 2019 · 0 comments
Open

Ringbuffer readAt get wrong result after resize #64

ans76 opened this issue Oct 24, 2019 · 0 comments

Comments

@ans76
Copy link

ans76 commented Oct 24, 2019

test ringbuffer code, looks like may get wrong value after resizing. and I'm not sure why ringbuffer need resize. if it's not a bug, would you mind explain the design of ringbuffer implementation in freecache?

code snippet below

    var d [6]byte                                                                                                                                                                                                                                                                                                                                                                                              
    rb := NewRingBuf(8, 0)                                                                                                                                                                              
    fmt.Println(string(rb.Dump()))                                                                                                                                                                      
                                                                                                                                                                                                        
    fmt.Println("write 123456  write-offset:", rb.End())                                                                                                                                                
    rb.Write([]byte("123456"))                                                                                                                                                                          
    fmt.Println("buffer: ",string(rb.Dump()))                                                                                                                                                           
    fmt.Println(rb.String())                                                                                                                                                                            
    fmt.Println("=========================================")                                                                                                                                            
                                                                                                                                                                                                        
    index := rb.End()                                                                                                                                                                                   
    fmt.Println("write abcdef  write-offset:", rb.End())                                                                                                                                                
    rb.Write([]byte("abcdef"))                                                                                                                                                                          
    fmt.Println("buffer: ", string(rb.Dump()))                                                                                                                                                          
    fmt.Println(rb.String())                                                                                                                                                                            
                                                                                                                                                                                                        
    fmt.Println("=========================================")                                                                                                                                            
                                                                                                                                                                                                        
    fmt.Println("after resize to 10")                                                                                                                                                                   
    rb.Resize(10)                                                                                                                                                                                       
    fmt.Println("buffer: ", string(rb.Dump()))                                                                                                                                                          
    fmt.Println(rb.String())                                                                                                                                                                            
    fmt.Println("=========================================")                                                                                                                                            
                                                                                                                                                                                                        
    fmt.Println("write AB")                                                                                                                                                                             
    rb.Write([]byte("AB"))                                                                                                                                                                              
    fmt.Println("buffer: ", string(rb.Dump()))                                                                                                                                                          
    fmt.Println(rb.String())                                                                                                                                                                            
    fmt.Println("=========================================")                                                                                                                                            
                                                                                                                                                                                                        
    fmt.Println("read abcdef by offset: ", index)                                                                                                                                                       
    rb.ReadAt(d[:], index)                                                                                                                                                                              
    fmt.Println(string(d[:])) 

result below

write 123456  write-offset: 0
buffer:  123456
[size:8, start:0, end:6, index:6]
=========================================
write abcdef  write-offset: 6
buffer:  cdef56ab
[size:8, start:4, end:12, index:4]
=========================================
after resize to 10
buffer:  56abcdef
[size:10, start:4, end:12, index:0]
=========================================
write AB
buffer:  ABabcdef
[size:10, start:4, end:14, index:2]
=========================================
read abcdef by offset:  6
cdef
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant