-
Notifications
You must be signed in to change notification settings - Fork 19
/
node.rs
191 lines (172 loc) · 5.24 KB
/
node.rs
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::*;
use near_sdk::{require, BlockHeight};
pub const EMPTY_KEY: &str = "";
pub const ERR_PERMISSION_DENIED: &str = "Permission Denied";
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct ValueAtHeight {
pub value: String,
pub block_height: BlockHeight,
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub enum NodeValue {
Value(ValueAtHeight),
Node(NodeId),
DeletedEntry(BlockHeight),
}
impl NodeValue {
pub fn into_current_height(mut self) -> Self {
match &mut self {
NodeValue::Value(v) => {
v.block_height = env::block_height();
}
NodeValue::Node(_) => {}
NodeValue::DeletedEntry(v) => {
*v = env::block_height();
}
};
self
}
pub fn get_block_height(&self) -> Option<BlockHeight> {
match self {
NodeValue::Value(v) => Some(v.block_height),
NodeValue::Node(_) => None,
NodeValue::DeletedEntry(v) => Some(*v),
}
}
}
#[derive(BorshSerialize, BorshDeserialize)]
pub struct Node {
#[borsh_skip]
pub node_id: NodeId,
pub block_height: BlockHeight,
pub children: UnorderedMap<String, NodeValue>,
}
#[derive(Serialize, Deserialize)]
#[serde(crate = "near_sdk::serde")]
pub struct PartialNode {
pub node_id: NodeId,
pub block_height: BlockHeight,
pub children: Vec<(String, NodeValue)>,
pub from_index: u32,
pub num_children: u32,
}
#[derive(BorshSerialize, BorshDeserialize)]
pub enum VNode {
Current(Node),
}
impl From<VNode> for Node {
fn from(v: VNode) -> Self {
match v {
VNode::Current(c) => c,
}
}
}
impl From<Node> for VNode {
fn from(c: Node) -> Self {
VNode::Current(c)
}
}
impl Node {
pub fn new(node_id: NodeId, value: Option<NodeValue>) -> Self {
let mut children = UnorderedMap::new(StorageKey::Node { node_id });
if let Some(value) = value {
require!(
!matches!(value, NodeValue::Node(_)),
"Invariant: empty key value can't be a node"
);
children.insert(&EMPTY_KEY.to_string(), &value);
}
Self {
node_id,
block_height: env::block_height(),
children,
}
}
pub fn set(&mut self, key: &String, value: &near_sdk::serde_json::Value) {
let value = if let Some(s) = value.as_str() {
NodeValue::Value(ValueAtHeight {
value: s.to_string(),
block_height: env::block_height(),
})
} else if value.is_null() {
NodeValue::DeletedEntry(env::block_height())
} else {
unreachable!("Invariant: value must be a string or null");
};
let prev_value = self.children.insert(&key, &value);
require!(
!matches!(prev_value, Some(NodeValue::Node(_))),
"Internal error, the replaced value was a node"
);
}
}
impl Contract {
pub fn internal_get_node(&self, node_id: NodeId) -> Option<Node> {
self.nodes.get(&node_id).map(|o| {
let mut node: Node = o.into();
node.node_id = node_id;
node
})
}
pub fn internal_unwrap_node(&self, node_id: NodeId) -> Node {
self.internal_get_node(node_id).expect("Node is missing")
}
pub fn internal_set_node(&mut self, mut node: Node) {
let node_id = node.node_id;
node.block_height = env::block_height();
self.nodes.insert(&node_id, &node.into());
}
}
#[near_bindgen]
impl Contract {
pub fn get_node_count(&self) -> u32 {
self.node_count
}
pub fn get_nodes(
&self,
from_index: Option<u32>,
limit: Option<u32>,
) -> Vec<Option<PartialNode>> {
let from_index = from_index.unwrap_or(0);
let limit = limit.unwrap_or(self.node_count);
(from_index..std::cmp::min(self.node_count, from_index + limit))
.map(|node_id| self.get_node(node_id, None, None))
.collect()
}
pub fn get_node(
&self,
node_id: NodeId,
from_index: Option<u32>,
limit: Option<u32>,
) -> Option<PartialNode> {
Some(if node_id == 0 {
partial_node_view(&self.root_node, from_index, limit)
} else {
partial_node_view(&self.internal_get_node(node_id)?, from_index, limit)
})
}
}
fn partial_node_view(node: &Node, from_index: Option<u32>, limit: Option<u32>) -> PartialNode {
let num_children = node.children.len() as _;
let from_index = from_index.unwrap_or(0);
let limit = limit.unwrap_or(num_children);
let keys = node.children.keys_as_vector();
let values = node.children.values_as_vector();
let children = (from_index..std::cmp::min(num_children, from_index + limit))
.map(|index| {
(
keys.get(index as _).unwrap(),
values.get(index as _).unwrap(),
)
})
.collect();
PartialNode {
node_id: node.node_id,
block_height: node.block_height,
children,
from_index,
num_children,
}
}