Exemplo n.º 1
0
 public bool Rollback()
 {
     try
     {
         return(RollbackQuery.Step((hasRow, reader, st) => true, (object)null));
     }
     finally
     {
         RollbackQuery.Reset();
     }
 }
Exemplo n.º 2
0
 public bool Commit()
 {
     try
     {
         return(CommitQuery.Step((hasRow, reader, st) => true, (object)null));
     }
     finally
     {
         CommitQuery.Reset();
     }
 }
Exemplo n.º 3
0
 public bool Begin()
 {
     try
     {
         return(BeginQuery.Step((hasRow, reader, st) => true, (object)null));
     }
     finally
     {
         BeginQuery.Reset();
     }
 }
Exemplo n.º 4
0
 public bool BeginConcurrent()
 {
     try
     {
         // TODO use raw step
         return(BeginConcurrentQuery.Step((hasRow, reader, st) => true, (object)null));
     }
     finally
     {
         BeginQuery.Reset();
     }
 }
            public List <MetadataRecord> FindMetadata(string path)
            {
                try
                {
                    FindMetadataQuery.Bind((binder, repo) =>
                    {
                        var zData = StringToHGlobalUTF8(repo, out var len);
                        if (binder.BindText(1, zData, len) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind repoPath");
                        }
                    }, path);

                    List <MetadataRecord> result = new List <MetadataRecord>();

                    while (FindMetadataQuery.Step((hasRow, reader, _) =>
                    {
                        if (hasRow)
                        {
                            var row = MapMetadataTableRow(reader);
                            result.Add(row);
                            return(true);
                        }
                        return(false);
                    }, (object)null)
                           )
                    {
                    }

                    return(result);
                }
                finally
                {
                    FindMetadataQuery.Reset();
                }
            }
            // TODO this should return IEnumerable of all new rows that should map to repo creation event
            // For external repos server must send the entire path with ids.
            // This is called inside a txn. A use case is recursive path creation that calls the query multiple times.
            // Parent must exist exactly in this call.

            public bool InsertMetadataRow(MetadataRecord record)
            {
                try
                {
                    InsertMetadataRowQuery.Bind((binder, r) =>
                    {
                        int rc;

                        var zData = StringToHGlobalUTF8(r.PK, out var len);
                        if ((rc = binder.BindText(1, zData, len)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind PK: " + rc);
                        }

                        if ((rc = binder.BindInt64(2, r.RepoId)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind RepoId: " + rc);
                        }

                        if ((rc = binder.BindInt64(3, r.ContainerId)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind ContainerId: " + rc);
                        }

                        if ((rc = binder.BindInt64(4, r.ParentId)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind ParentId: " + rc);
                        }

                        if ((rc = binder.BindInt64(5, r.Counter)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind Counter: " + rc);
                        }

                        if ((rc = binder.BindInt64(6, r.Version)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind Version: " + rc);
                        }

                        if ((rc = binder.BindInt64(7, r.RemoteId)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind RemoteId: " + rc);
                        }

                        if ((rc = binder.BindInt64(8, r.Permissions)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind Permissions: " + rc);
                        }

                        if ((rc = binder.BindInt64(9, r.SchemaType)) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind SchemaType: " + rc);
                        }

                        if (r.Schema != null)
                        {
                            zData = StringToHGlobalUTF8(JsonSerializer.ToJsonString(r.Schema, ExcludeNullStandardResolver.Instance), out len);
                            if ((rc = binder.BindText(10, zData, len)) != 0)
                            {
                                ThrowHelper.FailFast("Cannot bind Schema: " + rc);
                            }
                        }

                        if (r.Metadata != null)
                        {
                            zData = StringToHGlobalUTF8(JsonSerializer.ToJsonString(r.Metadata, ExcludeNullStandardResolver.Instance), out len);
                            if ((rc = binder.BindText(11, zData, len)) != 0)
                            {
                                ThrowHelper.FailFast("Cannot bind Metadata: " + rc);
                            }
                        }
                    }, record);
                    try
                    {
                        return(InsertMetadataRowQuery.Step((hasRow, reader, state) =>
                        {
                            if (hasRow)
                            {
                                ThrowHelper.FailFast("InsertChunk should not return rows");
                            }

                            return reader.Changes() == 1;
                        }, (object)null));
                    }
                    catch
                    {
                        return(false);
                    }
                }
                catch (Exception ex)
                {
                    ThrowHelper.FailFast("Cannot insert chunk: " + ex);
                    return(false);
                }
                finally
                {
                    InsertMetadataRowQuery.ClearAndReset();
                }
            }
            public int IncrementCounter(string repoPath)
            {
                // TODO exception handling (e.g. SQLite busy)
                // BeginConcurrentQuery.RawStep((hasRow, reader, st) => true, (object)null, out _);
                try
                {
                    IncrementCounterQuery.Bind((binder, repo) =>
                    {
                        var zData = StringToHGlobalUTF8(repo, out var len);
                        if (binder.BindText(1, zData, len) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind repoPath");
                        }
                    }, repoPath);

                    GetExactMetadataQuery.Bind((binder, repo) =>
                    {
                        var zData = StringToHGlobalUTF8(repo, out var len);
                        if (binder.BindText(1, zData, len) != 0)
                        {
                            ThrowHelper.FailFast("Cannot bind repoPath");
                        }
                    }, repoPath);

                    if (IncrementCounterQuery.Step((hasRow, reader, _) =>
                    {
                        if (hasRow)
                        {
                            ThrowHelper.FailFast("InsertChunk should not return rows.");
                        }

                        return(reader.Changes() == 1);
                    }, (object)null))
                    {
                        return(GetExactMetadataQuery.Step((hasRow, reader, _) =>
                        {
                            if (hasRow)
                            {
                                return MapMetadataTableRow(reader).Counter;
                            }
                            return -1;
                        }, (object)null));

                        //// TODO separate query
                        //var md = GetExactMetadata(repoPath, false);
                        //return md.Counter;
                    }
                    else
                    {
                        return(-1);
                    }
                }
                finally
                {
                    // CommitQuery.RawStep((hasRow, reader, st) => true, (object)null, out _);
                    IncrementCounterQuery.ClearAndReset();
                    GetExactMetadataQuery.ClearAndReset();
                    //BeginConcurrentQuery.Reset();
                    //CommitQuery.Reset();
                }
            }