// NewMDBStore returns a new MDBStore and potential // error. Requres a base directory from which to operate, // and a maximum size. If maxSize is not 0, a default value is used. func NewMDBStoreWithSize(base string, maxSize int64) (*MDBStore, error) { // Get the paths path := filepath.Join(base, mdbPath) if err := os.MkdirAll(path, 0755); err != nil { return nil, err } // Set the maxSize if not given if maxSize == 0 { maxSize = dbMaxMapSize } // Create the env env, err := lmdb.NewEnv() if err != nil { return nil, err } // Create the struct store := &MDBStore{ env: env, path: path, maxSize: maxSize, } // Initialize the db if err := store.initialize(); err != nil { env.Close() return nil, err } return store, nil }
func newEnv(numDB int, sizeDB int64) *lmdb.Env { env, err := lmdb.NewEnv() raiseError(err) err = env.SetMaxDBs(numDB) raiseError(err) err = env.SetMapSize(sizeDB) raiseError(err) return env }
// NewDBStore creates an instance of Store backed by LMDB database. DBStore uses // a single file for DB data (named `DBStoreName`). Parameter `dirpath` is a // directory where the file will be stored. Returns nil if initialization // failed. func NewDBStore(dirpath string) *DBStore { env, err := lmdb.NewEnv() if err != nil { log.Errorf("failed to create DB environment: %v", err) return nil } if err := env.Open(path.Join(dirpath, DBStoreName), lmdb.NoSubdir, 0600); err != nil { log.Errorf("failed to open DB environment: %v", err) return nil } return &DBStore{ env: env, } }
func createLMDB(path string, opt graph.Options) (env *lmdb.Env, err error) { err = os.Mkdir(path, 0700) if err != nil && !os.IsExist(err) { return nil, err } env, err = lmdb.NewEnv() if err != nil { return env, err } defer func() { if err != nil { env.Close() env = nil } }() maxreaders, _, err := opt.IntKey("maxreaders") if err != nil { return nil, err } if maxreaders > 0 { err = env.SetMaxReaders(maxreaders) if err != nil { return nil, err } } maxdbs, _, err := opt.IntKey("maxdbs") if err != nil { return nil, err } if maxdbs == 0 { maxdbs = 7 } err = env.SetMaxDBs(maxdbs) if err != nil { return nil, err } mapsize, _, err := opt.IntKey("mapsize") if err != nil { return nil, err } err = env.SetMapSize(int64(mapsize)) if err != nil { return nil, err } var flags uint dbnosync, _, err := opt.BoolKey("nosync") if err != nil { return nil, err } if dbnosync { flags |= lmdb.NoSync } err = env.Open(path, flags, 0600) if err != nil { return nil, err } return env, nil }