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,155 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Storage
|
|
5
|
+
# BufferPool - Caches pages in memory
|
|
6
|
+
class BufferPool
|
|
7
|
+
attr_reader :size, :hit_count, :miss_count
|
|
8
|
+
|
|
9
|
+
def initialize(size = Constants::DEFAULT_BUFFER_POOL_SIZE)
|
|
10
|
+
@size = size
|
|
11
|
+
@frames = {}
|
|
12
|
+
@lru_list = []
|
|
13
|
+
@hit_count = 0
|
|
14
|
+
@miss_count = 0
|
|
15
|
+
@lock = Mutex.new
|
|
16
|
+
@page_manager = nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def set_page_manager(page_manager)
|
|
20
|
+
@page_manager = page_manager
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def get_page(page_number)
|
|
24
|
+
@lock.synchronize do
|
|
25
|
+
if @frames.key?(page_number)
|
|
26
|
+
@hit_count += 1
|
|
27
|
+
touch_page(page_number)
|
|
28
|
+
return @frames[page_number]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@miss_count += 1
|
|
32
|
+
|
|
33
|
+
# Evict if necessary
|
|
34
|
+
if @frames.size >= @size
|
|
35
|
+
evict_page
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Load from disk
|
|
39
|
+
page = @page_manager.get_page(page_number)
|
|
40
|
+
frame = BufferFrame.new(page)
|
|
41
|
+
|
|
42
|
+
@frames[page_number] = frame
|
|
43
|
+
@lru_list.unshift(page_number)
|
|
44
|
+
|
|
45
|
+
frame
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def write_page(page)
|
|
50
|
+
@lock.synchronize do
|
|
51
|
+
if @frames.key?(page.page_number)
|
|
52
|
+
frame = @frames[page.page_number]
|
|
53
|
+
frame.dirty = true
|
|
54
|
+
frame.page = page
|
|
55
|
+
touch_page(page.page_number)
|
|
56
|
+
else
|
|
57
|
+
# Write directly to disk
|
|
58
|
+
@page_manager.write_page(page)
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def flush_all
|
|
64
|
+
@lock.synchronize do
|
|
65
|
+
@frames.each do |page_number, frame|
|
|
66
|
+
if frame.dirty
|
|
67
|
+
@page_manager.write_page(frame.page)
|
|
68
|
+
frame.dirty = false
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def flush_page(page_number)
|
|
75
|
+
@lock.synchronize do
|
|
76
|
+
if @frames.key?(page_number) && @frames[page_number].dirty
|
|
77
|
+
@page_manager.write_page(@frames[page_number].page)
|
|
78
|
+
@frames[page_number].dirty = false
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def remove_page(page_number)
|
|
84
|
+
@lock.synchronize do
|
|
85
|
+
@frames.delete(page_number)
|
|
86
|
+
@lru_list.delete(page_number)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def clear
|
|
91
|
+
@lock.synchronize do
|
|
92
|
+
flush_all
|
|
93
|
+
@frames.clear
|
|
94
|
+
@lru_list.clear
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def size
|
|
99
|
+
@frames.size
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def hit_rate
|
|
103
|
+
total = @hit_count + @miss_count
|
|
104
|
+
return 0.0 if total == 0
|
|
105
|
+
@hit_count.to_f / total
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def stats
|
|
109
|
+
{
|
|
110
|
+
size: @frames.size,
|
|
111
|
+
max_size: @size,
|
|
112
|
+
hit_count: @hit_count,
|
|
113
|
+
miss_count: @miss_count,
|
|
114
|
+
hit_rate: hit_rate
|
|
115
|
+
}
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
private
|
|
119
|
+
|
|
120
|
+
def touch_page(page_number)
|
|
121
|
+
@lru_list.delete(page_number)
|
|
122
|
+
@lru_list.unshift(page_number)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def evict_page
|
|
126
|
+
# Find a clean page to evict (LRU)
|
|
127
|
+
evict_candidates = @lru_list.reverse
|
|
128
|
+
|
|
129
|
+
evict_candidates.each do |page_number|
|
|
130
|
+
frame = @frames[page_number]
|
|
131
|
+
next if frame.dirty
|
|
132
|
+
|
|
133
|
+
# Evict this page
|
|
134
|
+
@frames.delete(page_number)
|
|
135
|
+
@lru_list.delete(page_number)
|
|
136
|
+
return true
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# If all pages are dirty, force flush the least recently used
|
|
140
|
+
if @lru_list.any?
|
|
141
|
+
page_number = @lru_list.last
|
|
142
|
+
frame = @frames[page_number]
|
|
143
|
+
@page_manager.write_page(frame.page)
|
|
144
|
+
frame.dirty = false
|
|
145
|
+
|
|
146
|
+
@frames.delete(page_number)
|
|
147
|
+
@lru_list.delete(page_number)
|
|
148
|
+
return true
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
false
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "monitor"
|
|
4
|
+
require "fileutils"
|
|
5
|
+
|
|
6
|
+
module RubyDB
|
|
7
|
+
module Storage
|
|
8
|
+
# Owns an embedded database path for exactly one engine process. RubyDB's
|
|
9
|
+
# page cache, metadata sidecars, WAL and MVCC files are coordinated inside
|
|
10
|
+
# one engine instance; opening them from independent processes is unsafe.
|
|
11
|
+
class DatabaseLock
|
|
12
|
+
@registry_lock = Monitor.new
|
|
13
|
+
@open_paths = {}
|
|
14
|
+
|
|
15
|
+
class << self
|
|
16
|
+
attr_reader :registry_lock, :open_paths
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
attr_reader :path
|
|
20
|
+
|
|
21
|
+
def initialize(database_path)
|
|
22
|
+
expanded = File.expand_path(database_path)
|
|
23
|
+
FileUtils.mkdir_p(File.dirname(expanded))
|
|
24
|
+
@database_path = if File.exist?(expanded)
|
|
25
|
+
File.realpath(expanded)
|
|
26
|
+
else
|
|
27
|
+
File.join(File.realpath(File.dirname(expanded)), File.basename(expanded))
|
|
28
|
+
end
|
|
29
|
+
@path = "#{@database_path}.lock"
|
|
30
|
+
@file = nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def acquire!
|
|
34
|
+
self.class.registry_lock.synchronize do
|
|
35
|
+
raise DatabaseError, "Database '#{@database_path}' is already open in this process" if self.class.open_paths.key?(@database_path)
|
|
36
|
+
|
|
37
|
+
FileUtils.mkdir_p(File.dirname(@path))
|
|
38
|
+
@file = File.open(@path, File::RDWR | File::CREAT, 0o600)
|
|
39
|
+
unless @file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
40
|
+
close_file
|
|
41
|
+
raise DatabaseError, "Database '#{@database_path}' is already open by another process"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
self.class.open_paths[@database_path] = self
|
|
45
|
+
true
|
|
46
|
+
end
|
|
47
|
+
rescue Errno::EACCES, Errno::EAGAIN
|
|
48
|
+
close_file
|
|
49
|
+
raise DatabaseError, "Database '#{@database_path}' is already open by another process"
|
|
50
|
+
rescue SystemCallError => error
|
|
51
|
+
close_file
|
|
52
|
+
raise DatabaseError, "Unable to lock database '#{@database_path}': #{error.message}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def release
|
|
56
|
+
self.class.registry_lock.synchronize do
|
|
57
|
+
return false unless self.class.open_paths[@database_path].equal?(self)
|
|
58
|
+
|
|
59
|
+
self.class.open_paths.delete(@database_path)
|
|
60
|
+
@file&.flock(File::LOCK_UN)
|
|
61
|
+
close_file
|
|
62
|
+
true
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
def close_file
|
|
69
|
+
@file&.close unless @file&.closed?
|
|
70
|
+
@file = nil
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "bigdecimal"
|
|
5
|
+
require "date"
|
|
6
|
+
require "time"
|
|
7
|
+
|
|
8
|
+
module RubyDB
|
|
9
|
+
module Storage
|
|
10
|
+
# Deserializer - Converts binary format to Ruby objects with full type support
|
|
11
|
+
class Deserializer
|
|
12
|
+
# Deserialize a single value based on its type
|
|
13
|
+
def self.deserialize(data, type, options = {})
|
|
14
|
+
return nil if data.nil? || data.empty?
|
|
15
|
+
|
|
16
|
+
type_obj = Types::TypeRegistry.lookup(type)
|
|
17
|
+
type_obj.deserialize(data)
|
|
18
|
+
rescue => e
|
|
19
|
+
raise CorruptionError, "Failed to deserialize value of type #{type}: #{e.message}"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Deserialize a full row from binary data
|
|
23
|
+
def self.deserialize_row(data, columns, options = {})
|
|
24
|
+
return {} if data.nil? || data.empty?
|
|
25
|
+
|
|
26
|
+
row = {}
|
|
27
|
+
null_bitmap = options[:null_bitmap]
|
|
28
|
+
bitmap_size = null_bitmap ? (columns.size + 7) / 8 : 0
|
|
29
|
+
bitmap = null_bitmap ? data.byteslice(0, bitmap_size).bytes : []
|
|
30
|
+
offset = bitmap_size
|
|
31
|
+
fixed_sizes = { integer: 4, bigint: 8, smallint: 2, float: 8, boolean: 1, date: 8, time: 8, timestamp: 8 }
|
|
32
|
+
variable_length_prefixes = options[:variable_length_prefixes]
|
|
33
|
+
|
|
34
|
+
columns.each_with_index do |col, idx|
|
|
35
|
+
begin
|
|
36
|
+
col_type = col.type_class
|
|
37
|
+
col_name = col.name
|
|
38
|
+
has_default = col.has_default?
|
|
39
|
+
|
|
40
|
+
# Check if fixed-size type
|
|
41
|
+
if fixed_sizes.key?(col_type)
|
|
42
|
+
length = fixed_sizes[col_type]
|
|
43
|
+
if offset + length <= data.bytesize
|
|
44
|
+
value_data = data[offset, length]
|
|
45
|
+
offset += length
|
|
46
|
+
row[col_name] = bitmap[col_index = idx / 8] && (bitmap[col_index] & (1 << (idx % 8))) != 0 ? nil : deserialize(value_data, col_type)
|
|
47
|
+
else
|
|
48
|
+
row[col_name] = has_default ? col.default : nil
|
|
49
|
+
end
|
|
50
|
+
else
|
|
51
|
+
if variable_length_prefixes
|
|
52
|
+
raise CorruptionError, "Missing length prefix for column '#{col_name}'" if offset + 4 > data.bytesize
|
|
53
|
+
|
|
54
|
+
length = data.byteslice(offset, 4).unpack1("N")
|
|
55
|
+
offset += 4
|
|
56
|
+
raise CorruptionError, "Invalid length for column '#{col_name}'" if offset + length > data.bytesize
|
|
57
|
+
|
|
58
|
+
value_data = data.byteslice(offset, length)
|
|
59
|
+
offset += length
|
|
60
|
+
row[col_name] = if bitmap[idx / 8] && (bitmap[idx / 8] & (1 << (idx % 8))) != 0
|
|
61
|
+
nil
|
|
62
|
+
else
|
|
63
|
+
deserialize(value_data, col_type)
|
|
64
|
+
end
|
|
65
|
+
row[col_name] = col.default if row[col_name].nil? && has_default
|
|
66
|
+
# Legacy records did not store variable-length field sizes.
|
|
67
|
+
# Only the final variable-width column can be recovered safely.
|
|
68
|
+
elsif idx == columns.size - 1
|
|
69
|
+
value_data = data[offset..-1]
|
|
70
|
+
row[col_name] = if bitmap[idx / 8] && (bitmap[idx / 8] & (1 << (idx % 8))) != 0
|
|
71
|
+
nil
|
|
72
|
+
elsif value_data.bytesize > 0
|
|
73
|
+
deserialize(value_data, col_type)
|
|
74
|
+
end
|
|
75
|
+
row[col_name] = col.default if row[col_name].nil? && has_default
|
|
76
|
+
else
|
|
77
|
+
row[col_name] = has_default ? col.default : nil
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
rescue => e
|
|
81
|
+
row[col_name] = col.default if col.has_default?
|
|
82
|
+
row[col_name] = nil if col.nullable?
|
|
83
|
+
if row[col_name].nil? && !col.nullable? && !col.has_default?
|
|
84
|
+
raise CorruptionError, "Failed to deserialize column '#{col_name}': #{e.message}"
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
row
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def self.variable_length_type?(type)
|
|
93
|
+
!%i[integer bigint smallint float boolean date time timestamp].include?(type.to_sym)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Deserialize a row with a header
|
|
97
|
+
def self.deserialize_row_with_header(data, columns)
|
|
98
|
+
return nil if data.nil? || data.empty?
|
|
99
|
+
|
|
100
|
+
# Parse header
|
|
101
|
+
header_data = deserialize_header(data)
|
|
102
|
+
|
|
103
|
+
# Extract row data (after header)
|
|
104
|
+
row_data = data[header_data[:header_size]..-1]
|
|
105
|
+
|
|
106
|
+
# Deserialize row
|
|
107
|
+
row = deserialize_row(row_data, columns, has_length_prefix: true)
|
|
108
|
+
|
|
109
|
+
# Add header info to row
|
|
110
|
+
row[:_row_id] = header_data[:row_id]
|
|
111
|
+
row[:_timestamp] = header_data[:timestamp]
|
|
112
|
+
row[:_version] = header_data[:version]
|
|
113
|
+
|
|
114
|
+
row
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
# Deserialize a record header
|
|
118
|
+
def self.deserialize_header(data)
|
|
119
|
+
return { header_size: 0 } if data.nil? || data.bytesize < 16
|
|
120
|
+
|
|
121
|
+
header_size = 0
|
|
122
|
+
|
|
123
|
+
# Parse row header
|
|
124
|
+
row_id, version, timestamp, flags, column_count = data.unpack("Q>Q>Q>C>S")
|
|
125
|
+
header_size = 8 + 8 + 8 + 1 + 2 # row_id(8) + version(8) + timestamp(8) + flags(1) + column_count(2)
|
|
126
|
+
|
|
127
|
+
{
|
|
128
|
+
row_id: row_id,
|
|
129
|
+
version: version,
|
|
130
|
+
timestamp: timestamp,
|
|
131
|
+
flags: flags,
|
|
132
|
+
column_count: column_count,
|
|
133
|
+
header_size: header_size
|
|
134
|
+
}
|
|
135
|
+
rescue => e
|
|
136
|
+
{ header_size: 0, error: e.message }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# Deserialize table metadata
|
|
140
|
+
def self.deserialize_metadata(data)
|
|
141
|
+
return {} if data.nil? || data.empty?
|
|
142
|
+
|
|
143
|
+
begin
|
|
144
|
+
JSON.parse(data.force_encoding("UTF-8"), symbolize_names: true)
|
|
145
|
+
rescue JSON::ParserError => e
|
|
146
|
+
# Try to handle different encodings
|
|
147
|
+
begin
|
|
148
|
+
JSON.parse(data.force_encoding("ASCII-8BIT"), symbolize_names: true)
|
|
149
|
+
rescue JSON::ParserError
|
|
150
|
+
{}
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Deserialize page header
|
|
156
|
+
def self.deserialize_page_header(data)
|
|
157
|
+
return nil if data.nil? || data.bytesize < PageHeader::SIZE
|
|
158
|
+
|
|
159
|
+
PageHeader.deserialize(data)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Deserialize a record from a page
|
|
163
|
+
def self.deserialize_record(page, offset, columns)
|
|
164
|
+
return nil if page.nil? || offset.nil?
|
|
165
|
+
|
|
166
|
+
# Read record header
|
|
167
|
+
record_header = page.read(offset, 16)
|
|
168
|
+
return nil if record_header.nil? || record_header.bytesize < 16
|
|
169
|
+
|
|
170
|
+
# Parse record header
|
|
171
|
+
record_id, record_size, flags, column_count = record_header.unpack("Q>L>C>S")
|
|
172
|
+
|
|
173
|
+
# Validate record size
|
|
174
|
+
if record_size <= 0 || record_size > 65535
|
|
175
|
+
raise CorruptionError, "Invalid record size: #{record_size}"
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# Read record data
|
|
179
|
+
record_data = page.read(offset + 16, record_size)
|
|
180
|
+
return nil if record_data.nil?
|
|
181
|
+
|
|
182
|
+
# Deserialize row
|
|
183
|
+
row = deserialize_row(record_data, columns, has_length_prefix: true)
|
|
184
|
+
|
|
185
|
+
# Add record metadata
|
|
186
|
+
row[:_record_id] = record_id
|
|
187
|
+
row[:_record_size] = record_size
|
|
188
|
+
row[:_flags] = flags
|
|
189
|
+
|
|
190
|
+
row
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Deserialize multiple records from a page
|
|
194
|
+
def self.deserialize_records(page, columns, max_records = nil)
|
|
195
|
+
records = []
|
|
196
|
+
offset = PageHeader::SIZE
|
|
197
|
+
count = 0
|
|
198
|
+
|
|
199
|
+
while offset < page.header.data_end
|
|
200
|
+
break if max_records && count >= max_records
|
|
201
|
+
|
|
202
|
+
record = deserialize_record(page, offset, columns)
|
|
203
|
+
break if record.nil?
|
|
204
|
+
|
|
205
|
+
records << record
|
|
206
|
+
offset += 16 + record[:_record_size]
|
|
207
|
+
count += 1
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
records
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Deserialize a tuple from binary data
|
|
214
|
+
def self.deserialize_tuple(data, columns)
|
|
215
|
+
return nil if data.nil? || data.empty?
|
|
216
|
+
|
|
217
|
+
tuple = Tuple.new(columns)
|
|
218
|
+
tuple.deserialize(data)
|
|
219
|
+
tuple
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
# Deserialize a value with type and length prefix
|
|
223
|
+
def self.deserialize_value_with_length(data, offset, type)
|
|
224
|
+
return [nil, offset] if data.nil? || offset >= data.bytesize
|
|
225
|
+
|
|
226
|
+
# Read length prefix (2 bytes)
|
|
227
|
+
if offset + 2 <= data.bytesize
|
|
228
|
+
length = data[offset, 2].unpack("S").first
|
|
229
|
+
offset += 2
|
|
230
|
+
|
|
231
|
+
if length == 0xFFFF # NULL marker
|
|
232
|
+
return [nil, offset]
|
|
233
|
+
elsif length > 0 && offset + length <= data.bytesize
|
|
234
|
+
value_data = data[offset, length]
|
|
235
|
+
offset += length
|
|
236
|
+
return [deserialize(value_data, type), offset]
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
[nil, offset]
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
# Deserialize an array of values
|
|
244
|
+
def self.deserialize_array(data, type, count)
|
|
245
|
+
return [] if data.nil? || data.empty? || count <= 0
|
|
246
|
+
|
|
247
|
+
values = []
|
|
248
|
+
offset = 0
|
|
249
|
+
|
|
250
|
+
count.times do
|
|
251
|
+
value, new_offset = deserialize_value_with_length(data, offset, type)
|
|
252
|
+
break if new_offset == offset # No progress
|
|
253
|
+
values << value
|
|
254
|
+
offset = new_offset
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
values
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Deserialize a JSON object from binary
|
|
261
|
+
def self.deserialize_json(data)
|
|
262
|
+
return nil if data.nil? || data.empty?
|
|
263
|
+
|
|
264
|
+
begin
|
|
265
|
+
JSON.parse(data.force_encoding("UTF-8"))
|
|
266
|
+
rescue JSON::ParserError
|
|
267
|
+
nil
|
|
268
|
+
end
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Deserialize a bitmap
|
|
272
|
+
def self.deserialize_bitmap(data, bit_count)
|
|
273
|
+
return [] if data.nil? || data.empty? || bit_count <= 0
|
|
274
|
+
|
|
275
|
+
bitmap = []
|
|
276
|
+
data.bytes.each_with_index do |byte, byte_idx|
|
|
277
|
+
(0..7).each do |bit_idx|
|
|
278
|
+
bit_pos = byte_idx * 8 + bit_idx
|
|
279
|
+
break if bit_pos >= bit_count
|
|
280
|
+
bitmap << ((byte >> bit_idx) & 1) == 1
|
|
281
|
+
end
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
bitmap
|
|
285
|
+
end
|
|
286
|
+
|
|
287
|
+
# Deserialize variable-length data with length prefix
|
|
288
|
+
def self.deserialize_variable(data, offset)
|
|
289
|
+
return [nil, offset] if data.nil? || offset >= data.bytesize
|
|
290
|
+
|
|
291
|
+
if offset + 2 <= data.bytesize
|
|
292
|
+
length = data[offset, 2].unpack("S").first
|
|
293
|
+
offset += 2
|
|
294
|
+
|
|
295
|
+
if length == 0xFFFF
|
|
296
|
+
return [nil, offset]
|
|
297
|
+
elsif length > 0 && offset + length <= data.bytesize
|
|
298
|
+
value_data = data[offset, length]
|
|
299
|
+
offset += length
|
|
300
|
+
return [value_data, offset]
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
[nil, offset]
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
private
|
|
308
|
+
|
|
309
|
+
# Get column length from data
|
|
310
|
+
def self.get_column_length(data, offset, col, columns, idx)
|
|
311
|
+
col_type = col.type_class
|
|
312
|
+
|
|
313
|
+
# Fixed length types - return their serialized size
|
|
314
|
+
fixed_sizes = {
|
|
315
|
+
integer: 8,
|
|
316
|
+
bigint: 8,
|
|
317
|
+
smallint: 2,
|
|
318
|
+
float: 8,
|
|
319
|
+
boolean: 1,
|
|
320
|
+
date: 8,
|
|
321
|
+
time: 8,
|
|
322
|
+
timestamp: 8
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if fixed_sizes.key?(col_type)
|
|
326
|
+
return fixed_sizes[col_type]
|
|
327
|
+
else
|
|
328
|
+
# Variable length (text, json, etc.) - read until end of data or next field boundary
|
|
329
|
+
# A final variable-width column consumes the remaining record bytes
|
|
330
|
+
# for compatibility with records that omit a trailing length prefix.
|
|
331
|
+
if idx == columns.size - 1
|
|
332
|
+
return data.bytesize - offset
|
|
333
|
+
else
|
|
334
|
+
# For middle columns, we need a length prefix or a delimiter
|
|
335
|
+
# For simplicity, use remaining data if we don't have more info
|
|
336
|
+
return nil
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
end
|