Пример #1
0
        public void TestGenericStructAndPersistenBlobDictionary()
        {
            try
            {
                using (var dictionary = new PersistentDictionary <int, ContainingStruct>(DictionaryPath))
                {
                    var blob       = new PersistentBlob(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                    var nestedBlob = new ContainingStruct()
                    {
                        Key = 3, Blob = blob,
                    };

                    RunDictionaryTests(dictionary, 1, nestedBlob);
                    RunDictionaryTests(dictionary, 2, new ContainingStruct());
                    RunDictionaryTests(dictionary,
                                       3,
                                       new ContainingStruct()
                    {
                        Key = 4, Blob = new PersistentBlob(new byte[0])
                    });
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                Assert.AreEqual("Not supported for SetColumn\r\nParameter name: TColumn\r\nActual value was EsentCollectionsTests.GenericDictionaryTests+ContainingStruct.", ex.Message);
            }
        }
Пример #2
0
        public bool TryParseLegacyDiskLayoutVersion(string dotGVFSPath, out int majorVersion)
        {
            string repoMetadataPath = Path.Combine(dotGVFSPath, EsentRepoMetadataName);

            majorVersion = 0;
            if (Directory.Exists(repoMetadataPath))
            {
                try
                {
                    using (PersistentDictionary <string, string> oldMetadata = new PersistentDictionary <string, string>(repoMetadataPath))
                    {
                        string versionString = oldMetadata[DiskLayoutEsentVersionKey];
                        if (!int.TryParse(versionString, out majorVersion))
                        {
                            return(false);
                        }
                    }
                }
                catch
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Пример #3
0
            private void Initialize(string workingDir)
            {
                if (String.IsNullOrEmpty(workingDir))
                {
                    throw new InvalidOperationException();
                }

                if (!Directory.Exists(workingDir))
                {
                    Directory.CreateDirectory(workingDir);
                }

                _dataDir = workingDir;

                _isExisted = PersistentDictionaryFile.Exists(_dataDir);
                if (_isExisted)
                {
                    _linksDatabase = new PersistentDictionary <string, string>(_dataDir);
                }
                else
                {
                    _linksDatabase = new PersistentDictionary <string, string>(_dataDir);
                }

                _msdnResolver        = new BuilderMsdnResolver();
                _msdnResolver.Locale = _locale;
            }
Пример #4
0
        /// <summary>
        /// Create and modify a generic dictionary.
        /// </summary>
        /// <typeparam name="TKey">The key type for the dictionary.</typeparam>
        /// <typeparam name="TValue">The value type for the dictionary.</typeparam>
        private static void TestGenericDictionary <TKey, TValue>() where TKey : IComparable <TKey>
        {
            using (var dictionary = new PersistentDictionary <TKey, TValue>(DictionaryPath))
            {
                TKey   key   = default(TKey);
                TValue value = default(TValue);
                RunDictionaryTests(dictionary, key, value);
            }

            // Reopen the database
            Dictionary <TKey, TValue> temp;

            using (var dictionary = new PersistentDictionary <TKey, TValue>(DictionaryPath))
            {
                temp = new Dictionary <TKey, TValue>(dictionary);
            }

            // Delete the database
            Assert.IsTrue(PersistentDictionaryFile.Exists(DictionaryPath), "Dictionary should exist");
            PersistentDictionaryFile.DeleteFiles(DictionaryPath);
            Assert.IsFalse(PersistentDictionaryFile.Exists(DictionaryPath), "Dictionary should have been deleted");

            // Recreate the database
            using (var dictionary = new PersistentDictionary <TKey, TValue>(temp, DictionaryPath))
            {
                DictionaryAssert.AreEqual(temp, dictionary);
            }

            PersistentDictionaryFile.DeleteFiles(DictionaryPath);
        }
Пример #5
0
            public static bool CheckDiskLayoutVersion(PersistentDictionary <string, string> repoMetadata, out string error)
            {
                error = string.Empty;
                int persistedVersionNumber;

                if (TryGetOnDiskLayoutVersion(repoMetadata, out persistedVersionNumber, out error))
                {
                    if (persistedVersionNumber < MinDiskLayoutVersion)
                    {
                        error = string.Format(
                            "Breaking change to GVFS disk layout has been made since cloning. \r\nEnlistment disk layout version: {0} \r\nGVFS disk layout version: {1} \r\nMinimum supported version: {2}",
                            persistedVersionNumber,
                            CurrentDiskLayoutVersion,
                            MinDiskLayoutVersion);

                        return(false);
                    }
                    else if (persistedVersionNumber > MaxDiskLayoutVersion)
                    {
                        error = string.Format(
                            "Changes to GVFS disk layout do not allow mounting after downgrade. Try mounting again using a more recent version of GVFS. \r\nEnlistment disk layout version: {0} \r\nGVFS disk layout version: {1}",
                            persistedVersionNumber,
                            CurrentDiskLayoutVersion);
                        return(false);
                    }

                    return(true);
                }

                return(false);
            }
Пример #6
0
 public void TestGenericStringUriDictionary()
 {
     using (var dictionary = new PersistentDictionary <string, Uri>(DictionaryPath))
     {
         RunDictionaryTests(dictionary, "http", new Uri("http://localhost"));
     }
 }
Пример #7
0
 public void TestGenericStringIpAddressDictionary()
 {
     using (var dictionary = new PersistentDictionary <string, IPAddress>(DictionaryPath))
     {
         RunDictionaryTests(dictionary, "localhost", IPAddress.Loopback);
     }
 }
Пример #8
0
 public void TestGenericStringDecimalDictionary()
 {
     using (var dictionary = new PersistentDictionary <string, decimal?>(DictionaryPath))
     {
         RunDictionaryTests(dictionary, "one", Decimal.One);
     }
 }
Пример #9
0
 public static PersistentDictionary<string, string> PersistentDictionary(string name)
 {
     if (persistenceDictionary.ContainsKey(name)) return persistenceDictionary[name];
     string dataPath = Path.Combine(Environment.CurrentDirectory, @"..\App_Data\" + name);
     persistenceDictionary[name] = new PersistentDictionary<string, string>(dataPath);
     return persistenceDictionary[name];
 }
Пример #10
0
        public static void AssertNotModifiedByAnotherTransaction(TableStorage storage, ITransactionStorageActions transactionStorageActions, string key, PersistentDictionary.ReadResult readResult, TransactionInformation transactionInformation)
        {
            if (readResult == null)
                return;
            var txIdAsBytes = readResult.Key.Value<byte[]>("txId");
            if (txIdAsBytes == null)
                return;

            var txId = new Guid(txIdAsBytes);
            if (transactionInformation != null && transactionInformation.Id == txId)
            {
                return;
            }

            var existingTx = storage.Transactions.Read(new JObject { { "txId", txId.ToByteArray() } });
            if (existingTx == null)//probably a bug, ignoring this as not a real tx
                return;

            var timeout = existingTx.Key.Value<DateTime>("timeout");
            if (DateTime.UtcNow > timeout)
            {
                transactionStorageActions.RollbackTransaction(txId);
                return;
            }

            throw new ConcurrencyException("Document '" + key + "' is locked by transacton: " + txId);
        }
Пример #11
0
        /// <summary>
        /// Test LINQ queries on the dictionary.
        /// </summary>
        /// <typeparam name="TKey">Key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">Value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to test.</param>
        /// <param name="key">Key that is present in the dictionary.</param>
        /// <param name="value">Value associated with the key in the dictionary.</param>
        private static void TestDictionaryLinq <TKey, TValue>(PersistentDictionary <TKey, TValue> dictionary, TKey key, TValue value)
            where TKey : IComparable <TKey>
        {
            var kvp = new KeyValuePair <TKey, TValue>(key, value);

            Assert.IsTrue(dictionary.Any(x => 0 == x.Key.CompareTo(key)), "Any == should have found {0}", key);
            Assert.IsTrue(dictionary.Any(x => 0 <= x.Key.CompareTo(key)), "Any <= should have found {0}", key);
            Assert.IsTrue(dictionary.Any(x => 0 >= x.Key.CompareTo(key)), "Any >= should have found {0}", key);
            Assert.IsTrue(dictionary.Any(x => !(0 != x.Key.CompareTo(key))), "Any !(!=) should have found {0}", key);

            var query = from x in dictionary where x.Key.CompareTo(key) == 0 select x.Value;

            Assert.AreEqual(value, query.Single(), "Where == failed");
            query = from x in dictionary where x.Key.CompareTo(key) == 0 select x.Value;
            Assert.AreEqual(value, query.Reverse().Single(), "Where == failed (reversed)");
            query = from x in dictionary where x.Key.CompareTo(key) <= 0 select x.Value;
            Assert.AreEqual(value, query.Single(), "Where <= failed");
            query = from x in dictionary where x.Key.CompareTo(key) >= 0 select x.Value;
            Assert.AreEqual(value, query.Single(), "Where >= failed");
            query = from x in dictionary where !(x.Key.CompareTo(key) != 0) select x.Value;
            Assert.AreEqual(value, query.Single(), "Where !(!=) failed");
            Assert.AreEqual(kvp, dictionary.Where(x => x.Key.CompareTo(key) >= 0).Reverse().Last(), "Where.Reverse.Last failed");

            Assert.AreEqual(kvp, dictionary.First(), "First");
            Assert.AreEqual(kvp, dictionary.First(x => x.Key.CompareTo(key) == 0), "First");
            Assert.AreEqual(kvp, dictionary.FirstOrDefault(), "FirstOrDefault");
            Assert.AreEqual(kvp, dictionary.FirstOrDefault(x => x.Key.CompareTo(key) == 0), "FirstOrDefault");
            Assert.AreEqual(kvp, dictionary.Last(), "Last");
            Assert.AreEqual(kvp, dictionary.Last(x => x.Key.CompareTo(key) == 0), "Last");
            Assert.AreEqual(kvp, dictionary.LastOrDefault(), "LastOrDefault");
            Assert.AreEqual(kvp, dictionary.LastOrDefault(x => x.Key.CompareTo(key) == 0), "LastOrDefault");

            Assert.AreEqual(1, dictionary.Count(x => x.Key.CompareTo(key) <= 0), "Count failed");
        }
Пример #12
0
 public LogIndex(LogStore store, string name, Func<NeithLog, string> getKeyFunc)
 {
     Name = name;
     GetKeyFunc = getKeyFunc;
     IndexDir = store.DataDir.PathCombine("index", name).GetFullPath();
     Index = new PersistentDictionary<string, IndexValue>(IndexDir);
 }
Пример #13
0
 protected void OpenDictionary()
 {
     persistentSource = new FileBasedPersistentSource(tempPath, "test_", TransactionMode.Safe);
     aggregateDictionary = new AggregateDictionary(persistentSource);
     persistentDictionary = aggregateDictionary.Add(new PersistentDictionary(persistentSource, JTokenComparer.Instance));
     aggregateDictionary.Initialze();
 }
Пример #14
0
		public frmSample59()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			
			dict = new PersistentDictionary<int, Customer>("EssentData");
			var essentSource = new EseentPingData<Customer>(dict);
			
			AddRows(1, 10000);
			
			pingGrid1.Columns.Add("TotalRevenue", "TotalRevenue property", typeof(int));
			pingGrid1.Columns.Add("Name", "Name property", typeof(string));
			
			
			pingGrid1.DataSource = essentSource;
			pingGrid1.Columns.StretchToFit();
			
			pingGrid1.VScrollPositionChanged += delegate { UpdateCount(); };
			toolStripMenuItem1.Click += this.ToolStripMenuItem1Click;
			
			UpdateCount();
		}
Пример #15
0
        public override void Initialize(string workingDir, bool createNotFound)
        {
            if (String.IsNullOrEmpty(workingDir))
            {
                throw new InvalidOperationException();
            }

            if (!Directory.Exists(workingDir))
            {
                Directory.CreateDirectory(workingDir);
            }
            if (_isSystem)
            {
                _dataSourceDir = workingDir;
            }
            else
            {
                string tempFile = Path.GetFileNameWithoutExtension(
                    Path.GetTempFileName());

                _dataSourceDir = Path.Combine(workingDir, tempFile);
            }

            _isExisted = PersistentDictionaryFile.Exists(_dataSourceDir);
            if (_isExisted)
            {
                this.CheckDataIndex(_dataSourceDir);

                _targetStorage = new PersistentDictionary <string, string>(_dataSourceDir);

                if (_targetStorage.ContainsKey("$DataCount$"))
                {
                    _count = Convert.ToInt32(_targetStorage["$DataCount$"]);
                    if (_count <= 0)
                    {
                        _count = _targetStorage.Count;
                    }
                }
                else
                {
                    _count = _targetStorage.Count;
                }
            }
            else
            {
                _count = 0;
                if (createNotFound)
                {
                    _targetStorage = new PersistentDictionary <string, string>(_dataSourceDir);
                }
            }

            if (_targetStorage != null)
            {
                _targetCache = new DatabaseTargetCache(100);
            }

            _isInitialized = true;
        }
 public void TestGenericStringDictionary()
 {
     using (var dictionary = new PersistentDictionary <string, string>(DictionaryPath))
     {
         RunDictionaryTests(dictionary, "foo", "bar");
         RunDictionaryTests(dictionary, String.Empty, String.Empty);
     }
 }
Пример #17
0
 protected void Dispose(bool disposing)
 {
     if (this.additionalDoNotProject != null)
     {
         this.additionalDoNotProject.Dispose();
         this.additionalDoNotProject = null;
     }
 }
Пример #18
0
 protected void Dispose(bool disposing)
 {
     if (this.repoMetadata != null)
     {
         this.repoMetadata.Dispose();
         this.repoMetadata = null;
     }
 }
Пример #19
0
 private Core()
 {
     ChunkPeers      = new PersistentChunkPeers();
     Rnd             = new Random();
     BackupFiles     = new PersistentDictionary <string, FileEntry>("backupfiles", "backupfiles");
     Log             = new ObservableLog();
     RunningServices = new List <IService>();
 }
Пример #20
0
 private void addLogEntry(logEntry_t logEntry)
 {
     using (PersistentDictionary <DateTime, logEntry_t> dictionary = new PersistentDictionary <DateTime, logEntry_t>(logpath))
     {
         dictionary.Add(DateTime.Now, logEntry);
     }
     this.Notify();
 }
Пример #21
0
        public override void Initialize(string workingDir, bool createNotFound)
        {
            if (String.IsNullOrEmpty(workingDir))
            {
                throw new InvalidOperationException();
            }

            _quickStorage = new MemoryTargetStorage();

            if (!Directory.Exists(workingDir))
            {
                Directory.CreateDirectory(workingDir);
            }

            // For non-system, we will delete the directory after use...
            _targetDataDir = workingDir;

            _isExisted = PersistentDictionaryFile.Exists(_targetDataDir);
            if (_isExisted)
            {
                this.CheckDataIndex(_targetDataDir);

                _targetStorage = new PersistentDictionary <string, string>(_targetDataDir);

                if (_targetStorage.ContainsKey("$DataCount$"))
                {
                    _count = Convert.ToInt32(_targetStorage["$DataCount$"]);
                    if (_count <= 0)
                    {
                        _count = _targetStorage.Count;
                    }
                }
                else
                {
                    _count = _targetStorage.Count;
                }

                if (_useQuickTargets)
                {
                    this.AddQuickTargets();
                }
            }
            else
            {
                _count = 0;
                if (createNotFound)
                {
                    _targetStorage = new PersistentDictionary <string, string>(_targetDataDir);
                }
            }

            if (_targetStorage != null)
            {
                _targetCache = new DatabaseTargetCache(100);
            }

            _isInitialized = true;
        }
Пример #22
0
 public static void SaveDiskLayoutVersion(string dotGVFSRoot, string value)
 {
     using (PersistentDictionary <string, string> dictionary = new PersistentDictionary <string, string>(
                Path.Combine(dotGVFSRoot, RepoMetadataDatabaseName)))
     {
         dictionary[DiskLayoutVersionKey] = value;
         dictionary.Flush();
     }
 }
Пример #23
0
 public PersistentObjectDictonary(string directory, Func <TKey, byte[]> keyEncoder, Func <byte[], TKey> keyDecoder, Func <TValue, byte[]> valueEncoder, Func <byte[], TValue> valueDecoder)
 {
     this.directory    = directory;
     this.keyEncoder   = keyEncoder;
     this.keyDecoder   = keyDecoder;
     this.valueEncoder = valueEncoder;
     this.valueDecoder = valueDecoder;
     this.dict         = new PersistentDictionary <string, PersistentBlob>(directory);
 }
Пример #24
0
        internal void Clear()
        {
            string path = _Library.DatabasePath;

            Flush();
            _Library.Dispose();
            PersistentDictionaryFile.DeleteFiles(path);
            _Library = new PersistentDictionary <TKey, TValue>(path);
        }
Пример #25
0
        public void TestPersistentDictionarySequentialInsertAndLookupSpeed()
        {
            using (var dictionary = new PersistentDictionary<long, string>(DictionaryLocation))
            {
                MeasureSequentialInsertAndLookupSpeed(dictionary);
            }

            Cleanup.DeleteDirectoryWithRetry(DictionaryLocation);
        }
Пример #26
0
        public void TestPersistentDictionaryRandomInsertAndLookupSpeed()
        {
            using (var dictionary = new PersistentDictionary <long, string>(DictionaryLocation))
            {
                MeasureRandomInsertAndLookupSpeed(dictionary);
            }

            Cleanup.DeleteDirectoryWithRetry(DictionaryLocation);
        }
 public ManagedEsentDatabase(string persistFolder)
 {
     _persistFolder = persistFolder;
     _database = new PersistentDictionary<string, string>(persistFolder);
     if (Logger.IsDebugEnabled)
     {
         Logger.Debug("Created managed esent task database at folder '{0}'", persistFolder);
     }
 }
        /// <summary>
        /// Test dictionary insert/replace/delete.
        /// </summary>
        /// <typeparam name="TKey">Key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">Value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to test.</param>
        /// <param name="key">Key that is present in the dictionary.</param>
        /// <param name="value">Value associated with the key in the dictionary.</param>
        private static void TestBasicOperations <TKey, TValue>(PersistentDictionary <TKey, TValue> dictionary, TKey key, TValue value)
            where TKey : IComparable <TKey>
        {
            var kvp = new KeyValuePair <TKey, TValue>(key, value);

            // Add a record
            dictionary.Add(key, value);

            // Test PersistentDictionary.Add error handling
            try
            {
                dictionary.Add(key, value);
                Assert.Fail("Expected ArgumentException from Add");
            }
            catch (ArgumentException)
            {
                // Expected
            }

            // Overwrite a value
            dictionary[key] = value;

            // Retrieve a value
            Assert.AreEqual(value, dictionary[key], "Retrieve with [] failed");
            TValue t;

            Assert.IsTrue(dictionary.TryGetValue(key, out t), "TryGetValue({0}) failed", key);
            Assert.AreEqual(value, t, "TryGetValue({0}) returned the wrong value", key);

            // Clear and re-insert
            dictionary.Clear();
            Assert.AreEqual(0, dictionary.Count, "Dictionary is empty. Count is wrong");
            dictionary[key] = value;
            Assert.AreEqual(1, dictionary.Count, "Item was just inserted. Count is wrong");

            // Get the keys and values
            Assert.AreEqual(dictionary.Keys.First(), key, "Keys collection");
            Assert.AreEqual(dictionary.Values.First(), value, "Values collection");

            // Test PersistentDictionary.Contains (true)
            Assert.IsTrue(dictionary.ContainsKey(key), "Dictionary should have contained key {0}", key);
            Assert.IsTrue(dictionary.ContainsValue(value), "Dictionary should have contained value {0}", value);
            Assert.IsTrue(dictionary.Contains(kvp), "Dictionary should have contained <{0},{1}>", key, value);

            // Test PersistentDictionary.Remove
            Assert.IsTrue(dictionary.Remove(key), "Key {0} should exist, but removal failed", key);
            Assert.IsFalse(dictionary.Remove(key), "Key {0} doesn't exist, but removal succeeded", key);

            dictionary.Add(kvp);
            Assert.IsTrue(dictionary.Remove(kvp), "KeyValuePair <{0},{1}> should exist, but removal failed", kvp.Key, kvp.Value);
            Assert.IsFalse(dictionary.Remove(kvp), "KeyValuePair <{0},{1}> doesn't exist, but removal succeeded", kvp.Key, kvp.Value);

            // Test PersistentDictionary.Contains (false)
            Assert.IsFalse(dictionary.ContainsKey(key), "Dictionary should have contained key {0}", key);
            Assert.IsFalse(dictionary.ContainsValue(value), "Dictionary should have contained value {0}", value);
            Assert.IsFalse(dictionary.Contains(kvp), "Dictionary should have contained <{0},{1}>", key, value);
        }
        public PlayerStatistics(int maxReportSize)
        {
            _maxReportSize = maxReportSize;

            _stats = new PersistentDictionary <PlayerStats>("Players", "PlayerStats", noKeyFolder: true);

            Directory.CreateDirectory("Reports");
            _bestPlayers = Collections.Load <BestPlayersItem>(BestPlayersFilename);
        }
Пример #30
0
        public SparseCheckoutAndDoNotProject(GVFSContext context, string virtualSparseCheckoutFilePath, string databaseName)
        {
            this.sparseCheckoutEntries    = new ConcurrentHashSet <string>(StringComparer.OrdinalIgnoreCase);
            this.sparseCheckoutSerializer = new FileSerializer(context, virtualSparseCheckoutFilePath);

            this.additionalDoNotProject = new PersistentDictionary <string, bool>(
                Path.Combine(context.Enlistment.DotGVFSRoot, databaseName));
            this.context = context;
        }
Пример #31
0
        public void VerifyNewPersistentDictionaryHasUpgradedGlobalsTable()
        {
            string directory = InteropApiTests.SetupHelper.CreateRandomDirectory();

            var dict = new PersistentDictionary <ulong, bool>(directory);

            dict.Dispose();
            Assert.IsTrue(this.CheckGlobalsTableIsUpgrdaded(directory));
        }
Пример #32
0
 private void SaveDiskLayoutVersion(string value)
 {
     using (PersistentDictionary <string, string> dictionary = new PersistentDictionary <string, string>(
                Path.Combine(this.Enlistment.DotGVFSRoot, RepoMetadataDatabaseName)))
     {
         dictionary[DiskLayoutVersionKey] = value;
         dictionary.Flush();
     }
 }
        /// <summary>
        /// Test dictionary enumeration.
        /// </summary>
        /// <typeparam name="TKey">Key type of the dictionary.</typeparam>
        /// <typeparam name="TValue">Value type of the dictionary.</typeparam>
        /// <param name="dictionary">The dictionary to test.</param>
        /// <param name="key">Key that is present in the dictionary.</param>
        /// <param name="value">Value associated with the key in the dictionary.</param>
        private static void TestDictionaryEnumeration <TKey, TValue>(PersistentDictionary <TKey, TValue> dictionary, TKey key, TValue value)
            where TKey : IComparable <TKey>
        {
            var         kvp        = new KeyValuePair <TKey, TValue>(key, value);
            IEnumerable enumerator = dictionary.Where(x => true);

            foreach (object o in enumerator)
            {
                Assert.AreEqual(o, kvp, "Dictionary query enumeration");
            }

            enumerator = dictionary.Keys.Where(x => true);
            foreach (object o in enumerator)
            {
                Assert.AreEqual(o, kvp.Key, "Dictionary query enumeration");
            }

            foreach (KeyValuePair <TKey, TValue> o in dictionary.Where(x => true).Reverse())
            {
                Assert.AreEqual(o, kvp, "Dictionary query enumeration");
            }

            foreach (object o in (IEnumerable)dictionary)
            {
                Assert.AreEqual(o, kvp, "Dictionary enumeration");
            }

            foreach (KeyValuePair <TKey, TValue> a in dictionary.Reverse())
            {
                Assert.AreEqual(a, kvp, "Reverse dictionary enumeration");
            }

            foreach (object o in (IEnumerable)dictionary.Keys)
            {
                Assert.AreEqual(o, key, "Key enumeration");
            }

            foreach (TKey k in dictionary.Keys.Reverse())
            {
                Assert.AreEqual(k, key, "Reverse key enumeration");
            }

            foreach (TValue v in dictionary.Values.Reverse())
            {
                Assert.AreEqual(v, value, "Value enumeration");
            }

            foreach (TKey k in ((IDictionary <TKey, TValue>)dictionary).Keys)
            {
                Assert.AreEqual(k, key, "Key enumeration");
            }

            foreach (TValue v in ((IDictionary <TKey, TValue>)dictionary).Values)
            {
                Assert.AreEqual(v, value, "Value enumeration");
            }
        }
Пример #34
0
 private static PersistentDictionary<string, string> existingOrNew(string path, int retry = 0)
 {
     lock (_dictionaries)
     {
         if (_dictionaries.ContainsKey(path)) return _dictionaries[path];
         var dictionary = new PersistentDictionary<string, string>(path);
         _dictionaries[path] = dictionary;
         return dictionary;
     }
 }
Пример #35
0
        public void VerifyOpenFailsOnMismatchedValueTypes()
        {
            const string DictionaryLocation = "IntIntDictionary";
            var          dict = new PersistentDictionary <int, int>(DictionaryLocation);

            dict.Dispose();
            var wrongDict = new PersistentDictionary <int, string>(DictionaryLocation);

            Cleanup.DeleteDirectoryWithRetry(DictionaryLocation);
        }
Пример #36
0
        public static void CreateEsentPlaceholderDatabase(string dotGVFSRoot)
        {
            string metadataPath = Path.Combine(dotGVFSRoot, EsentPlaceholderFolder);

            using (PersistentDictionary <string, string> placeholders = new PersistentDictionary <string, string>(metadataPath))
            {
                placeholders["mock:\\path"] = new string('0', 40);
                placeholders.Flush();
            }
        }
Пример #37
0
        protected void OpenDictionary()
        {
            persistentSource = new FileBasedPersistentSource(Path.GetTempPath(), "test_", TransactionMode.Lazy);
            aggregateDictionary = new AggregateDictionary(persistentSource);

            persistentDictionaryOne = aggregateDictionary.Add(new PersistentDictionary(persistentSource, JTokenComparer.Instance));
            persistentDictionaryTwo = aggregateDictionary.Add(new PersistentDictionary(persistentSource, JTokenComparer.Instance));

            aggregateDictionary.Initialze();
        }
Пример #38
0
        public static void SaveDiskLayoutVersionAsEsentDatabase(string dotGVFSRoot, string value)
        {
            string metadataPath = Path.Combine(dotGVFSRoot, EsentRepoMetadataFolder);

            using (PersistentDictionary <string, string> repoMetadata = new PersistentDictionary <string, string>(metadataPath))
            {
                repoMetadata[DiskLayoutVersionKey] = value;
                repoMetadata.Flush();
            }
        }
Пример #39
0
        public void TestPersistentDictionary()
        {
            const string Directory = "IDictionary";
            using (var dictionary = new PersistentDictionary<string, string>(Directory))
            {
                IDictionaryTest(dictionary);
            }

            Cleanup.DeleteDirectoryWithRetry(Directory);
        }
Пример #40
0
        public MainPage()
        {
            this.InitializeComponent();

            // Persist the dictionary to app's Local storage
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            string        dbPath      = Path.Combine(localFolder.Path, "Dictionary");

            universalWindowsDB = new PersistentDictionary <string, string>(dbPath);
        }
Пример #41
0
        static void Benchmark()
        {
            var payload = new byte[4 * 1024];

            var dir = "temp";
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            Utility.Timeit("File system", 1, () =>
            {
                var random = new Random();
                for (var i = 0; i < 40000; ++i)
                {
                    var path = string.Format("{0}/{1}.txt", dir, random.Next(1000));
                    switch (random.Next(2))
                    {
                        case 0:
                            File.WriteAllBytes(path, payload);
                            break;
                        case 1:
                            if (File.Exists(path)) File.Delete(path);
                            break;
                        default:
                            throw new NotSupportedException();
                    }
                }
            });

            Utility.Timeit("PersistentDictionary", 1, () =>
            {
                if (File.Exists("1.txt"))
                    File.Delete("1.txt");

                var random = new Random();
                using (var dict = new PersistentDictionary("1.txt"))
                {
                    for (var i = 0; i < 40000; ++i)
                    {
                        var key = string.Format("{0}/{1}.txt", dir, random.Next(1000));
                        switch (random.Next(2))
                        {
                            case 0:
                                if (dict.ContainsKey(key)) dict[key] = payload;
                                else dict.Add(key, payload);
                                break;
                            case 1:
                                if (dict.ContainsKey(key)) dict.Remove(key);
                                break;
                            default:
                                throw new NotSupportedException();
                        }
                    }
                }
            });
        }
Пример #42
0
        public EsentStorage(string directory = null, ISerializer serializer = null)
        {
            if (string.IsNullOrWhiteSpace(directory))
                directory = this.GetDefaultDirectory();

            if (serializer == null)
                serializer = new JsonSerializer();

            _serializer = serializer;
            _data = new PersistentDictionary<string, string>(SanitizeDirectory(directory));
        }
Пример #43
0
        private LogStore(string dir)
        {
            DataDir = dir;
            Dic = new PersistentDictionary<DateTime, NeithLog>(MastarDir);

            // Index
            List<LogIndex> indexes = new List<LogIndex>();

            IndexActor = new LogIndex(this, "actor", a => a.Actor); indexes.Add(IndexActor);

            Indexes = indexes.ToArray();
        }
Пример #44
0
        public static void Clear()
        {
            string path = Library.DatabasePath;

            // Dispose of the current library
            Library.Dispose();
            _Library = null;

            // Create a brand new one
            PersistentDictionaryFile.DeleteFiles(path);
            _Library = new PersistentDictionary<Guid, string>(path);
        }
Пример #45
0
 public void TestDictionary()
 {
     var a = new PersistentDictionary<string, int>();
     a.Add("one", 1);
     a.Store();
     a.Add("two", 2);
     a.Undo();
     Assert.True(a.Count == 1);
     Assert.True(a["one"] == 1);
     a.Redo();
     Assert.True(a.Count == 2);
     Assert.True(a["one"] == 1);
     Assert.True(a["two"] == 2);
 }
Пример #46
0
 /// <summary>
 /// Ask the user for their first name and see if we remember 
 /// their last name.
 /// </summary>
 public static void Main()
 {
     PersistentDictionary<string, string> dictionary = new PersistentDictionary<string, string>("Names");
     Console.WriteLine("What is your first name?");
     string firstName = Console.ReadLine();
     if (dictionary.ContainsKey(firstName))
     {
         Console.WriteLine("Welcome back {0} {1}", firstName, dictionary[firstName]);
     }
     else
     {
         Console.WriteLine("I don't know you, {0}. What is your last name?", firstName);
         dictionary[firstName] = Console.ReadLine();
     }
 }
Пример #47
0
        private KeyValueStorage()
        {
            if (!this.StorageExists()) Directory.CreateDirectory(this._storageFolder); // create the storage directory if it doesn't exists yet.

            try
            {
                this._dictionary = new PersistentDictionary<string, byte>(this._storageFolder);
            }
            catch(Microsoft.Isam.Esent.Interop.EsentErrorException e) // The database sanity may not be all okay, if so try to re-create a new database.
            {
                LogManager.Instance.Write(LogMessageTypes.Error, string.Format("An exception occured while trying to inititialize Esent database. Will be creating a new db. Exception: {0}", e));
                Directory.Delete(this._storageFolder,true);
                Directory.CreateDirectory(this._storageFolder);
                this._dictionary = new PersistentDictionary<string, byte>(this._storageFolder);
            }
        }
        private static void AssertValidEtag(string key, PersistentDictionary.ReadResult doc, PersistentDictionary.ReadResult docInTx, Guid? etag)
        {
            if (doc == null)
                return;
            var existingEtag =
                docInTx != null
                    ? new Guid(docInTx.Key.Value<byte[]>("etag"))
                    : new Guid(doc.Key.Value<byte[]>("etag"));


            if (etag != null && etag.Value != existingEtag)
            {
                throw new ConcurrencyException("PUT attempted on document '" + key +
                                               "' using a non current etag")
                {
                    ActualETag = etag.Value,
                    ExpectedETag = existingEtag
                };
            }
        }
Пример #49
0
        static void Test()
        {
            using (var dict = new PersistentDictionary("1.txt"))
            {
                string line;
                while ((line = Console.ReadLine()) != null)
                {
                    try
                    {
                        var tokens = line.Split();
                        switch (tokens[0].ToLower())
                        {
                            case "add":
                                dict.Add(tokens[1], Encoding.UTF8.GetBytes(tokens[2]));
                                break;
                            case "update":
                                dict[tokens[1]] = Encoding.UTF8.GetBytes(tokens[2]);
                                break;
                            case "remove":
                                dict.Remove(tokens[1]);
                                break;
                            case "dump":
                                {
                                    foreach (var kv in dict.OrderBy(kv => kv.Key))
                                        Console.WriteLine("\t{0}: {1}", kv.Key, Encoding.UTF8.GetString(kv.Value));
                                }
                                break;
                            default:
                                throw new NotSupportedException();
                        }

                        Console.WriteLine("Done.");
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }
        }
Пример #50
0
        static void Main(string[] args)
        {
            var path = Path.GetDirectoryName(typeof(Program).GetType().Assembly.Location);
            var students = new PersistentDictionary<int, Student>(path);

            while (true) {
                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.BackgroundColor = ConsoleColor.Black;

                Console.WriteLine("1) Create a student.");

                if (students.Count > 0) {
                    Console.WriteLine("2) List students.");
                }

                Console.WriteLine("Q. Quit");

                var line = Console.ReadLine();
                if (String.Equals(line, "Q")) {
                    break;
                }
              }
        }
Пример #51
0
        //=====================================================================

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="component">The build component that owns the dictionary.  This is useful for logging
        /// messages during initialization.</param>
        /// <param name="configuration">The target dictionary configuration</param>
        /// <returns>A target dictionary instance that uses a simple in-memory
        /// <see cref="Dictionary{TKey, TValue}"/> instance to store the targets.</returns>
        public ESentTargetDictionary(BuildComponentCore component, XPathNavigator configuration) :
          base(component, configuration)
        {
            bool noReload = false;
            int localCacheSize;

            string cachePath = configuration.GetAttribute("cachePath", String.Empty);

            if(String.IsNullOrWhiteSpace(cachePath))
                throw new ArgumentException("The cachePath attribute must contain a value", "configuration");

            string cacheSize = configuration.GetAttribute("localCacheSize", String.Empty);

            if(String.IsNullOrWhiteSpace(cacheSize) || !Int32.TryParse(cacheSize, out localCacheSize))
                localCacheSize = 1000;

            // This is a slightly modified version of Managed ESENT that provides the option to serialize
            // reference types.  In this case, we don't care about potential issues of persisted copies not
            // matching the original if modified as they are never updated once written to the cache.  We can
            // also turn off column compression for a slight performance increase since it doesn't benefit the
            // binary data that is serialized.
            PersistentDictionaryFile.AllowReferenceTypeSerialization = true;

            index = new PersistentDictionary<string, Target>(cachePath, false)
                { LocalCacheSize = localCacheSize };

            string noReloadValue = configuration.GetAttribute("noReload", String.Empty);

            // If noReload is true, skip reloading the dictionary if it contains any data.  This is used on
            // project targets to prevent reloading the data in the reference build if already loaded by the
            // conceptual build.
            if(!String.IsNullOrWhiteSpace(noReloadValue) && Boolean.TryParse(noReloadValue, out noReload) &&
              noReload && index.Count != 0)
                return;

            // Loading new targets can take a while so issue a diagnostic message as an alert
            int filesToLoad = 0;

            foreach(string file in Directory.EnumerateFiles(this.DirectoryPath, this.FilePattern, this.Recurse ?
              SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                if((this.NamespaceFileFilter.Count == 0 || this.NamespaceFileFilter.Contains(
                  Path.GetFileName(file))) && !this.ContainsKey("N:" + Path.GetFileNameWithoutExtension(file)))
                    filesToLoad++;

            // The time estimate is a ballpark figure and depends on the system
            if(filesToLoad != 0)
            {
                component.WriteMessage(MessageLevel.Diagnostic, "{0} file(s) need to be added to the ESENT " +
                    "reflection target cache database.  Indexing them will take about {1:N0} minute(s), " +
                    "please be patient.  Cache location: {2}", filesToLoad, Math.Ceiling(filesToLoad * 10 / 60.0),
                    cachePath);

                // Limit the degree of parallelism or it overwhelms the ESENT version store
                this.LoadTargetDictionary(3);
            }
        }
Пример #52
0
        public Esent()
        {
            dictionary = new PersistentDictionary<int, string>("Names");

            dictionary.Clear();
        }
Пример #53
0
 public void Setup()
 {
     Cleanup.DeleteDirectoryWithRetry(DictionaryLocation);
     CreateTestData();
     this.dictionary = new PersistentDictionary<string, string>(data, DictionaryLocation);
 }
Пример #54
0
        //=====================================================================

        /// <summary>
        /// This is used to create the index cache database
        /// </summary>
        /// <param name="configuration">The configuration to use</param>
        private PersistentDictionary<string, string> CreateCache(XPathNavigator configuration)
        {
            PersistentDictionary<string, string> cache = null;
            HashSet<string> namespaceFileFilter = new HashSet<string>();
            string groupId, cachePathAttrName, cachePath, baseDirectory, wildcardPath, recurseValue, dupWarning,
                fullPath, directoryPart, filePart;
            bool recurse, reportDuplicateIds, isProjectData = false;
            int localCacheSize, filesToLoad;

            var parent = configuration.Clone();
            parent.MoveToParent();

            groupId = configuration.GetAttribute("groupId", String.Empty);

            // If caching project data, they will all go into a common index
            if(groupId.StartsWith("Project_", StringComparison.OrdinalIgnoreCase))
            {
                cachePathAttrName = "projectCachePath";
                isProjectData = true;
            }
            else
                cachePathAttrName = "frameworkCachePath";

            cache = esentCaches.FirstOrDefault(c => c.Database.Contains(groupId));

            if(cache != null)
                cachePath = cache.Database;
            else
            {
                cachePath = parent.GetAttribute(cachePathAttrName, String.Empty);

                if(String.IsNullOrWhiteSpace(cachePath))
                    return null;

                cachePath = Path.Combine(cachePath, groupId);

                string cacheSize = parent.GetAttribute("localCacheSize", String.Empty);

                if(String.IsNullOrWhiteSpace(cacheSize) || !Int32.TryParse(cacheSize, out localCacheSize))
                    localCacheSize = 2500;

                // Column compression is left on here as it does benefit the string data
                cache = new PersistentDictionary<string, string>(cachePath)
                    { LocalCacheSize = localCacheSize };
            }

            baseDirectory = configuration.GetAttribute("base", String.Empty);

            if(!String.IsNullOrWhiteSpace(baseDirectory))
                baseDirectory = Environment.ExpandEnvironmentVariables(baseDirectory);

            wildcardPath = configuration.GetAttribute("files", String.Empty);

            if(String.IsNullOrWhiteSpace(wildcardPath))
                base.Component.WriteMessage(MessageLevel.Error, "Each data element must have a files attribute " +
                    "specifying which files to index.");

            wildcardPath = Environment.ExpandEnvironmentVariables(wildcardPath);

            recurseValue = configuration.GetAttribute("recurse", String.Empty);

            if(String.IsNullOrWhiteSpace(recurseValue) || !Boolean.TryParse(recurseValue, out recurse))
                recurse = false;

            // Support suppression of duplicate ID warnings.  This can happen a lot when common classes appear in
            // multiple assemblies.
            dupWarning = configuration.GetAttribute("duplicateWarning", String.Empty);

            if(String.IsNullOrWhiteSpace(dupWarning) || !Boolean.TryParse(dupWarning, out reportDuplicateIds))
                reportDuplicateIds = true;

            if(String.IsNullOrEmpty(baseDirectory))
                fullPath = wildcardPath;
            else
            {
                // Verify that the directory exists
                if(!Directory.Exists(baseDirectory))
                    throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The targets " +
                        "directory '{0}' does not exist.  The configuration is most likely out of date.  " +
                        "Please delete this component from the project, add it back, and reconfigure it.",
                        baseDirectory), "configuration");

                fullPath = Path.Combine(baseDirectory, wildcardPath);
            }

            fullPath = Environment.ExpandEnvironmentVariables(fullPath);

            directoryPart = Path.GetDirectoryName(fullPath);

            if(String.IsNullOrEmpty(directoryPart))
                directoryPart = Environment.CurrentDirectory;

            filePart = Path.GetFileName(fullPath);

            // Filtering reduces the number of files to load, especially for the core reflection data files
            namespaceFileFilter.Clear();

            foreach(XPathNavigator filter in configuration.Select("namespace/@file"))
                namespaceFileFilter.Add(filter.Value);

            // Loading new targets can take a while so issue a diagnostic message as an alert
            filesToLoad = 0;

            if(namespaceFileFilter.Count != 0)
            {
                // Reflection data can be filtered by namespace so some or all of it may already be there
                foreach(string file in Directory.EnumerateFiles(directoryPart, filePart, recurse ?
                  SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
                    if((namespaceFileFilter.Count == 0 || namespaceFileFilter.Contains(Path.GetFileName(file))) &&
                      !cache.ContainsKey("N:" + Path.GetFileNameWithoutExtension(file)))
                        filesToLoad++;
            }
            else
            {
                // Comments files can't be filtered by namespace so we'll assume that if the collection is not
                // empty, it has already been loaded unless it's a project comments file list.  In that case,
                // we will merge them if necessary.
                if(isProjectData || cache.Count == 0)
                    filesToLoad = Directory.EnumerateFiles(directoryPart, filePart, recurse ?
                        SearchOption.AllDirectories : SearchOption.TopDirectoryOnly).Count();
            }

            if(filesToLoad != 0)
            {
                // The time estimate is a ballpark figure and depends on the system
                base.Component.WriteMessage(MessageLevel.Diagnostic, "{0} file(s) need to be added to the " +
                    "ESENT index cache database.  Indexing them will take about {1:N0} minute(s), please be " +
                    "patient.  Cache location: {2}", filesToLoad, Math.Ceiling(filesToLoad / 60.0), cachePath);

                // Limit the degree of parallelism or it overwhelms the ESENT version store
                Parallel.ForEach(Directory.EnumerateFiles(directoryPart, filePart,
                  recurse ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly),
                  new ParallelOptions { MaxDegreeOfParallelism = 3 },
                  file =>
                  {
                      // Skip the file if not in a defined filter
                      if(namespaceFileFilter.Count != 0 && !namespaceFileFilter.Contains(Path.GetFileName(file)))
                          return;

                      // Get the keys from the file and add them to the index
                      foreach(var kv in base.GetValues(file))
                      {
                          // Only report the warning if wanted
                          if(cache.ContainsKey(kv.Key) && reportDuplicateIds)
                              this.Component.WriteMessage(MessageLevel.Warn, "An entry for the key '{0}' " +
                                  "already exists and will be replaced by the one from '{1}'", kv.Key, file);

                          cache[kv.Key] = kv.Value.InnerXml;
                      }
                  });
            }

            return cache;
        }
Пример #55
0
 /// <summary>
 /// Initializes instance of PersistentDictionary based on Esent storage
 /// </summary>
 private static void InitializeData()
 {
     data = new PersistentDictionary<string, string>(ConfigurationManager.ConnectionStrings["esent"].ConnectionString);
 }
 public EsentRunner( string directory )
 {
     m_PersistentDictionary = CreatePersistentDictionary( directory );
 }
Пример #57
0
 public void Setup()
 {
     this.expected = new Dictionary<string, string>();
     this.actual = new PersistentDictionary<string, string>(DictionaryLocation);
 }
Пример #58
0
        public void TestCloseAndDelete()
        {
            var rand = new Random();
            for (int i = 0; i < 64; ++i)
            {
                string k = rand.NextDouble().ToString();
                string v = rand.Next().ToString();
                this.expected.Add(k, v);
                this.actual.Add(k, v);
            }

            this.actual.Dispose();
            PersistentDictionaryFile.DeleteFiles(DictionaryLocation);

            // Deleting the files clears the dictionary
            this.expected.Clear();

            this.actual = new PersistentDictionary<string, string>(DictionaryLocation);
            DictionaryAssert.AreEqual(this.expected, this.actual);
        }
Пример #59
0
        public void TestCloseAndReopen()
        {
            var rand = new Random();
            for (int i = 0; i < 100; ++i)
            {
                string k = rand.Next().ToString();
                string v = rand.NextDouble().ToString();
                this.expected.Add(k, v);
                this.actual.Add(k, v);
            }

            this.actual.Dispose();
            this.actual = new PersistentDictionary<string, string>(DictionaryLocation);
            DictionaryAssert.AreEqual(this.expected, this.actual);
        }
 public void Setup()
 {
     this.dictionary = new PersistentDictionary<long, string>(DictionaryLocation);
 }