Example #1
0
func openSegment(path string) *segment {
	log.Info("Opening segment file %s", path)

	seg := &segment{
		nextSegments: make([]*segment, 0),
		prevSegments: make([]*segment, 0),
		writable:     false,
		lock:         new(sync.Mutex),
	}

	var err os.Error

	stat, err := os.Stat(path)
	if stat == nil || err != nil {
		log.Fatal("Couldn't stat segment file %s: %s", path, err)
	}

	var from, to Token
	_, err = fmt.Sscanf(stat.Name, "%016X_%04X_%04X.seg", &seg.positionStart, &from, &to)
	if err != nil {
		log.Fatal("Couldn't read segment file name %s: %s", path, err)
	}
	seg.tokens = TokenRange{from, to}

	seg.fd, err = os.Open(path)
	if err != nil {
		log.Fatal("Couldn't open segment %s: %s", path, err)
	}
	seg.typedFd = typedio.NewReadWriter(seg.fd)
	seg.positionEnd = seg.positionStart + uint64(stat.Size)

	return seg
}
Example #2
0
func createSegment(dataDir string, token_from, token_to Token, position uint64) *segment {
	seg := &segment{
		nextSegments:  make([]*segment, 0),
		prevSegments:  make([]*segment, 0),
		tokens:        TokenRange{token_from, token_to},
		positionStart: position,
		positionEnd:   position,
		lock:          new(sync.Mutex),
	}

	var err os.Error

	filePath := fmt.Sprintf("%s/%016X_%04X_%04X.seg", dataDir, position, token_from, token_to)
	seg.fd, err = os.Create(filePath)
	if err != nil {
		log.Fatal("Couldn't open segment %s: %s", filePath, err)
	}
	seg.typedFd = typedio.NewReadWriter(seg.fd)
	seg.writable = true

	seg.buf = bufio.NewWriter(seg.fd)
	seg.typedBuf = typedio.NewWriter(seg.buf)

	return seg
}
Example #3
0
func New(directory string) *CommitLog {
	cl := new(CommitLog)

	cl.mutations = make(map[byte]mutationInfo)
	cl.mutationsType = make(map[reflect.Type]mutationInfo)

	cl.fdMutex = new(sync.Mutex)

	// check if directory exists, create it otherwize
	dir, err := os.Stat(directory)
	if dir == nil || !dir.IsDirectory() {
		os.Mkdir(directory, 0777)
	}

	// check if log file exists and its at least the header size
	path := directory + "000.log"
	dir, err = os.Stat(path)
	existed := false
	if dir != nil && err == nil && dir.IsRegular() && dir.Size > HEADER_END {
		existed = true
	}

	// open the log
	flag := os.O_RDWR | os.O_CREATE
	cl.fd, err = os.OpenFile(path, flag, 0777)
	if err != nil {
		log.Fatal("CommitLog: Cannot open commit log file %s: %s", path, err)
	}
	cl.typedFd = typedio.NewReadWriter(cl.fd)

	// if commit log existed, read header
	if existed {
		cl.writePtr, _ = cl.typedFd.ReadInt64()
		cl.commitPtr, _ = cl.typedFd.ReadInt64()

	} else {
		// else, write header
		cl.writePtr = HEADER_END
		cl.commitPtr = HEADER_END

		cl.typedFd.WriteInt64(cl.writePtr)
		cl.typedFd.WriteInt64(cl.commitPtr)
	}

	return cl
}
Example #4
0
func New() *Buffer {
	r := new(Buffer)
	r.ReadWriter = typedio.NewReadWriter(r)
	return r
}