Пример #1
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
}
Пример #2
0
// Open opens the named kv DB file for reading/writing. It
// creates the file if it does not exist yet.
func Open(dbFile string, opts *kv.Options) (*kv.DB, error) {
	createOpen := kv.Open
	verb := "opening"
	if _, err := os.Stat(dbFile); os.IsNotExist(err) {
		createOpen = kv.Create
		verb = "creating"
	}
	if opts == nil {
		opts = &kv.Options{}
	}
	if opts.Locker == nil {
		opts.Locker = func(dbFile string) (io.Closer, error) {
			lkfile := dbFile + ".lock"
			cl, err := lock.Lock(lkfile)
			if err != nil {
				return nil, fmt.Errorf("failed to acquire lock on %s: %v", lkfile, err)
			}
			return cl, nil
		}
	}
	if v, _ := strconv.ParseBool(os.Getenv("CAMLI_KV_VERIFY")); v {
		opts.VerifyDbBeforeOpen = true
		opts.VerifyDbAfterOpen = true
		opts.VerifyDbBeforeClose = true
		opts.VerifyDbAfterClose = true
	}
	db, err := createOpen(dbFile, opts)
	if err != nil {
		return nil, fmt.Errorf("error %s %s: %v", verb, dbFile, err)
	}
	return db, nil
}