Skip to content

Commit

Permalink
Merge pull request #130 from Waidhoferj/array-multiple-integration-error
Browse files Browse the repository at this point in the history
Preliminary Array Insertion
  • Loading branch information
davidbrochart authored Mar 15, 2023
2 parents 422ad20 + 8478bc3 commit fe07f87
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/type_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl<'a> Prelim for CompatiblePyType<'a> {
let mut y_array = v.borrow_mut();
if let SharedType::Prelim(items) = y_array.0.to_owned() {
let len = array.len();
YArray::insert_multiple_at(&array, txn, len, items);
YArray::insert_multiple_at(&array, txn, len, items).unwrap();
}
y_array.0 = SharedType::Integrated(array.clone());
}
Expand Down
51 changes: 29 additions & 22 deletions src/y_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl YArray {
let items = Self::py_iter(items)?;
match &mut self.0 {
SharedType::Integrated(array) if array.len() >= index => {
Self::insert_multiple_at(array, txn, index, items);
Self::insert_multiple_at(array, txn, index, items)?;
Ok(())
}
SharedType::Prelim(vec) if vec.len() >= index as usize => {
Expand Down Expand Up @@ -437,35 +437,42 @@ impl YArray {
}
}

pub fn insert_multiple_at(dst: &Array, txn: &mut Transaction, index: u32, src: Vec<PyObject>) {
let mut j = index;
let mut i = 0;
pub fn insert_multiple_at(
dst: &Array,
txn: &mut Transaction,
index: u32,
src: Vec<PyObject>,
) -> PyResult<()> {
let mut index = index;

Python::with_gil(|py| {
while i < src.len() {
let mut iter = src
.iter()
.map(|element| CompatiblePyType::try_from(element.as_ref(py)))
.peekable();
while iter.peek().is_some() {
let mut anys: Vec<Any> = Vec::default();
while i < src.len() {
let converted_item: PyResult<Any> =
CompatiblePyType::try_from(src[i].as_ref(py)).and_then(Any::try_from);
if let Ok(any) = converted_item {
anys.push(any);
i += 1;
} else {
println!("{converted_item:?}");
break;
}
while let Some(py_type) =
iter.next_if(|element| !matches!(element, Ok(CompatiblePyType::YType(_))))
{
let any = Any::try_from(py_type?)?;
anys.push(any)
}

if !anys.is_empty() {
let len = anys.len() as u32;
dst.insert_range(txn, j, anys);
j += len;
} else {
let wrapper = PyObjectWrapper(src[i].clone());
dst.insert(txn, j, wrapper);
i += 1;
j += 1;
dst.insert_range(txn, index, anys);
index += len;
}

while let Some(y_type) =
iter.next_if(|element| matches!(element, Ok(CompatiblePyType::YType(_))))
{
dst.insert(txn, index, y_type?);
index += 1
}
}
Ok(())
})
}

Expand Down
12 changes: 12 additions & 0 deletions tests/test_y_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ def test_inserts():

assert list(x) == expected

# Ensure that preliminary types can be inserted
integrated_array = d1.get_array("prelim_container")
inserted_prelim = YArray(["insert"])
extended_prelim = YArray(["extend"])

with d1.begin_transaction() as txn:
integrated_array.insert(txn,0,inserted_prelim)
integrated_array.extend(txn, [extended_prelim])
values = [list(a) for a in integrated_array]
assert values == [["insert"], ["extend"]]



def test_to_string():
arr = YArray([7, "awesome", True, ["nested"], {"testing": "dicts"}])
Expand Down

0 comments on commit fe07f87

Please sign in to comment.