Пример #1
0
func New(dataDir string) (app *App, err error) {
	err = os.Mkdir(dataDir, 0700)
	if err != nil && !os.IsExist(err) {
		return nil, err
	}

	lockPath := filepath.Join(dataDir, "lock")
	lockFile, err := lock(lockPath)
	if err != nil {
		return nil, err
	}
	defer func() {
		if err != nil {
			// if we're reporting an error, also unlock
			_ = lockFile.Close()
		}
	}()

	kvpath := filepath.Join(dataDir, "chunks")
	err = kvfiles.Create(kvpath)
	if err != nil {
		return nil, err
	}

	dbpath := filepath.Join(dataDir, "bazil.bolt")
	database, err := db.Open(dbpath, 0600, nil)
	if err != nil {
		return nil, err
	}

	err = database.DB.Update(func(tx *bolt.Tx) error {
		if _, err := tx.CreateBucketIfNotExists([]byte(tokens.BucketBazil)); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		database.Close()
		return nil, err
	}

	keys, err := loadOrGenerateKeys(database.DB)
	if err != nil {
		return nil, err
	}

	app = &App{
		DataDir:  dataDir,
		lockFile: lockFile,
		DB:       database,
		Keys:     keys,
	}
	app.volumes.Cond.L = &app.volumes.Mutex
	app.volumes.open = make(map[db.VolumeID]*VolumeRef)
	return app, nil
}
Пример #2
0
func NewTestDB(t testing.TB) *TestDB {
	f, err := ioutil.TempFile("", "bazil-test-db-")
	if err != nil {
		t.Fatalf("cannot create temp file: %v", err)
	}
	path := f.Name()
	f.Close()

	db, err := db.Open(path, 0600, &bolt.Options{Timeout: 1 * time.Nanosecond})
	if err != nil {
		t.Fatalf("db open: %v", err)
	}
	return &TestDB{db}
}