Пример #1
0
        public SqliteStreamStore(SqliteStreamStoreSettings settings)
            : base(settings.MetadataMaxAgeCacheExpire,
                   settings.MetadataMaxAgeCacheMaxSize,
                   settings.GetUtcNow,
                   settings.LogName)
        {
            _settings = settings;
            SqliteCommandExtensions.WithSettings(settings);

            _streamStoreNotifier = new Lazy <IStreamStoreNotifier>(() =>
            {
                if (_settings.CreateStreamStoreNotifier == null)
                {
                    throw new InvalidOperationException(
                        "Cannot create notifier because supplied createStreamStoreNotifier was null");
                }

                return(settings.CreateStreamStoreNotifier.Invoke(this));
            });

            _resolveNextVersion = (messages, direction, currentVersion) =>
            {
                if (messages.Any())
                {
                    var mVers = messages.Last().StreamVersion;
                    mVers = direction == ReadDirection.Forward
                        ? mVers + 1
                        : mVers < StreamVersion.Start ? StreamVersion.Start : mVers - 1;

                    return(mVers);
                }

                currentVersion = direction == ReadDirection.Forward
                    ? currentVersion + 1
                    : currentVersion == StreamVersion.End ? StreamVersion.End : currentVersion - 1;

                return(currentVersion);
            };
        }
Пример #2
0
        public Task <IReadOnlyList <StreamMessage> > Read(
            ReadDirection direction,
            long?index,
            long maxCount,
            bool prefectchMessageBody,
            CancellationToken cancellationToken = default
            )
        {
            using (var command = CreateCommand())
            {
                var messages = new List <StreamMessage>();
                command.CommandText = @"SELECT streams.id_original As stream_id,
        messages.stream_version,
        messages.position,
        messages.event_id,
        messages.created_utc,
        messages.type,
        messages.json_metadata,
        CASE WHEN @includeJsonData = true THEN messages.json_data ELSE null END
   FROM messages
INNER JOIN streams
     ON messages.stream_id_internal = streams.id_internal
  WHERE
    CASE WHEN @readForward THEN messages.position >= @position ELSE messages.position <= @position END
ORDER BY 
    CASE WHEN @readForward THEN messages.position ELSE -messages.position END
  LIMIT @count;";
                command.Parameters.Clear();
                command.Parameters.AddWithValue("@position", index);
                command.Parameters.AddWithValue("@count", maxCount);
                command.Parameters.AddWithValue("@includeJsonData", prefectchMessageBody);
                command.Parameters.AddWithValue("@readForward", direction == ReadDirection.Forward);
                var reader = command.ExecuteReader(CommandBehavior.SequentialAccess);

                while (reader.Read())
                {
                    var streamId      = reader.GetString(0);
                    var streamVersion = reader.GetInt32(1);
                    var position      = reader.IsDBNull(2) ? Position.End : reader.GetInt64(2);
                    var messageId     = reader.GetGuid(3);
                    var created       = reader.GetDateTime(4);
                    var type          = reader.GetString(5);
                    var jsonMetadata  = reader.GetString(6);
                    var preloadJson   = (!reader.IsDBNull(7) && prefectchMessageBody)
                        ? reader.GetTextReader(7).ReadToEnd()
                        : default;

                    var message = new StreamMessage(streamId,
                                                    messageId,
                                                    streamVersion,
                                                    position,
                                                    created,
                                                    type,
                                                    jsonMetadata,
                                                    ct => prefectchMessageBody
                            ? Task.FromResult(preloadJson)
                            : SqliteCommandExtensions.GetJsonData(streamId, streamVersion));

                    messages.Add(message);
                }

                return(Task.FromResult <IReadOnlyList <StreamMessage> >(messages));
            }
        }