Changeset View
Changeset View
Standalone View
Standalone View
src/file/timeestimator.cpp
Show All 21 Lines | |||||
22 | 22 | | |||
23 | #include <cmath> | 23 | #include <cmath> | ||
24 | 24 | | |||
25 | #include "timeestimator.h" | 25 | #include "timeestimator.h" | ||
26 | #include "filecontentindexerprovider.h" | 26 | #include "filecontentindexerprovider.h" | ||
27 | 27 | | |||
28 | using namespace Baloo; | 28 | using namespace Baloo; | ||
29 | 29 | | |||
30 | TimeEstimator::TimeEstimator(FileIndexerConfig *config, QObject* parent) | 30 | TimeEstimator::TimeEstimator(QObject* parent) | ||
31 | : QObject(parent) | 31 | : QObject(parent) | ||
32 | , m_bufferIndex(0) | 32 | , m_bufferIndex(0) | ||
33 | , m_estimateReady(false) | 33 | , m_estimateReady(false) | ||
34 | , m_config(config) | | |||
35 | , m_batchSize(m_config->maxUncomittedFiles()) | | |||
36 | { | 34 | { | ||
37 | 35 | | |||
38 | } | 36 | } | ||
39 | 37 | | |||
40 | uint TimeEstimator::calculateTimeLeft(int filesLeft) | 38 | uint TimeEstimator::calculateTimeLeft(int filesLeft) | ||
41 | { | 39 | { | ||
42 | if (!m_estimateReady) { | 40 | if (!m_estimateReady) { | ||
43 | return 0; | 41 | return 0; | ||
44 | } | 42 | } | ||
45 | 43 | | |||
46 | //TODO: We should probably make the batch size a global macro | | |||
47 | float totalTime = 0; | 44 | float totalTime = 0; | ||
48 | float totalWeight = 0; | 45 | float totalWeight = 0; | ||
49 | 46 | | |||
50 | int bufferIndex = m_bufferIndex; | 47 | int bufferIndex = m_bufferIndex; | ||
51 | for (int i = 0; i < BUFFER_SIZE; ++i) { | 48 | for (int i = 0; i < BUFFER_SIZE; ++i) { | ||
52 | float weight = sqrt(i + 1); | 49 | float weight = sqrt(i + 1); | ||
53 | totalWeight += weight; | 50 | totalWeight += weight; | ||
54 | 51 | | |||
55 | totalTime += m_batchTimeBuffer[bufferIndex] * weight; | 52 | totalTime += m_batchTimeBuffer[bufferIndex] * weight; | ||
56 | bufferIndex = (bufferIndex + 1) % BUFFER_SIZE; | 53 | bufferIndex = (bufferIndex + 1) % BUFFER_SIZE; | ||
57 | } | 54 | } | ||
58 | 55 | | |||
59 | float weightedAverage = totalTime / totalWeight; | 56 | float weightedAverage = totalTime / totalWeight; | ||
60 | float batchesLeft = (float)filesLeft / (float)m_batchSize; | | |||
61 | 57 | | |||
62 | return weightedAverage * batchesLeft; | 58 | return weightedAverage * filesLeft; | ||
63 | } | 59 | } | ||
64 | 60 | | |||
65 | void TimeEstimator::handleNewBatchTime(uint time) | 61 | void TimeEstimator::handleNewBatchTime(uint time, uint batchSize) | ||
66 | { | 62 | { | ||
67 | // add the current batch time in place of the oldest batch time | 63 | // add the current batch time in place of the oldest batch time | ||
68 | m_batchTimeBuffer[m_bufferIndex] = time; | 64 | m_batchTimeBuffer[m_bufferIndex] = (float)time / batchSize; | ||
69 | 65 | | |||
70 | if (!m_estimateReady && m_bufferIndex == BUFFER_SIZE - 1) { | 66 | m_bufferIndex = (m_bufferIndex + 1) % BUFFER_SIZE; | ||
67 | | ||||
68 | if (!m_estimateReady && m_bufferIndex == 0) { | ||||
71 | // Buffer has been filled once. We are ready to estimate | 69 | // Buffer has been filled once. We are ready to estimate | ||
72 | m_estimateReady = true; | 70 | m_estimateReady = true; | ||
73 | } | 71 | } | ||
74 | | ||||
75 | m_bufferIndex = (m_bufferIndex + 1) % BUFFER_SIZE; | | |||
76 | } | 72 | } |