NewDB open a new db instance
(options config.Options)
| 57 | |
| 58 | // NewDB open a new db instance |
| 59 | func NewDB(options config.Options) (*DB, error) { |
| 60 | zap.L().Info("open db", zap.Any("options", options)) |
| 61 | // check options first |
| 62 | if err := checkOptions(options); err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | |
| 66 | // check data dir, if not exist, create it |
| 67 | if _, err := os.Stat(options.DirPath); os.IsNotExist(err) { |
| 68 | if err := os.MkdirAll(options.DirPath, os.ModePerm); err != nil { |
| 69 | return nil, err |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // init db instance |
| 74 | db := &DB{ |
| 75 | options: options, |
| 76 | lock: new(sync.RWMutex), |
| 77 | olderFiles: make(map[uint32]*data2.DataFile), |
| 78 | index: index.NewIndexer(options.IndexType, options.DirPath), |
| 79 | } |
| 80 | |
| 81 | // load merge files |
| 82 | if err := db.loadMergeFiles(); err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | // load data files |
| 87 | if err := db.loadDataFiles(); err != nil { |
| 88 | return nil, err |
| 89 | } |
| 90 | |
| 91 | // load index from hint file |
| 92 | if err := db.loadIndexFromHintFile(); err != nil { |
| 93 | return nil, err |
| 94 | } |
| 95 | |
| 96 | // load index from data files |
| 97 | if err := db.loadIndexFromDataFiles(); err != nil { |
| 98 | return nil, err |
| 99 | } |
| 100 | return db, nil |
| 101 | } |
| 102 | |
| 103 | func checkOptions(options config.Options) error { |
| 104 | if options.DirPath == "" { |