Skip to main content
summaryrefslogtreecommitdiffstats
blob: 63bd50305307a20b1d4be823f34626996fb20775 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.search.engine.internal.search;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.data.IAttributeType;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.message.SearchOptions;
import org.eclipse.osee.framework.core.message.SearchRequest;
import org.eclipse.osee.framework.core.message.SearchResponse;
import org.eclipse.osee.framework.core.model.cache.AttributeTypeCache;
import org.eclipse.osee.framework.core.model.cache.BranchCache;
import org.eclipse.osee.framework.core.model.type.AttributeType;
import org.eclipse.osee.framework.jdk.core.type.MatchLocation;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.search.engine.IAttributeTaggerProviderManager;
import org.eclipse.osee.framework.search.engine.ISearchEngine;
import org.eclipse.osee.framework.search.engine.attribute.AttributeData;
import org.eclipse.osee.framework.search.engine.attribute.AttributeDataStore;
import org.eclipse.osee.framework.search.engine.internal.Activator;
import org.eclipse.osee.framework.search.engine.utility.ITagCollector;
import org.eclipse.osee.framework.search.engine.utility.TagProcessor;

/**
 * @author Roberto E. Escobar
 */
public class SearchEngine implements ISearchEngine {

   private final SearchStatistics statistics;
   private final TagProcessor tagProcessor;
   private final IAttributeTaggerProviderManager taggingManager;
   private final AttributeTypeCache attributeTypeCache;
   private final BranchCache branchCache;
   private final AttributeDataStore attributeDataStore;

   public SearchEngine(SearchStatistics statistics, TagProcessor tagProcessor, IAttributeTaggerProviderManager taggingManager, AttributeTypeCache attributeTypeCache, BranchCache branchCache, AttributeDataStore attributeDataStore) {
      this.statistics = statistics;
      this.tagProcessor = tagProcessor;
      this.taggingManager = taggingManager;
      this.attributeTypeCache = attributeTypeCache;
      this.branchCache = branchCache;
      this.attributeDataStore = attributeDataStore;
   }

   private Collection<AttributeType> getAttributeTypes(Collection<IAttributeType> tokens) throws OseeCoreException {
      Collection<AttributeType> attributeTypes = new HashSet<AttributeType>();
      for (IAttributeType identity : tokens) {
         AttributeType type = attributeTypeCache.get(identity);
         if (type != null) {
            attributeTypes.add(type);
         } else {
            throw new OseeStateException(String.format("Search Attribute Type Filter - attribute type [%s] not found",
               identity));
         }
      }
      return attributeTypes;
   }

   @Override
   public void search(SearchRequest searchRequest, final SearchResponse searchResponse) throws Exception {
      String searchString = searchRequest.getRawSearch();

      long startTime = System.currentTimeMillis();

      final Map<String, Long> searchTags = searchResponse.getSearchTags();
      tagProcessor.collectFromString(searchString, new ITagCollector() {

         @Override
         public void addTag(String word, Long codedTag) {
            searchTags.put(word, codedTag);
         }
      });

      if (searchTags.isEmpty()) {
         searchResponse.setErrorMessage("No words found in search string. Please try again.");
      } else {
         long startDataStoreSearch = System.currentTimeMillis();

         SearchOptions options = searchRequest.getOptions();
         Collection<IAttributeType> attributeTypeTokens = options.getAttributeTypeFilter();

         int branchId = branchCache.get(searchRequest.getBranch()).getId();
         Collection<AttributeData> tagMatches =
            attributeDataStore.getAttributesByTags(branchId, options.getDeletionFlag(), searchTags.values(),
               attributeTypeTokens);
         String message =
            String.format("Attribute Search Query found [%d] in [%d] ms", tagMatches.size(),
               System.currentTimeMillis() - startDataStoreSearch);
         OseeLog.log(SearchEngine.class, Level.INFO, message);

         long timeAfterPass1 = System.currentTimeMillis() - startTime;
         long secondPass = System.currentTimeMillis();

         boolean bypassSecondPass = !options.isMatchWordOrder();
         if (bypassSecondPass) {
            for (AttributeData attributeData : tagMatches) {
               searchResponse.add(attributeData.getBranchId(), attributeData.getArtId(), attributeData.getGammaId());
            }
         } else {
            for (AttributeData attributeData : tagMatches) {
               try {
                  List<MatchLocation> locations = taggingManager.find(attributeData, searchString, options);
                  if (!locations.isEmpty()) {
                     searchResponse.add(attributeData.getBranchId(), attributeData.getArtId(),
                        attributeData.getGammaId(), locations);
                  }
               } catch (Exception ex) {
                  OseeLog.logf(Activator.class, Level.SEVERE, "Error processing: [%s]", attributeData);
               }
            }
         }
         secondPass = System.currentTimeMillis() - secondPass;

         String firstPassMsg =
            String.format("Pass 1: [%d items in %d ms] -",
               bypassSecondPass ? searchResponse.matches() : tagMatches.size(), timeAfterPass1);

         String secondPassMsg = String.format(" Pass 2: [%d items in %d ms]", searchResponse.matches(), secondPass);

         System.out.println(String.format("Search for [%s] - %s%s", searchString, firstPassMsg,
            bypassSecondPass ? "" : secondPassMsg));
         statistics.addEntry(searchRequest, searchResponse.matches(), System.currentTimeMillis() - startTime);
      }
   }

   @Override
   public void clearStatistics() {
      this.statistics.clear();
   }

   @Override
   public SearchStatistics getStatistics() {
      SearchStatistics toReturn = null;
      try {
         toReturn = this.statistics.clone();
      } catch (CloneNotSupportedException ex) {
         toReturn = SearchStatistics.EMPTY_STATS;
      }
      return toReturn;
   }
}

Back to the top