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.

54 lines
1.0 KiB

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
11 years ago
11 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) Put(key []byte, value []byte) {
  20. err := db.db.Put(key, value, nil)
  21. if err != nil { panic(err) }
  22. }
  23. func (db *LevelDB) Get(key []byte) ([]byte) {
  24. res, err := db.db.Get(key, nil)
  25. if err != nil { panic(err) }
  26. return res
  27. }
  28. func (db *LevelDB) Delete(key []byte) {
  29. err := db.db.Delete(key, nil)
  30. if err != nil { panic(err) }
  31. }
  32. func (db *LevelDB) Db() *leveldb.DB {
  33. return db.db
  34. }
  35. func (db *LevelDB) Close() {
  36. db.db.Close()
  37. }
  38. func (db *LevelDB) Print() {
  39. iter := db.db.NewIterator(nil, nil)
  40. for iter.Next() {
  41. key := iter.Key()
  42. value := iter.Value()
  43. fmt.Printf("[%x]:\t[%x]", key, value)
  44. }
  45. }