Skip to content

Commit

Permalink
new: return copy of payload (#627)
Browse files Browse the repository at this point in the history
* new: return copy of payload

* new: deepcopy new payload (#628)
  • Loading branch information
joein authored May 21, 2024
1 parent 8556cc8 commit 5ca9f9a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions qdrant_client/local/local_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
Any,
Callable,
Dict,
Iterable,
List,
Optional,
Sequence,
Tuple,
Union,
get_args,
)
from copy import deepcopy

import numpy as np
from pydantic.version import VERSION as PYDANTIC_VERSION
Expand Down Expand Up @@ -357,9 +357,11 @@ def _get_payload(
self,
idx: int,
with_payload: Union[bool, Sequence[str], types.PayloadSelector] = True,
return_copy: bool = True,
) -> Optional[models.Payload]:
payload = self.payload[idx]
return self._process_payload(payload, with_payload)
processed_payload = self._process_payload(payload, with_payload)
return deepcopy(processed_payload) if return_copy else processed_payload

def _get_vectors(
self, idx: int, with_vectors: Union[bool, Sequence[str]] = False
Expand Down Expand Up @@ -1235,7 +1237,9 @@ def _scroll_by_value(

def _update_point(self, point: models.PointStruct) -> None:
idx = self.ids[point.id]
self.payload[idx] = to_jsonable_python(point.payload) if point.payload is not None else {}
self.payload[idx] = deepcopy(
to_jsonable_python(point.payload) if point.payload is not None else {}
)

if isinstance(point.vector, list):
vectors = {DEFAULT_VECTOR_NAME: point.vector}
Expand Down Expand Up @@ -1272,7 +1276,9 @@ def _add_point(self, point: models.PointStruct) -> None:
self.ids[point.id] = idx
self.ids_inv.append(point.id)

self.payload.append(to_jsonable_python(point.payload) if point.payload is not None else {})
self.payload.append(
deepcopy(to_jsonable_python(point.payload) if point.payload is not None else {})
)
assert len(self.payload) == len(self.ids_inv), "Payload and ids_inv must be the same size"
self.deleted = np.append(self.deleted, 0)

Expand Down Expand Up @@ -1487,7 +1493,7 @@ def _persist_by_id(self, point_id: models.ExtendedPointId) -> None:
idx = self.ids[point_id]
point = models.PointStruct(
id=point_id,
payload=self._get_payload(idx, with_payload=True),
payload=self._get_payload(idx, with_payload=True, return_copy=False),
vector=self._get_vectors(idx, with_vectors=True),
)
self.storage.persist(point)
Expand All @@ -1504,7 +1510,7 @@ def set_payload(
key: Optional[str] = None,
) -> None:
ids = self._selector_to_ids(selector)
jsonable_payload = to_jsonable_python(payload)
jsonable_payload = deepcopy(to_jsonable_python(payload))

keys: Optional[List[JsonPathItem]] = parse_json_path(key) if key is not None else None

Expand All @@ -1531,7 +1537,7 @@ def overwrite_payload(
ids = self._selector_to_ids(selector)
for point_id in ids:
idx = self.ids[point_id]
self.payload[idx] = to_jsonable_python(payload) or {}
self.payload[idx] = deepcopy(to_jsonable_python(payload)) or {}
self._persist_by_id(point_id)

def delete_payload(
Expand Down

0 comments on commit 5ca9f9a

Please sign in to comment.