2012-09-03 22:41:24 +00:00
|
|
|
package sequence
|
|
|
|
|
|
|
|
import (
|
2013-11-05 09:59:00 +00:00
|
|
|
"bytes"
|
2013-08-09 06:57:22 +00:00
|
|
|
"code.google.com/p/weed-fs/go/glog"
|
2013-11-05 09:59:00 +00:00
|
|
|
"code.google.com/p/weed-fs/go/metastore"
|
2013-09-02 06:58:21 +00:00
|
|
|
"encoding/gob"
|
2013-01-17 08:56:56 +00:00
|
|
|
"sync"
|
2012-09-03 22:41:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
FileIdSaveInterval = 10000
|
|
|
|
)
|
|
|
|
|
|
|
|
type Sequencer interface {
|
|
|
|
NextFileId(count int) (uint64, int)
|
|
|
|
}
|
|
|
|
type SequencerImpl struct {
|
2013-11-10 09:31:50 +00:00
|
|
|
fileFullPath string
|
2012-09-03 22:41:24 +00:00
|
|
|
|
|
|
|
volumeLock sync.Mutex
|
|
|
|
sequenceLock sync.Mutex
|
|
|
|
|
|
|
|
FileIdSequence uint64
|
|
|
|
fileIdCounter uint64
|
2013-11-05 09:59:00 +00:00
|
|
|
|
|
|
|
metaStore *metastore.MetaStore
|
2012-09-03 22:41:24 +00:00
|
|
|
}
|
|
|
|
|
2013-11-10 09:31:50 +00:00
|
|
|
func NewFileSequencer(filepath string) (m *SequencerImpl) {
|
|
|
|
m = &SequencerImpl{fileFullPath: filepath}
|
2013-11-05 09:59:00 +00:00
|
|
|
m.metaStore = &metastore.MetaStore{metastore.NewMetaStoreFileBacking()}
|
2013-11-10 09:31:50 +00:00
|
|
|
m.initilize()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *SequencerImpl) initilize() {
|
|
|
|
if !m.metaStore.Has(m.fileFullPath) {
|
2013-01-17 08:56:56 +00:00
|
|
|
m.FileIdSequence = FileIdSaveInterval
|
2013-08-09 06:57:22 +00:00
|
|
|
glog.V(0).Infoln("Setting file id sequence", m.FileIdSequence)
|
2013-01-17 08:56:56 +00:00
|
|
|
} else {
|
2013-11-05 09:59:00 +00:00
|
|
|
var err error
|
2013-11-10 09:31:50 +00:00
|
|
|
if m.FileIdSequence, err = m.metaStore.GetUint64(m.fileFullPath); err != nil {
|
|
|
|
if data, err := m.metaStore.Get(m.fileFullPath); err == nil {
|
2013-11-05 09:59:00 +00:00
|
|
|
m.FileIdSequence = decode(data)
|
2013-11-10 09:31:50 +00:00
|
|
|
glog.V(0).Infoln("Decoding old version of FileIdSequence", m.FileIdSequence)
|
2013-11-05 09:59:00 +00:00
|
|
|
} else {
|
|
|
|
glog.V(0).Infof("No existing FileIdSequence: %s", err)
|
|
|
|
}
|
2013-02-27 06:54:22 +00:00
|
|
|
} else {
|
2013-11-05 09:59:00 +00:00
|
|
|
glog.V(0).Infoln("Loading file id sequence", m.FileIdSequence)
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2013-01-17 08:56:56 +00:00
|
|
|
//in case the server stops between intervals
|
|
|
|
}
|
|
|
|
return
|
2012-09-03 22:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//count should be 1 or more
|
|
|
|
func (m *SequencerImpl) NextFileId(count int) (uint64, int) {
|
|
|
|
if count <= 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
m.sequenceLock.Lock()
|
|
|
|
defer m.sequenceLock.Unlock()
|
|
|
|
if m.fileIdCounter < uint64(count) {
|
|
|
|
m.fileIdCounter = FileIdSaveInterval
|
|
|
|
m.FileIdSequence += FileIdSaveInterval
|
|
|
|
m.saveSequence()
|
|
|
|
}
|
|
|
|
m.fileIdCounter = m.fileIdCounter - uint64(count)
|
2013-03-19 20:37:36 +00:00
|
|
|
return m.FileIdSequence - m.fileIdCounter - uint64(count), count
|
2012-09-03 22:41:24 +00:00
|
|
|
}
|
|
|
|
func (m *SequencerImpl) saveSequence() {
|
2013-11-10 09:31:50 +00:00
|
|
|
glog.V(0).Infoln("Saving file id sequence", m.FileIdSequence, "to", m.fileFullPath)
|
|
|
|
if e := m.metaStore.SetUint64(m.fileFullPath, m.FileIdSequence); e != nil {
|
2013-11-05 09:59:00 +00:00
|
|
|
glog.Fatalf("Sequence id Save [ERROR] %s", e)
|
2013-01-17 08:56:56 +00:00
|
|
|
}
|
2013-11-05 09:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//decode are for backward compatible purpose
|
2013-11-10 09:31:50 +00:00
|
|
|
func decode(input string) uint64 {
|
2013-11-05 09:59:00 +00:00
|
|
|
var x uint64
|
2013-11-10 09:31:50 +00:00
|
|
|
b := bytes.NewReader([]byte(input))
|
2013-11-05 09:59:00 +00:00
|
|
|
decoder := gob.NewDecoder(b)
|
|
|
|
if e := decoder.Decode(&x); e == nil {
|
|
|
|
return x
|
2013-02-27 06:54:22 +00:00
|
|
|
}
|
2013-11-05 09:59:00 +00:00
|
|
|
return 0
|
2012-09-03 22:41:24 +00:00
|
|
|
}
|