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,150 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "monitor"
|
|
4
|
+
|
|
5
|
+
module RubyDB
|
|
6
|
+
module Indexes
|
|
7
|
+
# Hash index implementation (for exact equality lookups)
|
|
8
|
+
class HashIndex < Index
|
|
9
|
+
def initialize(name, table_name, columns, options = {})
|
|
10
|
+
super(name, table_name, columns, options)
|
|
11
|
+
@hash_table = {}
|
|
12
|
+
@bucket_count = options[:bucket_count] || 1024
|
|
13
|
+
@max_load_factor = options[:max_load_factor] || 0.75
|
|
14
|
+
@lock = Monitor.new
|
|
15
|
+
@is_built = false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def insert(key, row_id)
|
|
19
|
+
@lock.synchronize do
|
|
20
|
+
hash_key = hash_key(key)
|
|
21
|
+
@hash_table[hash_key] ||= []
|
|
22
|
+
|
|
23
|
+
# Check for duplicates if unique
|
|
24
|
+
if @unique
|
|
25
|
+
existing = @hash_table[hash_key].find { |entry| entry[:key] == key && entry[:row_id] == row_id }
|
|
26
|
+
return false if existing
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
@hash_table[hash_key] << { key: key, row_id: row_id }
|
|
30
|
+
@entries_count += 1
|
|
31
|
+
@modified_at = Time.now
|
|
32
|
+
|
|
33
|
+
# Rehash if load factor exceeded
|
|
34
|
+
rehash if load_factor > @max_load_factor
|
|
35
|
+
|
|
36
|
+
true
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def delete(key, row_id)
|
|
41
|
+
@lock.synchronize do
|
|
42
|
+
hash_key = hash_key(key)
|
|
43
|
+
bucket = @hash_table[hash_key]
|
|
44
|
+
return false unless bucket
|
|
45
|
+
|
|
46
|
+
bucket.delete_if { |entry| entry[:key] == key && entry[:row_id] == row_id }
|
|
47
|
+
@hash_table.delete(hash_key) if bucket.empty?
|
|
48
|
+
@entries_count -= 1
|
|
49
|
+
@modified_at = Time.now
|
|
50
|
+
true
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def search(key)
|
|
55
|
+
@lock.synchronize do
|
|
56
|
+
hash_key = hash_key(key)
|
|
57
|
+
bucket = @hash_table[hash_key]
|
|
58
|
+
return [] unless bucket
|
|
59
|
+
|
|
60
|
+
bucket.select { |entry| entry[:key] == key }.map { |entry| entry[:row_id] }
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def range_search(start_key, end_key)
|
|
65
|
+
# Hash index doesn't support efficient range queries
|
|
66
|
+
# Return all entries in the range (slow)
|
|
67
|
+
@lock.synchronize do
|
|
68
|
+
results = []
|
|
69
|
+
@hash_table.each do |_, bucket|
|
|
70
|
+
bucket.each do |entry|
|
|
71
|
+
if entry[:key] >= start_key && entry[:key] <= end_key
|
|
72
|
+
results << entry
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
results
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def build(rows)
|
|
81
|
+
@lock.synchronize do
|
|
82
|
+
clear
|
|
83
|
+
|
|
84
|
+
rows.each do |row|
|
|
85
|
+
key = extract_key(row)
|
|
86
|
+
row_id = row[:_row_id] || row["id"] || row[:id]
|
|
87
|
+
insert(key, row_id)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
@is_built = true
|
|
91
|
+
@modified_at = Time.now
|
|
92
|
+
true
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def clear
|
|
97
|
+
@lock.synchronize do
|
|
98
|
+
@hash_table.clear
|
|
99
|
+
@entries_count = 0
|
|
100
|
+
@is_built = false
|
|
101
|
+
true
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def analyze
|
|
106
|
+
super.merge({
|
|
107
|
+
bucket_count: @bucket_count,
|
|
108
|
+
load_factor: load_factor,
|
|
109
|
+
max_load_factor: @max_load_factor,
|
|
110
|
+
is_built: @is_built
|
|
111
|
+
})
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def hash_key(key)
|
|
117
|
+
key_hash = key.is_a?(Array) ? key.hash : key.hash
|
|
118
|
+
key_hash.abs % @bucket_count
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def extract_key(row)
|
|
122
|
+
if @columns.size == 1
|
|
123
|
+
column = @columns.first
|
|
124
|
+
row.key?(column) ? row[column] : row[column.to_s]
|
|
125
|
+
else
|
|
126
|
+
@columns.map { |col| row.key?(col) ? row[col] : row[col.to_s] }
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def load_factor
|
|
131
|
+
@entries_count.to_f / @bucket_count
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def rehash
|
|
135
|
+
new_bucket_count = @bucket_count * 2
|
|
136
|
+
old_table = @hash_table
|
|
137
|
+
@hash_table = {}
|
|
138
|
+
@bucket_count = new_bucket_count
|
|
139
|
+
|
|
140
|
+
old_table.each do |_, bucket|
|
|
141
|
+
bucket.each do |entry|
|
|
142
|
+
hash_key = hash_key(entry[:key])
|
|
143
|
+
@hash_table[hash_key] ||= []
|
|
144
|
+
@hash_table[hash_key] << entry
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RubyDB
|
|
4
|
+
module Indexes
|
|
5
|
+
# Base class for all index types
|
|
6
|
+
class Index
|
|
7
|
+
attr_reader :name, :table_name, :columns, :unique, :type, :options
|
|
8
|
+
attr_accessor :size, :entries_count
|
|
9
|
+
|
|
10
|
+
def initialize(name, table_name, columns, options = {})
|
|
11
|
+
@name = name
|
|
12
|
+
@table_name = table_name
|
|
13
|
+
@columns = columns.is_a?(Array) ? columns : [columns]
|
|
14
|
+
@unique = options[:unique] || false
|
|
15
|
+
@type = options[:type] || :btree
|
|
16
|
+
@options = options
|
|
17
|
+
@size = 0
|
|
18
|
+
@entries_count = 0
|
|
19
|
+
@created_at = Time.now
|
|
20
|
+
@modified_at = Time.now
|
|
21
|
+
@is_built = false
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def insert(key, row_id)
|
|
25
|
+
raise NotImplementedError, "#{self.class} must implement #insert"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def delete(key, row_id)
|
|
29
|
+
raise NotImplementedError, "#{self.class} must implement #delete"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def search(key)
|
|
33
|
+
raise NotImplementedError, "#{self.class} must implement #search"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def range_search(start_key, end_key)
|
|
37
|
+
raise NotImplementedError, "#{self.class} must implement #range_search"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def build(rows)
|
|
41
|
+
raise NotImplementedError, "#{self.class} must implement #build"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def clear
|
|
45
|
+
raise NotImplementedError, "#{self.class} must implement #clear"
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def analyze
|
|
49
|
+
{
|
|
50
|
+
name: @name,
|
|
51
|
+
table: @table_name,
|
|
52
|
+
columns: @columns,
|
|
53
|
+
type: @type,
|
|
54
|
+
unique: @unique,
|
|
55
|
+
entries: @entries_count,
|
|
56
|
+
size: @size,
|
|
57
|
+
created_at: @created_at,
|
|
58
|
+
modified_at: @modified_at
|
|
59
|
+
}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def to_s
|
|
63
|
+
"#{@type.to_s.upcase} INDEX #{@name} ON #{@table_name}(#{@columns.join(', ')})"
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def inspect
|
|
67
|
+
to_s
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "monitor"
|
|
5
|
+
|
|
6
|
+
module RubyDB
|
|
7
|
+
module Indexes
|
|
8
|
+
# IndexManager - Manages all indexes for the database
|
|
9
|
+
class IndexManager
|
|
10
|
+
attr_reader :indexes, :stats
|
|
11
|
+
|
|
12
|
+
def initialize(engine)
|
|
13
|
+
@engine = engine
|
|
14
|
+
@indexes = {}
|
|
15
|
+
@table_indexes = {}
|
|
16
|
+
@stats = {
|
|
17
|
+
index_creates: 0,
|
|
18
|
+
index_drops: 0,
|
|
19
|
+
index_builds: 0,
|
|
20
|
+
index_searches: 0,
|
|
21
|
+
index_inserts: 0,
|
|
22
|
+
index_deletes: 0,
|
|
23
|
+
cache_hits: 0,
|
|
24
|
+
cache_misses: 0
|
|
25
|
+
}
|
|
26
|
+
@cache = {}
|
|
27
|
+
@cache_size = 100
|
|
28
|
+
@lock = Monitor.new
|
|
29
|
+
|
|
30
|
+
load_indexes
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def create_index(name, table_name, columns, options = {})
|
|
34
|
+
@lock.synchronize do
|
|
35
|
+
# Check if index already exists
|
|
36
|
+
if @indexes.key?(name)
|
|
37
|
+
raise DatabaseError, "Index '#{name}' already exists" unless options[:if_not_exists]
|
|
38
|
+
return false
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Check if table exists
|
|
42
|
+
unless @engine.table_exists?(table_name)
|
|
43
|
+
raise DatabaseError, "Table '#{table_name}' does not exist"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Create index based on type
|
|
47
|
+
index_type = options[:type] || :btree
|
|
48
|
+
index = case index_type
|
|
49
|
+
when :btree
|
|
50
|
+
BTree.new(name, table_name, columns, options)
|
|
51
|
+
when :hash
|
|
52
|
+
HashIndex.new(name, table_name, columns, options)
|
|
53
|
+
else
|
|
54
|
+
raise ConfigurationError, "Unsupported index type: #{index_type}"
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
begin
|
|
58
|
+
# Store index
|
|
59
|
+
@indexes[name] = index
|
|
60
|
+
@table_indexes[table_name] ||= []
|
|
61
|
+
@table_indexes[table_name] << name
|
|
62
|
+
|
|
63
|
+
# Build the in-memory structure first, then publish its durable
|
|
64
|
+
# definition once. A failed publication must not leave an index
|
|
65
|
+
# visible in this process but absent from the on-disk catalog.
|
|
66
|
+
build_index(name, persist: false) unless options[:skip_build]
|
|
67
|
+
|
|
68
|
+
@stats[:index_creates] += 1
|
|
69
|
+
save_indexes
|
|
70
|
+
true
|
|
71
|
+
rescue Exception
|
|
72
|
+
@indexes.delete(name)
|
|
73
|
+
if @table_indexes[table_name]
|
|
74
|
+
@table_indexes[table_name].delete(name)
|
|
75
|
+
@table_indexes.delete(table_name) if @table_indexes[table_name].empty?
|
|
76
|
+
end
|
|
77
|
+
@stats[:index_creates] -= 1 if @stats[:index_creates].positive?
|
|
78
|
+
raise
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def drop_index(name, options = {})
|
|
84
|
+
@lock.synchronize do
|
|
85
|
+
unless @indexes.key?(name)
|
|
86
|
+
return false if options[:if_exists]
|
|
87
|
+
raise DatabaseError, "Index '#{name}' does not exist"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
index = @indexes[name]
|
|
91
|
+
table_name = index.table_name
|
|
92
|
+
|
|
93
|
+
# Remove from table index list
|
|
94
|
+
if @table_indexes[table_name]
|
|
95
|
+
@table_indexes[table_name].delete(name)
|
|
96
|
+
@table_indexes.delete(table_name) if @table_indexes[table_name].empty?
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Clear index
|
|
100
|
+
index.clear
|
|
101
|
+
|
|
102
|
+
# Remove from cache
|
|
103
|
+
@cache.delete(name)
|
|
104
|
+
|
|
105
|
+
@indexes.delete(name)
|
|
106
|
+
@stats[:index_drops] += 1
|
|
107
|
+
save_indexes
|
|
108
|
+
|
|
109
|
+
true
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def get_index(name)
|
|
114
|
+
@lock.synchronize do
|
|
115
|
+
@indexes[name]
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def get_indexes_for_table(table_name)
|
|
120
|
+
@lock.synchronize do
|
|
121
|
+
(@table_indexes[table_name] || []).map { |name| @indexes[name] }.compact
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def index_exists?(name)
|
|
126
|
+
@indexes.key?(name)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def build_index(name, persist: true)
|
|
130
|
+
@lock.synchronize do
|
|
131
|
+
index = @indexes[name]
|
|
132
|
+
return false unless index
|
|
133
|
+
|
|
134
|
+
# Get all rows from table
|
|
135
|
+
columns = @engine.table_columns(index.table_name)
|
|
136
|
+
rows = @engine.select_rows(index.table_name, columns)
|
|
137
|
+
|
|
138
|
+
# Build index
|
|
139
|
+
index.build(rows)
|
|
140
|
+
|
|
141
|
+
@stats[:index_builds] += 1
|
|
142
|
+
save_indexes if persist
|
|
143
|
+
true
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def rebuild_index(name)
|
|
148
|
+
@lock.synchronize do
|
|
149
|
+
index = @indexes[name]
|
|
150
|
+
raise DatabaseError, "Index '#{name}' does not exist" unless index
|
|
151
|
+
|
|
152
|
+
table_name = index.table_name
|
|
153
|
+
columns = index.columns.dup
|
|
154
|
+
options = index.options.dup
|
|
155
|
+
drop_index(name, if_exists: true)
|
|
156
|
+
create_index(name, table_name, columns, options)
|
|
157
|
+
true
|
|
158
|
+
end
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def rebuild_all_indexes
|
|
162
|
+
@lock.synchronize do
|
|
163
|
+
@indexes.keys.each do |name|
|
|
164
|
+
rebuild_index(name)
|
|
165
|
+
end
|
|
166
|
+
true
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def insert_row(table_name, row)
|
|
171
|
+
@lock.synchronize do
|
|
172
|
+
indexes = get_indexes_for_table(table_name)
|
|
173
|
+
return unless indexes.any?
|
|
174
|
+
|
|
175
|
+
validate_insert!(table_name, row)
|
|
176
|
+
row_id = row[:_row_id] || row["id"] || row[:id]
|
|
177
|
+
|
|
178
|
+
indexes.each do |index|
|
|
179
|
+
key = extract_key(row, index.columns)
|
|
180
|
+
index.insert(key, row_id)
|
|
181
|
+
@stats[:index_inserts] += 1
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
def validate_insert!(table_name, row)
|
|
187
|
+
@lock.synchronize do
|
|
188
|
+
indexes = get_indexes_for_table(table_name)
|
|
189
|
+
row_id = row[:_row_id] || row["id"] || row[:id]
|
|
190
|
+
indexes.each do |index|
|
|
191
|
+
next unless index.unique
|
|
192
|
+
key = extract_key(row, index.columns)
|
|
193
|
+
existing = index.search(key)
|
|
194
|
+
if !existing.nil? && existing != row_id
|
|
195
|
+
raise DatabaseError, "Duplicate value for unique index '#{index.name}'"
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
true
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
def delete_row(table_name, row)
|
|
203
|
+
@lock.synchronize do
|
|
204
|
+
indexes = get_indexes_for_table(table_name)
|
|
205
|
+
return unless indexes.any?
|
|
206
|
+
|
|
207
|
+
indexes.each do |index|
|
|
208
|
+
key = extract_key(row, index.columns)
|
|
209
|
+
row_id = row[:_row_id] || row["id"] || row[:id]
|
|
210
|
+
index.delete(key, row_id)
|
|
211
|
+
@stats[:index_deletes] += 1
|
|
212
|
+
end
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def update_row(table_name, old_row, new_row)
|
|
217
|
+
@lock.synchronize do
|
|
218
|
+
indexes = get_indexes_for_table(table_name)
|
|
219
|
+
return unless indexes.any?
|
|
220
|
+
|
|
221
|
+
validate_update!(table_name, old_row, new_row)
|
|
222
|
+
row_id = new_row[:_row_id] || new_row["id"] || new_row[:id]
|
|
223
|
+
|
|
224
|
+
indexes.each do |index|
|
|
225
|
+
old_key = extract_key(old_row, index.columns)
|
|
226
|
+
new_key = extract_key(new_row, index.columns)
|
|
227
|
+
|
|
228
|
+
if old_key != new_key
|
|
229
|
+
# Update index
|
|
230
|
+
index.delete(old_key, row_id)
|
|
231
|
+
index.insert(new_key, row_id)
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
def validate_update!(table_name, old_row, new_row)
|
|
238
|
+
@lock.synchronize do
|
|
239
|
+
indexes = get_indexes_for_table(table_name)
|
|
240
|
+
row_id = new_row[:_row_id] || new_row["id"] || new_row[:id]
|
|
241
|
+
indexes.each do |index|
|
|
242
|
+
next unless index.unique
|
|
243
|
+
old_key = extract_key(old_row, index.columns)
|
|
244
|
+
new_key = extract_key(new_row, index.columns)
|
|
245
|
+
next if old_key == new_key
|
|
246
|
+
existing = index.search(new_key)
|
|
247
|
+
if !existing.nil? && existing != row_id
|
|
248
|
+
raise DatabaseError, "Duplicate value for unique index '#{index.name}'"
|
|
249
|
+
end
|
|
250
|
+
end
|
|
251
|
+
true
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
def find_index_for_query(table_name, conditions)
|
|
256
|
+
@lock.synchronize do
|
|
257
|
+
indexes = get_indexes_for_table(table_name)
|
|
258
|
+
return nil if indexes.empty?
|
|
259
|
+
|
|
260
|
+
# Try to find the best index for the query
|
|
261
|
+
indexes.each do |index|
|
|
262
|
+
# Check if all columns in condition are in index
|
|
263
|
+
index_columns = index.columns
|
|
264
|
+
condition_keys = conditions.keys.map(&:to_s)
|
|
265
|
+
|
|
266
|
+
if index_columns.all? { |col| condition_keys.include?(col.to_s) }
|
|
267
|
+
# Check if the condition has an operator that can use the index
|
|
268
|
+
if index.type == :hash
|
|
269
|
+
# Hash index works best for equality
|
|
270
|
+
return index if conditions.values.all? { |v| v[:operator] == :eq }
|
|
271
|
+
elsif index.type == :btree
|
|
272
|
+
# B-Tree works for equality, range, and prefix
|
|
273
|
+
return index
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
# Return first index if no better match
|
|
279
|
+
indexes.first
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def analyze_index(name)
|
|
284
|
+
@lock.synchronize do
|
|
285
|
+
index = @indexes[name]
|
|
286
|
+
return nil unless index
|
|
287
|
+
index.analyze
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
def analyze_all_indexes
|
|
292
|
+
@lock.synchronize do
|
|
293
|
+
@indexes.transform_values(&:analyze)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
296
|
+
|
|
297
|
+
def stats
|
|
298
|
+
@lock.synchronize do
|
|
299
|
+
{
|
|
300
|
+
total_indexes: @indexes.size,
|
|
301
|
+
index_creates: @stats[:index_creates],
|
|
302
|
+
index_drops: @stats[:index_drops],
|
|
303
|
+
index_builds: @stats[:index_builds],
|
|
304
|
+
index_searches: @stats[:index_searches],
|
|
305
|
+
index_inserts: @stats[:index_inserts],
|
|
306
|
+
index_deletes: @stats[:index_deletes],
|
|
307
|
+
cache_hits: @stats[:cache_hits],
|
|
308
|
+
cache_misses: @stats[:cache_misses],
|
|
309
|
+
cache_hit_rate: cache_hit_rate,
|
|
310
|
+
by_type: index_count_by_type
|
|
311
|
+
}
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
private
|
|
316
|
+
|
|
317
|
+
def extract_key(row, columns)
|
|
318
|
+
if columns.size == 1
|
|
319
|
+
column = columns.first
|
|
320
|
+
row.key?(column) ? row[column] : row[column.to_s]
|
|
321
|
+
else
|
|
322
|
+
columns.map { |col| row.key?(col) ? row[col] : row[col.to_s] }
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def index_count_by_type
|
|
327
|
+
counts = Hash.new(0)
|
|
328
|
+
@indexes.each do |_, index|
|
|
329
|
+
counts[index.type] += 1
|
|
330
|
+
end
|
|
331
|
+
counts
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
def cache_hit_rate
|
|
335
|
+
total = @stats[:cache_hits] + @stats[:cache_misses]
|
|
336
|
+
return 0.0 if total == 0
|
|
337
|
+
(@stats[:cache_hits].to_f / total * 100).round(2)
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
def load_indexes
|
|
341
|
+
@lock.synchronize do
|
|
342
|
+
begin
|
|
343
|
+
index_path = index_metadata_path
|
|
344
|
+
if File.exist?(index_path)
|
|
345
|
+
data = File.read(index_path)
|
|
346
|
+
parsed = JSON.parse(data, symbolize_names: true)
|
|
347
|
+
|
|
348
|
+
parsed[:indexes]&.each do |name, index_data|
|
|
349
|
+
table_name = index_data[:table_name]
|
|
350
|
+
table_name = table_name.to_sym if table_name.respond_to?(:to_sym)
|
|
351
|
+
name = name.to_sym if name.respond_to?(:to_sym)
|
|
352
|
+
columns = Array(index_data[:columns]).map { |column| column.respond_to?(:to_sym) ? column.to_sym : column }
|
|
353
|
+
options = (index_data[:options] || {}).transform_keys(&:to_sym)
|
|
354
|
+
options[:type] = index_data[:type].to_sym if index_data[:type]
|
|
355
|
+
options[:unique] = index_data[:unique] unless index_data[:unique].nil?
|
|
356
|
+
create_index(name, table_name, columns, options)
|
|
357
|
+
end
|
|
358
|
+
end
|
|
359
|
+
rescue StandardError => error
|
|
360
|
+
@indexes.clear
|
|
361
|
+
@table_indexes.clear
|
|
362
|
+
raise DatabaseError, "Invalid persisted index metadata at #{index_metadata_path}: #{error.message}"
|
|
363
|
+
end
|
|
364
|
+
end
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
def save_indexes
|
|
368
|
+
@lock.synchronize do
|
|
369
|
+
data = {
|
|
370
|
+
indexes: {},
|
|
371
|
+
timestamp: Time.now.iso8601
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
@indexes.each do |name, index|
|
|
375
|
+
data[:indexes][name] = {
|
|
376
|
+
table_name: index.table_name,
|
|
377
|
+
columns: index.columns,
|
|
378
|
+
type: index.type,
|
|
379
|
+
unique: index.unique,
|
|
380
|
+
options: index.options,
|
|
381
|
+
entries: index.entries_count,
|
|
382
|
+
created_at: index.instance_variable_get(:@created_at).iso8601
|
|
383
|
+
}
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
path = index_metadata_path
|
|
387
|
+
temporary = "#{path}.tmp-#{Process.pid}-#{Thread.current.object_id}-#{Time.now.to_f.hash.abs}"
|
|
388
|
+
File.open(temporary, "wb") do |file|
|
|
389
|
+
file.write(JSON.generate(data))
|
|
390
|
+
file.flush
|
|
391
|
+
file.fsync
|
|
392
|
+
end
|
|
393
|
+
begin
|
|
394
|
+
File.rename(temporary, path)
|
|
395
|
+
ensure
|
|
396
|
+
File.delete(temporary) if File.file?(temporary)
|
|
397
|
+
end
|
|
398
|
+
end
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def index_metadata_path
|
|
402
|
+
"#{@engine.instance_variable_get(:@path)}.indexes"
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
end
|
|
406
|
+
end
|