Skip to content

Commit 0d03e91

Browse files
authored
bugfix: fixed next page id calculation (#195)
# Goal The goal of this PR is fix the bug which exports only the last page instead of all of them when more than 1 page is created via adding connections. Closes #194 # Checklist - [X] updated versions - [X] Tests added
1 parent a04476c commit 0d03e91

File tree

12 files changed

+105
-37
lines changed

12 files changed

+105
-37
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridge/common/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "dsnp-graph-sdk-common"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
publish = false
77

88
[dependencies]
9-
dsnp-graph-core = { version = "1.1.0", path = "../../core" }
10-
dsnp-graph-config = { version = "1.1.0", path = "../../config" }
9+
dsnp-graph-core = { version = "1.1.1", path = "../../core" }
10+
dsnp-graph-config = { version = "1.1.1", path = "../../config" }
1111
libc = "0.2.153"
1212
protobuf = { version = "3.4.0", features = ["with-bytes"] }

bridge/ffi/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dsnp-graph-sdk-ffi"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
publish = false
@@ -10,8 +10,8 @@ name = "dsnp_graph_sdk_ffi"
1010
crate-type = ["staticlib"]
1111

1212
[dependencies]
13-
dsnp-graph-core = { version = "1.1.0", path = "../../core" }
14-
dsnp-graph-config = { version = "1.1.0", path = "../../config" }
13+
dsnp-graph-core = { version = "1.1.1", path = "../../core" }
14+
dsnp-graph-config = { version = "1.1.1", path = "../../config" }
1515
libc = "0.2.153"
1616
lazy_static = "1.4.0"
1717
anyhow = "1.0.80"

bridge/jni/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dsnp-graph-sdk-jni"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
publish = false
@@ -10,9 +10,9 @@ name = "dsnp_graph_sdk_jni"
1010
crate-type = ["cdylib"]
1111

1212
[dependencies]
13-
dsnp-graph-core = { version = "1.1.0", path = "../../core" }
14-
dsnp-graph-config = { version = "1.1.0", path = "../../config" }
15-
dsnp-graph-sdk-common = { version = "1.1.0", path = "../common" }
13+
dsnp-graph-core = { version = "1.1.1", path = "../../core" }
14+
dsnp-graph-config = { version = "1.1.1", path = "../../config" }
15+
dsnp-graph-sdk-common = { version = "1.1.1", path = "../common" }
1616
jni = "0.21.1"
1717
cfg-if = "1.0.0"
1818
log = { version = "^0.4.21", features = ["std", "max_level_debug", "release_max_level_debug"] }

bridge/node/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "dsnp-graph-sdk-node"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
edition = "2021"
55
license = "Apache-2.0"
66
publish = false
@@ -11,8 +11,8 @@ name = "dsnp_graph_sdk_node"
1111
crate-type = ["cdylib"]
1212

1313
[dependencies]
14-
dsnp-graph-core = { version = "1.1.0", path = "../../core" }
15-
dsnp-graph-config = { version = "1.1.0", path = "../../config" }
14+
dsnp-graph-core = { version = "1.1.1", path = "../../core" }
15+
dsnp-graph-config = { version = "1.1.1", path = "../../config" }
1616
neon = { version = "1.0.0", default-features = false, features = ["napi-6"] }
1717
serde = { version = "1.0.197", features = ["derive"] }
1818
serde_json = "1.0.114"

bridge/node/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bridge/node/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dsnp/graph-sdk",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"author": "Amplica Labs",
55
"license": "Apache-2.0",
66
"description": "dsnp-graph-sdk-node",
@@ -55,6 +55,6 @@
5555
},
5656
"homepage": "https://github.com/LibertyDSNP/graph-sdk/bridge/node/README.md",
5757
"customProperties": {
58-
"uploadedBinariesVersion": "v1.1.0"
58+
"uploadedBinariesVersion": "v1.1.1"
5959
}
6060
}

