Esempio n. 1
0
        /// <summary>
        ///     Publish the list of tables
        /// </summary>
        /// <param name="tables">The list of tables</param>
        public override async void PublishTables(Dictionary <Type, ITableDefinition> tables, Func <string, Type> resolveType)
        {
            _fileHelper.EnsureDirectory(_pathProvider.GetDatabasePath(_basePath, DatabaseInstanceName, this));

            var typePath = _pathProvider.GetTypesPath(_basePath, DatabaseInstanceName, this);

            if (!_fileHelper.FileExists(typePath))
            {
                return;
            }

            using (var typeFile = _fileHelper.GetReader(typePath))
            {
                var count = typeFile.ReadInt32();

                for (var x = 0; x < count; x++)
                {
                    var fullTypeName = typeFile.ReadString();

                    var tableType = resolveType(fullTypeName);

                    if (tableType == null)
                    {
                        throw new SterlingTableNotFoundException(fullTypeName, DatabaseInstanceName);
                    }

                    await GetTypeIndexAsync(tableType.AssemblyQualifiedName).ConfigureAwait(false);
                }
            }

            var pathLock = PathLock.GetLock(DatabaseInstanceName);

            using (await pathLock.LockAsync().ConfigureAwait(false))
            {
                foreach (var type in tables.Keys)
                {
                    _tables.Add(type);
                    _fileHelper.EnsureDirectory(_pathProvider.GetTablePath(_basePath, DatabaseInstanceName, type, this));
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        ///     Serialize a double index
        /// </summary>
        /// <typeparam name="TKey">The type of the key</typeparam>
        /// <typeparam name="TIndex1">The type of the first index</typeparam>
        /// <typeparam name="TIndex2">The type of the second index</typeparam>
        /// <param name="type">The type of the parent table</param>
        /// <param name="indexName">The name of the index</param>
        /// <param name="indexMap">The index map</param>
        public override async Task SerializeIndexAsync <TKey, TIndex1, TIndex2>(Type type, string indexName, Dictionary <TKey, Tuple <TIndex1, TIndex2> > indexMap)
        {
            var indexPath = _pathProvider.GetIndexPath(_basePath, DatabaseInstanceName, type, this, indexName);

            var pathLock = PathLock.GetLock(type.FullName);

            using (await pathLock.LockAsync().ConfigureAwait(false))
            {
                using (var indexFile = _fileHelper.GetWriter(indexPath))
                {
                    indexFile.Write(indexMap.Count);

                    foreach (var index in indexMap)
                    {
                        DatabaseSerializer.Serialize(index.Key, indexFile);
                        DatabaseSerializer.Serialize(index.Value.Item1, indexFile);
                        DatabaseSerializer.Serialize(index.Value.Item2, indexFile);
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     Deserialize the keys
        /// </summary>
        /// <param name="type">Type of the parent table</param>
        /// <param name="keyType">Type of the key</param>
        /// <param name="dictionary">Empty dictionary</param>
        /// <returns>The key list</returns>
        public override async Task <IDictionary> DeserializeKeysAsync(Type type, Type keyType, IDictionary dictionary)
        {
            var keyPath = _pathProvider.GetKeysPath(_basePath, DatabaseInstanceName, type, this);

            if (_fileHelper.FileExists(keyPath))
            {
                var pathLock = PathLock.GetLock(type.FullName);

                using (await pathLock.LockAsync().ConfigureAwait(false))
                {
                    using (var keyFile = _fileHelper.GetReader(keyPath))
                    {
                        var count = keyFile.ReadInt32();

                        for (var x = 0; x < count; x++)
                        {
                            dictionary.Add(DatabaseSerializer.Deserialize(keyType, keyFile), keyFile.ReadInt32());
                        }
                    }
                }
            }

            return(dictionary);
        }