示例#1
0
// NewRW function reads or creates a block on given directory.
// It will automatically load all existing block files.
func NewRW(dir string, rsz int64) (b *RWBlock, err error) {
	rbs := rsz * pointsz
	sfp := path.Join(dir, prefix)
	sfs := segsz - (segsz % rbs)
	ssz := sfs / rbs
	m, err := segmmap.New(sfp, sfs, false)
	if err != nil {
		return nil, err
	}

	b = &RWBlock{
		records:   [][]protocol.Point{},
		recsMtx:   new(sync.RWMutex),
		segments:  m,
		recLength: rsz,
		recBytes:  rbs,
		segRecs:   ssz,
		emptyRec:  make([]protocol.Point, rsz),
	}

	// This will use the segment.Read method until it reaches the EOF
	// Make sure no other operation uses segment.Read/Write methods.
	// If it becomes necessary, save the offset value in this struct.
	if err := b.readRecords(); err != nil {
		return nil, err
	}

	return b, nil
}
示例#2
0
文件: logs.go 项目: kadirahq/kadiyadb
// NewLogs creates a log type index persister.
func NewLogs(dir string) (l *Logs, err error) {
	sfpath := path.Join(dir, prefixlogs)
	f, err := segmmap.New(sfpath, segszlogs, false)
	if err != nil {
		return nil, err
	}

	l = &Logs{
		logFile: f,
		nextID:  0,
		nextOff: 0,
		iomutex: &sync.Mutex{},
	}

	return l, nil
}