Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #143 from EOSIO/remaining-db-functions-120
Browse files Browse the repository at this point in the history
Implement and test remaining db functions
  • Loading branch information
bytemaster committed Aug 8, 2017
2 parents 4ae8042 + bdd91ca commit 867a538
Show file tree
Hide file tree
Showing 11 changed files with 1,231 additions and 391 deletions.
39 changes: 25 additions & 14 deletions contracts/eoslib/db.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ extern "C" {
*/
int32_t store_i64( AccountName scope, TableName table, const void* data, uint32_t datalen );

/**
* @return 1 if the record was updated, 0 if no record with key was found
*/
int32_t update_i64( AccountName scope, TableName table, const void* data, uint32_t datalen );

/**
* @param scope - the account scope that will be read, must exist in the transaction scopes list
* @param code - identifies the code that controls write-access to the data
Expand All @@ -94,11 +99,17 @@ int32_t store_i64( AccountName scope, TableName table, const void* data, uint32_
int32_t load_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );
int32_t front_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );
int32_t back_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );
int32_t next_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );
int32_t previous_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );
int32_t lower_bound_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );
int32_t upper_bound_i64( AccountName scope, AccountName code, TableName table, void* data, uint32_t datalen );

/**
* @param data - must point to at lest 8 bytes containing primary key
*
* @return 1 if a record was removed, and 0 if no record with key was found
*/
int32_t remove_i64( AccountName scope, TableName table, uint64_t key );
int32_t remove_i64( AccountName scope, TableName table, void* data );

///@} db_i64

Expand Down Expand Up @@ -145,34 +156,34 @@ int32_t previous_primary_i128i128( AccountName scope, AccountName code, TableNam

int32_t load_primary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );

int32_t upper_bound_primary_i128i128( AccountName scope, AccountName code, TableName table,
const void* key, void* data, uint32_t len );
int32_t lower_bound_primary_i128i128( AccountName scope, AccountName code, TableName table,
const void* key, void* data, uint32_t len );
int32_t upper_bound_primary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );
int32_t lower_bound_primary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );

int32_t front_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );
int32_t back_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );
int32_t next_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );
int32_t previous_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );


int32_t upper_bound_secondary_i128i128( AccountName scope, AccountName code, TableName table,
const void* key, void* data, uint32_t len );
int32_t lower_bound_secondary_i128i128( AccountName scope, AccountName code, TableName table,
const void* key, void* data, uint32_t len );
int32_t upper_bound_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );
int32_t lower_bound_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );

int32_t load_secondary_i128i128( AccountName scope, AccountName code, TableName table, void* data, uint32_t len );

/**
* @param data - must point to at lest 32 bytes containing {primary,secondary}
*
* @return true if the record was removed, false if no record was found
* @return 1 if a record was removed, and 0 if no record with key was found
*/
bool remove_i128i128( AccountName scope, TableName table, const void* data );
int32_t remove_i128i128( AccountName scope, TableName table, const void* data );
/**
* @return 1 if a new record was created, 0 if an existing record was updated
*/
int32_t store_i128i128( AccountName scope, TableName table, const void* data, uint32_t len );

/**
* Creates or updates a record and returns true if successful
* @return 1 if the record was updated, 0 if no record with key was found
*/
bool store_i128i128( AccountName scope, TableName table, const void* data, uint32_t len );
int32_t update_i128i128( AccountName scope, TableName table, const void* data, uint32_t len );

///@} dbi128i128
}
87 changes: 71 additions & 16 deletions contracts/eoslib/db.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,50 @@ struct table_impl<sizeof(uint128_t),sizeof(uint128_t)> {
static int32_t load_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return load_primary_i128i128( scope, code, table, data, len );
}
static int32_t front_secondary( AccountName scope, AccountName code, TableName table, void* data, uint32_t len ) {
return front_secondary_i128i128( scope, code, table, data, len );
static int32_t next_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return next_primary_i128i128( scope, code, table, data, len );
}
static int32_t back_secondary( AccountName scope, AccountName code, TableName table, void* data, uint32_t len ) {
return back_secondary_i128i128( scope, code, table, data, len );
static int32_t previous_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return previous_primary_i128i128( scope, code, table, data, len );
}
static int32_t upper_bound_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return upper_bound_primary_i128i128( scope, code, table, data, len );
}
static int32_t lower_bound_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return lower_bound_primary_i128i128( scope, code, table, data, len );
}

static int32_t front_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return front_secondary_i128i128( scope, code, table, data, len );
}
static int32_t back_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return back_secondary_i128i128( scope, code, table, data, len );
}
static int32_t load_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return load_secondary_i128i128( scope, code, table, data, len );
}
static int32_t next_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return next_secondary_i128i128( scope, code, table, data, len );
}
static int32_t previous_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return previous_secondary_i128i128( scope, code, table, data, len );
}
static int32_t upper_bound_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return upper_bound_secondary_i128i128( scope, code, table, data, len );
}
static int32_t lower_bound_secondary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return lower_bound_secondary_i128i128( scope, code, table, data, len );
}

