public void ActivateConsolidationStore(string storeLocation)
        {
#if WINDOWS_PHONE || PORTABLE
            var tempFileName = Path.Combine(storeLocation, Guid.NewGuid().ToString("N"));
#else
            var tempFileName = Path.Combine(storeLocation, Path.GetRandomFileName());
#endif
            var consolidateDataPath = Path.Combine(storeLocation, ConsolidateFileName);
            var storeDataPath       = Path.Combine(storeLocation, DataFileName);
            PageCache.Instance.Clear(storeDataPath);
            _persistenceManager.RenameFile(storeDataPath, tempFileName);
            try
            {
                _persistenceManager.RenameFile(consolidateDataPath, storeDataPath);
            }
            catch (Exception)
            {
                _persistenceManager.RenameFile(tempFileName, storeDataPath);
                throw;
            }
            _persistenceManager.DeleteFile(tempFileName);
        }
        public void ConsolidateStore(Store store, string storeLocation, Guid jobId)
        {
            // delete consolidate file if for some reason it was still there
            if (_persistenceManager.FileExists(store.StoreConsolidateFile))
            {
                _persistenceManager.DeleteFile(store.StoreConsolidateFile);
            }

            // var inputStream = _persistenceManager.GetInputStream(store.StoreDataFile);
            var   inputStream  = store.InputDataStream;
            var   outputStream = _persistenceManager.GetOutputStream(store.StoreConsolidateFile, FileMode.CreateNew);
            ulong offset       = 0;

            try
            {
                using (var writer = new BinaryWriter(outputStream))
                {
                    var objectLocationManager = store.ObjectLocationManager;
                    foreach (var container in objectLocationManager.Containers)
                    {
                        foreach (var objLoc in container.ObjectOffsets)
                        {
                            // load object
                            var objType = GetObjectType(objLoc.Type);
                            if (objType.Equals(typeof(Store)))
                            {
                                // dont write the store.
                                continue;
                            }

                            var obj = ReadObject(inputStream, objLoc.Offset, GetObjectType(objLoc.Type));

                            // save object and update location manager
                            var bytes = obj.Save(writer, offset);

                            // manage offsets
                            objectLocationManager.SetObjectOffset(obj.ObjectId, offset,
                                                                  PersistantTypeIdentifiers[obj.GetType()], 1);
                            offset += (ulong)bytes;
                        }
                    }

                    // write the store
                    objectLocationManager.SetObjectOffset(store.ObjectId, offset,
                                                          PersistantTypeIdentifiers[store.GetType()], 1);
                    store.Save(writer, offset);

                    // delete store
                    inputStream.Close();
                    inputStream.Dispose();
                    _persistenceManager.DeleteFile(store.StoreDataFile);
                }

                // rename to new
                _persistenceManager.RenameFile(store.StoreConsolidateFile, store.StoreDataFile);

                // update the masterfile
                AppendCommitPoint(storeLocation, new CommitPoint(offset, 0ul, DateTime.UtcNow, jobId), true);
            }
            catch (Exception ex)
            {
                Logging.LogError(new BrightstarEventId(), "Unable to consolidate store. Error was " + ex.Message);
                throw;
            }
            finally
            {
                _persistenceManager.DeleteFile(store.StoreConsolidateFile);
            }
        }