示例#1
0
文件: db.go 项目: eswdd/bosun
func (s MemStore) Open(path string, cfg *config.Config) (driver.IDB, error) {
	db := new(DB)
	db.path = path
	db.cfg = &cfg.LevelDB

	db.initOpts()

	var err error
	db.db, err = leveldb.Open(storage.NewMemStorage(), db.opts)
	if err != nil {
		return nil, err
	}

	return db, nil
}
示例#2
0
func newTestStorage(t *testing.T) *testStorage {
	var stor storage.Storage
	var closeFn func() error
	if tsFS {
		for {
			tsMU.Lock()
			num := tsNum
			tsNum++
			tsMU.Unlock()
			tempdir := tsTempdir
			if tempdir == "" {
				tempdir = os.TempDir()
			}
			path := filepath.Join(tempdir, fmt.Sprintf("goleveldb-test%d0%d0%d", os.Getuid(), os.Getpid(), num))
			if _, err := os.Stat(path); err != nil {
				stor, err = storage.OpenFile(path)
				if err != nil {
					t.Fatalf("F: cannot create storage: %v", err)
				}
				t.Logf("I: storage created: %s", path)
				closeFn = func() error {
					for _, name := range []string{"LOG.old", "LOG"} {
						f, err := os.Open(filepath.Join(path, name))
						if err != nil {
							continue
						}
						if log, err := ioutil.ReadAll(f); err != nil {
							t.Logf("---------------------- %s ----------------------", name)
							t.Logf("cannot read log: %v", err)
							t.Logf("---------------------- %s ----------------------", name)
						} else if len(log) > 0 {
							t.Logf("---------------------- %s ----------------------\n%s", name, string(log))
							t.Logf("---------------------- %s ----------------------", name)
						}
						f.Close()
					}
					if t.Failed() {
						t.Logf("testing failed, test DB preserved at %s", path)
						return nil
					}
					if tsKeepFS {
						return nil
					}
					return os.RemoveAll(path)
				}

				break
			}
		}
	} else {
		stor = storage.NewMemStorage()
	}
	ts := &testStorage{
		t:              t,
		Storage:        stor,
		closeFn:        closeFn,
		opens:          make(map[uint64]bool),
		emuErrOnceMap:  make(map[uint64]uint),
		emuRandErrProb: 0x999,
		emuRandRand:    rand.New(rand.NewSource(0xfacedead)),
	}
	ts.cond.L = &ts.mu
	return ts
}