Skip to main content
summaryrefslogtreecommitdiffstats
blob: b8beb94b259cd126f5ee3932078092bce390abc6 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*******************************************************************************
 * Copyright (c) 2013 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.orcs.core.internal.search;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.eclipse.osee.executor.admin.CancellableCallable;
import org.eclipse.osee.framework.core.data.BranchId;
import org.eclipse.osee.framework.core.data.IOseeBranch;
import org.eclipse.osee.framework.core.data.TokenFactory;
import org.eclipse.osee.framework.core.enums.LoadLevel;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.type.ResultSets;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.logger.Log;
import org.eclipse.osee.orcs.OrcsSession;
import org.eclipse.osee.orcs.core.ds.BranchData;
import org.eclipse.osee.orcs.core.ds.LoadDataHandlerAdapter;
import org.eclipse.osee.orcs.core.ds.OptionsUtil;
import org.eclipse.osee.orcs.core.ds.QueryCollector;
import org.eclipse.osee.orcs.core.ds.QueryData;
import org.eclipse.osee.orcs.core.ds.QueryEngine;
import org.eclipse.osee.orcs.data.BranchReadable;

/**
 * @author Roberto E. Escobar
 */
public class BranchCallableQueryFactory {

   private final Log logger;
   private final QueryEngine queryEngine;
   private final QueryCollector collector;

   public BranchCallableQueryFactory(Log logger, QueryEngine queryEngine, QueryCollector collector) {
      super();
      this.logger = logger;
      this.queryEngine = queryEngine;
      this.collector = collector;
   }

   public CancellableCallable<Integer> createBranchCount(OrcsSession session, QueryData queryData) {
      return new AbstractSearchCallable<Integer>(session, queryData) {
         @Override
         protected Integer innerCall() throws Exception {
            Integer results = queryEngine.createBranchCount(getSession(), getQueryData()).call();
            setItemsFound(results);
            return results;
         }
      };
   }

   public CancellableCallable<ResultSet<BranchReadable>> createBranchSearch(OrcsSession session, QueryData queryData) {
      return new AbstractSearchCallable<ResultSet<BranchReadable>>(session, queryData) {

         @Override
         protected ResultSet<BranchReadable> innerCall() throws Exception {
            BranchBuilder<BranchReadable> handler = new BranchBuilder<BranchReadable>() {
               @Override
               public BranchReadable createBranch(BranchData data) {
                  // For now we assume the row is also readable
                  // This will change once write branch API is added.
                  return (BranchReadable) data;
               }
            };
            OptionsUtil.setLoadLevel(getQueryData().getOptions(), LoadLevel.ALL);
            queryEngine.createBranchQuery(getSession(), getQueryData(), handler).call();
            List<BranchReadable> results = handler.getBranches();
            setItemsFound(results.size());
            return ResultSets.newResultSet(results);
         }
      };
   }

   public CancellableCallable<ResultSet<IOseeBranch>> createBranchAsIdSearch(OrcsSession session, QueryData queryData) {
      return new AbstractSearchCallable<ResultSet<IOseeBranch>>(session, queryData) {

         @Override
         protected ResultSet<IOseeBranch> innerCall() throws Exception {
            BranchBuilder<IOseeBranch> handler = new BranchBuilder<IOseeBranch>() {

               @Override
               public IOseeBranch createBranch(BranchData data) {
                  return TokenFactory.createBranch(data.getUuid(), data.getName());
               }

            };
            OptionsUtil.setLoadLevel(getQueryData().getOptions(), LoadLevel.ALL);
            queryEngine.createBranchQuery(getSession(), getQueryData(), handler).call();
            List<IOseeBranch> results = handler.getBranches();
            setItemsFound(results.size());
            return ResultSets.newResultSet(results);
         }
      };
   }

   private abstract class BranchBuilder<T extends BranchId> extends LoadDataHandlerAdapter {

      private Map<Long, T> branchMap;
      private List<T> results;

      @Override
      public void onLoadStart() throws OseeCoreException {
         super.onLoadStart();
         branchMap = new LinkedHashMap<>();
      }

      @Override
      public void onLoadEnd() throws OseeCoreException {
         super.onLoadEnd();
         results = new LinkedList<>(branchMap.values());
         branchMap.clear();
      }

      @Override
      public void onData(BranchData data) throws OseeCoreException {
         Long key = data.getUuid();
         T branch = branchMap.get(key);
         if (branch == null) {
            branch = createBranch(data);
            branchMap.put(key, branch);
         }
      }

      public List<T> getBranches() {
         return results;
      }

      public abstract T createBranch(BranchData data) throws OseeCoreException;

   }

   private abstract class AbstractSearchCallable<T> extends CancellableCallable<T> {

      private final OrcsSession session;
      private final QueryData queryData;
      private int itemsFound = 0;

      public AbstractSearchCallable(OrcsSession session, QueryData queryData) {
         super();
         this.session = session;
         this.queryData = queryData;
      }

      protected OrcsSession getSession() {
         return session;
      }

      protected QueryData getQueryData() {
         return queryData;
      }

      protected void setItemsFound(int itemsFound) {
         this.itemsFound = itemsFound;
      }

      @Override
      public final T call() throws Exception {
         long startTime = System.currentTimeMillis();
         long endTime = startTime;
         T result = null;
         try {
            if (logger.isTraceEnabled()) {
               logger.trace("%s [start] - [%s]", getClass().getSimpleName(), queryData);
            }
            result = innerCall();
         } finally {
            endTime = System.currentTimeMillis() - startTime;
         }
         if (result != null) {
            notifyStats(endTime);
         }
         if (logger.isTraceEnabled()) {
            logger.trace("%s [%s] - completed [%s]", getClass().getSimpleName(), Lib.asTimeString(endTime), queryData);
         }
         return result;
      }

      private void notifyStats(long processingTime) {
         if (collector != null) {
            try {
               collector.collect(session, itemsFound, processingTime, queryData);
            } catch (Exception ex) {
               logger.error(ex, "Error reporting search to search collector\n%s", queryData);
            }
         }
      }

      protected abstract T innerCall() throws Exception;

   }

}

Back to the top