func Open(cfg *config.Config) (*Ledis, error) { if len(cfg.DataDir) == 0 { cfg.DataDir = config.DefaultDataDir } if cfg.Databases == 0 { cfg.Databases = 16 } else if cfg.Databases > MaxDatabases { cfg.Databases = MaxDatabases } os.MkdirAll(cfg.DataDir, 0755) var err error l := new(Ledis) l.cfg = cfg if l.lock, err = filelock.Lock(path.Join(cfg.DataDir, "LOCK")); err != nil { return nil, err } l.quit = make(chan struct{}) if l.ldb, err = store.Open(cfg); err != nil { return nil, err } if cfg.UseReplication { if l.r, err = rpl.NewReplication(cfg); err != nil { return nil, err } l.rc = make(chan struct{}, 1) l.rbatch = l.ldb.NewWriteBatch() l.rDoneCh = make(chan struct{}, 1) l.wg.Add(1) go l.onReplication() //first we must try wait all replication ok //maybe some logs are not committed l.WaitReplication() } else { l.r = nil } l.dbs = make(map[int]*DB, 16) l.checkTTL() return l, nil }
func NewApp(cfg *config.Config) (*App, error) { if len(cfg.DataDir) == 0 { println("use default datadir %s", config.DefaultDataDir) cfg.DataDir = config.DefaultDataDir } app := new(App) app.quit = make(chan struct{}) app.closed = false app.cfg = cfg app.slaves = make(map[string]*client) app.slaveSyncAck = make(chan uint64) app.rcs = make(map[*respClient]struct{}) app.migrateClients = make(map[string]*goredis.Client) app.newMigrateKeyLockers() var err error if app.info, err = newInfo(app); err != nil { return nil, err } if app.listener, err = net.Listen(netType(cfg.Addr), cfg.Addr); err != nil { return nil, err } if len(cfg.HttpAddr) > 0 { if app.httpListener, err = net.Listen(netType(cfg.HttpAddr), cfg.HttpAddr); err != nil { return nil, err } } if len(cfg.AccessLog) > 0 { if path.Dir(cfg.AccessLog) == "." { app.access, err = newAcessLog(path.Join(cfg.DataDir, cfg.AccessLog)) } else { app.access, err = newAcessLog(cfg.AccessLog) } if err != nil { return nil, err } } if app.snap, err = newSnapshotStore(cfg); err != nil { return nil, err } if len(app.cfg.SlaveOf) > 0 { //slave must readonly app.cfg.Readonly = true } if app.ldb, err = ledis.Open(cfg); err != nil { return nil, err } app.m = newMaster(app) app.openScript() app.ldb.AddNewLogEventHandler(app.publishNewLog) return app, nil }