/// <summary>
        /// Reads a log message from the specified sqlite reader.
        /// </summary>
        /// <param name="reader">Sqlite reader to read from.</param>
        /// <param name="message">Receives the read log message.</param>
        private void ReadMessage(SQLiteDataReader reader, out LogFileMessage message)
        {
            // columns in result:
            // 0 = message id
            // 1 = timestamp
            // 2 = timezone offset
            // 3 = high precision timestamp
            // 4 = lost message count
            // 5 = process id
            // 6 = process name
            // 7 = application name
            // 8 = log writer name
            // 9 = log level name
            // 10 = has tags
            // 11 = text name
            long   messageId              = reader.GetInt64(0);
            var    timezoneOffset         = TimeSpan.FromTicks(reader.GetInt64(2));
            var    timestamp              = new DateTimeOffset(reader.GetInt64(1) + timezoneOffset.Ticks, timezoneOffset);
            long   highPrecisionTimestamp = reader.GetInt64(3);
            int    lostMessageCount       = reader.GetInt32(4);
            int    processId              = reader.GetInt32(5);
            string processName            = reader.GetString(6);
            string applicationName        = reader.GetString(7);
            string logWriterName          = reader.GetString(8);
            string logLevelName           = reader.GetString(9);
            bool   hasTags = reader.GetBoolean(10);
            string text    = reader.GetString(11);

            message = new LogFileMessage().InitWith(
                messageId,
                timestamp,
                highPrecisionTimestamp,
                lostMessageCount,
                mStringPool.Intern(logWriterName),
                mStringPool.Intern(logLevelName),
                TagSet.Empty,
                mStringPool.Intern(applicationName),
                mStringPool.Intern(processName),
                processId,
                text);

            // initialize tags, if there are tags associated with the message
            if (hasTags)
            {
                message.Tags = mAccessor.GetTagsOfMessage(messageId);
            }

            // protect message from changes
            message.Protect();
        }
        public void Intern()
        {
            var pool = new StringPool();

            const string source = "abcabcabcabcabc";

            Assert.AreEqual(
                "",
                pool.Intern(source, 0, 0));
            Assert.AreEqual(
                "a",
                pool.Intern(source, 0, 1));
            Assert.AreEqual(
                "ab",
                pool.Intern(source, 0, 2));
            Assert.AreEqual(
                "abc",
                pool.Intern(source, 0, 3));

            Assert.AreEqual(4, pool.Count);

            Assert.AreEqual(
                "",
                pool.Intern(source, 3, 0));
            Assert.AreEqual(
                "a",
                pool.Intern(source, 3, 1));
            Assert.AreEqual(
                "ab",
                pool.Intern(source, 3, 2));
            Assert.AreEqual(
                "abc",
                pool.Intern(source, 3, 3));

            Assert.AreEqual(4, pool.Count);

            Assert.AreSame(
                pool.Intern(source, 0, 3),
                pool.Intern(source, 0, 3));
            Assert.AreSame(
                pool.Intern(source, 0, 3),
                pool.Intern(source, 3, 3));

            Assert.AreNotEqual(
                pool.Intern(source, 0, 3),
                pool.Intern(source, 0, 2));
            Assert.AreNotEqual(
                pool.Intern(source, 0, 3),
                pool.Intern(source, 1, 3));

            Assert.AreEqual(5, pool.Count);
        }
Esempio n. 3
0
        public IAssetBundle Read(string filename, int filterindex)
        {
            // An sdltm file is an SQLite database.
            // Quickly check the signature will accelerate auto-detection processes.
            // (I know the following code looks silly.)
            using (var s = File.OpenRead(filename))
            {
                if (s.ReadByte() != 'S' ||
                    s.ReadByte() != 'Q' ||
                    s.ReadByte() != 'L' ||
                    s.ReadByte() != 'i' ||
                    s.ReadByte() != 't' ||
                    s.ReadByte() != 'e' ||
                    s.ReadByte() != ' ' ||
                    s.ReadByte() != 'f' ||
                    s.ReadByte() != 'o' ||
                    s.ReadByte() != 'r' ||
                    s.ReadByte() != 'm' ||
                    s.ReadByte() != 'a' ||
                    s.ReadByte() != 't' ||
                    s.ReadByte() != ' ' ||
                    s.ReadByte() != '3' ||
                    s.ReadByte() != '\0')
                {
                    return(null);
                }
            }

            IDbConnection connection = null;
            IDataReader   reader     = null;

            try
            {
                // Try to open the file.

                try
                {
                    var b = new SQLiteConnectionStringBuilder()
                    {
                        DataSource = filename
                    };
                    connection = new SQLiteConnection(b.ConnectionString);
                    connection.Open();
                }
                catch (Exception)
                {
                    return(null);
                }

                // Some sanity check.

                var version = ExecScalar(connection, @"SELECT value FROM parameters WHERE name = 'VERSION'") as string;
                if (version?.StartsWith("8.") != true)
                {
                    return(null);
                }

                var tm_min   = ExecScalarValue <int>(connection, @"SELECT min(id) FROM translation_memories");
                var tm_max   = ExecScalarValue <int>(connection, @"SELECT max(id) FROM translation_memories");
                var tm_count = tm_max - tm_min + 1;
                if (tm_count <= 0 || tm_count > 1000)
                {
                    return(null);
                }

                // Read asset metadata.
                // An asset corresponds to a memory in an sdltm file.

                var assets = new SdltmAsset[tm_count];

                reader = ExecReader(connection, @"SELECT id, name, source_language, target_language FROM translation_memories");
                while (reader.Read())
                {
                    var tmid = reader.GetInt32(0);
                    assets[tmid - tm_min] = new SdltmAsset()
                    {
                        Package    = filename,
                        Original   = reader.GetString(1),
                        SourceLang = reader.GetString(2),
                        TargetLang = reader.GetString(3),
                    };
                }
                reader.Close();
                reader.Dispose();
                reader = null;

                // Read the contents of assets (memories).

                var pool    = new StringPool();
                var matcher = new TagMatcher();

                reader = ExecReader(connection, @"SELECT translation_memory_id, id, source_segment, target_segment, creation_date, creation_user, change_date, change_user FROM translation_units");
                while (reader.Read())
                {
                    var tmid = reader.GetInt32(0);
                    var pair = new SdltmPair()
                    {
                        Id            = reader.GetInt32(1).ToString(),
                        Source        = GetInlineString(reader.GetString(2)),
                        Target        = GetInlineString(reader.GetString(3)),
                        SourceLang    = assets[tmid - tm_min].SourceLang,
                        TargetLang    = assets[tmid - tm_min].TargetLang,
                        _CreationDate = pool.Intern(reader.GetString(4)),
                        _CreationUser = pool.Intern(reader.GetString(5)),
                        _ChangeDate   = pool.Intern(reader.GetString(6)),
                        _ChangeUser   = pool.Intern(reader.GetString(7)),
                    };
                    matcher.MatchTags(pair.Source, pair.Target, reader.GetString(2), reader.GetString(3));
                    assets[tmid - tm_min]._TransPairs.Add(pair);
                }
                reader.Close();
                reader.Dispose();
                reader = null;

                // Fix the serial numbers.

                int serial = 0;
                foreach (var asset in assets)
                {
                    foreach (var pair in asset._TransPairs)
                    {
                        pair.Serial = ++serial;
                    }
                }

                // That's all.

                return(new SimpleAssetBundle(assets, ReaderManager.FriendlyFilename(filename)));
            }
            finally
            {
                reader?.Close();
                reader?.Dispose();
                connection?.Dispose();
            }
        }