示例#1
0
/*
SaveData ...
*/
func (m *ManagerStruct) SaveData(ID string, data []byte, offset int64) {
	f := m.getFileFromCache(ID)
	buf := new(bytes.Buffer)
	err := binary.Write(buf, binary.BigEndian, data)
	utils.PanicOnError(err)
	_, err = f.WriteAt(buf.Bytes(), offset)
	utils.PanicOnError(err)
}
示例#2
0
/*
LoadData ...
*/
func (m *ManagerStruct) LoadData(ID string, offset int64, length int64) []byte {
	f := m.getFileFromCache(ID)
	if length == 0 {
		info, err := f.Stat()
		utils.PanicOnError(err)
		length = info.Size()
		length -= offset
	}
	data := make([]byte, length)
	_, err := f.ReadAt(data, offset)
	utils.PanicOnError(err)
	return data
}
示例#3
0
func newManager() *ManagerStruct {
	conf = config.GetConfig()
	dataPath = conf.GetDataDir()
	//FIXME: size of cache should be read from config
	cache, err := lru.NewWithEvict(250, onFileEvicted)
	utils.PanicOnError(err)
	return &ManagerStruct{cache}
}
示例#4
0
func setupTests() {
	os.Setenv("COUNTS_DATA_DIR", "/tmp/count_data")
	path, err := os.Getwd()
	utils.PanicOnError(err)
	path = filepath.Dir(path)
	configPath := filepath.Join(path, "config/default.toml")
	os.Setenv("COUNTS_CONFIG", configPath)
}
示例#5
0
func parseConfigTOML() *Config {
	configPath := os.Getenv("COUNTS_CONFIG")
	if configPath == "" {
		path, err := os.Getwd()
		utils.PanicOnError(err)
		path, err = filepath.Abs(path)
		utils.PanicOnError(err)
		configPath = filepath.Join(path, "config/default.toml")
	}
	_, err := os.Open(configPath)
	utils.PanicOnError(err)
	config = &Config{}
	if _, err := toml.DecodeFile(configPath, &config); err != nil {
		utils.PanicOnError(err)
	}
	return config
}
示例#6
0
func (m *ManagerStruct) getFileFromCache(ID string) *os.File {
	v, ok := m.cache.Get(ID)
	var f *os.File
	var err error
	if !ok {
		f, err = os.Open(filepath.Join(dataPath, ID))
		utils.PanicOnError(err)
	} else {
		f = v.(*os.File)
	}
	return f
}
示例#7
0
/*
GetConfig returns a singleton Configuration
*/
func GetConfig() *Config {
	if config == nil {
		config = parseConfigTOML()
		usr, err := user.Current()
		utils.PanicOnError(err)
		dir := usr.HomeDir

		infoDir := strings.TrimSpace(os.Getenv("COUNTS_INFO_DIR"))
		if len(infoDir) == 0 {
			if config.InfoDir[:2] == "~/" {
				infoDir = strings.Replace(config.InfoDir, "~", dir, 1)
			}
		}
		os.Mkdir(infoDir, 0777)

		dataDir := strings.TrimSpace(os.Getenv("COUNTS_DATA_DIR"))
		if len(dataDir) == 0 {
			if config.DataDir[:2] == "~/" {
				dataDir = strings.Replace(config.DataDir, "~", dir, 1)
			}
		}
		os.Mkdir(dataDir, 0777)

		port, err := strconv.Atoi(strings.TrimSpace(os.Getenv("COUNTS_PORT")))
		if err != nil {
			port = config.Port
		}

		config = &Config{
			infoDir,
			dataDir,
			config.SliceSize,
			config.CacheSize,
			config.SliceCacheSize,
			port,
		}
	}
	return config
}
示例#8
0
/*
Create storage
*/
func (m *ManagerStruct) Create(ID string) {
	f, err := os.Create(filepath.Join(dataPath, ID))
	utils.PanicOnError(err)
	m.cache.Add(ID, f)
}