2014-04-09 00:36:05 +08:00
|
|
|
package mmap_span
|
2013-09-26 11:42:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2014-04-08 17:39:34 +08:00
|
|
|
|
2013-09-26 11:42:24 +08:00
|
|
|
"launchpad.net/gommap"
|
|
|
|
)
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
type segment struct {
|
2013-09-26 11:42:24 +08:00
|
|
|
gommap.MMap
|
|
|
|
}
|
|
|
|
|
2014-04-09 00:36:05 +08:00
|
|
|
func (me segment) Size() int64 {
|
2013-09-26 11:42:24 +08:00
|
|
|
return int64(len(me.MMap))
|
|
|
|
}
|
|
|
|
|
2014-12-05 14:54:55 +08:00
|
|
|
type MMapSpan struct {
|
|
|
|
span
|
|
|
|
}
|
2013-09-26 11:42:24 +08:00
|
|
|
|
2014-12-05 14:54:55 +08:00
|
|
|
func (me *MMapSpan) Append(mmap gommap.MMap) {
|
|
|
|
me.span = append(me.span, segment{mmap})
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
2013-09-26 17:49:15 +08:00
|
|
|
func (me MMapSpan) Close() {
|
2014-12-05 14:54:55 +08:00
|
|
|
for _, mMap := range me.span {
|
|
|
|
mMap.(segment).UnsafeUnmap()
|
2013-09-26 17:49:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (me MMapSpan) Size() (ret int64) {
|
2014-12-05 14:54:55 +08:00
|
|
|
for _, seg := range me.span {
|
2014-04-09 00:36:05 +08:00
|
|
|
ret += seg.Size()
|
2013-09-26 11:42:24 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:49:15 +08:00
|
|
|
func (me MMapSpan) ReadAt(p []byte, off int64) (n int, err error) {
|
2014-12-05 14:54:55 +08:00
|
|
|
me.ApplyTo(off, func(intervalOffset int64, interval sizer) (stop bool) {
|
2014-04-09 00:36:05 +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
|
|
|
|
}
|
|
|
|
|
2013-10-07 07:35:10 +08:00
|
|
|
func (me MMapSpan) WriteSectionTo(w io.Writer, off, n int64) (written int64, err error) {
|
2014-12-05 14:54:55 +08:00
|
|
|
me.ApplyTo(off, func(intervalOffset int64, interval sizer) (stop bool) {
|
2013-10-07 07:35:10 +08:00
|
|
|
var _n int
|
2014-04-09 00:36:05 +08:00
|
|
|
p := interval.(segment).MMap[intervalOffset:]
|
2013-10-07 07:35:10 +08:00
|
|
|
if n < int64(len(p)) {
|
|
|
|
p = p[:n]
|
|
|
|
}
|
|
|
|
_n, err = w.Write(p)
|
|
|
|
written += int64(_n)
|
|
|
|
n -= int64(_n)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2013-10-07 15:58:33 +08:00
|
|
|
return n == 0
|
2013-10-07 07:35:10 +08:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-26 17:49:15 +08:00
|
|
|
func (me MMapSpan) WriteAt(p []byte, off int64) (n int, err error) {
|
2014-12-05 14:54:55 +08:00
|
|
|
me.ApplyTo(off, func(iOff int64, i sizer) (stop bool) {
|
2014-04-09 00:36:05 +08:00
|
|
|
mMap := i.(segment)
|
2013-10-20 22:07:01 +08:00
|
|
|
_n := copy(mMap.MMap[iOff:], p)
|
|
|
|
// 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
|
|
|
|
}
|