config/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ homepage = "https://spec.dsnp.org"
55
license = "Apache-2.0"
66
publish = false
77
repository = "https://github.com/libertyDSNP/graph-sdk/"
8-
version = "1.1.0"
8+
version = "1.1.1"
99

1010
[lib]
1111
name = "dsnp_graph_config"

core/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ homepage = "https://spec.dsnp.org"
55
license = "Apache-2.0"
66
publish = false
77
repository = "https://github.com/libertyDSNP/graph-sdk/"
8-
version = "1.1.0"
8+
version = "1.1.1"
99

1010
[lib]
1111
name = "dsnp_graph_core"
@@ -16,7 +16,7 @@ doctest = false
1616
anyhow = "1.0.69"
1717
apache-avro = { version = "0.16.0", features = ["snappy"] }
1818
dryoc = "0.5.3"
19-
dsnp-graph-config = { version = "1.1.0", path = "../config" }
19+
dsnp-graph-config = { version = "1.1.1", path = "../config" }
2020
lazy_static = "1.4.0"
2121
log = { version = "^0.4.21", features = ["std", "max_level_debug", "release_max_level_debug"] }
2222
log-result-proc-macro = { path = "../log-result-proc-macro" }

core/src/api/api.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ mod test {
733733
}
734734

735735
#[test]
736-
#[timeout(3000)]
736+
#[timeout(100000)]
737737
fn add_large_number_of_follows_to_private_follow_graph_should_succeed() {
738738
// arrange
739739
let env = Environment::Mainnet;
@@ -751,9 +751,9 @@ mod test {
751751
key_type: GraphKeyType::X25519,
752752
};
753753
let dsnp_user_id = 7002;
754-
let input = ImportBundleBuilder::new(env, dsnp_user_id, schema_id)
755-
.with_key_pairs(&vec![keypair])
756-
.with_encryption_key(resolved_key)
754+
let input = ImportBundleBuilder::new(env.clone(), dsnp_user_id, schema_id)
755+
.with_key_pairs(&vec![keypair.clone()])
756+
.with_encryption_key(resolved_key.clone())
757757
.build();
758758

759759
// act
@@ -792,6 +792,34 @@ mod test {
792792

793793
// assert
794794
assert!(res.is_ok());
795+
796+
let connections =
797+
state.get_connections_for_user_graph(&dsnp_user_id, &schema_id, true).unwrap();
798+
let before_export_set: HashSet<_> = connections.iter().map(|e| e.user_id).collect();
799+
800+
let export = state.export_updates();
801+
802+
assert!(export.is_ok());
803+
println!("after export physical mem: {}", mem_usage.physical_mem);
804+
805+
let updates = export.unwrap();
806+
807+
let mut updated_state = GraphState::new(env.clone());
808+
let updated_input = ImportBundleBuilder::new(env.clone(), dsnp_user_id, schema_id)
809+
.with_key_pairs(&vec![keypair])
810+
.with_encryption_key(resolved_key.clone())
811+
.build();
812+
813+
let new_import = ImportBundleBuilder::build_from(&updated_input, &updates);
814+
let res = updated_state.import_users_data(&vec![new_import]);
815+
816+
assert!(res.is_ok());
817+
818+
let connections = updated_state
819+
.get_connections_for_user_graph(&dsnp_user_id, &schema_id, false)
820+
.unwrap();
821+
let after_reimport_set: HashSet<_> = connections.iter().map(|e| e.user_id).collect();
822+
assert_eq!(before_export_set, after_reimport_set);
795823
}
796824

797825
#[test]

core/src/graph/graph.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,17 @@ impl Graph {
114114
}
115115

116116
/// Get next available PageId for this graph
117-
pub fn get_next_available_page_id(&self) -> Option<PageId> {
118-
let existing_pages = self.pages.inner().keys().cloned().collect::<HashSet<PageId>>();
117+
pub fn get_next_available_page_id(
118+
&self,
119+
updated_pages: &BTreeMap<PageId, GraphPage>,
120+
) -> Option<PageId> {
121+
let existing_pages = self
122+
.pages
123+
.inner()
124+
.keys()
125+
.cloned()
126+
.chain(updated_pages.keys().cloned())
127+
.collect::<HashSet<PageId>>();
119128
(0..=(self.environment.get_config().max_page_id as PageId))
120129
.find(|&pid| !existing_pages.contains(&pid))
121130
}
@@ -338,7 +347,7 @@ impl Graph {
338347
// At this point, all existing pages are aggressively full. Add new pages
339348
// as needed to accommodate any remaining connections to be added, filling aggressively.
340349
while let Some(_) = add_iter.peek() {
341-
let mut new_page = match self.get_next_available_page_id() {
350+
let mut new_page = match self.get_next_available_page_id(&updated_pages) {
342351
Some(next_page_id) =>
343352
Ok(GraphPage::new(self.get_connection_type().privacy_type(), next_page_id)),
344353
None => Err(DsnpGraphError::GraphIsFull),
@@ -862,7 +871,7 @@ mod test {
862871
))),
863872
};
864873

865-
assert_eq!(graph.get_next_available_page_id(), None);
874+
assert_eq!(graph.get_next_available_page_id(&BTreeMap::default()), None);
866875
}
867876

868877
#[test]
@@ -890,7 +899,35 @@ mod test {
890899
))),
891900
};
892901

