github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/db.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library 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  // The go-ethereum library 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 the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/eth"
    21  	"github.com/ethereum/go-ethereum/rpc/codec"
    22  	"github.com/ethereum/go-ethereum/rpc/shared"
    23  	"github.com/ethereum/go-ethereum/xeth"
    24  )
    25  
    26  const (
    27  	DbApiversion = "1.0"
    28  )
    29  
    30  var (
    31  	// mapping between methods and handlers
    32  	DbMapping = map[string]dbhandler{
    33  		"db_getString": (*dbApi).GetString,
    34  		"db_putString": (*dbApi).PutString,
    35  		"db_getHex":    (*dbApi).GetHex,
    36  		"db_putHex":    (*dbApi).PutHex,
    37  	}
    38  )
    39  
    40  // db callback handler
    41  type dbhandler func(*dbApi, *shared.Request) (interface{}, error)
    42  
    43  // db api provider
    44  type dbApi struct {
    45  	xeth     *xeth.XEth
    46  	ethereum *eth.Ethereum
    47  	methods  map[string]dbhandler
    48  	codec    codec.ApiCoder
    49  }
    50  
    51  // create a new db api instance
    52  func NewDbApi(xeth *xeth.XEth, ethereum *eth.Ethereum, coder codec.Codec) *dbApi {
    53  	return &dbApi{
    54  		xeth:     xeth,
    55  		ethereum: ethereum,
    56  		methods:  DbMapping,
    57  		codec:    coder.New(nil),
    58  	}
    59  }
    60  
    61  // collection with supported methods
    62  func (self *dbApi) Methods() []string {
    63  	methods := make([]string, len(self.methods))
    64  	i := 0
    65  	for k := range self.methods {
    66  		methods[i] = k
    67  		i++
    68  	}
    69  	return methods
    70  }
    71  
    72  // Execute given request
    73  func (self *dbApi) Execute(req *shared.Request) (interface{}, error) {
    74  	if callback, ok := self.methods[req.Method]; ok {
    75  		return callback(self, req)
    76  	}
    77  
    78  	return nil, &shared.NotImplementedError{req.Method}
    79  }
    80  
    81  func (self *dbApi) Name() string {
    82  	return shared.DbApiName
    83  }
    84  
    85  func (self *dbApi) ApiVersion() string {
    86  	return DbApiversion
    87  }
    88  
    89  func (self *dbApi) GetString(req *shared.Request) (interface{}, error) {
    90  	args := new(DbArgs)
    91  	if err := self.codec.Decode(req.Params, &args); err != nil {
    92  		return nil, shared.NewDecodeParamError(err.Error())
    93  	}
    94  
    95  	if err := args.requirements(); err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	ret, err := self.xeth.DbGet([]byte(args.Database + args.Key))
   100  	return string(ret), err
   101  }
   102  
   103  func (self *dbApi) PutString(req *shared.Request) (interface{}, error) {
   104  	args := new(DbArgs)
   105  	if err := self.codec.Decode(req.Params, &args); err != nil {
   106  		return nil, shared.NewDecodeParamError(err.Error())
   107  	}
   108  
   109  	if err := args.requirements(); err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	return self.xeth.DbPut([]byte(args.Database+args.Key), args.Value), nil
   114  }
   115  
   116  func (self *dbApi) GetHex(req *shared.Request) (interface{}, error) {
   117  	args := new(DbHexArgs)
   118  	if err := self.codec.Decode(req.Params, &args); err != nil {
   119  		return nil, shared.NewDecodeParamError(err.Error())
   120  	}
   121  
   122  	if err := args.requirements(); err != nil {
   123  		return nil, err
   124  	}
   125  
   126  	if res, err := self.xeth.DbGet([]byte(args.Database + args.Key)); err == nil {
   127  		return newHexData(res), nil
   128  	} else {
   129  		return nil, err
   130  	}
   131  }
   132  
   133  func (self *dbApi) PutHex(req *shared.Request) (interface{}, error) {
   134  	args := new(DbHexArgs)
   135  	if err := self.codec.Decode(req.Params, &args); err != nil {
   136  		return nil, shared.NewDecodeParamError(err.Error())
   137  	}
   138  
   139  	if err := args.requirements(); err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	return self.xeth.DbPut([]byte(args.Database+args.Key), args.Value), nil
   144  }