You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
962 B

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
  1. package db
  2. import (
  3. "fmt"
  4. "github.com/syndtr/goleveldb/leveldb"
  5. "path"
  6. )
  7. type LevelDB struct {
  8. db *leveldb.DB
  9. }
  10. func NewLevelDB(name string) (*LevelDB, error) {
  11. dbPath := path.Join(name)
  12. db, err := leveldb.OpenFile(dbPath, nil)
  13. if err != nil {
  14. return nil, err
  15. }
  16. database := &LevelDB{db: db}
  17. return database, nil
  18. }
  19. func (db *LevelDB) Set(key []byte, value []byte) {
  20. err := db.db.Put(key, value, nil)
  21. if err != nil {
  22. panic(err)
  23. }
  24. }
  25. func (db *LevelDB) Get(key []byte) []byte {
  26. res, err := db.db.Get(key, nil)
  27. if err != nil {
  28. panic(err)
  29. }
  30. return res
  31. }
  32. func (db *LevelDB) Delete(key []byte) {
  33. err := db.db.Delete(key, nil)
  34. if err != nil {
  35. panic(err)
  36. }
  37. }
  38. func (db *LevelDB) Db() *leveldb.DB {
  39. return db.db
  40. }
  41. func (db *LevelDB) Close() {
  42. db.db.Close()
  43. }
  44. func (db *LevelDB) Print() {
  45. iter := db.db.NewIterator(nil, nil)
  46. for iter.Next() {
  47. key := iter.Key()
  48. value := iter.Value()
  49. fmt.Printf("[%X]:\t[%X]\n", key, value)
  50. }
  51. }