Exemplo n.º 1
0
        static void SingleTest()
        {
            var clock = new Stopwatch();
            var path = Path.GetFullPath("test.db");
            try
            {
                var buf = new byte[sizeof(long)];
                TransactionId tx;
                tmpbuf = Encoding.ASCII.GetBytes(TXT);
                using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite, 4 * 4096, false))
                {
                    // log header
                    fs.Write(buf, 0, sizeof(int));
                    fs.Write(buf, 0, sizeof(int));
                    fs.Write(buf, 0, sizeof(int));
                    fs.Write(buf, 0, sizeof(int));
                    fs.Write(buf, 0, sizeof(int));
                    fs.Write(buf, 0, sizeof(int));

                    clock.Start();
                    for (int i = 0; i < 3 * ITER; ++i)
                    {
                        // begin FileLog.Append
                        Monitor.Enter(fs);
                        tx = log.First;
                        var file = new BoundedStream(fs, fs.Length, int.MaxValue);
                        try
                        {
                            var start = file.Position;

                            // end FileLog.Append
                            file.Write(tmpbuf, 0, tmpbuf.Length);

                            // begin FileLog.Append.Dispose
                            var length = file.Length - start;
                            if (length > 0)
                            {
                                if (file.Position != file.Length)
                                    file.Seek(0, SeekOrigin.End);
                                buf.Write(length);
                                file.Write(buf, 0, sizeof(int));
                                file.Flush();
                                var pos = file.Position;
                                file.Seek(16, SeekOrigin.Begin);
                                buf.Write(pos);
                                file.Write(buf, 0, sizeof(long));
                                file.Flush();
                                file.Seek(0, SeekOrigin.End);
                            }
                        }
                        finally
                        {
                            Monitor.Exit(fs);
                        }
                        //end FileLog.Appender.Dispose
                    }
                    clock.Stop();
                    Console.WriteLine("Single size: {0} kB", fs.Length);
                }
                PrintStats("Single", clock.ElapsedMilliseconds);
            }
            finally
            {
                File.Delete(path);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Atomically append data to the durable store.
 /// </summary>
 /// <param name="transaction">The transaction being written.</param>
 /// <param name="transaction">The transaction being written.</param>
 /// <returns>A stream for writing.</returns>
 /// <remarks>
 /// The <paramref name="async"/> parameter is largely optional, in that it's safe to simply
 /// provide 'false' and everything will still work.
 /// </remarks>
 public IDisposable Append(out Stream output, out TransactionId transaction)
 {
     var x = writer;
     if (x == null) throw new ObjectDisposedException(string.Format("FileLog ({0}) has been disposed.", path));
     Monitor.Enter(x);
     if (writer == null) throw new ObjectDisposedException(string.Format("FileLog ({0}) has been disposed.", path));
     transaction = new TransactionId(next, path);
     x.Seek(next, SeekOrigin.Begin);
     output = new BoundedStream(x, next, int.MaxValue);
     return new Appender(this, writeBuffer);
 }