comparison base/StorageAdviser.cpp @ 170:b23eea68357e

* flesh out StorageAdviser
author Chris Cannam
date Tue, 26 Sep 2006 12:58:12 +0000
parents 04baa690f90d
children 058a82e8bc3c
comparison
equal deleted inserted replaced
169:603991c63ff6 170:b23eea68357e
25 StorageAdviser::Recommendation 25 StorageAdviser::Recommendation
26 StorageAdviser::recommend(Criteria criteria, 26 StorageAdviser::recommend(Criteria criteria,
27 int minimumSize, 27 int minimumSize,
28 int maximumSize) 28 int maximumSize)
29 { 29 {
30 std::cerr << "StorageAdviser::recommend: Criteria " << criteria
31 << ", minimumSize " << minimumSize
32 << ", maximumSize " << maximumSize << std::endl;
33
30 QString path = TempDirectory::getInstance()->getPath(); 34 QString path = TempDirectory::getInstance()->getPath();
35 int discFree = GetDiscSpaceMBAvailable(path.toLocal8Bit());
36 int memoryFree, memoryTotal;
37 GetRealMemoryMBAvailable(memoryFree, memoryTotal);
31 38
32 int discSpace = GetDiscSpaceMBAvailable(path.toLocal8Bit()); 39 std::cerr << "Disc space: " << discFree << ", memory free: " << memoryFree << ", memory total: " << memoryTotal << std::endl;
33 int memory = GetRealMemoryMBAvailable();
34 40
35 std::cerr << "Disc space: " << discSpace << ", memory: " << memory << std::endl; 41 enum StorageStatus {
42 Unknown,
43 Insufficient,
44 Marginal,
45 Sufficient
46 };
36 47
37 return Recommendation(0); 48 StorageStatus memoryStatus = Unknown;
49 StorageStatus discStatus = Unknown;
50
51 int minmb = minimumSize / 1024 + 1;
52 int maxmb = maximumSize / 1024 + 1;
53
54 if (memoryFree == -1) memoryStatus = Unknown;
55 else if (minmb > (memoryFree * 3) / 4) memoryStatus = Insufficient;
56 else if (maxmb > (memoryFree * 3) / 4) memoryStatus = Marginal;
57 else if (minmb > (memoryFree / 3)) memoryStatus = Marginal;
58 else if (memoryTotal == -1 ||
59 minmb > (memoryTotal / 10)) memoryStatus = Marginal;
60 else memoryStatus = Sufficient;
61
62 if (discFree == -1) discStatus = Unknown;
63 else if (minmb > (discFree * 3) / 4) discStatus = Insufficient;
64 else if (maxmb > (discFree * 3) / 4) discStatus = Marginal;
65 else if (minmb > (discFree / 3)) discStatus = Marginal;
66 else discStatus = Sufficient;
67
68 std::cerr << "Memory status: " << memoryStatus << ", disc status "
69 << discStatus << std::endl;
70
71 int recommendation = NoRecommendation;
72
73 if (memoryStatus == Insufficient || memoryStatus == Unknown) {
74
75 recommendation |= UseDisc;
76
77 if (discStatus == Insufficient && minmb > discFree) {
78 throw InsufficientDiscSpace(path, minmb, discFree);
79 }
80
81 if (discStatus == Insufficient || discStatus == Marginal) {
82 recommendation |= ConserveSpace;
83 } else if (discStatus == Unknown && !(criteria & PrecisionCritical)) {
84 recommendation |= ConserveSpace;
85 } else {
86 recommendation |= UseAsMuchAsYouLike;
87 }
88
89 } else if (memoryStatus == Marginal) {
90
91 if (((criteria & SpeedCritical) ||
92 (criteria & FrequentLookupLikely)) &&
93 !(criteria & PrecisionCritical) &&
94 !(criteria & LongRetentionLikely)) {
95
96 // requirements suggest a preference for memory
97
98 if (discStatus != Insufficient) {
99 recommendation |= PreferMemory;
100 } else {
101 recommendation |= UseMemory;
102 }
103
104 recommendation |= ConserveSpace;
105
106 } else {
107
108 if (discStatus == Insufficient) {
109 recommendation |= (UseMemory | ConserveSpace);
110 } else if (discStatus == Marginal) {
111 recommendation |= (PreferMemory | ConserveSpace);
112 } else if (discStatus == Unknown) {
113 recommendation |= (PreferDisc | ConserveSpace);
114 } else {
115 recommendation |= (UseDisc | UseAsMuchAsYouLike);
116 }
117 }
118
119 } else {
120
121 if (discStatus == Insufficient) {
122 recommendation |= (UseMemory | ConserveSpace);
123 } else if (discStatus != Sufficient) {
124 recommendation |= (PreferMemory | ConserveSpace);
125 } else {
126
127 if ((criteria & SpeedCritical) ||
128 (criteria & FrequentLookupLikely)) {
129 recommendation |= PreferMemory;
130 if (criteria & PrecisionCritical) {
131 recommendation |= UseAsMuchAsYouLike;
132 } else {
133 recommendation |= ConserveSpace;
134 }
135 } else {
136 recommendation |= PreferDisc;
137 recommendation |= UseAsMuchAsYouLike;
138 }
139 }
140 }
141
142 return Recommendation(recommendation);
38 } 143 }
39 144
40
41