Exemple #1
0
func main() {
	m := martini.Classic()

	m.Use(martini.Static("public"))

	m.Use(render.Renderer(render.Options{
		Layout:     "layout",
		Extensions: []string{".tmpl.html"},
	}))

	var opts db.Options
	level, err := leveldb.Open("database/messageDB", &opts)
	if err != nil {
		panic(err)
	}
	defer level.Close()

	m.Map(level)

	m.Get("/", Index)
	m.Get("/add", Add)
	m.Post("/create", binding.Bind(Message{}), Create)

	m.Run()
}
Exemple #2
0
func NewLevelDbFriendJar(filename string) (this *LevelDbFriendJar) {
	this = &LevelDbFriendJar{}
	db, err := leveldb.Open(filename, nil)
	util.Try(err)
	this.db = db
	return this
}
Exemple #3
0
func NewLevelDB(path string, restore bool) (*LevelDB, error) {
	fpath, err := filepath.Abs(path)
	if err != nil {
		return nil, err
	}

	if !restore {
		err = os.RemoveAll(fpath)
		if err != nil {
			return nil, err
		}
	}

	ldb, err := leveldb.Open(fpath, nil) // TODO: tune the option
	if err != nil {
		return nil, err
	}

	ret := &LevelDB{
		fpath: fpath,
		ldb:   ldb,
		wsync: &db.WriteOptions{Sync: true},
	}
	return ret, nil
}
func doSnapshot(logger *log.Logger, params martini.Params, r render.Render) {
	lang := params["language"]

	// levelDBへ保存
	level, err := leveldb.Open("snapshot", &db.Options{})
	if err != nil {
		r.Error(400)
	}
	defer level.Close()

	key := createSnapshotKey(lang, time.Now())
	logger.Println("key: ", key)
	if _, err := level.Get([]byte(key), &db.ReadOptions{}); err != nil {
		res, err := http.Get(fmt.Sprintf(gitHubStarURL, lang))
		if err != nil {
			r.Error(400)
		}
		data, err := ioutil.ReadAll(res.Body)
		if err != nil {
			r.Error(400)
		}
		if err := level.Set([]byte(key), data, &db.WriteOptions{}); err != nil {
			r.Error(400)
		}
	}

	r.JSON(200, nil)
}
func readGitHubStarRanking(lang string) ([]Ranking, error) {

	res, err := http.Get(fmt.Sprintf(gitHubStarURL, lang))
	if err != nil {
		return nil, err
	}
	data, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}
	// parse
	var reps Repositories
	if err := json.Unmarshal(data, &reps); err != nil {
		return nil, err
	}

	var snapshot Repositories
	level, err := leveldb.Open("snapshot", &db.Options{})
	if err != nil {
		return nil, err
	}
	defer level.Close()

	key := createSnapshotKey(lang, time.Now().AddDate(0, 0, -1))
	if d, err := level.Get([]byte(key), &db.ReadOptions{}); err == nil {
		if err := json.Unmarshal(d, &snapshot); err != nil {
			return nil, err
		}
	}

	return newRankings(&reps, &snapshot), nil
}
Exemple #6
0
// open db
func (this *GoServer) OpenDB(name string) error {
	log.Println("open db ...", name)
	var err error
	this.db, err = leveldb.Open(name, nil)
	if err != nil {
		log.Println(err.Error())
		return err
	}
	return nil
}
func (self *ConfigPlugin) Init() (err error) {
	self.ConnectOptions = &db.Options{}
	conn, err := leveldb.Open(self.GetConfigOr("plugins.config.db.path", "/tmp/corona").(string), self.ConnectOptions)

	if err != nil {
		return
	}

	self.Connection = conn
	return
}
Exemple #8
0
func GetDatabase(filepath string) *DBClerk {
	ck := new(DBClerk)
	ck.filepath = filepath

	db, err := leveldb.Open(filepath, nil)
	if err != nil {
		log.Fatal("Could not open LevelDB at %s: %v\n", filepath, err)
	}
	ck.db = db
	return ck
}
func main() {
	dbConn, err = leveldb.Open("./data", opts)
	if err != nil {
		log.Fatal("Failed to open db dir ./data", err)
		return
	}

	http.HandleFunc("/leveldb/", RequestHandler)
	err = http.ListenAndServe(":8799", nil)
	if err != nil {
		log.Fatal("ListenAndServe: ", err)
	}
}
Exemple #10
0
/*
func main() {
	//fs := memfs.New()
	db, error := leveldb.Open("testdb", nil)
	if nil != error {
		fmt.Println("open testdb done")
		db.Close()
	}
}
*/
func main() {
	// open
	db_folder := "tmp.db"
	db, err := leveldb.Open(db_folder, nil)
	if err != nil {
		log.Println(err.Error())
		return
	}
	// set
	err = db.Set([]byte("lee"), []byte("lihenglin"), nil)
	if err != nil {
		log.Println(err.Error())
		return
	}
	// get
	var result []byte
	result, err = db.Get([]byte("lee"), nil)
	if err != nil {
		log.Println(err.Error())
		return
	}
	log.Println(string(result))
	db.Close()
}
Exemple #11
0
func TestLevelDB(t *testing.T) {
	path := "/tmp/leveldb_test_10101"
	os.RemoveAll(path)

	options := db.Options{}
	if ldb, err := leveldb.Open(path, &options); err == nil {
		key := []byte("test-test hwl0dsfds")
		val := []byte("value")
		ldb.Set(key, val, &db.WriteOptions{})

		if r, err := ldb.Get(key, &db.ReadOptions{}); err == nil && reflect.DeepEqual(r, val) {
			// it := ldb.Find(nil, nil)
			// for it.Next() {
			// 	log.Printf("%s => %s", it.Key(), it.Value())
			// }

		} else {
			t.Fail()
		}
	} else {
		t.Fail()
	}

}