@@ -108,7 +108,7 @@ def exists(self):
108108 Returns
109109 -------
110110 bool
111- Indicates whether stream exists .
111+ Indicates whether stream is extant in the BTrDB server .
112112 """
113113
114114 if self ._known_to_exist :
@@ -425,7 +425,7 @@ def _update_tags_collection(self, tags, collection):
425425 collection = collection
426426 )
427427
428- def _update_annotations (self , annotations , encoder ):
428+ def _update_annotations (self , annotations , encoder , replace ):
429429 # make a copy of the annotations to prevent accidental mutable object mutation
430430 serialized = deepcopy (annotations )
431431 if encoder is not None :
@@ -434,32 +434,64 @@ def _update_annotations(self, annotations, encoder):
434434 for k , v in serialized .items ()
435435 }
436436
437+ removals = []
438+ if replace :
439+ removals = [i for i in self ._annotations .keys () if i not in annotations .keys ()]
440+
437441 self ._btrdb .ep .setStreamAnnotations (
438442 uu = self .uuid ,
439443 expected = self ._property_version ,
440- changes = serialized
444+ changes = serialized ,
445+ removals = removals
441446 )
442447
443- def update (self , tags = None , annotations = None , collection = None , encoder = AnnotationEncoder ):
448+ def update (self , tags = None , annotations = None , collection = None , encoder = AnnotationEncoder , replace = False ):
444449 """
445- Updates metadata including tags, annotations, and collection.
450+ Updates metadata including tags, annotations, and collection as an
451+ UPSERT operation.
452+
453+ By default, the update will only affect the keys and values in the
454+ specified tags and annotations dictionaries, inserting them if they
455+ don't exist, or updating the value for the key if it does exist. If
456+ any of the update arguments (i.e. tags, annotations, collection) are
457+ None, they will remain unchanged in the database.
458+
459+ To delete either tags or annotations, you must specify exactly which
460+ keys and values you want set for the field and set `replace=True`. For
461+ example:
462+
463+ >>> annotations, _ = stream.anotations()
464+ >>> del annotations["key_to_delete"]
465+ >>> stream.update(annotations=annotations, replace=True)
466+
467+ This ensures that all of the keys and values for the annotations are
468+ preserved except for the key to be deleted.
446469
447470 Parameters
448- ----------
449- tags: dict
450- dict of tag information for the stream.
451- annotations: dict
452- dict of annotation information for the stream.
453- collection: str
454- The collection prefix for a stream
455- encoder: json.JSONEncoder or None
456- JSON encoder to class to use for annotation serializations, set to
457- None to prevent JSON encoding of the annotations.
471+ -----------
472+ tags : dict, optional
473+ Specify the tag key/value pairs as a dictionary to upsert or
474+ replace. If None, the tags will remain unchanged in the database.
475+ annotations : dict, optional
476+ Specify the annotations key/value pairs as a dictionary to upsert
477+ or replace. If None, the annotations will remain unchanged in the
478+ database.
479+ collection : str, optional
480+ Specify a new collection for the stream. If None, the collection
481+ will remain unchanged.
482+ encoder : json.JSONEncoder or None
483+ JSON encoder class to use for annotation serialization. Set to None
484+ to prevent JSON encoding of the annotations.
485+ replace : bool, default: False
486+ Replace all annotations or tags with the specified dictionaries
487+ instead of performing the normal upsert operation. Specifying True
488+ is the only way to remove annotation keys.
458489
459490 Returns
460491 -------
461492 int
462493 The version of the metadata (separate from the version of the data)
494+ also known as the "property version".
463495
464496 """
465497 if tags is None and annotations is None and collection is None :
@@ -479,7 +511,7 @@ def update(self, tags=None, annotations=None, collection=None, encoder=Annotatio
479511 self .refresh_metadata ()
480512
481513 if annotations is not None :
482- self ._update_annotations (annotations , encoder )
514+ self ._update_annotations (annotations , encoder , replace )
483515 self .refresh_metadata ()
484516
485517 return self ._property_version
0 commit comments