Exemplo n.º 1
0
        public async Task <IEnumerable <AssetIndexRecord> > GetAllAssetRecordsAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var result = new ConcurrentBag <AssetIndexRecord>();

            try {
                LOG.Debug($"Opening Index DB '{_indexDbPath}'");
                await _indexDbConnection.OpenAsync(cancellationToken);

                var sql     = "SELECT asset_id, position, type, created_on, deleted FROM VFSDataIndex";
                var command = new SqliteCommand(sql, _indexDbConnection);
                using (var reader = await command.ExecuteReaderAsync(cancellationToken)) {
                    while (await reader.ReadAsync(cancellationToken))
                    {
                        var  idString = (string)reader["asset_id"];
                        Guid id;
                        if (!Guid.TryParse(idString, out id))
                        {
                            LOG.Warn($"Found invalid asset ID '{idString}' in '{_indexDbPath}'.");
                            continue;
                        }

                        /* BUG: Invalid cast exceptions...
                         * var createdOnString = (string)reader["created_on"];
                         * DateTimeOffset createdOn;
                         * if (!DateTimeOffset.TryParse(createdOnString, out createdOn)) {
                         *      LOG.Warn($"Found invalid asset creation date '{createdOnString}' in '{_indexDbPath}'.");
                         *      continue;
                         * }
                         */

                        var position = (long)reader["position"];
                        var type     = (byte)(long)reader["type"];                     // Don't you love SQLite?
                        var deleted  = (byte)reader["deleted"] == 1;

                        var indexRecord = new AssetIndexRecord {
                            //CreatedOn = createdOn,
                            DataFilePosition = position,
                            Deleted          = deleted,
                            Id    = id,
                            Scope = _scope,
                            Type  = type,
                        };

                        result.Add(indexRecord);
                    }
                }
                _indexDbConnection.Close();
                LOG.Debug($"Closed Index DB '{_indexDbPath}'");
            }
            catch (Exception e) {
                LOG.Error($"Error reading all asset index records from Index DB '{_indexDbPath}'", e);
                throw;
            }

            return(result);
        }
Exemplo n.º 2
0
        public async Task <Asset> GetAssetAsync(AssetIndexRecord indexRecord, CancellationToken cancellationToken = default(CancellationToken))
        {
            Contract.Requires(indexRecord != null);
            Contract.Requires(indexRecord.DataFilePosition > 0);

            LOG.Debug($"Reading asset ID '{indexRecord.Id}' from Data DB '{_dataDbPath}'");

            try {
                int assetLength;

                using (var stream = _mmf.CreateViewStream(indexRecord.DataFilePosition, 4)) {
                    // Read the asset size (first 4 bytes) stored in network byte order
                    var lengthBytes = new byte[4];
                    await stream.ReadAsync(lengthBytes, 0, 4, cancellationToken);

                    if (BitConverter.IsLittleEndian)
                    {
                        Array.Reverse(lengthBytes);
                    }
                    assetLength = BitConverter.ToInt32(lengthBytes, 0);
                }
                using (var stream = _mmf.CreateViewStream(indexRecord.DataFilePosition + 4, assetLength)) {
                    // Read in the asset
                    var assetData = new byte[assetLength];
                    await stream.ReadAsync(assetData, 0, assetLength, cancellationToken);

                    return(new Asset {
                        RawData = assetData,
                    });
                }
            }
            catch (Exception e) {
                LOG.Error($"Error reading asset ID '{indexRecord.Id}' from Data DB '{_dataDbPath}'", e);
                throw;
            }
        }