-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapping_w_index_and_delete.sol
110 lines (79 loc) · 3.83 KB
/
mapping_w_index_and_delete.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// This is improve simple_mapping storage patern with using array of mapping KEYS
// in order to solve problem with impossibility to loop and enumerates throw the
// KEY-VALUE storage to get all of entitities
// and also fixing problem with DELETING data from an array
pragma solidity 0.8.0;
contract mappedWithUnorderedIndexAndDelete {
// We have a struct that contain some data
struct EntityStruct {
uint entityData;
// each struct will have an index in the array
uint listPointer; //0
}
// We have mapping
mapping(address => EntityStruct) public entityStructs;
// And we have array that save all of the KEYS of addresses to the mapping
address[] public entityList;
function isEntity(address entityAddress) public view returns(bool isIndeed) {
if(entityList.length == 0) return false;
return (entityList[entityStructs[entityAddress].listPointer] == entityAddress);
}
function getEntityCount() public view returns(uint entityCount) {
return entityList.length;
}
function newEntity(address entityAddress, uint entityData) public returns(bool success) {
// check if we this address already have an entity in this contract, if yes -> thrown an error and revert
// because we want each address have one row of data in order to avoid duplicates of data
if(isEntity(entityAddress)) revert();
// set the data
entityStructs[entityAddress].entityData = entityData;
// push address into array
entityList.push(entityAddress);
// and set the pointer
entityStructs[entityAddress].listPointer = entityList.length - 1;
return true;
}
// We need function to update data
function updateEntity(address entityAddress, uint entityData) public returns(bool success) {
// Check is entity exist
if(!isEntity(entityAddress)) revert();
//Replace data with the new data
entityStructs[entityAddress].entityData = entityData;
return true;
}
// BEFORE DELETING ADDRESS 2
// [ADRESS1, ADDRESS2, ADDRESS3, ADDRESS4].pop()
// AFTER DELETING ADDRESS 2
// [ADRESS1, ADDRESS4, ADDRESS3]
function deleteEntity(address entityAddress) public returns(bool success) {
// Check is entity exist before delete it
if(!isEntity(entityAddress)) revert();
// Find what to delete
uint rowToDelete = entityStructs[entityAddress].listPointer; // = 1
// Find address at the end of the list
address keyToMove = entityList[entityList.length-1]; //save address4
// Delete data and put data from the last address
entityList[rowToDelete] = keyToMove;
// Set the new list pointer
entityStructs[keyToMove].listPointer = rowToDelete; // from 4 to 2
// Remove the last element from array
entityList.pop();
// We can also set initial value in struct for this address
delete entityStructs[entityAddress];
return true;
}
}
/*
Benefits of this type of pattern :
- We don't have dublication, now we can directly get the entity of the address. No needs of looping to get data
- Now we can count the number of inserted values into the mapping(number of users or entities etc.)
- Now we can enumarate this data(loop)
- This array is solving problem with "0 value struct", because we have the list of addresses in array
- Now we have solution to delete data from an array
Drawbacks of this type of pattern :
- There are always data in the mapping. Regardless of the key we use if we haven't set the mapping
it will be set to the initial value, which is 0 for uint, empty string for string, false for bool and so on.
- So we need to add extra data and it's just sort messy to work with
- Changing bool isEntity to uint listPointer, because bool is just 1 bit and tekr less space, uint take more space
- More complex code
*/