tengqm commented on code in PR #6250:
URL: https://github.com/apache/gravitino/pull/6250#discussion_r1976868313


##########
core/src/main/java/org/apache/gravitino/listener/ModelEventDispatcher.java:
##########
@@ -0,0 +1,386 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.gravitino.listener;
+
+import java.util.Map;
+import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.Namespace;
+import org.apache.gravitino.catalog.ModelDispatcher;
+import org.apache.gravitino.exceptions.ModelAlreadyExistsException;
+import 
org.apache.gravitino.exceptions.ModelVersionAliasesAlreadyExistException;
+import org.apache.gravitino.exceptions.NoSuchModelException;
+import org.apache.gravitino.exceptions.NoSuchModelVersionException;
+import org.apache.gravitino.exceptions.NoSuchSchemaException;
+import org.apache.gravitino.listener.api.event.DeleteModelPreEvent;
+import org.apache.gravitino.listener.api.event.DeleteModelVersionPreEvent;
+import org.apache.gravitino.listener.api.event.GetModelPreEvent;
+import org.apache.gravitino.listener.api.event.GetModelVersionPreEvent;
+import org.apache.gravitino.listener.api.event.LinkModelVersionPreEvent;
+import org.apache.gravitino.listener.api.event.ListModelPreEvent;
+import org.apache.gravitino.listener.api.event.ListModelVersionPreEvent;
+import org.apache.gravitino.listener.api.event.RegisterModelPreEvent;
+import org.apache.gravitino.listener.api.info.ModelInfo;
+import org.apache.gravitino.listener.api.info.ModelVersionInfo;
+import org.apache.gravitino.model.Model;
+import org.apache.gravitino.model.ModelCatalog;
+import org.apache.gravitino.model.ModelVersion;
+import org.apache.gravitino.utils.PrincipalUtils;
+
+/**
+ * {@code ModelEventDispatcher} is a decorator for {@link ModelDispatcher} 
that not only delegates
+ * model operations to the underlying catalog dispatcher but also dispatches 
corresponding events to
+ * an {@link org.apache.gravitino.listener.EventBus} after each operation is 
completed. This allows
+ * for event-driven workflows or monitoring of model operations.
+ */
+public class ModelEventDispatcher implements ModelDispatcher {
+  private final EventBus eventBus;
+  private final ModelDispatcher dispatcher;
+
+  /**
+   * Constructs a {@link ModelEventDispatcher} with a specified EventBus and 
{@link
+   * ModelDispatcher}.
+   *
+   * @param eventBus The EventBus to which events will be dispatched.
+   * @param dispatcher The underlying {@link ModelDispatcher} that will 
perform the actual model
+   *     operations.
+   */
+  public ModelEventDispatcher(EventBus eventBus, ModelDispatcher dispatcher) {
+    this.eventBus = eventBus;
+    this.dispatcher = dispatcher;
+  }
+
+  /**
+   * Register a model in the catalog if the model is not existed, otherwise 
the {@link
+   * ModelAlreadyExistsException} will be thrown. The {@link Model} object 
will be created when the
+   * model is registered, users can call {@link 
ModelCatalog#linkModelVersion(NameIdentifier,
+   * String, String[], String, Map)} to link the model version to the 
registered {@link Model}.
+   *
+   * @param ident The name identifier of the model.
+   * @param comment The comment of the model. The comment is optional and can 
be null.
+   * @param properties The properties of the model. The properties are 
optional and can be null or
+   *     empty.
+   * @return The registered model object.
+   * @throws NoSuchSchemaException If the schema does not exist.
+   * @throws ModelAlreadyExistsException If the model already registered.
+   */
+  @Override
+  public Model registerModel(NameIdentifier ident, String comment, Map<String, 
String> properties)
+      throws NoSuchSchemaException, ModelAlreadyExistsException {
+    ModelInfo registerRequest = new ModelInfo(ident.name(), properties, 
comment);
+    eventBus.dispatchEvent(
+        new RegisterModelPreEvent(PrincipalUtils.getCurrentUserName(), ident, 
registerRequest));
+    try {
+      Model model = dispatcher.registerModel(ident, comment, properties);
+      // TODO: ModelEvent

Review Comment:
   In my view, this is not a good practice for raising PRs.
   Each PR is supposed to be:
   
   - small, e.g. <500 lines of code changes;
   - self-contained, with all irrelevant changes stripped off;
   - good enough, i.e. not half-baked;
   - easy to roll-back, if some bugs are introduced unfortunately
   
   It is perfectly okay to raise 10+ smaller PRs a day, because we know that 
small steps forward make big progress.
   
   For this specific PR, I'd suggest we implement Model Event dispatcher with 
test cases;
   then we raise PRs for each specific events, each with its own test cases.
   
   WDYT?
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to