Beispiel #1
0
func (mFile *mmapFileImpl) Seek(pos int, from fs.FileOffset) {
	switch from {
	case fs.Beginning:
		mFile.pos = pos
	case fs.Current:
		mFile.pos = pos + pos
	case fs.End:
		mFile.pos = mFile.mapSize - pos
	}
	mFile.pos = math.MaxInt(0, math.MinInt(mFile.pos, mFile.mapSize-1))
}
Beispiel #2
0
// used for building graphs from scratch
func (g *graph) addVertex(id int) int {
	v := &vertex{
		id:       id,
		excess:   0,
		height:   0,
		g:        g,
		mappings: make(map[int]wrapper),
	}
	g.mapping[v.id] = wrapper{value: v, idx: g.NumVertices()}
	g.vertices = append(g.vertices, v)
	g.id = math.MaxInt(g.id, id+1)
	return id
}
Beispiel #3
0
// NewFile creates a new memory mapped file
func NewFile(fName string) (File, error) {
	var err error

	result := new(mmapFileImpl)
	result.name = fName

	// Create/Open file
	result.file, err = os.OpenFile(fName, os.O_CREATE|os.O_RDWR, 0644)
	if err != nil {
		return nil, errors.New("Could not open file for reading")
	}

	// Check to see if new
	info, _ := os.Stat(fName)
	result.mapSize = math.MaxInt(_InitialSize, int(info.Size()))

	if info.Size() == 0 {
		result.newFile = true
		result.file.Truncate(int64(result.mapSize))
	} else {
		result.newFile = false
	}

	// Map file to memory
	result.memmap, err = mmap.Map(result.file, mmap.RDWR, 0)

	// Validate
	if err != nil {
		return nil, err
	}

	result.lock = &sync.Mutex{}

	if !result.newFile {

		head := result.readHeader()

		assert.Assert(bytes.Equal(head.sanity, _Sanity), "Sanity check failed '"+string(head.sanity)+"'")
		assert.Assert(head.ver == _Version, "Versions do not match: ", head.ver, " vs. ", _Version)
		assert.Assert(int(head.mSize) == result.mapSize, fName+": Sizes do not match: ", head.mSize, result.mapSize)

	} else {
		result.writeHeader()
	}
	return result, nil
}