Commit e9b2d9bc authored by LiXiaoyu's avatar LiXiaoyu

init

parents
cmake-build-debug/
.idea/
cmake_minimum_required(VERSION 3.12)
project(contract_kv_interface)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
add_executable(contract_kv_interface main.cpp kv.h contract/myContract.cpp contract/myContract.h contract/contract.hpp kvImpl.cpp)
target_link_libraries(contract_kv_interface leveldb)
#pragma once
#include "../kv.h"
#include <string>
namespace eosio {
const uint64_t invalid_handle = -1;
/**
* @defgroup contracttype Contract Type
* @ingroup types
* @brief Defines contract type which is %base class for every EOSIO contract
*
* @{
*
*/
/**
* @brief %Base class for EOSIO contract.
* @details %Base class for EOSIO contract. %A new contract should derive from this class, so it can make use of EOSIO_ABI macro.
*/
class contract {
public:
/**
* Construct a new contract given the contract name
*
* @brief Construct a new contract object.
* @param n - The name of this contract
*/
contract( account_name n ):_self(n), kv_handle(invalid_handle) {}
/**
*
* @return
*/
bool kv_handle_valid() const
{
return kv_handle != invalid_handle;
}
/**
*
* @return
*/
bool open() {
if( kv_get_handle(&kv_handle) == -1 ) {
kv_handle = invalid_handle;
return false;
}
return true;
}
//It's contract writer's responsibility to check handle validity before set/get
/**
*
* @param key
* @param value
* @return
*/
bool set(const std::string &key, const std::string &value) const
{
if(!kv_handle_valid())
return false;
return kv_set(kv_handle, key.c_str(), key.length(), value.c_str(), value.length()) != -1;
}
/**
*
* @param key
* @param value
* @return
*/
bool get(const std::string &key, std::string &value) const
{
uint32_t len = 0;
void *data = nullptr;
if( -1 == kv_get(kv_handle, key.c_str(), key.length(), &data, &len) ) {
return false;
}
//todo move
value = std::string(reinterpret_cast<char *>(data), len);
delete data;
}
/**
*
* Get this contract name
*
* @brief Get this contract name.
* @return account_name - The name of this contract
*/
inline account_name get_self()const { return _self; }
protected:
/**
* The name of this contract
*
* @brief The name of this contract.
*/
account_name _self;
uint64_t kv_handle;
};
/// @} contracttype
} /// namespace eosio
//
// Created by lxy on 8/29/18.
//
#include "myContract.h"
//
// Created by lxy on 8/29/18.
//
#ifndef CONTRACT_KV_INTERFACE_MYCONTRACT_H
#define CONTRACT_KV_INTERFACE_MYCONTRACT_H
#include <iostream>
#include "contract.hpp"
class myContract : public eosio::contract {
public:
myContract(account_name n) : eosio::contract(n) {
open();
}
void action1() {
set("key1", "value1");
}
void action2() {
std::string v;
get("key1", v);
std::cout << v << '\n';
}
};
#endif //CONTRACT_KV_INTERFACE_MYCONTRACT_H
//
// Created by lxy on 8/29/18.
//
//Defines C API for interfacing with chain33 kv
#pragma once
#include <cstdint>
//stub
typedef uint64_t account_name;
extern "C" {
//or open?
//is it necessary
//uint64_t kv_get_handle();
//return : -1 on failure, non-negative on success
int32_t kv_get_handle(uint64_t *handle);
//wasm alloc, wasm free
//int32_t get(uint64_t handle, const void* key, uint32_t key_len, void* data, uint32_t buf_len, uint32_t *data_len);
//env alloc, wasm copy & free
//return : -1 on failure, non-negative on success
int32_t kv_get(uint64_t handle, const void* key, uint32_t key_len, void** data, uint32_t* data_len);
//return : -1 on failure, non-negative on success
int32_t kv_set(uint64_t handle, const void* key, uint32_t key_len, const void* data, uint32_t data_len);
/*
int32_t batch_get();
int32_t roll_back();
int32_t commit();
*/
}
//
// Created by lxy on 8/30/18.
//
#include "kv.h"
#include <leveldb/db.h>
#include <iostream>
leveldb::DB* db;
leveldb::Options options;
bool init() {
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "mzya.db", &db);
return status.ok();
}
int32_t kv_get_handle(uint64_t *handle) {
//or map it
*handle = reinterpret_cast<uint64_t>(db);
return 0;
}
int32_t kv_get(uint64_t handle, const void* key, uint32_t key_len, void** data, uint32_t* data_len) {
//assert key_len = key.len
leveldb::DB *db = reinterpret_cast<decltype(db)>(handle);
std::string value;
leveldb::Status s = db->Get(leveldb::ReadOptions(), reinterpret_cast<const char *>(key), &value);
if(! s.ok()) {
std::cerr << s.ToString() << '\n';
return -1;
}
*data = new char[value.length() + 1];
strcpy(reinterpret_cast<char*>(*data), value.c_str());
*data_len = value.length();
}
int32_t kv_set(uint64_t handle, const void* key, uint32_t key_len, const void* data, uint32_t data_len) {
}
#include <iostream>
#include <sstream>
#include <leveldb/db.h>
int main() {
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "mzya.db", &db);
if(false == status.ok()) {
std::cerr << status.ToString() << '\n';
return -1;
}
/*
leveldb::WriteOptions wirteOpt;
for(size_t i = 0; i < 256; ++i) {
std::ostringstream key;
key << i;
db->Put(wirteOpt, key.str(), "hello");
}*/
//leveldb::Iterator
/*
auto it = db->NewIterator(leveldb::ReadOptions());
for(it->SeekToFirst(); it->Valid(); it->Next()) {
std::cout << it->key().ToString() << " : " << it->value().ToString() << '\n';
}
if (false == it->status().ok())
std::cerr << it->status().ToString() << '\n';
delete it;
*/
std::string value;
leveldb::Status s = db->Get(leveldb::ReadOptions(), "1999", &value);
if(! s.ok()) {
std::cerr << s.ToString() << '\n';
return -1;
}
std::cout << value << '\n';
delete db;
return 0;
}
/*
* #include <iostream>
#include <sstream>
#include <string>
#include "leveldb/db.h"
using namespace std;
int main(int argc, char** argv)
{
// Set up database connection information and open database
leveldb::DB* db;
leveldb::Options options;
options.create_if_missing = true;
leveldb::Status status = leveldb::DB::Open(options, "./testdb", &db);
if (false == status.ok())
{
cerr << "Unable to open/create test database './testdb'" << endl;
cerr << status.ToString() << endl;
return -1;
}
// Add 256 values to the database
leveldb::WriteOptions writeOptions;
for (unsigned int i = 0; i < 256; ++i)
{
ostringstream keyStream;
keyStream << "Key" << i;
ostringstream valueStream;
valueStream << "Test data value: " << i;
db->Put(writeOptions, keyStream.str(), valueStream.str());
}
// Iterate over each item in the database and print them
leveldb::Iterator* it = db->NewIterator(leveldb::ReadOptions());
for (it->SeekToFirst(); it->Valid(); it->Next())
{
cout << it->key().ToString() << " : " << it->value().ToString() << endl;
}
if (false == it->status().ok())
{
cerr << "An error was found during the scan" << endl;
cerr << it->status().ToString() << endl;
}
delete it;
// Close the database
delete db;
}
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment