func NewMDB(path string, config interface{}) (Engine, error) { c, ok := config.(*MDBConfiguration) if !ok { return nil, fmt.Errorf("Got config of type %T instead of %T", config, MDBConfiguration{}) } if c.MapSize == 0 { c.MapSize = 10 * 1024 * 1024 * 1024 } env, err := mdb.NewEnv() if err != nil { return MDB{}, err } // TODO: max dbs should be configurable if err := env.SetMaxDBs(1); err != nil { return MDB{}, err } if err := env.SetMapSize(uint64(c.MapSize)); err != nil { return MDB{}, err } if _, err := os.Stat(path); err != nil { err = os.MkdirAll(path, 0755) if err != nil { return MDB{}, err } } err = env.Open(path, mdb.WRITEMAP|mdb.MAPASYNC|mdb.CREATE, 0755) if err != nil { return MDB{}, err } tx, err := env.BeginTxn(nil, 0) if err != nil { return MDB{}, err } dbi, err := tx.DBIOpen(nil, mdb.CREATE) if err != nil { return MDB{}, err } if err := tx.Commit(); err != nil { return MDB{}, err } db := MDB{ env: env, db: dbi, path: path, } return db, nil }
func Open(c *Config) (MDB, error) { path := c.Path if c.MapSize == 0 { c.MapSize = 1024 * 1024 * 1024 } env, err := mdb.NewEnv() if err != nil { return MDB{}, err } // TODO: max dbs should be configurable if err := env.SetMaxDBs(1); err != nil { return MDB{}, err } if err := env.SetMapSize(uint64(c.MapSize)); err != nil { return MDB{}, err } if _, err := os.Stat(path); err != nil { err = os.MkdirAll(path, 0755) if err != nil { return MDB{}, err } } err = env.Open(path, mdb.NOSYNC|mdb.NOMETASYNC|mdb.WRITEMAP|mdb.MAPASYNC|mdb.CREATE, 0755) if err != nil { return MDB{}, err } tx, err := env.BeginTxn(nil, 0) if err != nil { return MDB{}, err } dbi, err := tx.DBIOpen(nil, mdb.CREATE) if err != nil { return MDB{}, err } if err := tx.Commit(); err != nil { return MDB{}, err } db := MDB{ env: env, db: dbi, path: path, } return db, nil }