Exemplo n.º 1
0
            public (long, long) SelectTest(long a, long b)
            {
                const int aColumn = 1;

                const int bColumn = 2;

                try
                {
                    var sw = new SpinWait();

                    SelectTestQuery.Bind((binder, tuple) =>
                    {
                        int rc;
                        if ((rc = binder.BindInt64(aColumn, tuple.a)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind chunk.StreamId: " + rc);
                        }

                        if ((rc = binder.BindInt64(bColumn, tuple.b)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind chunk.FirstVersion: " + rc);
                        }
                    }, (a, b));

RETRY_TEST:
                    var rcTest = SelectTestQuery.RawStep((hasRow, reader, state) =>
                    {
                        if (hasRow)
                        {
                            unsafe
                            {
                                var ax = reader.ColumnInt64(0);
                                var bx = reader.ColumnInt64(1);
                                //var len = reader.ColumnBytes(2);
                                //var ptr = reader.ColumnBlob(2);
                                //var db = new DirectBuffer(len, (byte*)ptr);
                                //var rm = BufferPool.Retain(len, true);
                                //db.Span.CopyTo(rm.Span);
                                return(ax, bx);
                            }
                        }

                        return(default);
Exemplo n.º 2
0
            public unsafe (bool, long) InsertTest(long a, long b, RetainedMemory <byte> rawData)
            {
                const int aColumn = 1;

                const int bColumn = 2;

                const int dataColumn = 3;

                try
                {
                    var sw = new SpinWait();

                    // TODO serialize before bind because we need to release temp buffer
                    InsertTestQuery.Bind((binder, tuple) =>
                    {
                        var blobSize = tuple.rawData.Length;
                        var ptr      = (IntPtr)tuple.rawData.Pointer;
                        Debug.Assert(blobSize > 0);

                        int rc;
                        if ((rc = binder.BindInt64(aColumn, tuple.a)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind chunk.StreamId: " + rc);
                        }

                        if ((rc = binder.BindInt64(bColumn, tuple.b)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind chunk.FirstVersion: " + rc);
                        }

                        if ((rc = binder.BindBlob(dataColumn, ptr, blobSize)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind chunk blob: " + rc);
                        }
                    }, (a, b, rawData));

RETRY:
                    //  var rcBegin = BeginConcurrentQuery.RawStep((hasRow, reader, st) => true, (object)null, out _);
                    // Console.WriteLine("rcBegin: " + rcBegin);

RETRY_TEST:
                    var rcTest = InsertTestQuery.RawStep((hasRow, reader, state) =>
                    {
                        if (hasRow)
                        {
                            ThrowHelper.FailFast("InsertChunk should not return rows");
                        }

                        return(reader.Changes() == 1, reader.LastRowId());
                    }, (object)null, out var result);

                    //if (rcTest != Spreads.SQLite.Interop.Constants.SQLITE_DONE)
                    //{
                    //    Console.WriteLine("rcTest: " + rcTest);
                    //}

RETRY_COMMIT:
                    // var rcCommit = CommitQuery.RawStep((hasRow, reader, st) => true, (object)null, out _);
                    if (rcTest != Spreads.SQLite.Interop.Constants.SQLITE_DONE)
                    {
                        // Console.WriteLine(rcCommit);
                        sw.SpinOnce();
                        goto RETRY_TEST;

                        ////// Console.WriteLine(rcTest);
                        //if (rcCommit == Spreads.SQLite.Interop.Constants.SQLITE_BUSY_SNAPSHOT)
                        //{
                        //    // Console.WriteLine("SQLITE_BUSY_SNAPSHOT");
                        //}
                        //else
                        //{
                        //    //Console.WriteLine("RETRY");
                        //    sw.SpinOnce();
                        //    // Console.WriteLine(rcCommit);
                        //    goto RETRY_COMMIT;
                        //}
                        //////// Console.WriteLine("NOT DONE: " + rcTest);

                        //Rollback();

                        //BeginConcurrentQuery.Reset();
                        //TestQuery.Reset();
                        //CommitQuery.Reset();

                        //sw.SpinOnce();
                        //goto RETRY;
                    }

                    return(result);
                }
                catch (Exception ex)
                {
                    ThrowHelper.FailFast("Cannot insert chunk: " + ex);
                    return(false, 0);
                }
                finally
                {
                    InsertTestQuery.ClearAndReset();

                    //BeginConcurrentQuery.Reset();
                    //CommitQuery.Reset();
                }
            }