1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
|
// Copyright ©2011-2012 The bíogo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package morass implements file system-backed sorting.
//
// Use morass when you don't want your data to be a quagmire.
//
// Sort data larger than can fit in memory.
//
// morass məˈras/
// 1. An area of muddy or boggy ground.
// 2. A complicated or confused situation.
package morass
import (
"container/heap"
"encoding/gob"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"reflect"
"runtime"
"sort"
"sync"
)
var (
registerLock = &sync.Mutex{}
registered = make(map[reflect.Type]struct{})
nextID = 0
)
func register(e interface{}, t reflect.Type) {
registerLock.Lock()
defer registerLock.Unlock()
defer func() {
recover() // The only panic that we can get is from trying to register a base type.
registered[t] = struct{}{} // Remember for next time.
}()
if _, exists := registered[t]; !exists {
registered[t] = struct{}{}
gob.RegisterName(fmt.Sprintf("ℳ%d", nextID), e)
nextID++
}
}
// LessInterface wraps the Less method.
type LessInterface interface {
// Is the receiver less than the parameterised interface
Less(i interface{}) bool
}
type sorter []LessInterface
func (s sorter) Len() int { return len(s) }
func (s sorter) Less(i, j int) bool { return s[i].Less(s[j]) }
func (s sorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type file struct {
head LessInterface
file *os.File
encoder *gob.Encoder
decoder *gob.Decoder
}
type files []*file
func (f files) Len() int { return len(f) }
func (f files) Less(i, j int) bool { return f[i].head.Less(f[j].head) }
func (f files) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f *files) Pop() (i interface{}) {
i = (*f)[len(*f)-1]
*f = (*f)[:len(*f)-1]
return
}
func (f *files) Push(x interface{}) { *f = append(*f, x.(*file)) }
// Morass implements sorting of very large data sets.
type Morass struct {
typ reflect.Type
pos, len int64
// dir and prefix specify the location
// of temporary files.
dir string
prefix string
// AutoClear specifies that the Morass
// should call Clear when emptied by
// a call to Pull.
AutoClear bool
// AutoClean specifies that the Morass
// should call delete temporary sort
// files when it has been emptied by
// a call to Pull.
AutoClean bool
// fast indicates sorting was performed
// entirely in memory.
fast bool
chunk sorter
chunkSize int
pool chan sorter
writable chan sorter
filesLock sync.Mutex
files files
errLock sync.Mutex
_err error
}
// New creates a new Morass. prefix and dir are passed to ioutil.TempDir. chunkSize specifies
// the amount of sorting to be done in memory, concurrent specifies that temporary file
// writing occurs concurrently with sorting.
// An error is returned if no temporary directory can be created.
// Note that the type is registered with the underlying gob encoder using the name ℳn, where
// n is a sequentially assigned integer string, when the type registered. This is done to avoid using
// too much space and will cause problems when using gob itself on this type. If you intend
// use gob itself with this the type, preregister with gob and morass will use the existing
// registration.
func New(e interface{}, prefix, dir string, chunkSize int, concurrent bool) (*Morass, error) {
d, err := ioutil.TempDir(dir, prefix)
if err != nil {
return nil, err
}
m := &Morass{
chunkSize: chunkSize,
prefix: prefix,
dir: d,
pool: make(chan sorter, 2),
writable: make(chan sorter, 1),
files: files{},
}
m.typ = reflect.TypeOf(e)
register(e, m.typ)
m.chunk = make(sorter, 0, chunkSize)
if concurrent {
m.pool <- nil
}
runtime.SetFinalizer(m, func(x *Morass) {
if x.AutoClean {
x.CleanUp()
}
})
return m, nil
}
// Push a value on to the Morass. Returns any error that occurs.
func (m *Morass) Push(e LessInterface) error {
if typ := reflect.TypeOf(e); typ != m.typ {
return fmt.Errorf("morass: type mismatch: %s != %s", typ, m.typ)
}
if err := m.err(); err != nil {
return err
}
if m.chunk == nil {
return errors.New("morass: push on finalised morass")
}
if len(m.chunk) == m.chunkSize {
m.writable <- m.chunk
go m.write()
m.chunk = <-m.pool
if err := m.err(); err != nil {
return err
}
if cap(m.chunk) == 0 {
m.chunk = make(sorter, 0, m.chunkSize)
}
}
m.chunk = append(m.chunk, e)
m.pos++
m.len++
return nil
}
func (m *Morass) write() {
writing := <-m.writable
defer func() {
m.pool <- writing[:0]
}()
sort.Sort(writing)
tf, err := ioutil.TempFile(m.dir, m.prefix)
if err != nil {
m.setErr(err)
return
}
enc := gob.NewEncoder(tf)
dec := gob.NewDecoder(tf)
f := &file{head: nil, file: tf, encoder: enc, decoder: dec}
m.filesLock.Lock()
m.files = append(m.files, f)
m.filesLock.Unlock()
for _, e := range writing {
if err := enc.Encode(&e); err != nil {
m.setErr(err)
return
}
}
m.setErr(tf.Sync())
}
func (m *Morass) setErr(err error) {
m.errLock.Lock()
m._err = err
m.errLock.Unlock()
}
func (m *Morass) err() error {
m.errLock.Lock()
defer m.errLock.Unlock()
return m._err
}
// Pos returns the current position of the cursor in the Morass.
func (m *Morass) Pos() int64 { return m.pos }
// Len returns the current length of the Morass.
func (m *Morass) Len() int64 { return m.len }
// Finalise is called to indicate that the last element has been pushed on to the Morass
// and write out final data.
func (m *Morass) Finalise() error {
if err := m.err(); err != nil {
return err
}
if m.chunk != nil {
if m.pos < int64(cap(m.chunk)) {
m.fast = true
sort.Sort(m.chunk)
} else {
if len(m.chunk) > 0 {
m.writable <- m.chunk
m.chunk = nil
m.write()
if err := m.err(); err != nil {
return err
}
}
}
m.pos = 0
} else {
return nil
}
if !m.fast {
for _, f := range m.files {
_, err := f.file.Seek(0, 0)
if err != nil {
return err
}
err = f.decoder.Decode(&f.head)
if err != nil && err != io.EOF {
return err
}
}
heap.Init(&m.files)
}
return nil
}
// Clear resets the Morass to an empty state.
func (m *Morass) Clear() error {
var err error
for _, f := range m.files {
err = f.file.Close()
if err != nil {
return err
}
err = os.Remove(f.file.Name())
if err != nil {
return err
}
}
m._err = nil
m.files = m.files[:0]
m.pos = 0
m.len = 0
select {
case m.chunk = <-m.pool:
if m.chunk == nil {
m.chunk = make(sorter, 0, m.chunkSize)
}
default:
}
return nil
}
// CleanUp deletes the file system components of the Morass. After this call
// the Morass is not usable.
func (m *Morass) CleanUp() error {
return os.RemoveAll(m.dir)
}
// Pull sets the settable value e to the lowest value in the Morass.
// If io.EOF is returned the Morass is empty. Any other error results
// in no value being set on e.
func (m *Morass) Pull(e LessInterface) error {
var err error
v := reflect.ValueOf(e)
if !reflect.Indirect(v).CanSet() {
return errors.New("morass: cannot set e")
}
if m.fast {
switch {
case m.chunk != nil && m.pos < int64(len(m.chunk)):
e = m.chunk[m.pos].(LessInterface)
m.pos++
case m.chunk != nil:
m.pool <- m.chunk[:0]
m.chunk = nil
fallthrough
default:
if m.AutoClear {
m.Clear()
}
err = io.EOF
}
} else {
if m.files.Len() > 0 {
low := heap.Pop(&m.files).(*file)
e = low.head
m.pos++
switch err = low.decoder.Decode(&low.head); err {
case nil:
heap.Push(&m.files, low)
case io.EOF:
err = nil
fallthrough
default:
low.file.Close()
if m.AutoClear {
os.Remove(low.file.Name())
}
}
} else {
if m.AutoClear {
m.Clear()
}
if m.AutoClean {
os.RemoveAll(m.dir)
}
err = io.EOF
}
}
if err != nil {
return err
}
reflect.Indirect(v).Set(reflect.ValueOf(e))
return nil
}
|