Beispiel #1
0
func TestMakeTestData(t *testing.T) {
	if !*mk {
		return
	}

	db, err := kv.Create(filepath.Join(testdata, "dump.db"), &kv.Options{})
	if err != nil {
		t.Fatal(err)
	}

	defer func() {
		if err := db.Close(); err != nil {
			t.Fatal(err)
		}
	}()

	for i := 0; i < 10; i++ {
		c := 'a' + i
		if err = db.Set(
			[]byte(fmt.Sprintf("abc%c", c)),
			[]byte(fmt.Sprintf("%d", 1e6+i)),
		); err != nil {
			t.Error(err)
			return
		}
	}
}
Beispiel #2
0
func CreateMarkovChain(prefixlen int, dbpath string, create bool, getchan func() (chan Token, error)) (mc *MarkovChain, err error) {
	var opts = new(kv.Options)
	var conerr error

	mc = new(MarkovChain)
	opts.Compare = bytes.Compare
	mc.prefixLen = prefixlen

	if create {
		mc.CorpusDB, conerr = kv.Create(dbpath, opts)
		if conerr != nil {
			if mc.CorpusDB, conerr = kv.Open(dbpath, opts); conerr != nil {
				err = errors.New(fmt.Sprintf("cannot create new database %s : %s", dbpath, conerr))
				return
			}
		} else {
			c, e := getchan()
			if e != nil {
				mc.CorpusDB.Close()
				os.Remove(dbpath)
				err = nil
				return
			}

			mc.PopulateCorpus(c)
		}
	} else {
		mc.CorpusDB, conerr = kv.Open(dbpath, opts)
		if conerr != nil {
			err = errors.New(fmt.Sprintf("cannot open database %s : %s", dbpath, conerr))
			return
		}
	}
	return
}
Beispiel #3
0
// CreateBucket creates a new bucket
func (m master) CreateBucket(owner s3intf.Owner, bucket string) error {
	m.Lock()
	defer m.Unlock()
	o, ok := m.owners[owner.ID()]
	if !ok {
		dir := filepath.Join(m.baseDir, owner.ID())
		err := os.MkdirAll(dir, 0750)
		if err != nil {
			return err
		}
		o = wOwner{dir: dir, buckets: make(map[string]wBucket, 1)}
		m.owners[owner.ID()] = o
		//} else if o.buckets == nil {
		//	o.buckets = make(map[string]wBucket, 1)
	}
	o.Lock()
	defer o.Unlock()
	_, ok = o.buckets[bucket]
	if ok {
		return nil //AlreadyExists ?
	}
	b := wBucket{filename: filepath.Join(o.dir, bucket+".kv"), created: time.Now()}
	var err error
	if b.db, err = kv.Create(b.filename, kvOptions()); err != nil {
		return err
	}
	o.buckets[bucket] = b
	return nil
}
Beispiel #4
0
// 打开或者创建KV数据库
// 当path指向的数据库存在时打开该数据库,否则尝试在该路径处创建新数据库
func OpenOrCreateKv(path string, options *kv.Options) (*kv.DB, error) {
	db, errOpen := kv.Open(path, options)
	if errOpen != nil {
		var errCreate error
		db, errCreate = kv.Create(path, options)
		if errCreate != nil {
			return db, errCreate
		}
	}

	return db, nil
}
Beispiel #5
0
func openKVStorage(path string) (Storage, error) {
	options := &kv.Options{}
	db, errOpen := kv.Open(path, options)
	if errOpen != nil {
		var errCreate error
		db, errCreate = kv.Create(path, options)
		if errCreate != nil {
			return &kvStorage{db}, errCreate
		}
	}
	return &kvStorage{db}, nil
}
Beispiel #6
0
func NewKVCollection(path string) (c Collection, err error) {
	kvdb := &KVCollection{}

	opts := &kv.Options{}

	kvdb.db, err = kv.Create(path, opts)
	if err != nil {
		err = fmt.Errorf("unable to open %s: %v", path, err)
		return
	}

	return kvdb, err
}
Beispiel #7
0
func Open(opts Opts) (c *Client, err error) {
	c = new(Client)
	c.cards, err = readCardData(opts.CardData)
	if err != nil {
		return
	}
	c.db, err = kv.Open(opts.Filename, &kv.Options{})
	if err != nil {
		log.Printf("Creating new database... %v", err)
		c.db, err = kv.Create(opts.Filename, &kv.Options{})
	}
	return
}
Beispiel #8
0
func mustOpenShellDb(name string) *kv.DB {
	var err error
	var db *kv.DB
	_, err = os.Stat(name)
	if os.IsNotExist(err) {
		db, err = kv.Create(name, &kv.Options{})
	} else if err == nil {
		db, err = kv.Open(name, &kv.Options{})
	}
	if err != nil {
		panic(err)
	}
	return db
}
Beispiel #9
0
func (is *kvis) Wipe() error {
	// Unlock the already open DB.
	if err := is.db.Close(); err != nil {
		return err
	}
	if err := os.Remove(is.path); err != nil {
		return err
	}

	db, err := kv.Create(is.path, is.opts)
	if err != nil {
		return fmt.Errorf("error creating %s: %v", is.path, err)
	}
	is.db = db
	return nil
}
Beispiel #10
0
Datei: db.go Projekt: andrebq/exp
func openOrCreate(dbfile string) (*kv.DB, error) {
	opt := &kv.Options{VerifyDbBeforeOpen: true,
		VerifyDbAfterOpen:   true,
		VerifyDbBeforeClose: true,
		VerifyDbAfterClose:  true}

	if len(dbfile) == 0 {
		// in memory database
		return kv.CreateMem(opt)
	}
	_, err := os.Stat(dbfile)
	if os.IsNotExist(err) {
		return kv.Create(dbfile, opt)
	} else {
		return kv.Open(dbfile, opt)
	}
}
Beispiel #11
0
// Create a new store at the given path.
func New(path string) (*Store, error) {
	// Create a new DB
	db, err := kv.Create(path, nil)
	if err != nil {
		return nil, err
	}

	// Close the DB, then change file permissions to 0600
	db.Close()
	err = os.Chmod(path, 0600)
	if err != nil {
		return nil, err
	}

	// Re-open the DB, returning what that does
	return Open(path)
}
Beispiel #12
0
func (g *Globals) openDatabases() {
	log.Debug("globals.openDatabases START")
	dbOpts := &kv.Options{
		VerifyDbBeforeOpen:  true,
		VerifyDbAfterOpen:   true,
		VerifyDbBeforeClose: true,
		VerifyDbAfterClose:  true,
	}

	var err error
	if common.FileExists(g.Config.DBFilename) {
		g.db, err = kv.Open(g.Config.DBFilename, dbOpts)
	} else {
		g.db, err = kv.Create(g.Config.DBFilename, dbOpts)
	}
	if err != nil {
		log.Error("config.g open db error", "err", err)
		panic("config.g open  db error " + err.Error())
	}
	if u := g.GetUser("admin"); u == nil {
		log.Info("config.g openDatabases creating 'admin' user with password 'admin'")
		admin := &User{
			Login:  "******",
			Name:   "Administrator",
			Role:   "ADMIN",
			Active: true,
		}
		admin.UpdatePassword("admin")
		g.SaveUser(admin)
	} else {
		if !u.Active {
			log.Warn("config.g openDatabases re-active Admin user")
			u.Active = true
			g.SaveUser(u)
		}
	}
	if g.GetUser("admin") == nil {
		panic("missing admin")
	}
	log.Debug("globals.openDatabases DONE")
}
Beispiel #13
0
func main() {
	defer func() {
		if r := recover(); r != nil {
			log.Print(r)
		}
	}()
	log.SetFlags(0)
	flag.Parse()

	if *cpuprofile != "" {
		f, err := os.Create(*cpuprofile)
		if err != nil {
			log.Fatal(err)
		}
		pprof.StartCPUProfile(f)
		defer pprof.StopCPUProfile()
	}

	db, err := kv.Create(*dbpath, &kv.Options{})
	if err != nil {
		db, err = kv.Open(*dbpath, &kv.Options{})
		fatalif(err)
	}
	defer db.Close()

	err = db.BeginTransaction()
	fatalif(err)
	defer db.Commit()

	if *list {
		path := path.Join(kvback.TagsPrefix, flag.Arg(0))
		enum, _, _ := db.Seek([]byte(path))
		for {
			key, _, err := enum.Next()
			if err == io.EOF {
				break
			}
			fatalif(err)

			if !bytes.HasPrefix(key, []byte(kvback.TagsPrefix)) {
				break
			}

			fmt.Printf("%s\n", key)
		}
		return
	}

	for _, fname := range flag.Args() {
		fpath, err := filepath.Abs(fname)
		fatalif(err)
		f, err := os.Open(fpath)
		fatalif(err)
		h := kvback.New(db, fpath)
		fatalif(err)

		rs := rollsum.NewCustom(rollsum.DefaultWindow, 1024*8)
		fatalif(rbup.Split(f, rs, h))
		fatalif(f.Close())
	}
}