/// <summary> /// Sample code that uses this VirtualCache. /// </summary> public static void Run() { Console.WriteLine("{0}: Virtual Cache demo started...", DateTime.Now); VirtualCacheGenerics vc = new VirtualCacheGenerics(); ISortedDictionary <int, Person> cache1 = vc.GetObjectCache(1); const int MaxCount = 40000; for (int i = 0; i < MaxCount; i++) { Person p = new Person(); p.FirstName = string.Format("Joe{0}", i); p.LastName = "Castanas"; p.PhoneNumber = "510-324-2222"; p.Address = string.Format("5434 {0} Coventry Court, Fremont, Ca. 94888, U.S.A.", i); cache1.Add(i, p); } Console.WriteLine("{0}: Finished inserting {1} records, reading 'em starts now...", DateTime.Now, MaxCount); cache1.MoveFirst(); cache1.HintSequentialRead = true; for (int i = 0; i < MaxCount; i++) { Person p = cache1.CurrentValue; if (p == null || p.FirstName != string.Format("Joe{0}", i)) { Console.WriteLine("Error, data for iteration {0} not found.", i); } cache1.MoveNext(); } Console.WriteLine("{0}: Virtual Cache demo ended... {1} records were read.", DateTime.Now, MaxCount); }
// "Bulk" read all Person and Address records from respective Stores... private void Read(ISortedDictionary <PersonKey, Person> PeopleStore, ISortedDictionary <int, Address> AddressStore, int MaxCount) { PeopleStore.MoveFirst(); // tell People Store it can do read ahead of 77 Persons. PeopleStore.HintBatchCount = 77; AddressStore.MoveFirst(); // tell Address Store it can do read ahead of 78 Addresses. AddressStore.HintBatchCount = 78; KeyValuePair <int, int>[] AddressIDs = new KeyValuePair <int, int> [AddressBatchCount]; int AddressBatchIndex = 0; for (int i = 1; i <= MaxCount; i++) { Person p = PeopleStore.CurrentValue; if (p.FirstName != string.Format("Joe{0}", i)) { Console.WriteLine("Error detected, expected Joe{0} not found in this sequence from disk", i); } else { AddressIDs[AddressBatchIndex++] = new KeyValuePair <int, int>(p.AddressID, i); if (AddressBatchIndex == AddressBatchCount) { int[] a2 = new int[AddressBatchCount]; for (int i2 = 0; i2 < AddressBatchCount; i2++) { a2[i2] = AddressIDs[i2].Key; } // Query a batch of 1000 addresses. NOTE: doing batch query is optimal operation // as it minimizes segment jumps of the HDD "disk head". QueryResult <int>[] Addresses; if (AddressStore.Query(QueryExpression <int> .Package(a2), out Addresses)) { for (int i2 = 0; i2 < AddressBatchCount; i2++) { Address addr = (Address)Addresses[i2].Value; if (addr == null || addr.Street != string.Format("143{0} LoveLane", AddressIDs[i2].Value)) { Console.WriteLine("Error detected, expected Address 143{0} not found in this sequence from disk", AddressIDs[i2].Value); } } } AddressBatchIndex = 0; } } PeopleStore.MoveNext(); } if (!PeopleStore.EndOfTree()) { Console.WriteLine("Expected EOT but isn't."); } Console.WriteLine("Reading all data({0}) ended.", MaxCount); }
void ReadAll(ObjectServer server, ISortedDictionary <long, Person> PeopleStore, int maxCount ) { for (int i = 0; i < maxCount; i++) { var v = PeopleStore.CurrentValue; if (!PeopleStore.MoveNext()) { break; } } }
static void ReadRecords(ISortedDictionary <int, Person> store, int MaxCount, string Salt) { store.MoveFirst(); store.HintBatchCount = 99; for (int i = 0; i < MaxCount; i++) { Person p = store.CurrentValue; if (p == null || p.FirstName != string.Format("Joe{0}{1}", i, Salt)) { Console.WriteLine("Error, data for iteration {0} not found.", i); } store.MoveNext(); } if (store.EndOfTree()) { Console.WriteLine("Store's End of tree reached."); } }
void ReadAll(ISortedDictionary <long, Person> PeopleStore) { if (PeopleStore.MoveFirst()) { PeopleStore.HintBatchCount = 103; int Ctr = 0; do { Ctr++; Person p = PeopleStore.CurrentValue; } while (PeopleStore.MoveNext()); if (Ctr != PeopleStore.Count) { Console.WriteLine("Failed! Read {0}, expected {1}", Ctr, PeopleStore.Count); } else { Console.WriteLine("Read {0} items.", Ctr); } } }
void ReadAll(ISortedDictionary <int, Person> PeopleStore, ISortedDictionary <int, Address> AddressStore, int MaxCount) { Console.WriteLine("{0}: Start reading {1} records.", DateTime.Now, PeopleStore.Count); PeopleStore.MoveFirst(); PeopleStore.HintBatchCount = 200; int[] Aids = new int[1000]; int AidsIndex = 0; int Ctr = 0; while (!PeopleStore.EndOfTree()) { Ctr++; Person p = PeopleStore.CurrentValue; if (p == null) { throw new InvalidOperationException("Person record not found."); } Aids[AidsIndex++] = p.AddressID; if (AidsIndex == 1000) { //** Do batch Query in set of 1000 Addresses... here we're fully utilizing SOP/disk buffers //** and minimizes file pointer jumps QueryResult <int>[] addressFound; if (AddressStore.Query(QueryExpression <int> .Package(Aids), out addressFound)) { foreach (QueryResult <int> v in addressFound) { if (!v.Found) { Console.WriteLine("Address '{0}' not found.", v.Key); } } } AidsIndex = 0; } if (!PeopleStore.MoveNext()) { break; } if (Ctr > PeopleStore.Count + 4) { Console.WriteLine("Error... Ctr > People Count."); break; } } if (AidsIndex > 0) { int[] Aids2 = new int[AidsIndex]; Array.Copy(Aids, 0, Aids2, 0, AidsIndex); QueryResult <int>[] AddressFound; if (AddressStore.Query(QueryExpression <int> .Package(Aids2), out AddressFound)) { foreach (QueryResult <int> v in AddressFound) { if (!v.Found) { Console.WriteLine("Address '{0}' not found.", v.Key); } } } } if (Ctr != PeopleStore.Count) { Console.WriteLine("Error, count of records doesn't match tree traversal count!"); } else { Console.WriteLine("{0}: Finished reading {1} records.", DateTime.Now, PeopleStore.Count); } }