1 //%2003////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2000, 2001, 2002 BMC Software, Hewlett-Packard Development
4 // Company, L. P., IBM Corp., The Open Group, Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L. P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
16 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
17 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
19 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //==============================================================================
26 // Author: Mike Brasher (mbrasher@bmc.com)
28 // Modified By: Carol Ann Krug Graves, Hewlett-Packard Company
29 // (carolann_graves@hp.com)
31 //%/////////////////////////////////////////////////////////////////////////////
33 #include <Pegasus/Common/Config.h>
34 #include <Pegasus/Common/Destroyer.h>
38 #include <Pegasus/Repository/InstanceIndexFile.h>
39 #include <Pegasus/Repository/InstanceDataFile.h>
41 PEGASUS_USING_PEGASUS;
44 static const char* tmpDir;
48 String indexPath (tmpDir);
49 indexPath.append("/X.idx");
50 CString ipd = indexPath.getCString();
51 const char* PATH = ipd;
59 Boolean result = InstanceIndexFile::createEntry(PATH,
60 CIMObjectPath("X.key1=1001,key2=\"Hello World 1\""), index, size);
65 result = InstanceIndexFile::createEntry(PATH,
66 CIMObjectPath("X.key1=1002,key2=\"Hello World 2\""), index, size);
71 result = InstanceIndexFile::createEntry(PATH,
72 CIMObjectPath("X.key1=1003,key2=\"Hello World 3\""), index, size);
77 result = InstanceIndexFile::createEntry(PATH,
78 CIMObjectPath("X.key1=1004,key2=\"Hello World 4\""), index, size);
83 result = InstanceIndexFile::createEntry(PATH,
84 CIMObjectPath("X.key1=1005,key2=\"Hello World 5\""), index, size);
87 // delete the 3rd entry
88 result = InstanceIndexFile::deleteEntry(PATH,
89 CIMObjectPath("X.key2=\"Hello World 3\",key1=1003"), freeCount);
95 result = InstanceIndexFile::createEntry(
96 PATH, CIMObjectPath("X.key1=1003,key2=\"Hello World 3\""),
100 // delete the newly created entry
101 result = InstanceIndexFile::deleteEntry(PATH,
102 CIMObjectPath("X.key2=\"Hello World 3\",key1=1003"), freeCount);
105 // delete the first entry
106 result = InstanceIndexFile::deleteEntry(PATH,
107 CIMObjectPath("X.key1=1001,key2=\"Hello World 1\""), freeCount);
110 // modify the 5th entry
113 result = InstanceIndexFile::modifyEntry(PATH,
114 CIMObjectPath("X.key1=1005,key2=\"Hello World 5\""),
115 index, size, freeCount);
119 // Iterate the entries in the file:
120 // There should be 7 entries
121 // The 1st, 3rd, 5th and 6th entries should be marked as 'free'.
125 Array<Uint32> freeFlags;
126 Array<Uint32> indices;
128 Array<CIMObjectPath> instanceNames;
130 Boolean flag = InstanceIndexFile::enumerateEntries(
131 PATH, freeFlags, indices, sizes, instanceNames, true);
135 assert(freeFlags.size() == indices.size());
136 assert(indices.size() == sizes.size());
137 assert(sizes.size() == instanceNames.size());
139 assert( freeFlags[0] == 1 &&
146 // Now attempt to compact:
148 assert(InstanceIndexFile::compact(PATH));
151 // Verify the result:
152 // There should be 3 entries and no 'free' entries
155 Array<Uint32> freeFlags;
156 Array<Uint32> indices;
158 Array<CIMObjectPath> instanceNames;
160 Boolean flag = InstanceIndexFile::enumerateEntries(
161 PATH, freeFlags, indices, sizes, instanceNames, true);
165 assert(freeFlags.size() == 3);
166 assert(freeFlags.size() == indices.size());
167 assert(indices.size() == sizes.size());
168 assert(sizes.size() == instanceNames.size());
170 for (Uint32 i = 0; i < freeFlags.size(); i++)
172 assert(freeFlags[i] == 0);
179 String instancesPath (tmpDir);
180 instancesPath.append("/X.instances");
181 CString ipd = instancesPath.getCString();
182 const char* PATH = ipd;
185 // Append some instances:
191 data.append("AAAAAAAA", 8);
192 InstanceDataFile::appendInstance(PATH, data, index);
195 data.append("BBBBBBBB", 8);
196 InstanceDataFile::appendInstance(PATH, data, index);
199 data.append("CCCCCCCC", 8);
200 InstanceDataFile::appendInstance(PATH, data, index);
204 // Load some instances:
207 InstanceDataFile::loadInstance(PATH, 8, 8, data);
208 assert(memcmp(data.getData(), "BBBBBBBB", 8) == 0);
211 InstanceDataFile::loadInstance(PATH, 0, 8, data);
212 assert(memcmp(data.getData(), "AAAAAAAA", 8) == 0);
215 InstanceDataFile::loadInstance(PATH, 16, 8, data);
216 assert(memcmp(data.getData(), "CCCCCCCC", 8) == 0);
220 // Load all instances:
223 InstanceDataFile::loadAllInstances(PATH, data);
224 assert(memcmp(data.getData(), "AAAAAAAABBBBBBBBCCCCCCCC", 24) == 0);
225 assert(data.size() == 3 * 8);
229 // Now attempt rollback:
232 assert(InstanceDataFile::beginTransaction(PATH));
234 data.append("ZZZZZZZZ", 8);
235 InstanceDataFile::appendInstance(PATH, data, index);
238 assert(InstanceDataFile::rollbackTransaction(PATH));
241 // Now attempt commit:
244 assert(InstanceDataFile::beginTransaction(PATH));
246 data.append("DDDDDDDD", 8);
247 InstanceDataFile::appendInstance(PATH, data, index);
250 assert(InstanceDataFile::commitTransaction(PATH));
253 // Verify the result:
256 InstanceDataFile::loadAllInstances(PATH, data);
257 assert(memcmp(data.getData(), "AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD", 32) == 0);
258 assert(data.size() == 4 * 8);
262 // Now attempt to compact:
265 Array<Uint32> freeFlags;
266 Array<Uint32> indices;
277 assert(InstanceDataFile::compact(PATH, freeFlags, indices, sizes));
280 // Verify the result:
283 InstanceDataFile::loadAllInstances(PATH, data);
284 assert(memcmp(data.getData(), "AAAAAAAACCCCCCCC", 16) == 0);
285 assert(data.size() == 2 * 8);
289 int main(int argc, char** argv)
291 tmpDir = getenv ("PEGASUS_TMP");
305 cerr << "Error: " << e.getMessage() << endl;
309 cout << "+++++ passed all tests" << endl;