coocood/freecache

Ringbuffer readAt get wrong result after resize

ans76 opened this issue · 0 comments

ans76 commented

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