static bool remove( uint64_t scope, uint64_t table, const void* data ) {
static int32_t remove( uint64_t scope, uint64_t table, const void* data ) {
return remove_i128i128( scope, table, data );
}
static bool store( AccountName scope, TableName table, const void* data, uint32_t len ) {
static int32_t store( AccountName scope, TableName table, const void* data, uint32_t len ) {
return store_i128i128( scope, table, data, len );
}
static int32_t update( AccountName scope, TableName table, const void* data, uint32_t len ) {
return update_i128i128( scope, table, data, len );
}
};

template<>
Expand All @@ -79,15 +110,31 @@ struct table_impl<sizeof(uint64_t),0> {
static int32_t back_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return back_i64( scope, code, table, data, len );
}
static bool remove( uint64_t scope, uint64_t table, const void* data ) {
return remove_i64( scope, table, *((uint64_t*)data) );
}
static int32_t load_primary( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return load_i64( scope, code, table, data, len );
}
static bool store( AccountName scope, TableName table, const void* data, uint32_t len ) {
static int32_t next( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return next_i64( scope, code, table, data, len );
}
static int32_t previous( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return previous_i64( scope, code, table, data, len );
}
static int32_t lower_bound( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return lower_bound_i64( scope, code, table, data, len );
}
static int32_t upper_bound( uint64_t scope, uint64_t code, uint64_t table, void* data, uint32_t len ) {
return upper_bound_i64( scope, code, table, data, len );
}

static int32_t remove( uint64_t scope, uint64_t table, const void* data ) {
return remove_i64( scope, table, (uint64_t*)data);
}
static int32_t store( AccountName scope, TableName table, const void* data, uint32_t len ) {
return store_i64( scope, table, data, len );
}
static int32_t update( AccountName scope, TableName table, const void* data, uint32_t len ) {
return update_i64( scope, table, data, len );
}
};


Expand Down Expand Up @@ -142,7 +189,7 @@ struct Table {
return impl::upper_bound_primary( scope, code, table, &p &r, sizeof(Record) ) == sizeof(Record);
}
static bool remove( const Record& r, uint64_t s = scope ) {
return impl::remove( s, table, &r );
return impl::remove( s, table, &r ) != 0;
}
};

Expand All @@ -169,7 +216,7 @@ struct Table {
return impl::upper_bound_secondary( s, code, table, &p &r, sizeof(Record) ) == sizeof(Record);
}
static bool remove( const Record& r, uint64_t s = scope ) {
return impl::remove( s, table, &r );
return impl::remove( s, table, &r ) != 0;
}
};

Expand All @@ -182,8 +229,12 @@ struct Table {
assert( impl::store( s, table, &r, sizeof(r) ), "error storing record" );
return true;
}
static bool update( const Record& r, uint64_t s = scope ) {
assert( impl::update( s, table, &r, sizeof(r) ), "error updating record" );
return true;
}
static bool remove( const Record& r, uint64_t s = scope ) {
return impl::remove( s, table, &r );
return impl::remove( s, table, &r ) != 0;
}
};

Expand Down Expand Up @@ -232,7 +283,7 @@ struct Table<scope,code,table,Record,PrimaryType,void> {
return impl::upper_bound_primary( scope, code, table, &p &r, sizeof(Record) ) == sizeof(Record);
}
static bool remove( const Record& r ) {
return impl::remove( scope, table, &r );
return impl::remove( scope, table, &r ) != 0;
}
};

Expand All @@ -249,11 +300,15 @@ struct Table<scope,code,table,Record,PrimaryType,void> {
}

static bool store( const Record& r, uint64_t s = scope ) {
return impl::store( s, table, &r, sizeof(r) );
return impl::store( s, table, &r, sizeof(r) ) != 0;
}

static bool update( const Record& r, uint64_t s = scope ) {
return impl::update( s, table, &r, sizeof(r) ) != 0;
}

static bool remove( const Record& r, uint64_t s = scope ) {
return impl::remove( s, table, &r );
return impl::remove( s, table, &r ) != 0;
}

};
Expand Down
2 changes: 2 additions & 0 deletions contracts/test_api/test_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ extern "C" {
WASM_TEST_HANDLER(test_db, key_i64_store_scope);
WASM_TEST_HANDLER(test_db, key_i64_remove_scope);
WASM_TEST_HANDLER(test_db, key_i64_not_found);
WASM_TEST_HANDLER(test_db, key_i64_front_back);

WASM_TEST_HANDLER(test_db, key_i128i128_general);

//test crypto
Expand Down
1 change: 1 addition & 0 deletions contracts/test_api/test_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct test_db {
static unsigned int key_i64_store_scope();
static unsigned int key_i64_remove_scope();
static unsigned int key_i64_not_found();
static unsigned int key_i64_front_back();

static unsigned int key_i128i128_general();
};
Expand Down
Loading

0 comments on commit 867a538

Please sign in to comment.