db.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package api
  17. import (
  18. "github.com/ethereum/go-ethereum/eth"
  19. "github.com/ethereum/go-ethereum/rpc/codec"
  20. "github.com/ethereum/go-ethereum/rpc/shared"
  21. "github.com/ethereum/go-ethereum/xeth"
  22. )
  23. const (
  24. DbApiversion = "1.0"
  25. )
  26. var (
  27. // mapping between methods and handlers
  28. DbMapping = map[string]dbhandler{
  29. "db_getString": (*dbApi).GetString,
  30. "db_putString": (*dbApi).PutString,
  31. "db_getHex": (*dbApi).GetHex,
  32. "db_putHex": (*dbApi).PutHex,
  33. }
  34. )
  35. // db callback handler
  36. type dbhandler func(*dbApi, *shared.Request) (interface{}, error)
  37. // db api provider
  38. type dbApi struct {
  39. xeth *xeth.XEth
  40. ethereum *eth.Ethereum
  41. methods map[string]dbhandler
  42. codec codec.ApiCoder
  43. }
  44. // create a new db api instance
  45. func NewDbApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *dbApi {
  46. return &dbApi{
  47. xeth: xeth,
  48. ethereum: ethereum,
  49. methods: DbMapping,
  50. codec: coder.New(nil),
  51. }
  52. }
  53. // collection with supported methods
  54. func (self *dbApi) Methods() []string {
  55. methods := make([]string, len(self.methods))
  56. i := 0
  57. for k := range self.methods {
  58. methods[i] = k
  59. i++
  60. }
  61. return methods
  62. }
  63. // Execute given request
  64. func (self *dbApi) Execute(req *shared.Request) (interface{}, error) {
  65. if callback, ok := self.methods[req.Method]; ok {
  66. return callback(self, req)
  67. }
  68. return nil, &shared.NotImplementedError{req.Method}
  69. }
  70. func (self *dbApi) Name() string {
  71. return shared.DbApiName
  72. }
  73. func (self *dbApi) ApiVersion() string {
  74. return DbApiversion
  75. }
  76. func (self *dbApi) GetString(req *shared.Request) (interface{}, error) {
  77. args := new(DbArgs)
  78. if err := self.codec.Decode(req.Params, &args); err != nil {
  79. return nil, shared.NewDecodeParamError(err.Error())
  80. }
  81. if err := args.requirements(); err != nil {
  82. return nil, err
  83. }
  84. ret, err := self.xeth.DbGet([]byte(args.Database + args.Key))
  85. return string(ret), err
  86. }
  87. func (self *dbApi) PutString(req *shared.Request) (interface{}, error) {
  88. args := new(DbArgs)
  89. if err := self.codec.Decode(req.Params, &args); err != nil {
  90. return nil, shared.NewDecodeParamError(err.Error())
  91. }
  92. if err := args.requirements(); err != nil {
  93. return nil, err
  94. }
  95. return self.xeth.DbPut([]byte(args.Database+args.Key), args.Value), nil
  96. }
  97. func (self *dbApi) GetHex(req *shared.Request) (interface{}, error) {
  98. args := new(DbHexArgs)
  99. if err := self.codec.Decode(req.Params, &args); err != nil {
  100. return nil, shared.NewDecodeParamError(err.Error())
  101. }
  102. if err := args.requirements(); err != nil {
  103. return nil, err
  104. }
  105. if res, err := self.xeth.DbGet([]byte(args.Database + args.Key)); err == nil {
  106. return newHexData(res), nil
  107. } else {
  108. return nil, err
  109. }
  110. }
  111. func (self *dbApi) PutHex(req *shared.Request) (interface{}, error) {
  112. args := new(DbHexArgs)
  113. if err := self.codec.Decode(req.Params, &args); err != nil {
  114. return nil, shared.NewDecodeParamError(err.Error())
  115. }
  116. if err := args.requirements(); err != nil {
  117. return nil, err
  118. }
  119. return self.xeth.DbPut([]byte(args.Database+args.Key), args.Value), nil
  120. }