893-
assert_eq!(graph.get_next_available_page_id(), Some(8));
902+
assert_eq!(graph.get_next_available_page_id(&BTreeMap::default()), Some(8));
903+
}
904+
905+
#[test]
906+
fn get_next_available_page_should_include_updated_pages() {
907+
let environment = Environment::Mainnet;
908+
let user_id = 3;
909+
const CONN_TYPE: ConnectionType = ConnectionType::Follow(PrivacyType::Public);
910+
const PRIV_TYPE: PrivacyType = CONN_TYPE.privacy_type();
911+
let schema_id = environment
912+
.get_config()
913+
.get_schema_id_from_connection_type(CONN_TYPE)
914+
.expect("should exist");
915+
let mut updated_pages: BTreeMap<_, _> = (0..environment.get_config().max_page_id as PageId)
916+
.map(|page_id: PageId| (page_id, GraphPage::new(PRIV_TYPE, page_id)))
917+
.collect();
918+
updated_pages.remove(&8);
919+
let graph = Graph {
920+
environment,
921+
schema_id, // doesn't matter which type
922+
user_id,
923+
pages: PageMap::new(),
924+
user_key_manager: Arc::new(RwLock::new(UserKeyManager::new(
925+
user_id,
926+
Arc::new(RwLock::new(SharedStateManager::new())),
927+
))),
928+
};
929+
930+
assert_eq!(graph.get_next_available_page_id(&updated_pages), Some(8));
894931
}
895932

896933
#[test]

core/src/tests/helpers.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use base64::{engine::general_purpose, Engine as _};
2020
use ctor::ctor;
2121
use dryoc::keypair::StackKeyPair;
2222
use dsnp_graph_config::{DsnpVersion, Environment, GraphKeyType};
23-
use std::sync::{Arc, RwLock};
23+
use std::{
24+
collections::BTreeMap,
25+
sync::{Arc, RwLock},
26+
};
2427

2528
#[ctor]
2629
fn test_harness_init() {
@@ -134,7 +137,7 @@ pub fn create_aggressively_full_page(
134137
shared_state: &Arc<RwLock<SharedStateManager>>,
135138
) -> PageId {
136139
let connection_type = graph.get_connection_type();
137-
let page_id = graph.get_next_available_page_id().unwrap();
140+
let page_id = graph.get_next_available_page_id(&BTreeMap::default()).unwrap();
138141
let mut page = GraphPage::new(connection_type.privacy_type(), page_id);
139142
let mut connection_id = start_conn_id;
140143
let encryption_key = graph

0 commit comments

Comments
 (0)