def update_graph(self, metadata): """Get the new metadata and store them in the graph as properties""" session = get_graph_session() gq_coll = gq_get_vertex_collection(self) # old_meta stores the metadata present in the graph before updating old_meta = self.get_graph_metadata() # new_meta stores the set of final metadata we want new_meta = metadata old_meta_keys = set(old_meta.keys()) new_meta_keys = set(new_meta.keys()) # Delete metadata present in old but not in new for meta_to_delete in old_meta_keys.difference(new_meta_keys): session.execute_graph("""{}.properties('{}').drop()""".format( gq_coll, meta_to_delete)) # Update metadata present in both old and new (check if value changed ?) for meta_to_update in old_meta_keys.intersection(new_meta_keys): session.execute_graph("""{}.property('{}', '{}')""".format( gq_coll, meta_to_update, new_meta[meta_to_update])) # Add metadata present in new but not in old for meta_to_add in new_meta_keys.difference(old_meta_keys): session.execute_graph("""{}.property('{}', '{}')""".format( gq_coll, meta_to_add, new_meta[meta_to_add]))
def delete(self, username=None): """Delete a collection and the associated row in the tree entry table""" from indigo.models import Notification if self.is_root: return cfg = get_config(None) session = connection.get_session() keyspace = cfg.get('KEYSPACE', 'indigo') session.set_keyspace(keyspace) query = SimpleStatement( """DELETE FROM tree_entry WHERE container=%s""") session.execute(query, (self.path, )) # Get the row that describe the collection as a child of its parent child = TreeEntry.objects.filter(container=self.container, name=u"{}/".format( self.name)).first() if child: child.delete() session = get_graph_session() session.execute_graph("""v_coll = {}.drop(); """.format(gq_get_vertex_collection(self))) state = self.mqtt_get_state() payload = self.mqtt_payload(state, {}) Notification.delete_collection(username, self.path, payload) self.reset()
def check_graph_root(cls): """Return True if the root vertex does exist""" root = Collection.find("/") session = get_graph_session() query = gq_get_vertex_collection(root) rset = session.execute_graph("v_root = {};".format(query)) try: root = next(rset.current_rows) return True except StopIteration: return False
def create(cls, container, name, uuid=None, metadata=None, url=None, mimetype=None, username=None, size=None): """Create a new resource in the tree_entry table""" from indigo.models import Collection from indigo.models import Notification # Check if parent collection exists parent = Collection.find(container) if parent is None: raise NoSuchCollectionError(container) if uuid is None: uuid = default_cdmi_id() create_ts = datetime.now() modified_ts = create_ts path = merge(container, name) if metadata: metadata_cass = meta_cdmi_to_cassandra(metadata) # Check the container exists collection = Collection.find(container) if not collection: raise NoSuchCollectionError(container) # Make sure parent/name are not in use. existing = cls.find(path) if existing: raise ResourceConflictError(path) kwargs = { "container": container, "name": name, "url": url, "uuid": uuid, } if is_reference(url): kwargs["create_ts"] = create_ts kwargs["modified_ts"] = modified_ts kwargs["mimetype"] = mimetype if metadata: kwargs["metadata"] = metadata_cass else: obj_id = url.replace("cassandra://", "") data_obj = DataObject.find(obj_id) if metadata: data_obj.update(mimetype=mimetype, metadata=metadata_cass) else: if mimetype: data_obj.update(mimetype=mimetype) if size: data_obj.update(size=size) data_entry = TreeEntry.create(**kwargs) new = Resource(data_entry) session = get_graph_session() add_user_edge = "" if username: user = User.find(username) if user: add_user_edge = """v_user = {}.next(); v_user.addEdge('owns', v_new); """.format(gq_get_vertex_user(user)) else: add_user_edge = "" session.execute_graph("""v_parent = {}.next(); v_new = {}; v_parent.addEdge('son', v_new); {} """.format(gq_get_vertex_collection(parent), gq_add_vertex_resource(new), add_user_edge)) if metadata: new.update_graph(metadata) state = new.mqtt_get_state() payload = new.mqtt_payload({}, state) Notification.create_resource(username, path, payload) # Index the resource new.index() return new
def create(cls, name, container='/', metadata=None, username=None): """Create a new collection""" from indigo.models import Notification from indigo.models import Resource path = merge(container, name) # Check if parent collection exists parent = Collection.find(container) if parent is None: raise NoSuchCollectionError(container) resource = Resource.find(merge(container, name)) if resource is not None: raise ResourceConflictError(container) collection = Collection.find(path) if collection is not None: raise CollectionConflictError(container) now = datetime.now() # If we try to create a tree entry with no metadata, cassandra-driver # will fail as it tries to delete a static column if metadata: metadata_cass = meta_cdmi_to_cassandra(metadata) coll_entry = TreeEntry.create(container=path, name='.', container_create_ts=now, container_modified_ts=now, container_metadata=metadata_cass) else: coll_entry = TreeEntry.create(container=path, name='.', container_create_ts=now, container_modified_ts=now) coll_entry.update(uuid=coll_entry.container_uuid) child_entry = TreeEntry.create(container=container, name=name + '/', uuid=coll_entry.container_uuid) new = Collection.find(path) add_user_edge = "" if username: user = User.find(username) if user: add_user_edge = """v_user = {}.next(); v_user.addEdge('owns', v_new); """.format(gq_get_vertex_user(user)) else: add_user_edge = "" session = get_graph_session() session.execute_graph("""v_parent = {}.next(); v_new = {}; v_parent.addEdge('son', v_new); {} """.format(gq_get_vertex_collection(parent), gq_add_vertex_collection(new), add_user_edge)) if metadata: new.update_graph(metadata) state = new.mqtt_get_state() payload = new.mqtt_payload({}, state) Notification.create_collection(username, path, payload) # Index the collection new.index() return new