markap14 commented on code in PR #7870:
URL: https://github.com/apache/nifi/pull/7870#discussion_r1357030282


##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-administration/src/main/java/org/apache/nifi/admin/service/EntityStoreAuditService.java:
##########
@@ -0,0 +1,636 @@
+/*
+ * 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.nifi.admin.service;
+
+import jetbrains.exodus.entitystore.Entity;
+import jetbrains.exodus.entitystore.EntityId;
+import jetbrains.exodus.entitystore.EntityIterable;
+import jetbrains.exodus.entitystore.PersistentEntityStore;
+import jetbrains.exodus.entitystore.PersistentEntityStores;
+import jetbrains.exodus.entitystore.StoreTransaction;
+import jetbrains.exodus.env.Environment;
+import jetbrains.exodus.env.EnvironmentConfig;
+import jetbrains.exodus.env.Environments;
+import org.apache.nifi.action.Action;
+import org.apache.nifi.action.Component;
+import org.apache.nifi.action.FlowChangeAction;
+import org.apache.nifi.action.Operation;
+import org.apache.nifi.action.component.details.ComponentDetails;
+import org.apache.nifi.action.component.details.ExtensionDetails;
+import org.apache.nifi.action.component.details.FlowChangeExtensionDetails;
+import 
org.apache.nifi.action.component.details.FlowChangeRemoteProcessGroupDetails;
+import org.apache.nifi.action.component.details.RemoteProcessGroupDetails;
+import org.apache.nifi.action.details.ActionDetails;
+import org.apache.nifi.action.details.ConfigureDetails;
+import org.apache.nifi.action.details.ConnectDetails;
+import org.apache.nifi.action.details.FlowChangeConfigureDetails;
+import org.apache.nifi.action.details.FlowChangeConnectDetails;
+import org.apache.nifi.action.details.FlowChangeMoveDetails;
+import org.apache.nifi.action.details.FlowChangePurgeDetails;
+import org.apache.nifi.action.details.MoveDetails;
+import org.apache.nifi.action.details.PurgeDetails;
+import org.apache.nifi.admin.service.entity.ActionEntity;
+import org.apache.nifi.admin.service.entity.ActionLink;
+import org.apache.nifi.admin.service.entity.ConfigureDetailsEntity;
+import org.apache.nifi.admin.service.entity.ConnectDetailsEntity;
+import org.apache.nifi.admin.service.entity.EntityProperty;
+import org.apache.nifi.admin.service.entity.EntityType;
+import org.apache.nifi.admin.service.entity.MoveDetailsEntity;
+import org.apache.nifi.admin.service.entity.PurgeDetailsEntity;
+import org.apache.nifi.history.History;
+import org.apache.nifi.history.HistoryQuery;
+import org.apache.nifi.history.PreviousValue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Stream;
+
+/**
+ * Audit Service implementation based on JetBrains Xodus Entity Store
+ */
+public class EntityStoreAuditService implements AuditService, Closeable {
+    private static final long FIRST_START_TIME = 0;
+
+    private static final int PREVIOUS_VALUES_LIMIT = 5;
+
+    private static final String ASCENDING_SORT_ORDER = "ASC";
+
+    private static final int DEFAULT_COUNT = 100;
+
+    private static final String BACKUP_FILE_NAME_FORMAT = "%s.backup.%d";
+
+    private static final Logger logger = 
LoggerFactory.getLogger(EntityStoreAuditService.class);
+
+    private final PersistentEntityStore entityStore;
+
+    private final Environment environment;
+
+    /**
+     * Entity Store Audit Service constructor with required properties for 
persistent location
+     *
+     * @param directory Persistent Entity Store directory
+     */
+    public EntityStoreAuditService(final File directory) {
+        environment = loadEnvironment(directory);
+        entityStore = PersistentEntityStores.newInstance(environment);
+        logger.info("Environment configured with directory [{}]", directory);
+    }
+
+    /**
+     * Add Actions to Persistent Store
+     *
+     * @param actions Collections of Actions to be added
+     */
+    @Override
+    public void addActions(final Collection<Action> actions) {
+        Objects.requireNonNull(actions, "Actions required");
+
+        entityStore.executeInExclusiveTransaction(storeTransaction -> {
+            for (final Action action : actions) {
+                addAction(storeTransaction, action);
+            }
+            logger.debug("Actions added [{}]", actions.size());
+        });
+    }
+
+    /**
+     * Get Previous Values for specified Component Identifier
+     *
+     * @param componentId Component Identifier for which previous property 
values should be retrieved
+     * @return Map of Property Name to List of Previous Property Values
+     */
+    @Override
+    public Map<String, List<PreviousValue>> getPreviousValues(final String 
componentId) {
+        Objects.requireNonNull(componentId, "Component Identifier required");
+
+        return entityStore.computeInReadonlyTransaction(storeTransaction -> {
+            final Map<String, List<PreviousValue>> previousValuesFound = new 
LinkedHashMap<>();
+
+            final EntityIterable actionEntities = 
storeTransaction.find(EntityType.ACTION.getEntityType(), 
ActionEntity.SOURCE_ID.getProperty(), componentId);
+            for (Entity actionEntity : actionEntities) {
+                final Entity configureDetails = 
actionEntity.getLink(ActionLink.CONFIGURE_DETAILS.getProperty());
+                if (configureDetails != null) {
+                    final String name = getProperty(configureDetails, 
ConfigureDetailsEntity.NAME);
+                    final String value = getProperty(configureDetails, 
ConfigureDetailsEntity.VALUE);
+
+                    final PreviousValue previousValue = new PreviousValue();
+                    previousValue.setPreviousValue(value);
+                    previousValue.setUserIdentity(getProperty(actionEntity, 
ActionEntity.USER_IDENTITY));
+                    previousValue.setTimestamp(getDateProperty(actionEntity, 
ActionEntity.TIMESTAMP));
+
+                    final List<PreviousValue> previousValues = 
previousValuesFound.get(name);
+                    if (previousValues == null) {
+                        final List<PreviousValue> newPreviousValues = new 
ArrayList<>();
+                        newPreviousValues.add(previousValue);
+                        previousValuesFound.put(name, newPreviousValues);
+                    } else if (previousValues.size() < PREVIOUS_VALUES_LIMIT) {
+                        previousValues.add(previousValue);
+                    }

Review Comment:
   nitpick and not worth delaying the PR, but if there are other problems that 
arise, it's worth nothing that this can be simplified to just:
   ```
   final List<PreviousValue> previousValues = 
previousValuesFound.computeIfAbsent(name, () -> new ArrayList<>());
   previousValuesFound.add(previousValue);
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to