Esempio n. 1
0
File: ybc.go Progetto: rjmcguire/ybc
// Opens a cache.
//
// If force is true, then tries fixing the following non-critical errors
// instead of returning ErrOpenFailed:
//   * creates missing index or data files.
//   * fixes incorrect sizes for index or data files.
//
// Do not open the same cache more than once at the same time!
//
// The returned cache must be closed with cache.Close() call!
// Prefer using defer for closing opened caches:
//
//   cache, err := config.OpenCache(true)
//   if err != nil {
//     log.Fatalf("Error when opening the cache: [%s]", err)
//   }
//   defer cache.Close()
//
func (cfg *Config) OpenCache(force bool) (cache *Cache, err error) {
	c := cfg.internal()
	defer C.ybc_config_destroy(c.ctx)

	c.cg.Acquire()
	err = errPanic
	defer func() {
		if err != nil {
			c.cg.Release()
		}
	}()

	cache = &Cache{
		buf: make([]byte, cacheSize),
		cg:  c.cg,
	}
	mForce := C.int(0)
	if force {
		mForce = 1
	}
	if C.ybc_open(cache.ctx(), c.ctx, mForce) == 0 {
		cache = nil
		err = ErrOpenFailed
		return
	}
	cache.dg.Init()
	err = nil
	return
}
Esempio n. 2
0
File: ybc.go Progetto: rjmcguire/ybc
// Removes cache files from filesystem.
func (cfg *Config) RemoveCache() {
	c := cfg.internal()
	defer C.ybc_config_destroy(c.ctx)

	C.ybc_remove(c.ctx)
}