aboutsummaryrefslogtreecommitdiffstats
path: root/database.go
blob: b147756b61b8c822dc16f7c1dfeaefdcd1cc9f3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main

import (
  "path"
  "os/user"
  "github.com/syndtr/goleveldb/leveldb"
  "fmt"
)

type LDBDatabase struct {
  db        *leveldb.DB
  trie      *Trie
}

func NewLDBDatabase() (*LDBDatabase, error) {
  // This will eventually have to be something like a resource folder.
  // it works on my system for now. Probably won't work on Windows
  usr, _ := user.Current()
  dbPath := path.Join(usr.HomeDir, ".ethereum", "database")

  // Open the db
  db, err := leveldb.OpenFile(dbPath, nil)
  if err != nil {
    return nil, err
  }

  database := &LDBDatabase{db: db}

  // Bootstrap database. Sets a few defaults; such as the last block
  database.Bootstrap()

  return database, nil
}

func (db *LDBDatabase) Bootstrap() error {
  //db.trie = NewTrie(db)

  return nil
}

func (db *LDBDatabase) Put(key []byte, value []byte) {
  err := db.db.Put(key, value, nil)
  if err != nil {
    fmt.Println("Error put", err)
  }
}

func (db *LDBDatabase) Get(key []byte) ([]byte, error) {
  return nil, nil
}

func (db *LDBDatabase) Close() {
  // Close the leveldb database
  db.db.Close()
}