2014-04-09 00:36:05 +08:00
|
|
|
package mmap_span
|
2013-09-26 11:42:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-03-28 19:40:29 +08:00
|
|
|
"log"
|
2018-01-06 13:39:52 +08:00
|
|
|
"sync"
|
2014-04-08 17:39:34 +08:00
|
|
|
|
2015-12-23 00:50:34 +08:00
|
|
|
"github.com/edsrzf/mmap-go"
|
2013-09-26 11:42:24 +08:00
|
|
|
)
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
type segment struct {
|
2015-12-23 00:50:34 +08:00
|
|
|
*mmap.MMap
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (s segment) Size() int64 {
|
|
|
|
return int64(len(*s.MMap))
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
2014-12-05 14:54:55 +08:00
|
|
|
type MMapSpan struct {
|
2018-01-06 13:39:52 +08:00
|
|
|
mu sync.RWMutex
|
2014-12-05 14:54:55 +08:00
|
|
|
span
|
|
|
|
}
|
2013-09-26 11:42:24 +08:00
|
|
|
|
2016-04-19 12:11:11 +08:00
|
|
|
func (ms *MMapSpan) Append(mmap mmap.MMap) {
|
|
|
|
ms.span = append(ms.span, segment{&mmap})
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
2018-01-06 13:39:52 +08:00
|
|
|
func (ms *MMapSpan) Close() error {
|
|
|
|
ms.mu.Lock()
|
|
|
|
defer ms.mu.Unlock()
|
2016-04-19 12:11:11 +08:00
|
|
|
for _, mMap := range ms.span {
|
2016-03-28 19:40:29 +08:00
|
|
|
err := mMap.(segment).Unmap()
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
2016-03-28 19:40:29 +08:00
|
|
|
return nil
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
|
|
|
|
2018-01-06 13:39:52 +08:00
|
|
|
func (ms *MMapSpan) Size() (ret int64) {
|
|
|
|
ms.mu.RLock()
|
|
|
|
defer ms.mu.RUnlock()
|
2016-04-19 12:11:11 +08:00
|
|
|
for _, seg := range ms.span {
|
2014-04-09 00:36:05 +08:00
|
|
|
ret += seg.Size()
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-06 13:39:52 +08:00
|
|
|
func (ms *MMapSpan) ReadAt(p []byte, off int64) (n int, err error) {
|
|
|
|
ms.mu.RLock()
|
|
|
|
defer ms.mu.RUnlock()
|
2016-04-19 12:11:11 +08:00
|
|
|
ms.ApplyTo(off, func(intervalOffset int64, interval sizer) (stop bool) {
|
2015-12-23 00:50:34 +08:00
|
|
|
_n := copy(p, (*interval.(segment).MMap)[intervalOffset:])
|
2013-09-26 11:42:24 +08:00
|
|
|
p = p[_n:]
|
|
|
|
n += _n
|
|
|
|
return len(p) == 0
|
|
|
|
})
|
|
|
|
if len(p) != 0 {
|
|
|
|
err = io.EOF
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-06 13:39:52 +08:00
|
|
|
func (ms *MMapSpan) WriteAt(p []byte, off int64) (n int, err error) {
|
|
|
|
ms.mu.RLock()
|
|
|
|
defer ms.mu.RUnlock()
|
2016-04-19 12:11:11 +08:00
|
|
|
ms.ApplyTo(off, func(iOff int64, i sizer) (stop bool) {
|
2014-04-09 00:36:05 +08:00
|
|
|
mMap := i.(segment)
|
2015-12-23 00:50:34 +08:00
|
|
|
_n := copy((*mMap.MMap)[iOff:], p)
|
2013-10-20 22:07:01 +08:00
|
|
|
// err = mMap.Sync(gommap.MS_ASYNC)
|
|
|
|
// if err != nil {
|
|
|
|
// return true
|
|
|
|
// }
|
2013-09-26 11:42:24 +08:00
|
|
|
p = p[_n:]
|
|
|
|
n += _n
|
|
|
|
return len(p) == 0
|
|
|
|
})
|
2013-10-20 22:07:01 +08:00
|
|
|
if err != nil && len(p) != 0 {
|
2013-10-02 17:54:23 +08:00
|
|
|
err = io.ErrShortWrite
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|