safe pullall

main
Ziyang Hu 2 years ago
parent bacb30d5ca
commit 6806b0f217

@ -42,7 +42,10 @@ impl SessionTx {
recursive_seen: &mut Option<HashSet<EntityId>>, recursive_seen: &mut Option<HashSet<EntityId>>,
) -> Result<()> { ) -> Result<()> {
match spec { match spec {
PullSpec::PullAll => self.pull_all(eid, vld, collector), PullSpec::PullAll => {
let mut seen = HashSet::default();
self.pull_all(eid, vld, collector, &mut seen)
}
PullSpec::Attr(a_spec) => { PullSpec::Attr(a_spec) => {
if a_spec.reverse { if a_spec.reverse {
self.pull_attr_rev(eid, vld, a_spec, collector, recursive_seen) self.pull_attr_rev(eid, vld, a_spec, collector, recursive_seen)
@ -249,6 +252,7 @@ impl SessionTx {
eid: EntityId, eid: EntityId,
vld: Validity, vld: Validity,
collector: &mut Map<String, JsonValue>, collector: &mut Map<String, JsonValue>,
pull_all_seen: &mut HashSet<EntityId>,
) -> Result<()> { ) -> Result<()> {
let mut current = encode_eav_key(eid, AttrId::MIN_PERM, &Value::Null, Validity::MAX); let mut current = encode_eav_key(eid, AttrId::MIN_PERM, &Value::Null, Validity::MAX);
let upper_bound = encode_eav_key(eid, AttrId::MAX_PERM, &Value::Bottom, Validity::MIN); let upper_bound = encode_eav_key(eid, AttrId::MAX_PERM, &Value::Bottom, Validity::MIN);
@ -287,17 +291,26 @@ impl SessionTx {
decode_value_from_key(k_slice)? decode_value_from_key(k_slice)?
}; };
collector.insert("_id".to_string(), eid.0.into()); collector.insert("_id".to_string(), eid.0.into());
pull_all_seen.insert(eid);
if attr.cardinality.is_many() { if attr.cardinality.is_many() {
if attr.val_type == AttributeTyping::Component { if attr.val_type == AttributeTyping::Component {
let val_id = value.get_entity_id()?; let val_id = value.get_entity_id()?;
if pull_all_seen.contains(&val_id) {
let arr = collector
.entry(attr.keyword.to_string_no_prefix())
.or_insert_with(|| json!([]));
let arr = arr.as_array_mut().unwrap();
arr.push(value.into());
} else {
let mut subcollector = Map::default(); let mut subcollector = Map::default();
self.pull_all(val_id, vld, &mut subcollector)?; self.pull_all(val_id, vld, &mut subcollector, pull_all_seen)?;
let arr = collector let arr = collector
.entry(attr.keyword.to_string_no_prefix()) .entry(attr.keyword.to_string_no_prefix())
.or_insert_with(|| json!([])); .or_insert_with(|| json!([]));
let arr = arr.as_array_mut().unwrap(); let arr = arr.as_array_mut().unwrap();
arr.push(subcollector.into()); arr.push(subcollector.into());
}
} else { } else {
let arr = collector let arr = collector
.entry(attr.keyword.to_string_no_prefix()) .entry(attr.keyword.to_string_no_prefix())
@ -308,9 +321,13 @@ impl SessionTx {
} else { } else {
if attr.val_type == AttributeTyping::Component { if attr.val_type == AttributeTyping::Component {
let val_id = value.get_entity_id()?; let val_id = value.get_entity_id()?;
if pull_all_seen.contains(&val_id) {
collector.insert(attr.keyword.to_string_no_prefix(), value.into());
} else {
let mut subcollector = Map::default(); let mut subcollector = Map::default();
self.pull_all(val_id, vld, &mut subcollector)?; self.pull_all(val_id, vld, &mut subcollector, pull_all_seen)?;
collector.insert(attr.keyword.to_string_no_prefix(), subcollector.into()); collector.insert(attr.keyword.to_string_no_prefix(), subcollector.into());
}
} else { } else {
collector.insert(attr.keyword.to_string_no_prefix(), value.into()); collector.insert(attr.keyword.to_string_no_prefix(), value.into());
} }

Loading…
Cancel
Save