Exemplo n.º 1
0
func newSegmentMap() *segmentMap {
	return &segmentMap{
		entries: skiplist.NewCustomMap(func(l, r interface{}) bool {
			return l.(uint64) > r.(uint64)
		}),
	}
}
Exemplo n.º 2
0
// New returns a disk-backed WriteAheadLogger
func New(name string, dataPath string, segmentMaxBytes int64, syncTimeout time.Duration, logger logger) (WriteAheadLogger, error) {
	var err error

	w := &wal{
		name:            name,
		dataPath:        dataPath,
		segmentMaxBytes: segmentMaxBytes,
		exitChan:        make(chan int),
		syncTimeout:     syncTimeout,
		logger:          logger,
		version:         1,

		// uint64 keyed skip-list with *reverse* ordering so that
		// GetGreaterOrEqual actually returns LT | EQ
		segmentList: skiplist.NewCustomMap(func(l, r interface{}) bool {
			return l.(uint64) > r.(uint64)
		}),
	}

	rwl := &rwLock{&w.metadataLock}
	w.writeCond = sync.NewCond(rwl)
	w.rollCond = sync.NewCond(rwl)

	err = w.scanSegments()
	if err != nil {
		w.logf("ERROR: scanSegments - %s", err)
	}

	segment, err := w.openSegment(w.segmentNum, w.idx)
	if err != nil {
		return nil, err
	}
	w.segment = segment
	w.segmentOffset = w.segment.offset

	if w.syncTimeout > 0 {
		go w.syncLoop()
	}

	return w, err
}