/* 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) }
/* 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 }
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} }
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) }
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 }
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 }
/* 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 }
/* Create storage */ func (m *ManagerStruct) Create(ID string) { f, err := os.Create(filepath.Join(dataPath, ID)) utils.PanicOnError(err) m.cache.Add(ID, f) }