now not permitting duplicate keys in the database

This commit is contained in:
jmjatlanta 2016-12-05 14:34:10 -05:00
parent a7d8bc82b7
commit 8a80d2afc7
4 changed files with 46 additions and 13 deletions

View file

@ -234,6 +234,7 @@ struct Node * N_Create_From_Data(unsigned char * data, size_t data_size)
mynode->data = data; mynode->data = data;
mynode->data_size = data_size; mynode->data_size = data_size;
mynode->cached = NULL; mynode->cached = NULL;
mynode->link_ammount = 0;
return mynode; return mynode;
} }

View file

@ -1,5 +1,6 @@
#include "libp2p/crypto/encoding/base64.h" #include <stdio.h>
#include "libp2p/crypto/encoding/base64.h"
#include "ipfs/repo/config/datastore.h" #include "ipfs/repo/config/datastore.h"
#include "ipfs/repo/fsrepo/fs_repo.h" #include "ipfs/repo/fsrepo/fs_repo.h"
#include "ipfs/os/utils.h" #include "ipfs/os/utils.h"

View file

@ -31,23 +31,31 @@ int repo_fsrepo_lmdb_put(const char* key, size_t key_size, unsigned char* data,
retVal = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn); retVal = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn);
if (retVal != 0) if (retVal != 0)
return 0; return 0;
retVal = mdb_dbi_open(mdb_txn, NULL, 0, &mdb_dbi); retVal = mdb_dbi_open(mdb_txn, NULL, MDB_DUPSORT, &mdb_dbi);
if (retVal != 0) if (retVal != 0)
return 0; return 0;
// write // prepare data
db_key.mv_size = key_size; db_key.mv_size = key_size;
db_key.mv_data = (char*)key; db_key.mv_data = (char*)key;
// write
db_value.mv_size = data_size; db_value.mv_size = data_size;
db_value.mv_data = data; db_value.mv_data = data;
retVal = mdb_put(mdb_txn, mdb_dbi, &db_key, &db_value, MDB_NODUPDATA); retVal = mdb_put(mdb_txn, mdb_dbi, &db_key, &db_value, MDB_NODUPDATA | MDB_NOOVERWRITE);
if (retVal != 0) if (retVal == 0) // the normal case
return 0; retVal = 1;
else {
if (retVal == MDB_KEYEXIST) // We tried to add a key that already exists. Skip.
retVal = 1;
else
retVal = 0;
}
// cleanup // cleanup
mdb_dbi_close(mdb_env, mdb_dbi); mdb_dbi_close(mdb_env, mdb_dbi);
mdb_txn_commit(mdb_txn); mdb_txn_commit(mdb_txn);
return 1; return retVal;
} }

View file

@ -41,8 +41,10 @@ int test_merkledag_add_data() {
struct Node* node1 = N_Create_From_Data(binary_data, 255); struct Node* node1 = N_Create_From_Data(binary_data, 255);
retVal = ipfs_merkledag_add(node1, fs_repo); retVal = ipfs_merkledag_add(node1, fs_repo);
if (retVal == 0) if (retVal == 0) {
Node_Delete(node1);
return 0; return 0;
}
// make sure everything is correct // make sure everything is correct
if (node1->cached == NULL) if (node1->cached == NULL)
@ -50,28 +52,39 @@ int test_merkledag_add_data() {
int first_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb"); int first_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
if (first_add_size == start_file_size) { // uh oh, database should have increased in size if (first_add_size == start_file_size) { // uh oh, database should have increased in size
Node_Delete(node1);
return 0; return 0;
} }
// adding the same binary again should do nothing (the hash should be the same) // adding the same binary again should do nothing (the hash should be the same)
struct Node* node2 = N_Create_From_Data(binary_data, 255); struct Node* node2 = N_Create_From_Data(binary_data, 255);
retVal = ipfs_merkledag_add(node2, fs_repo); retVal = ipfs_merkledag_add(node2, fs_repo);
if (retVal == 0) if (retVal == 0) {
Node_Delete(node1);
Node_Delete(node2);
return 0; return 0;
}
// make sure everything is correct // make sure everything is correct
if (node2->cached == NULL) if (node2->cached == NULL) {
Node_Delete(node1);
Node_Delete(node2);
return 0; return 0;
}
for(int i = 0; i < node1->cached->hash_length; i++) { for(int i = 0; i < node1->cached->hash_length; i++) {
if (node1->cached->hash[i] != node2->cached->hash[i]) { if (node1->cached->hash[i] != node2->cached->hash[i]) {
printf("hash of node1 does not match node2 at position %d\n", i); printf("hash of node1 does not match node2 at position %d\n", i);
Node_Delete(node1);
Node_Delete(node2);
return 0; return 0;
} }
} }
int second_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb"); int second_add_size = os_utils_file_size("/tmp/.ipfs/datastore/data.mdb");
if (first_add_size != second_add_size) { // uh oh, the database shouldn't have changed size if (first_add_size != second_add_size) { // uh oh, the database shouldn't have changed size
fprintf("looks as if a new record was added when it shouldn't have. Old file size: %d, new file size: %d\n", first_add_size, second_add_size); printf("looks as if a new record was added when it shouldn't have. Old file size: %d, new file size: %d\n", first_add_size, second_add_size);
Node_Delete(node1);
Node_Delete(node2);
return 0; return 0;
} }
@ -81,12 +94,20 @@ int test_merkledag_add_data() {
struct Node* node3 = N_Create_From_Data(binary_data, 255); struct Node* node3 = N_Create_From_Data(binary_data, 255);
retVal = ipfs_merkledag_add(node3, fs_repo); retVal = ipfs_merkledag_add(node3, fs_repo);
if (retVal == 0) if (retVal == 0) {
Node_Delete(node1);
Node_Delete(node2);
Node_Delete(node3);
return 0; return 0;
}
// make sure everything is correct // make sure everything is correct
if (node3->cached == NULL) if (node3->cached == NULL) {
Node_Delete(node1);
Node_Delete(node2);
Node_Delete(node3);
return 0; return 0;
}
Node_Delete(node1); Node_Delete(node1);
Node_Delete(node2); Node_Delete(node2);
@ -97,5 +118,7 @@ int test_merkledag_add_data() {
return 0; return 0;
} }
ipfs_repo_fsrepo_free(fs_repo);
return 1; return 1;
} }