/
BaseTopBottomAccumulator.java
232 lines (207 loc) · 5.23 KB
/
BaseTopBottomAccumulator.java
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*******************************************************************************
* Copyright (c) 2004, 2008 Actuate Corporation.
* 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:
* Actuate Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.birt.data.aggregation.impl.rank;
import java.io.File;
import org.eclipse.birt.data.aggregation.impl.RunningAccumulator;
import org.eclipse.birt.data.engine.api.DataEngineThreadLocal;
import org.eclipse.birt.data.engine.cache.BasicCachedArray;
import org.eclipse.birt.data.engine.core.DataException;
import org.eclipse.birt.data.engine.core.security.FileSecurity;
import org.eclipse.birt.data.engine.i18n.ResourceConstants;
/**
* The most common part of all Top and Bottom accumulator.
*
*/
public abstract class BaseTopBottomAccumulator extends RunningAccumulator {
//
protected BasicCachedListExt cachedValues;
private double N;
private int passNo = 0;
private BasicCachedArray targetValue;
private int currentIndex = -1;
private Object value = null;
private static Boolean trueValue = Boolean.TRUE;
private static Boolean falseValue = Boolean.FALSE;
private String tempDir;
public BaseTopBottomAccumulator( )
{
if ( DataEngineThreadLocal.getInstance( ).getPathManager( ) != null )
{
tempDir = DataEngineThreadLocal.getInstance( )
.getPathManager( )
.getTempFileName( "AggregationPlugin_temp",
this.hashCode( ),
null ) + File.separator;
}
else
{
tempDir = System.getProperty( "java.io.tmpdir" )
+ "AggregationPlugin_temp" + this.hashCode( )
+ File.separator;
}
targetValue = new BasicCachedArray( tempDir, 0 );
}
/*
* (non-Javadoc)
* @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#start()
*/
public void start( ) throws DataException
{
super.start( );
passNo++;
if (passNo == 1) {
cachedValues = new BasicCachedListExt(tempDir);
N = -1;
}
else
{
this.targetValue = this.getTargetValueIndex();
}
}
/*
* (non-Javadoc)
* @see org.eclipse.birt.data.engine.aggregation.SummaryAccumulator#getSummaryValue()
*/
private BasicCachedArray getTargetValueIndex( ) throws DataException
{
int n = adjustNValue( N );
BasicCachedArray result = new BasicCachedArray( tempDir, (int) ( n < cachedValues.size( )
? n : cachedValues.size( ) ) );
for ( int i = 0; i < n && i < cachedValues.size( ); i++ )
{
int ind = getNextIndex( );
if ( ind == -1 )
return result;
result.set( i, Integer.valueOf( ind ) );
}
this.cachedValues = null;
return result;
}
public Object getValue( ) throws DataException
{
return value;
}
/*
* (non-Javadoc)
*
* @see org.eclipse.birt.data.engine.aggregation.Accumulator#onRow(java.lang.Object[])
*/
public void onRow( Object[] args ) throws DataException
{
assert ( args.length == 2 );
if( passNo == 1)
{
if ( args[0] != null )
{
cachedValues.add( args[0] );
}else
{
cachedValues.add( RankAggregationUtil.getNullObject( ) );
}
if ( N == -1 )
{
if( args.length < 2 )
throw new DataException(ResourceConstants.INVALID_TOP_BOTTOM_N_ARGUMENT);
N = populateNValue( args[1] );
}
}else
{
this.currentIndex ++;
this.value = populateValue();
}
}
/**
* Populate the return value.
* @return
*/
private Boolean populateValue( )
{
for( int i = 0; i < this.targetValue.length( ); i++ )
{
if ( this.currentIndex == ( (Integer) this.targetValue.get( i ) ).intValue( ) )
return trueValue;
}
return falseValue;
}
/**
* Get index of next topmost or bottommost value in cachedValues.
* @return
* @throws DataException
*/
protected abstract int getNextIndex( ) throws DataException;
/**
* Populate the N value get from argument.
*
* @param N
* @return
* @throws DataException
*/
protected abstract double populateNValue( Object N ) throws DataException;
/**
* Adjust the N value.
*
* @param N
* @return
*/
protected abstract int adjustNValue( double N );
/*
* (non-Javadoc)
* @see org.eclipse.birt.data.engine.api.aggregation.Accumulator#finish()
*/
public void finish( )
{
//the calculation has been finish, so we can release the resources
if( this.passNo >1 )
{
File tempFile = new File( tempDir );
if ( !FileSecurity.fileExist( tempFile )
|| !FileSecurity.fileIsDirectory( tempFile ) )
{
return;
}
deleteDirectory( tempFile );
}
}
/**
*
* @param dir
*/
private static void deleteDirectory( File dir )
{
File[] subFiles = FileSecurity.fileListFiles( dir );
if( subFiles != null )
{
for( int i = 0; i < subFiles.length; i++ )
{
if( FileSecurity.fileIsDirectory( subFiles[i] ) )
{
deleteDirectory( subFiles[i] );
}
else
{
safeDelete( subFiles[i] );
}
}
}
safeDelete( dir );
}
/**
*
* @param file
*/
private static void safeDelete( File file )
{
if( !FileSecurity.fileDelete( file ) )
{
FileSecurity.fileDeleteOnExit( file );
}
}
}