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 }
// OpenAllDb opens all files with the given suffix in the given dir func OpenAllDb(dir, suffix string) (<-chan *kv.DB, <-chan error) { dest := make(chan *kv.DB) errch := make(chan error, 1) var ( db *kv.DB err error fn string ) go func() { if err = MapDirItems(dir, func(fi os.FileInfo) bool { return fi.Mode().IsRegular() && (suffix == "" || strings.HasSuffix(fi.Name(), suffix)) }, func(fi os.FileInfo) error { fn = filepath.Join(dir, fi.Name()) if db, err = kv.Open(fn, kvOptions); err != nil { return err } dest <- db return nil }); err != nil { errch <- err } close(dest) close(errch) }() return dest, errch }
// Open a store at the given path. func Open(path string) (*Store, error) { db, err := kv.Open(path, nil) if err != nil { return nil, err } ret := &Store{ db: db, } return ret, nil }
// 打开或者创建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 }
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 }
func main() { datastore, err := kv.Open("mpush.kvdb", &kv.Options{}) if err != nil { log.Fatal(err) } defer datastore.Close() wsContainer := restful.NewContainer() restful.Filter(globalLogging) dsl := DistributionListResource{datastore} dsl.Register(wsContainer) wsPush := new(restful.WebService) wsPush.Path("/api/push") wsPush.Route(wsPush.POST(""). Consumes("application/x-www-form-urlencoded"). To(func(request *restful.Request, response *restful.Response) { pushHandler(request, response, datastore) }). // docs Doc("push to a distribution list"). Param(wsPush.BodyParameter("List", "list to send to").DataType("string")). Param(wsPush.BodyParameter("Title", "title of notification").DataType("string")). Param(wsPush.BodyParameter("Body", "body of notification").DataType("string")). Reads(PushNotification{})) // from the request wsContainer.Add(wsPush) // static files wsStatic := new(restful.WebService) wsStatic.Route(wsStatic.GET("/static/{resource}").To(staticFromPathParam)) wsStatic.Route(wsStatic.GET("/static").To(staticFromQueryParam)) wsContainer.Add(wsStatic) // Optionally, you can install the Swagger Service which provides a nice Web UI on your REST API // You need to download the Swagger HTML5 assets and change the FilePath location in the config below. // Open http://localhost:8080/apidocs and enter http://localhost:8080/apidocs.json in the api input field. config := swagger.Config{ WebServices: wsContainer.RegisteredWebServices(), // you control what services are visible WebServicesUrl: "http://localhost:8080", ApiPath: "/apidocs.json", // Optionally, specifiy where the UI is located SwaggerPath: "/apidocs/", SwaggerFilePath: "/home/dgryski/work/src/cvs/swagger-ui/dist"} swagger.RegisterSwaggerService(config, wsContainer) log.Printf("start listening on localhost:8080") server := &http.Server{Addr: ":8080", Handler: wsContainer} log.Fatal(server.ListenAndServe()) }
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 }
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 }
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) } }
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") }
func openBucket(filename string) (b wBucket, err error) { fh, e := os.Open(filename) if e != nil { err = e return } fi, e := fh.Stat() fh.Close() if e != nil { err = e return } //b = bucket{filename: filename, created: fi.ModTime()} b.filename, b.created = filename, fi.ModTime() b.db, err = kv.Open(filename, kvOptions()) if err != nil { err = fmt.Errorf("error opening buckets db %s: %s", filename, err) return } return }
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()) } }