rubydb 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitattributes +20 -0
- data/.github/ISSUE_TEMPLATE/bug.yml +0 -0
- data/.github/ISSUE_TEMPLATE/feature.yml +0 -0
- data/.github/ISSUE_TEMPLATE/performance.yml +0 -0
- data/.github/ISSUE_TEMPLATE/security.yml +0 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.github/dependabot.yml +0 -0
- data/.github/workflows/benchmark.yml +26 -0
- data/.github/workflows/compatibility.yml +63 -0
- data/.github/workflows/fuzz.yml +33 -0
- data/.github/workflows/lint.yml +21 -0
- data/.github/workflows/operations.yml +24 -0
- data/.github/workflows/production-validation.yml +111 -0
- data/.github/workflows/release.yml +77 -0
- data/.github/workflows/security.yml +37 -0
- data/.github/workflows/test.yml +26 -0
- data/.github/workflows/workload.yml +58 -0
- data/.gitignore +43 -0
- data/.rspec +4 -0
- data/.rubocop.yml +44 -0
- data/.ruby-version +1 -0
- data/.standard.yml +14 -0
- data/ARCHITECTURE.md +21 -0
- data/CHANGELOG.md +31 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/CONTRIBUTING.md +29 -0
- data/GOVERNANCE.md +16 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +71 -0
- data/LICENSE +60 -0
- data/README.md +162 -0
- data/ROADMAP.md +27 -0
- data/Rakefile +71 -0
- data/SECURITY.md +54 -0
- data/SUPPORT.md +14 -0
- data/adapters/activerecord/Gemfile +11 -0
- data/adapters/activerecord/README.md +9 -0
- data/adapters/activerecord/lib/active_record/connection_adapters/rubydb_adapter.rb +887 -0
- data/adapters/activerecord/rubydb-activerecord.gemspec +27 -0
- data/adapters/activerecord/spec/rubydb_adapter_integration_spec.rb +151 -0
- data/adapters/ruby/README.md +18 -0
- data/adapters/sequel/README.md +11 -0
- data/bin/rubydb +19 -0
- data/bin/rubydb-server +126 -0
- data/config/development.yml +0 -0
- data/config/monitoring/prometheus-alerts.yml +39 -0
- data/config/production.yml +36 -0
- data/config/test.yml +0 -0
- data/docs/README.md +71 -0
- data/docs/architecture/concurrency.md +14 -0
- data/docs/architecture/current-state.md +125 -0
- data/docs/architecture/execution-engine.md +25 -0
- data/docs/architecture/indexes.md +19 -0
- data/docs/architecture/mvcc.md +19 -0
- data/docs/architecture/overview.md +13 -0
- data/docs/architecture/pages.md +11 -0
- data/docs/architecture/production-roadmap.md +82 -0
- data/docs/architecture/query-planner.md +20 -0
- data/docs/architecture/recovery.md +18 -0
- data/docs/architecture/sql-engine.md +12 -0
- data/docs/architecture/storage-engine.md +14 -0
- data/docs/architecture/transactions.md +10 -0
- data/docs/architecture/wal.md +28 -0
- data/docs/cli-cheatsheet.md +98 -0
- data/docs/cli.md +275 -0
- data/docs/contributing/architecture.md +9 -0
- data/docs/contributing/benchmarking.md +14 -0
- data/docs/contributing/development.md +16 -0
- data/docs/contributing/release-process.md +49 -0
- data/docs/contributing/testing.md +16 -0
- data/docs/debugging.md +229 -0
- data/docs/developer/branching.md +10 -0
- data/docs/developer/database-diff.md +10 -0
- data/docs/developer/local-development.md +17 -0
- data/docs/developer/snapshots.md +9 -0
- data/docs/developer/temporal-data.md +10 -0
- data/docs/developer-guide.md +297 -0
- data/docs/getting-started/first-database.md +16 -0
- data/docs/getting-started/first-query.md +13 -0
- data/docs/getting-started/installation.md +19 -0
- data/docs/getting-started/local-to-production.md +300 -0
- data/docs/getting-started/quickstart.md +17 -0
- data/docs/getting-started/rails.md +16 -0
- data/docs/hardening_backlog.md +93 -0
- data/docs/lessons-learned.md +112 -0
- data/docs/operations/backups.md +33 -0
- data/docs/operations/disaster-recovery.md +31 -0
- data/docs/operations/failover.md +30 -0
- data/docs/operations/monitoring.md +25 -0
- data/docs/operations/production-guide.md +295 -0
- data/docs/operations/production-runbook.md +45 -0
- data/docs/operations/replication.md +33 -0
- data/docs/operations/restore.md +6 -0
- data/docs/operations/runbook.md +34 -0
- data/docs/operations/upgrades.md +14 -0
- data/docs/operations/workload-testing.md +17 -0
- data/docs/production-readiness.md +118 -0
- data/docs/production_validation.md +150 -0
- data/docs/rails/active-record.md +11 -0
- data/docs/rails/compatibility-guide.md +90 -0
- data/docs/rails/database-yml.md +92 -0
- data/docs/rails/installation.md +17 -0
- data/docs/rails/migrations.md +17 -0
- data/docs/rails/production.md +82 -0
- data/docs/rails/troubleshooting.md +18 -0
- data/docs/release.md +59 -0
- data/docs/server/architecture.md +10 -0
- data/docs/server/authentication.md +10 -0
- data/docs/server/configuration.md +16 -0
- data/docs/server/connection-pooling.md +10 -0
- data/docs/server/deployment.md +10 -0
- data/docs/server/protocol.md +12 -0
- data/docs/sql/compatibility-guide.md +82 -0
- data/docs/sql/compatibility.md +39 -0
- data/docs/sql/data-types.md +10 -0
- data/docs/sql/functions.md +9 -0
- data/docs/sql/joins.md +9 -0
- data/docs/sql/operators.md +9 -0
- data/docs/sql/sqlite-compatibility.md +21 -0
- data/docs/sql/syntax.md +10 -0
- data/docs/sql/transactions.md +10 -0
- data/docs/troubleshooting.md +244 -0
- data/exe/rubydb +23 -0
- data/lib/rubydb/backup/archive.rb +334 -0
- data/lib/rubydb/backup/backup.rb +401 -0
- data/lib/rubydb/backup/incremental.rb +353 -0
- data/lib/rubydb/backup/restore.rb +290 -0
- data/lib/rubydb/backup/snapshot.rb +267 -0
- data/lib/rubydb/backup/verification.rb +279 -0
- data/lib/rubydb/branching/branch.rb +181 -0
- data/lib/rubydb/branching/branch_manager.rb +311 -0
- data/lib/rubydb/branching/branch_metadata.rb +140 -0
- data/lib/rubydb/branching/checkout.rb +166 -0
- data/lib/rubydb/branching/copy_on_write.rb +272 -0
- data/lib/rubydb/branching/diff.rb +138 -0
- data/lib/rubydb/branching/merge.rb +285 -0
- data/lib/rubydb/build_info.rb +15 -0
- data/lib/rubydb/catalog/catalog.rb +391 -0
- data/lib/rubydb/catalog/column.rb +112 -0
- data/lib/rubydb/catalog/constraint.rb +180 -0
- data/lib/rubydb/catalog/database.rb +184 -0
- data/lib/rubydb/catalog/index.rb +97 -0
- data/lib/rubydb/catalog/schema.rb +103 -0
- data/lib/rubydb/catalog/sequence.rb +90 -0
- data/lib/rubydb/catalog/system_catalog.rb +698 -0
- data/lib/rubydb/catalog/table.rb +178 -0
- data/lib/rubydb/catalog/trigger.rb +102 -0
- data/lib/rubydb/catalog/view.rb +66 -0
- data/lib/rubydb/cli/application.rb +163 -0
- data/lib/rubydb/cli/commands/backup.rb +81 -0
- data/lib/rubydb/cli/commands/branch.rb +72 -0
- data/lib/rubydb/cli/commands/checkout.rb +54 -0
- data/lib/rubydb/cli/commands/create.rb +58 -0
- data/lib/rubydb/cli/commands/diff.rb +77 -0
- data/lib/rubydb/cli/commands/doctor.rb +74 -0
- data/lib/rubydb/cli/commands/drop.rb +57 -0
- data/lib/rubydb/cli/commands/init.rb +102 -0
- data/lib/rubydb/cli/commands/inspect.rb +95 -0
- data/lib/rubydb/cli/commands/merge.rb +63 -0
- data/lib/rubydb/cli/commands/migrate.rb +62 -0
- data/lib/rubydb/cli/commands/restart.rb +39 -0
- data/lib/rubydb/cli/commands/restore.rb +121 -0
- data/lib/rubydb/cli/commands/shell.rb +365 -0
- data/lib/rubydb/cli/commands/snapshot.rb +79 -0
- data/lib/rubydb/cli/commands/start.rb +82 -0
- data/lib/rubydb/cli/commands/status.rb +92 -0
- data/lib/rubydb/cli/commands/stop.rb +47 -0
- data/lib/rubydb/cli/commands/vacuum.rb +58 -0
- data/lib/rubydb/cli/formatter.rb +221 -0
- data/lib/rubydb/cli/output.rb +168 -0
- data/lib/rubydb/client/client.rb +304 -0
- data/lib/rubydb/client/connection.rb +415 -0
- data/lib/rubydb/client/connection_pool.rb +168 -0
- data/lib/rubydb/client/connection_url.rb +96 -0
- data/lib/rubydb/client/prepared_statement.rb +60 -0
- data/lib/rubydb/client/result.rb +123 -0
- data/lib/rubydb/client/statement.rb +52 -0
- data/lib/rubydb/client/transaction.rb +130 -0
- data/lib/rubydb/concurrency/concurrency.rb +19 -0
- data/lib/rubydb/concurrency/deadlock_detector.rb +150 -0
- data/lib/rubydb/concurrency/latch.rb +101 -0
- data/lib/rubydb/concurrency/lock_graph.rb +165 -0
- data/lib/rubydb/concurrency/mutex.rb +183 -0
- data/lib/rubydb/concurrency/rw_lock.rb +180 -0
- data/lib/rubydb/concurrency/scheduler.rb +250 -0
- data/lib/rubydb/concurrency/worker_pool.rb +143 -0
- data/lib/rubydb/configuration/config.rb +170 -0
- data/lib/rubydb/configuration/defaults.rb +179 -0
- data/lib/rubydb/configuration/environment.rb +152 -0
- data/lib/rubydb/configuration/parser.rb +185 -0
- data/lib/rubydb/configuration/validation.rb +221 -0
- data/lib/rubydb/constants.rb +74 -0
- data/lib/rubydb/constraints/check.rb +181 -0
- data/lib/rubydb/constraints/constraint.rb +101 -0
- data/lib/rubydb/constraints/foreign_key.rb +130 -0
- data/lib/rubydb/constraints/not_null.rb +64 -0
- data/lib/rubydb/constraints/primary_key.rb +99 -0
- data/lib/rubydb/constraints/unique.rb +108 -0
- data/lib/rubydb/constraints/validator.rb +350 -0
- data/lib/rubydb/errors/authentication_error.rb +10 -0
- data/lib/rubydb/errors/authorization_error.rb +23 -0
- data/lib/rubydb/errors/client_error.rb +10 -0
- data/lib/rubydb/errors/configuration_error.rb +10 -0
- data/lib/rubydb/errors/connection_error.rb +10 -0
- data/lib/rubydb/errors/constraint_error.rb +23 -0
- data/lib/rubydb/errors/corruption_error.rb +10 -0
- data/lib/rubydb/errors/database_error.rb +10 -0
- data/lib/rubydb/errors/error.rb +20 -0
- data/lib/rubydb/errors/execution_error.rb +10 -0
- data/lib/rubydb/errors/parser_error.rb +10 -0
- data/lib/rubydb/errors/recovery_error.rb +10 -0
- data/lib/rubydb/errors/replication_error.rb +10 -0
- data/lib/rubydb/errors/server_error.rb +6 -0
- data/lib/rubydb/errors/storage_error.rb +10 -0
- data/lib/rubydb/errors/transaction_error.rb +10 -0
- data/lib/rubydb/execution/aggregate_executor.rb +138 -0
- data/lib/rubydb/execution/delete_executor.rb +112 -0
- data/lib/rubydb/execution/distinct_executor.rb +135 -0
- data/lib/rubydb/execution/executor.rb +1192 -0
- data/lib/rubydb/execution/expression.rb +193 -0
- data/lib/rubydb/execution/index_scan.rb +142 -0
- data/lib/rubydb/execution/insert_executor.rb +217 -0
- data/lib/rubydb/execution/join_executor.rb +249 -0
- data/lib/rubydb/execution/limit_executor.rb +85 -0
- data/lib/rubydb/execution/optimizer.rb +215 -0
- data/lib/rubydb/execution/plan.rb +361 -0
- data/lib/rubydb/execution/planner.rb +546 -0
- data/lib/rubydb/execution/predicate.rb +235 -0
- data/lib/rubydb/execution/scan.rb +49 -0
- data/lib/rubydb/execution/sequential_scan.rb +63 -0
- data/lib/rubydb/execution/sort_executor.rb +185 -0
- data/lib/rubydb/execution/update_executor.rb +162 -0
- data/lib/rubydb/functions/aggregate.rb +70 -0
- data/lib/rubydb/functions/date_functions.rb +278 -0
- data/lib/rubydb/functions/function.rb +85 -0
- data/lib/rubydb/functions/json_functions.rb +215 -0
- data/lib/rubydb/functions/numeric_functions.rb +346 -0
- data/lib/rubydb/functions/scalar.rb +52 -0
- data/lib/rubydb/functions/string_functions.rb +383 -0
- data/lib/rubydb/functions/system_functions.rb +246 -0
- data/lib/rubydb/history/as_of.rb +238 -0
- data/lib/rubydb/history/change.rb +105 -0
- data/lib/rubydb/history/history.rb +131 -0
- data/lib/rubydb/history/history_manager.rb +229 -0
- data/lib/rubydb/history/temporal_query.rb +202 -0
- data/lib/rubydb/history/timeline.rb +144 -0
- data/lib/rubydb/indexes/btree.rb +186 -0
- data/lib/rubydb/indexes/btree_cursor.rb +258 -0
- data/lib/rubydb/indexes/btree_node.rb +385 -0
- data/lib/rubydb/indexes/hash_index.rb +150 -0
- data/lib/rubydb/indexes/index.rb +71 -0
- data/lib/rubydb/indexes/index_manager.rb +406 -0
- data/lib/rubydb/indexes/index_scan.rb +470 -0
- data/lib/rubydb/migrations/migration.rb +254 -0
- data/lib/rubydb/migrations/migration_lock.rb +146 -0
- data/lib/rubydb/migrations/migration_manager.rb +176 -0
- data/lib/rubydb/migrations/migration_version.rb +71 -0
- data/lib/rubydb/migrations/schema_diff.rb +211 -0
- data/lib/rubydb/migrations/schema_version.rb +64 -0
- data/lib/rubydb/monitoring/events.rb +160 -0
- data/lib/rubydb/monitoring/health.rb +222 -0
- data/lib/rubydb/monitoring/logger.rb +193 -0
- data/lib/rubydb/monitoring/metrics.rb +359 -0
- data/lib/rubydb/monitoring/performance.rb +176 -0
- data/lib/rubydb/monitoring/statistics.rb +170 -0
- data/lib/rubydb/mvcc/garbage_collector.rb +199 -0
- data/lib/rubydb/mvcc/mvcc.rb +16 -0
- data/lib/rubydb/mvcc/snapshot.rb +147 -0
- data/lib/rubydb/mvcc/vacuum.rb +180 -0
- data/lib/rubydb/mvcc/version.rb +106 -0
- data/lib/rubydb/mvcc/version_store.rb +398 -0
- data/lib/rubydb/mvcc/visibility.rb +109 -0
- data/lib/rubydb/protocol/capabilities.rb +125 -0
- data/lib/rubydb/protocol/decoder.rb +145 -0
- data/lib/rubydb/protocol/encoder.rb +136 -0
- data/lib/rubydb/protocol/handshake.rb +305 -0
- data/lib/rubydb/protocol/message.rb +121 -0
- data/lib/rubydb/protocol/protocol.rb +277 -0
- data/lib/rubydb/protocol/version.rb +54 -0
- data/lib/rubydb/rails/adapter.rb +239 -0
- data/lib/rubydb/rails/connection.rb +314 -0
- data/lib/rubydb/rails/database_statements.rb +122 -0
- data/lib/rubydb/rails/migration.rb +131 -0
- data/lib/rubydb/rails/quoting.rb +109 -0
- data/lib/rubydb/rails/result.rb +120 -0
- data/lib/rubydb/rails/schema_statements.rb +339 -0
- data/lib/rubydb/rails/transaction.rb +105 -0
- data/lib/rubydb/rails/type.rb +126 -0
- data/lib/rubydb/recovery/checkpoint.rb +257 -0
- data/lib/rubydb/recovery/consistency.rb +467 -0
- data/lib/rubydb/recovery/corruption_detector.rb +5 -0
- data/lib/rubydb/recovery/crash_recovery.rb +387 -0
- data/lib/rubydb/recovery/recovery_manager.rb +206 -0
- data/lib/rubydb/recovery/redo.rb +237 -0
- data/lib/rubydb/recovery/undo.rb +206 -0
- data/lib/rubydb/replication/failover.rb +5 -0
- data/lib/rubydb/replication/fencing.rb +63 -0
- data/lib/rubydb/replication/primary.rb +450 -0
- data/lib/rubydb/replication/replica.rb +384 -0
- data/lib/rubydb/replication/replication_log.rb +200 -0
- data/lib/rubydb/replication/replication_manager.rb +308 -0
- data/lib/rubydb/replication/replication_slot.rb +295 -0
- data/lib/rubydb/replication/replication_stream.rb +201 -0
- data/lib/rubydb/rubydb.rb +560 -0
- data/lib/rubydb/security/access_control.rb +254 -0
- data/lib/rubydb/security/audit_log.rb +213 -0
- data/lib/rubydb/security/authentication.rb +302 -0
- data/lib/rubydb/security/authorization.rb +282 -0
- data/lib/rubydb/security/credentials.rb +196 -0
- data/lib/rubydb/security/password.rb +215 -0
- data/lib/rubydb/security/permissions.rb +74 -0
- data/lib/rubydb/security/role.rb +101 -0
- data/lib/rubydb/security/user.rb +86 -0
- data/lib/rubydb/server/connection.rb +366 -0
- data/lib/rubydb/server/connection_pool.rb +193 -0
- data/lib/rubydb/server/lifecycle.rb +228 -0
- data/lib/rubydb/server/listener.rb +136 -0
- data/lib/rubydb/server/request_handler.rb +276 -0
- data/lib/rubydb/server/server.rb +364 -0
- data/lib/rubydb/server/session.rb +369 -0
- data/lib/rubydb/server/worker.rb +210 -0
- data/lib/rubydb/server/worker_pool.rb +168 -0
- data/lib/rubydb/sql/ast/alter_table.rb +169 -0
- data/lib/rubydb/sql/ast/begin_transaction.rb +47 -0
- data/lib/rubydb/sql/ast/commit.rb +37 -0
- data/lib/rubydb/sql/ast/constraint.rb +83 -0
- data/lib/rubydb/sql/ast/create_database.rb +41 -0
- data/lib/rubydb/sql/ast/create_index.rb +61 -0
- data/lib/rubydb/sql/ast/create_schema.rb +52 -0
- data/lib/rubydb/sql/ast/create_table.rb +187 -0
- data/lib/rubydb/sql/ast/delete.rb +54 -0
- data/lib/rubydb/sql/ast/drop_database.rb +41 -0
- data/lib/rubydb/sql/ast/drop_index.rb +41 -0
- data/lib/rubydb/sql/ast/drop_schema.rb +49 -0
- data/lib/rubydb/sql/ast/drop_table.rb +49 -0
- data/lib/rubydb/sql/ast/explain.rb +64 -0
- data/lib/rubydb/sql/ast/expression.rb +604 -0
- data/lib/rubydb/sql/ast/insert.rb +82 -0
- data/lib/rubydb/sql/ast/node.rb +42 -0
- data/lib/rubydb/sql/ast/rollback.rb +63 -0
- data/lib/rubydb/sql/ast/savepoint.rb +59 -0
- data/lib/rubydb/sql/ast/select.rb +88 -0
- data/lib/rubydb/sql/ast/set_operation.rb +20 -0
- data/lib/rubydb/sql/ast/trigger.rb +29 -0
- data/lib/rubydb/sql/ast/update.rb +88 -0
- data/lib/rubydb/sql/ast/vacuum.rb +19 -0
- data/lib/rubydb/sql/ast/view.rb +32 -0
- data/lib/rubydb/sql/ast/with.rb +32 -0
- data/lib/rubydb/sql/grammar.rb +86 -0
- data/lib/rubydb/sql/keywords.rb +156 -0
- data/lib/rubydb/sql/lexer.rb +214 -0
- data/lib/rubydb/sql/operators.rb +100 -0
- data/lib/rubydb/sql/parser.rb +1185 -0
- data/lib/rubydb/sql/planner/analyzer.rb +302 -0
- data/lib/rubydb/sql/planner/binder.rb +550 -0
- data/lib/rubydb/sql/planner/type_checker.rb +431 -0
- data/lib/rubydb/sql/token.rb +210 -0
- data/lib/rubydb/storage/buffer_frame.rb +44 -0
- data/lib/rubydb/storage/buffer_pool.rb +155 -0
- data/lib/rubydb/storage/database_lock.rb +74 -0
- data/lib/rubydb/storage/deserializer.rb +342 -0
- data/lib/rubydb/storage/engine.rb +2338 -0
- data/lib/rubydb/storage/file_manager.rb +187 -0
- data/lib/rubydb/storage/free_space_map.rb +81 -0
- data/lib/rubydb/storage/page.rb +94 -0
- data/lib/rubydb/storage/page_allocator.rb +855 -0
- data/lib/rubydb/storage/page_header.rb +67 -0
- data/lib/rubydb/storage/page_manager.rb +131 -0
- data/lib/rubydb/storage/record.rb +58 -0
- data/lib/rubydb/storage/row.rb +78 -0
- data/lib/rubydb/storage/serializer.rb +51 -0
- data/lib/rubydb/storage/storage_layout.rb +151 -0
- data/lib/rubydb/storage/storage_manager.rb +114 -0
- data/lib/rubydb/storage/tuple.rb +461 -0
- data/lib/rubydb/storage/visibility_map.rb +984 -0
- data/lib/rubydb/transactions/commit_manager.rb +220 -0
- data/lib/rubydb/transactions/isolation.rb +98 -0
- data/lib/rubydb/transactions/lock.rb +76 -0
- data/lib/rubydb/transactions/lock_manager.rb +362 -0
- data/lib/rubydb/transactions/savepoint.rb +143 -0
- data/lib/rubydb/transactions/transaction.rb +215 -0
- data/lib/rubydb/transactions/transaction_id.rb +84 -0
- data/lib/rubydb/transactions/transaction_log.rb +257 -0
- data/lib/rubydb/transactions/transaction_manager.rb +435 -0
- data/lib/rubydb/types/bigint.rb +36 -0
- data/lib/rubydb/types/blob.rb +37 -0
- data/lib/rubydb/types/boolean.rb +34 -0
- data/lib/rubydb/types/date.rb +39 -0
- data/lib/rubydb/types/decimal.rb +48 -0
- data/lib/rubydb/types/float.rb +34 -0
- data/lib/rubydb/types/integer.rb +36 -0
- data/lib/rubydb/types/json.rb +41 -0
- data/lib/rubydb/types/null.rb +34 -0
- data/lib/rubydb/types/smallint.rb +36 -0
- data/lib/rubydb/types/text.rb +37 -0
- data/lib/rubydb/types/time.rb +46 -0
- data/lib/rubydb/types/timestamp.rb +39 -0
- data/lib/rubydb/types/type.rb +119 -0
- data/lib/rubydb/types/uuid.rb +47 -0
- data/lib/rubydb/types/varchar.rb +37 -0
- data/lib/rubydb/version.rb +33 -0
- data/lib/rubydb/wal/archive.rb +193 -0
- data/lib/rubydb/wal/checkpoint.rb +183 -0
- data/lib/rubydb/wal/lsn.rb +94 -0
- data/lib/rubydb/wal/reader.rb +260 -0
- data/lib/rubydb/wal/record.rb +105 -0
- data/lib/rubydb/wal/segment.rb +193 -0
- data/lib/rubydb/wal/wal.rb +452 -0
- data/lib/rubydb/wal/writer.rb +236 -0
- data/lib/rubydb.rb +7 -0
- data/packaging/docker/Dockerfile +36 -0
- data/packaging/docker/docker-compose.failover.yml +43 -0
- data/packaging/docker/entrypoint.sh +15 -0
- data/packaging/homebrew/rubydb.rb +19 -0
- data/packaging/systemd/rubydb.service +22 -0
- data/rubydb.gemspec +57 -0
- data/scripts/benchmark +7 -0
- data/scripts/durability_drill +37 -0
- data/scripts/format +0 -0
- data/scripts/fuzz +63 -0
- data/scripts/lint +0 -0
- data/scripts/release +50 -0
- data/scripts/release_check +43 -0
- data/scripts/replication_failover_drill +250 -0
- data/scripts/replication_network_failover_drill +255 -0
- data/scripts/restore_drill +45 -0
- data/scripts/security +0 -0
- data/scripts/setup +0 -0
- data/scripts/test +0 -0
- data/scripts/test-chaos +0 -0
- data/scripts/test-crash +0 -0
- data/scripts/test-integration +0 -0
- data/scripts/test-unit +0 -0
- data/visibility_map.json +1 -0
- metadata +538 -0
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Branching
|
|
5
|
+
# Checkout - Handles branch checkout operations
|
|
6
|
+
class Checkout
|
|
7
|
+
attr_reader :stats
|
|
8
|
+
|
|
9
|
+
def initialize(engine, branch_manager, config = {})
|
|
10
|
+
@engine = engine
|
|
11
|
+
@branch_manager = branch_manager
|
|
12
|
+
@config = config
|
|
13
|
+
@checkout_history = []
|
|
14
|
+
@stats = {
|
|
15
|
+
checkouts: 0,
|
|
16
|
+
successful_checkouts: 0,
|
|
17
|
+
failed_checkouts: 0,
|
|
18
|
+
checkout_time_ms: 0,
|
|
19
|
+
avg_checkout_time_ms: 0,
|
|
20
|
+
last_checkout: nil
|
|
21
|
+
}
|
|
22
|
+
@lock = Mutex.new
|
|
23
|
+
@max_history = config[:max_history] || 100
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def checkout(branch_name, options = {})
|
|
27
|
+
@lock.synchronize do
|
|
28
|
+
start_time = Time.now
|
|
29
|
+
@stats[:checkouts] += 1
|
|
30
|
+
|
|
31
|
+
# Validate branch exists
|
|
32
|
+
branch = @branch_manager.get_branch(branch_name)
|
|
33
|
+
unless branch
|
|
34
|
+
@stats[:failed_checkouts] += 1
|
|
35
|
+
return { success: false, error: "Branch '#{branch_name}' not found" }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Check if branch can be checked out
|
|
39
|
+
if branch.locked? && !options[:force]
|
|
40
|
+
@stats[:failed_checkouts] += 1
|
|
41
|
+
return { success: false, error: "Branch '#{branch_name}' is locked" }
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
begin
|
|
45
|
+
# Perform checkout
|
|
46
|
+
result = perform_checkout(branch, options)
|
|
47
|
+
|
|
48
|
+
if result[:success]
|
|
49
|
+
# Update branch manager
|
|
50
|
+
@branch_manager.checkout(branch_name)
|
|
51
|
+
|
|
52
|
+
# Record history
|
|
53
|
+
record_checkout(branch_name, options)
|
|
54
|
+
|
|
55
|
+
@stats[:successful_checkouts] += 1
|
|
56
|
+
@stats[:last_checkout] = Time.now
|
|
57
|
+
|
|
58
|
+
elapsed_ms = (Time.now - start_time) * 1000
|
|
59
|
+
@stats[:checkout_time_ms] += elapsed_ms
|
|
60
|
+
@stats[:avg_checkout_time_ms] = @stats[:checkout_time_ms] / @stats[:successful_checkouts]
|
|
61
|
+
else
|
|
62
|
+
@stats[:failed_checkouts] += 1
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
result.merge(elapsed_ms: elapsed_ms)
|
|
66
|
+
|
|
67
|
+
rescue => e
|
|
68
|
+
@stats[:failed_checkouts] += 1
|
|
69
|
+
{ success: false, error: e.message }
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def checkout_or_create(branch_name, options = {})
|
|
75
|
+
@lock.synchronize do
|
|
76
|
+
branch = @branch_manager.get_branch(branch_name)
|
|
77
|
+
|
|
78
|
+
if branch
|
|
79
|
+
checkout(branch_name, options)
|
|
80
|
+
else
|
|
81
|
+
# Create branch then checkout
|
|
82
|
+
result = @branch_manager.create_branch(branch_name, options)
|
|
83
|
+
if result[:success]
|
|
84
|
+
checkout(branch_name, options)
|
|
85
|
+
else
|
|
86
|
+
result
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def checkout_latest(options = {})
|
|
93
|
+
@lock.synchronize do
|
|
94
|
+
# Find latest branch by creation time
|
|
95
|
+
branches = @branch_manager.list_branches
|
|
96
|
+
return { success: false, error: "No branches available" } if branches.empty?
|
|
97
|
+
|
|
98
|
+
latest = branches.max_by { |b| b[:created_at] }
|
|
99
|
+
checkout(latest[:name], options)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def checkout_history
|
|
104
|
+
@checkout_history.dup
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def stats
|
|
108
|
+
@lock.synchronize do
|
|
109
|
+
@stats.merge({
|
|
110
|
+
history_size: @checkout_history.size,
|
|
111
|
+
max_history: @max_history
|
|
112
|
+
})
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
def perform_checkout(branch, options)
|
|
119
|
+
branch_name = branch.name
|
|
120
|
+
|
|
121
|
+
# Get branch data
|
|
122
|
+
branch_data = get_branch_data(branch_name)
|
|
123
|
+
|
|
124
|
+
# Apply branch state to engine
|
|
125
|
+
apply_branch_state(branch_data)
|
|
126
|
+
|
|
127
|
+
# Update engine to branch head
|
|
128
|
+
@engine.apply_branch_head(branch.head_lsn) if branch.head_lsn
|
|
129
|
+
|
|
130
|
+
{ success: true, branch_name: branch_name, head_lsn: branch.head_lsn }
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def get_branch_data(branch_name)
|
|
134
|
+
branch = @branch_manager.get_branch(branch_name)
|
|
135
|
+
raise ArgumentError, "Branch '#{branch_name}' not found" unless branch
|
|
136
|
+
snapshot = branch.respond_to?(:state_snapshot) ? branch.state_snapshot : nil
|
|
137
|
+
changes = branch.respond_to?(:logical_changes) ? branch.logical_changes : branch.changes
|
|
138
|
+
{ base: snapshot, changes: changes }
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def apply_branch_state(branch_data)
|
|
142
|
+
unless @engine.respond_to?(:apply_branch_state)
|
|
143
|
+
raise ArgumentError, "Branch checkout requires an engine state-application hook"
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
result = @engine.apply_branch_state(branch_data)
|
|
147
|
+
raise ArgumentError, "Engine rejected branch state" if result == false
|
|
148
|
+
result
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def record_checkout(branch_name, options)
|
|
152
|
+
entry = {
|
|
153
|
+
branch_name: branch_name,
|
|
154
|
+
timestamp: Time.now.iso8601,
|
|
155
|
+
options: options
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
@checkout_history << entry
|
|
159
|
+
|
|
160
|
+
if @checkout_history.size > @max_history
|
|
161
|
+
@checkout_history.shift
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require "pathname"
|
|
6
|
+
|
|
7
|
+
module RubyDB
|
|
8
|
+
module Branching
|
|
9
|
+
# CopyOnWrite - Manages copy-on-write for branches
|
|
10
|
+
class CopyOnWrite
|
|
11
|
+
attr_reader :stats
|
|
12
|
+
|
|
13
|
+
def initialize(engine, config = {})
|
|
14
|
+
@engine = engine
|
|
15
|
+
@config = config
|
|
16
|
+
@cow_dir = config[:cow_dir] || "cow"
|
|
17
|
+
@branch_data = {}
|
|
18
|
+
@stats = {
|
|
19
|
+
cow_files_created: 0,
|
|
20
|
+
cow_files_accessed: 0,
|
|
21
|
+
cow_files_updated: 0,
|
|
22
|
+
cow_files_deleted: 0,
|
|
23
|
+
total_cow_size: 0
|
|
24
|
+
}
|
|
25
|
+
@lock = Mutex.new
|
|
26
|
+
|
|
27
|
+
FileUtils.mkdir_p(@cow_dir)
|
|
28
|
+
load_cow_data
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def create_cow(branch_name, base_path)
|
|
32
|
+
@lock.synchronize do
|
|
33
|
+
cow_path = branch_path(branch_name)
|
|
34
|
+
return { success: false, error: "Invalid branch name" } unless cow_path
|
|
35
|
+
base_root = File.expand_path(base_path)
|
|
36
|
+
return { success: false, error: "Base path does not exist" } unless Dir.exist?(base_root)
|
|
37
|
+
FileUtils.mkdir_p(cow_path)
|
|
38
|
+
|
|
39
|
+
# Create COW for each file
|
|
40
|
+
files = Dir.glob(File.join(base_root, "**/*")).select { |file| File.file?(file) }
|
|
41
|
+
files.each do |file|
|
|
42
|
+
relative_path = Pathname.new(file).relative_path_from(Pathname.new(base_root)).to_s
|
|
43
|
+
cow_file = nested_path(cow_path, relative_path)
|
|
44
|
+
raise "Invalid source path for copy-on-write" unless cow_file
|
|
45
|
+
FileUtils.mkdir_p(File.dirname(cow_file))
|
|
46
|
+
|
|
47
|
+
# Create hard link or copy
|
|
48
|
+
begin
|
|
49
|
+
File.link(file, cow_file)
|
|
50
|
+
rescue
|
|
51
|
+
FileUtils.cp(file, cow_file)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
@stats[:cow_files_created] += 1
|
|
55
|
+
@stats[:total_cow_size] += File.size(cow_file)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
@branch_data[branch_name] = {
|
|
59
|
+
base_path: base_root,
|
|
60
|
+
cow_path: cow_path,
|
|
61
|
+
created_at: Time.now.iso8601,
|
|
62
|
+
files: files.map { |file| Pathname.new(file).relative_path_from(Pathname.new(base_root)).to_s }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
save_cow_data
|
|
66
|
+
|
|
67
|
+
{ success: true, cow_path: cow_path, file_count: files.size }
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def read_file(branch_name, file_path)
|
|
72
|
+
@lock.synchronize do
|
|
73
|
+
cow_path = branch_path(branch_name)
|
|
74
|
+
cow_file = cow_path && nested_path(cow_path, file_path)
|
|
75
|
+
return nil unless cow_file
|
|
76
|
+
|
|
77
|
+
if File.exist?(cow_file)
|
|
78
|
+
@stats[:cow_files_accessed] += 1
|
|
79
|
+
return File.read(cow_file)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Read from base
|
|
83
|
+
branch_data = @branch_data[branch_name]
|
|
84
|
+
return nil unless branch_data
|
|
85
|
+
|
|
86
|
+
base_file = nested_path(branch_data[:base_path], file_path)
|
|
87
|
+
return nil unless File.exist?(base_file)
|
|
88
|
+
|
|
89
|
+
File.read(base_file)
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def write_file(branch_name, file_path, content)
|
|
94
|
+
@lock.synchronize do
|
|
95
|
+
cow_path = branch_path(branch_name)
|
|
96
|
+
cow_file = cow_path && nested_path(cow_path, file_path)
|
|
97
|
+
return { success: false, error: "Invalid branch name or file path" } unless cow_file
|
|
98
|
+
FileUtils.mkdir_p(File.dirname(cow_file))
|
|
99
|
+
|
|
100
|
+
File.write(cow_file, content)
|
|
101
|
+
@stats[:cow_files_updated] += 1
|
|
102
|
+
|
|
103
|
+
# Update branch data
|
|
104
|
+
@branch_data[branch_name] ||= { files: [] }
|
|
105
|
+
unless @branch_data[branch_name][:files].include?(file_path)
|
|
106
|
+
@branch_data[branch_name][:files] << file_path
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
save_cow_data
|
|
110
|
+
|
|
111
|
+
{ success: true, path: cow_file }
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def delete_file(branch_name, file_path)
|
|
116
|
+
@lock.synchronize do
|
|
117
|
+
cow_path = branch_path(branch_name)
|
|
118
|
+
cow_file = cow_path && nested_path(cow_path, file_path)
|
|
119
|
+
return { success: false, error: "Invalid branch name or file path" } unless cow_file
|
|
120
|
+
|
|
121
|
+
if File.exist?(cow_file)
|
|
122
|
+
File.delete(cow_file)
|
|
123
|
+
@stats[:cow_files_deleted] += 1
|
|
124
|
+
|
|
125
|
+
# Update branch data
|
|
126
|
+
if @branch_data[branch_name]
|
|
127
|
+
@branch_data[branch_name][:files].delete(file_path)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
save_cow_data
|
|
131
|
+
return { success: true }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
{ success: false, error: "File not found in COW" }
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def list_cow_files(branch_name)
|
|
139
|
+
@lock.synchronize do
|
|
140
|
+
cow_path = branch_path(branch_name)
|
|
141
|
+
return [] unless cow_path
|
|
142
|
+
return [] unless Dir.exist?(cow_path)
|
|
143
|
+
|
|
144
|
+
Dir.glob(File.join(cow_path, "**/*")).select do |f|
|
|
145
|
+
File.file?(f)
|
|
146
|
+
end.map { |f| f.sub(cow_path + "/", "") }
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def delete_branch_cow(branch_name)
|
|
151
|
+
@lock.synchronize do
|
|
152
|
+
cow_path = branch_path(branch_name)
|
|
153
|
+
return { success: false, error: "Invalid branch name" } unless cow_path
|
|
154
|
+
if Dir.exist?(cow_path)
|
|
155
|
+
size = calculate_dir_size(cow_path)
|
|
156
|
+
FileUtils.rm_rf(cow_path)
|
|
157
|
+
@stats[:total_cow_size] -= size
|
|
158
|
+
@stats[:cow_files_deleted] += 1
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
@branch_data.delete(branch_name)
|
|
162
|
+
save_cow_data
|
|
163
|
+
|
|
164
|
+
{ success: true }
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def snapshot(branch_name, snapshot_name)
|
|
169
|
+
@lock.synchronize do
|
|
170
|
+
cow_path = branch_path(branch_name)
|
|
171
|
+
return { success: false, error: "Invalid branch name" } unless cow_path
|
|
172
|
+
return { success: false, error: "Branch COW not found" } unless Dir.exist?(cow_path)
|
|
173
|
+
|
|
174
|
+
snapshot_path = snapshot_path(snapshot_name)
|
|
175
|
+
return { success: false, error: "Invalid snapshot name" } unless snapshot_path
|
|
176
|
+
FileUtils.mkdir_p(File.dirname(snapshot_path))
|
|
177
|
+
|
|
178
|
+
# Copy all COW files
|
|
179
|
+
FileUtils.cp_r(cow_path, snapshot_path)
|
|
180
|
+
|
|
181
|
+
{ success: true, snapshot_path: snapshot_path }
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def restore_snapshot(snapshot_name, branch_name)
|
|
186
|
+
@lock.synchronize do
|
|
187
|
+
snapshot_path = snapshot_path(snapshot_name)
|
|
188
|
+
return { success: false, error: "Invalid snapshot name" } unless snapshot_path
|
|
189
|
+
return { success: false, error: "Snapshot not found" } unless Dir.exist?(snapshot_path)
|
|
190
|
+
|
|
191
|
+
cow_path = branch_path(branch_name)
|
|
192
|
+
return { success: false, error: "Invalid branch name" } unless cow_path
|
|
193
|
+
FileUtils.rm_rf(cow_path) if Dir.exist?(cow_path)
|
|
194
|
+
FileUtils.cp_r(snapshot_path, cow_path)
|
|
195
|
+
|
|
196
|
+
{ success: true }
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def stats
|
|
201
|
+
@lock.synchronize do
|
|
202
|
+
@stats.merge({
|
|
203
|
+
branches: @branch_data.size,
|
|
204
|
+
cow_dir: @cow_dir,
|
|
205
|
+
snapshots: Dir.glob(File.join(@cow_dir, "snapshots", "*")).size
|
|
206
|
+
})
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
private
|
|
211
|
+
|
|
212
|
+
def branch_path(branch_name)
|
|
213
|
+
name = branch_name.to_s
|
|
214
|
+
return nil unless safe_name?(name)
|
|
215
|
+
|
|
216
|
+
nested_path(@cow_dir, name)
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def snapshot_path(snapshot_name)
|
|
220
|
+
name = snapshot_name.to_s
|
|
221
|
+
return nil unless safe_name?(name)
|
|
222
|
+
|
|
223
|
+
nested_path(File.join(@cow_dir, "snapshots"), name)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def safe_name?(name)
|
|
227
|
+
!name.empty? && name == File.basename(name) && !Pathname.new(name).absolute?
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def nested_path(root_path, relative_path)
|
|
231
|
+
relative = relative_path.to_s
|
|
232
|
+
return nil if relative.empty? || Pathname.new(relative).absolute?
|
|
233
|
+
|
|
234
|
+
root = File.expand_path(root_path)
|
|
235
|
+
path = File.expand_path(File.join(root, relative))
|
|
236
|
+
path.start_with?("#{root}#{File::SEPARATOR}") ? path : nil
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def load_cow_data
|
|
240
|
+
data_file = File.join(@cow_dir, "cow_data.json")
|
|
241
|
+
return unless File.exist?(data_file)
|
|
242
|
+
|
|
243
|
+
begin
|
|
244
|
+
data = JSON.parse(File.read(data_file), symbolize_names: true)
|
|
245
|
+
@branch_data = data[:branch_data] || {}
|
|
246
|
+
@stats = data[:stats] || @stats
|
|
247
|
+
rescue
|
|
248
|
+
@branch_data = {}
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def save_cow_data
|
|
253
|
+
data = {
|
|
254
|
+
branch_data: @branch_data,
|
|
255
|
+
stats: @stats,
|
|
256
|
+
updated_at: Time.now.iso8601
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
data_file = File.join(@cow_dir, "cow_data.json")
|
|
260
|
+
temp_file = "#{data_file}.tmp"
|
|
261
|
+
File.write(temp_file, JSON.generate(data))
|
|
262
|
+
File.rename(temp_file, data_file)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
def calculate_dir_size(path)
|
|
266
|
+
Dir.glob(File.join(path, "**/*")).sum do |f|
|
|
267
|
+
File.size(f) if File.file?(f)
|
|
268
|
+
end || 0
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Branching
|
|
5
|
+
# Diff - Compares branches
|
|
6
|
+
class Diff
|
|
7
|
+
attr_reader :stats
|
|
8
|
+
|
|
9
|
+
# Diff types
|
|
10
|
+
DIFF_TABLE = :table
|
|
11
|
+
DIFF_SCHEMA = :schema
|
|
12
|
+
DIFF_DATA = :data
|
|
13
|
+
DIFF_ALL = :all
|
|
14
|
+
|
|
15
|
+
def initialize(engine, branch_manager, config = {})
|
|
16
|
+
@engine = engine
|
|
17
|
+
@branch_manager = branch_manager
|
|
18
|
+
@config = config
|
|
19
|
+
@stats = {
|
|
20
|
+
diffs: 0,
|
|
21
|
+
diff_time_ms: 0,
|
|
22
|
+
avg_diff_time_ms: 0,
|
|
23
|
+
tables_diffed: 0,
|
|
24
|
+
rows_diffed: 0
|
|
25
|
+
}
|
|
26
|
+
@lock = Mutex.new
|
|
27
|
+
@diff_cache = {}
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def diff(branch_a, branch_b, options = {})
|
|
31
|
+
@lock.synchronize do
|
|
32
|
+
start_time = Time.now
|
|
33
|
+
@stats[:diffs] += 1
|
|
34
|
+
|
|
35
|
+
a = @branch_manager.get_branch(branch_a)
|
|
36
|
+
b = @branch_manager.get_branch(branch_b)
|
|
37
|
+
|
|
38
|
+
unless a
|
|
39
|
+
return { success: false, error: "Branch '#{branch_a}' not found" }
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
unless b
|
|
43
|
+
return { success: false, error: "Branch '#{branch_b}' not found" }
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
diff_type = options[:type] || DIFF_ALL
|
|
47
|
+
tables = options[:tables]
|
|
48
|
+
|
|
49
|
+
result = case diff_type
|
|
50
|
+
when DIFF_TABLE
|
|
51
|
+
diff_tables(a, b, tables)
|
|
52
|
+
when DIFF_SCHEMA
|
|
53
|
+
diff_schemas(a, b, tables)
|
|
54
|
+
when DIFF_DATA
|
|
55
|
+
diff_data(a, b, tables)
|
|
56
|
+
else
|
|
57
|
+
diff_all(a, b, tables)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
elapsed_ms = (Time.now - start_time) * 1000
|
|
61
|
+
@stats[:diff_time_ms] += elapsed_ms
|
|
62
|
+
@stats[:avg_diff_time_ms] = @stats[:diff_time_ms] / @stats[:diffs]
|
|
63
|
+
@stats[:tables_diffed] += result[:tables]&.size || 0
|
|
64
|
+
@stats[:rows_diffed] += result[:rows]&.size || 0
|
|
65
|
+
|
|
66
|
+
result.merge(elapsed_ms: elapsed_ms)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def diff_tables(branch_a, branch_b, tables = nil)
|
|
71
|
+
compare_changes(branch_a, branch_b, tables, :tables)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def diff_schemas(branch_a, branch_b, tables = nil)
|
|
75
|
+
compare_changes(branch_a, branch_b, tables, :schemas)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def diff_data(branch_a, branch_b, tables = nil)
|
|
79
|
+
compare_changes(branch_a, branch_b, tables, :data)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def diff_all(branch_a, branch_b, tables = nil)
|
|
83
|
+
compare_changes(branch_a, branch_b, tables, :all)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def diff_table(branch_a, branch_b, table)
|
|
87
|
+
@lock.synchronize do
|
|
88
|
+
cache_key = "#{branch_a}_#{branch_b}_#{table}"
|
|
89
|
+
if @diff_cache.key?(cache_key)
|
|
90
|
+
return @diff_cache[cache_key]
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
result = perform_table_diff(branch_a, branch_b, table)
|
|
94
|
+
@diff_cache[cache_key] = result if result[:success]
|
|
95
|
+
|
|
96
|
+
result
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def clear_cache
|
|
101
|
+
@lock.synchronize do
|
|
102
|
+
@diff_cache.clear
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def stats
|
|
107
|
+
@lock.synchronize do
|
|
108
|
+
@stats.merge({
|
|
109
|
+
cache_size: @diff_cache.size
|
|
110
|
+
})
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def perform_table_diff(branch_a, branch_b, table)
|
|
117
|
+
compare_changes(branch_a, branch_b, [table], :table).merge(table: table)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def compare_changes(branch_a, branch_b, tables, type)
|
|
121
|
+
a = branch_a.respond_to?(:logical_changes) ? branch_a : @branch_manager.get_branch(branch_a)
|
|
122
|
+
b = branch_b.respond_to?(:logical_changes) ? branch_b : @branch_manager.get_branch(branch_b)
|
|
123
|
+
return { success: false, error: "Branch not found: #{branch_a.inspect}, #{branch_b.inspect}" } unless a && b
|
|
124
|
+
|
|
125
|
+
filter = tables && Array(tables).map(&:to_s)
|
|
126
|
+
changes_a = a.respond_to?(:logical_changes) ? a.logical_changes : a.changes
|
|
127
|
+
changes_b = b.respond_to?(:logical_changes) ? b.logical_changes : b.changes
|
|
128
|
+
changes_a = changes_a.select { |change| filter.include?(change[:table].to_s) } if filter
|
|
129
|
+
changes_b = changes_b.select { |change| filter.include?(change[:table].to_s) } if filter
|
|
130
|
+
added = changes_b.reject { |change| changes_a.include?(change) }
|
|
131
|
+
removed = changes_a.reject { |change| changes_b.include?(change) }
|
|
132
|
+
{ success: true, type: type, tables: (changes_a + changes_b).map { |c| c[:table] }.compact.uniq,
|
|
133
|
+
rows: { added: added, removed: removed }, changes: { added: added, removed: removed } }
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|