@mikeatlast/ponder 0.16.1-fork.1
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.
- package/CHANGELOG.md +3415 -0
- package/README.md +186 -0
- package/dist/esm/bin/commands/codegen.js +46 -0
- package/dist/esm/bin/commands/codegen.js.map +1 -0
- package/dist/esm/bin/commands/createViews.js +196 -0
- package/dist/esm/bin/commands/createViews.js.map +1 -0
- package/dist/esm/bin/commands/dev.js +430 -0
- package/dist/esm/bin/commands/dev.js.map +1 -0
- package/dist/esm/bin/commands/list.js +148 -0
- package/dist/esm/bin/commands/list.js.map +1 -0
- package/dist/esm/bin/commands/prune.js +223 -0
- package/dist/esm/bin/commands/prune.js.map +1 -0
- package/dist/esm/bin/commands/serve.js +198 -0
- package/dist/esm/bin/commands/serve.js.map +1 -0
- package/dist/esm/bin/commands/start.js +253 -0
- package/dist/esm/bin/commands/start.js.map +1 -0
- package/dist/esm/bin/isolatedController.js +200 -0
- package/dist/esm/bin/isolatedController.js.map +1 -0
- package/dist/esm/bin/isolatedWorker.js +146 -0
- package/dist/esm/bin/isolatedWorker.js.map +1 -0
- package/dist/esm/bin/ponder.js +137 -0
- package/dist/esm/bin/ponder.js.map +1 -0
- package/dist/esm/bin/utils/codegen.js +25 -0
- package/dist/esm/bin/utils/codegen.js.map +1 -0
- package/dist/esm/bin/utils/exit.js +100 -0
- package/dist/esm/bin/utils/exit.js.map +1 -0
- package/dist/esm/build/config.js +745 -0
- package/dist/esm/build/config.js.map +1 -0
- package/dist/esm/build/factory.js +82 -0
- package/dist/esm/build/factory.js.map +1 -0
- package/dist/esm/build/index.js +567 -0
- package/dist/esm/build/index.js.map +1 -0
- package/dist/esm/build/plugin.js +53 -0
- package/dist/esm/build/plugin.js.map +1 -0
- package/dist/esm/build/pre.js +83 -0
- package/dist/esm/build/pre.js.map +1 -0
- package/dist/esm/build/schema.js +202 -0
- package/dist/esm/build/schema.js.map +1 -0
- package/dist/esm/build/stacktrace.js +137 -0
- package/dist/esm/build/stacktrace.js.map +1 -0
- package/dist/esm/client/index.js +441 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/config/address.js +2 -0
- package/dist/esm/config/address.js.map +1 -0
- package/dist/esm/config/eventFilter.js +2 -0
- package/dist/esm/config/eventFilter.js.map +1 -0
- package/dist/esm/config/index.js +2 -0
- package/dist/esm/config/index.js.map +1 -0
- package/dist/esm/config/utilityTypes.js +2 -0
- package/dist/esm/config/utilityTypes.js.map +1 -0
- package/dist/esm/database/actions.js +445 -0
- package/dist/esm/database/actions.js.map +1 -0
- package/dist/esm/database/index.js +604 -0
- package/dist/esm/database/index.js.map +1 -0
- package/dist/esm/database/queryBuilder.js +314 -0
- package/dist/esm/database/queryBuilder.js.map +1 -0
- package/dist/esm/drizzle/bigint.js +38 -0
- package/dist/esm/drizzle/bigint.js.map +1 -0
- package/dist/esm/drizzle/bytes.js +47 -0
- package/dist/esm/drizzle/bytes.js.map +1 -0
- package/dist/esm/drizzle/hex.js +40 -0
- package/dist/esm/drizzle/hex.js.map +1 -0
- package/dist/esm/drizzle/index.js +28 -0
- package/dist/esm/drizzle/index.js.map +1 -0
- package/dist/esm/drizzle/json.js +123 -0
- package/dist/esm/drizzle/json.js.map +1 -0
- package/dist/esm/drizzle/kit/index.js +927 -0
- package/dist/esm/drizzle/kit/index.js.map +1 -0
- package/dist/esm/drizzle/onchain.js +184 -0
- package/dist/esm/drizzle/onchain.js.map +1 -0
- package/dist/esm/drizzle/text.js +61 -0
- package/dist/esm/drizzle/text.js.map +1 -0
- package/dist/esm/graphql/graphiql.html.js +59 -0
- package/dist/esm/graphql/graphiql.html.js.map +1 -0
- package/dist/esm/graphql/index.js +934 -0
- package/dist/esm/graphql/index.js.map +1 -0
- package/dist/esm/graphql/json.js +42 -0
- package/dist/esm/graphql/json.js.map +1 -0
- package/dist/esm/graphql/middleware.js +83 -0
- package/dist/esm/graphql/middleware.js.map +1 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/indexing/addStackTrace.js +54 -0
- package/dist/esm/indexing/addStackTrace.js.map +1 -0
- package/dist/esm/indexing/client.js +675 -0
- package/dist/esm/indexing/client.js.map +1 -0
- package/dist/esm/indexing/index.js +663 -0
- package/dist/esm/indexing/index.js.map +1 -0
- package/dist/esm/indexing/profile.js +584 -0
- package/dist/esm/indexing/profile.js.map +1 -0
- package/dist/esm/indexing-store/cache.js +666 -0
- package/dist/esm/indexing-store/cache.js.map +1 -0
- package/dist/esm/indexing-store/index.js +461 -0
- package/dist/esm/indexing-store/index.js.map +1 -0
- package/dist/esm/indexing-store/profile.js +428 -0
- package/dist/esm/indexing-store/profile.js.map +1 -0
- package/dist/esm/indexing-store/utils.js +111 -0
- package/dist/esm/indexing-store/utils.js.map +1 -0
- package/dist/esm/internal/common.js +2 -0
- package/dist/esm/internal/common.js.map +1 -0
- package/dist/esm/internal/errors.js +300 -0
- package/dist/esm/internal/errors.js.map +1 -0
- package/dist/esm/internal/logger.js +178 -0
- package/dist/esm/internal/logger.js.map +1 -0
- package/dist/esm/internal/metrics.js +1049 -0
- package/dist/esm/internal/metrics.js.map +1 -0
- package/dist/esm/internal/options.js +73 -0
- package/dist/esm/internal/options.js.map +1 -0
- package/dist/esm/internal/shutdown.js +24 -0
- package/dist/esm/internal/shutdown.js.map +1 -0
- package/dist/esm/internal/telemetry.js +200 -0
- package/dist/esm/internal/telemetry.js.map +1 -0
- package/dist/esm/internal/types.js +2 -0
- package/dist/esm/internal/types.js.map +1 -0
- package/dist/esm/rpc/actions.js +988 -0
- package/dist/esm/rpc/actions.js.map +1 -0
- package/dist/esm/rpc/http.js +130 -0
- package/dist/esm/rpc/http.js.map +1 -0
- package/dist/esm/rpc/index.js +749 -0
- package/dist/esm/rpc/index.js.map +1 -0
- package/dist/esm/runtime/events.js +664 -0
- package/dist/esm/runtime/events.js.map +1 -0
- package/dist/esm/runtime/filter.js +476 -0
- package/dist/esm/runtime/filter.js.map +1 -0
- package/dist/esm/runtime/fragments.js +478 -0
- package/dist/esm/runtime/fragments.js.map +1 -0
- package/dist/esm/runtime/historical.js +954 -0
- package/dist/esm/runtime/historical.js.map +1 -0
- package/dist/esm/runtime/index.js +316 -0
- package/dist/esm/runtime/index.js.map +1 -0
- package/dist/esm/runtime/init.js +12 -0
- package/dist/esm/runtime/init.js.map +1 -0
- package/dist/esm/runtime/isolated.js +464 -0
- package/dist/esm/runtime/isolated.js.map +1 -0
- package/dist/esm/runtime/multichain.js +511 -0
- package/dist/esm/runtime/multichain.js.map +1 -0
- package/dist/esm/runtime/omnichain.js +546 -0
- package/dist/esm/runtime/omnichain.js.map +1 -0
- package/dist/esm/runtime/realtime.js +722 -0
- package/dist/esm/runtime/realtime.js.map +1 -0
- package/dist/esm/server/error.js +56 -0
- package/dist/esm/server/error.js.map +1 -0
- package/dist/esm/server/index.js +121 -0
- package/dist/esm/server/index.js.map +1 -0
- package/dist/esm/sync-historical/index.js +703 -0
- package/dist/esm/sync-historical/index.js.map +1 -0
- package/dist/esm/sync-realtime/bloom.js +76 -0
- package/dist/esm/sync-realtime/bloom.js.map +1 -0
- package/dist/esm/sync-realtime/index.js +918 -0
- package/dist/esm/sync-realtime/index.js.map +1 -0
- package/dist/esm/sync-store/encode.js +105 -0
- package/dist/esm/sync-store/encode.js.map +1 -0
- package/dist/esm/sync-store/index.js +885 -0
- package/dist/esm/sync-store/index.js.map +1 -0
- package/dist/esm/sync-store/migrations.js +1595 -0
- package/dist/esm/sync-store/migrations.js.map +1 -0
- package/dist/esm/sync-store/schema.js +181 -0
- package/dist/esm/sync-store/schema.js.map +1 -0
- package/dist/esm/types/db.js +2 -0
- package/dist/esm/types/db.js.map +1 -0
- package/dist/esm/types/eth.js +2 -0
- package/dist/esm/types/eth.js.map +1 -0
- package/dist/esm/types/utils.js +2 -0
- package/dist/esm/types/utils.js.map +1 -0
- package/dist/esm/types/virtual.js +2 -0
- package/dist/esm/types/virtual.js.map +1 -0
- package/dist/esm/ui/app.js +157 -0
- package/dist/esm/ui/app.js.map +1 -0
- package/dist/esm/ui/index.js +29 -0
- package/dist/esm/ui/index.js.map +1 -0
- package/dist/esm/ui/patch.js +140 -0
- package/dist/esm/ui/patch.js.map +1 -0
- package/dist/esm/utils/abi.js +55 -0
- package/dist/esm/utils/abi.js.map +1 -0
- package/dist/esm/utils/bigint.js +37 -0
- package/dist/esm/utils/bigint.js.map +1 -0
- package/dist/esm/utils/chains.js +21 -0
- package/dist/esm/utils/chains.js.map +1 -0
- package/dist/esm/utils/checkpoint.js +139 -0
- package/dist/esm/utils/checkpoint.js.map +1 -0
- package/dist/esm/utils/chunk.js +8 -0
- package/dist/esm/utils/chunk.js.map +1 -0
- package/dist/esm/utils/copy.js +129 -0
- package/dist/esm/utils/copy.js.map +1 -0
- package/dist/esm/utils/date.js +27 -0
- package/dist/esm/utils/date.js.map +1 -0
- package/dist/esm/utils/debug.js +2 -0
- package/dist/esm/utils/debug.js.map +1 -0
- package/dist/esm/utils/decodeAbiParameters.js +290 -0
- package/dist/esm/utils/decodeAbiParameters.js.map +1 -0
- package/dist/esm/utils/decodeEventLog.js +75 -0
- package/dist/esm/utils/decodeEventLog.js.map +1 -0
- package/dist/esm/utils/dedupe.js +29 -0
- package/dist/esm/utils/dedupe.js.map +1 -0
- package/dist/esm/utils/duplicates.js +19 -0
- package/dist/esm/utils/duplicates.js.map +1 -0
- package/dist/esm/utils/estimate.js +6 -0
- package/dist/esm/utils/estimate.js.map +1 -0
- package/dist/esm/utils/finality.js +38 -0
- package/dist/esm/utils/finality.js.map +1 -0
- package/dist/esm/utils/format.js +20 -0
- package/dist/esm/utils/format.js.map +1 -0
- package/dist/esm/utils/generators.js +121 -0
- package/dist/esm/utils/generators.js.map +1 -0
- package/dist/esm/utils/hash.js +11 -0
- package/dist/esm/utils/hash.js.map +1 -0
- package/dist/esm/utils/interval.js +171 -0
- package/dist/esm/utils/interval.js.map +1 -0
- package/dist/esm/utils/lowercase.js +7 -0
- package/dist/esm/utils/lowercase.js.map +1 -0
- package/dist/esm/utils/mutex.js +26 -0
- package/dist/esm/utils/mutex.js.map +1 -0
- package/dist/esm/utils/never.js +4 -0
- package/dist/esm/utils/never.js.map +1 -0
- package/dist/esm/utils/offset.js +101 -0
- package/dist/esm/utils/offset.js.map +1 -0
- package/dist/esm/utils/order.js +18 -0
- package/dist/esm/utils/order.js.map +1 -0
- package/dist/esm/utils/partition.js +46 -0
- package/dist/esm/utils/partition.js.map +1 -0
- package/dist/esm/utils/pg.js +175 -0
- package/dist/esm/utils/pg.js.map +1 -0
- package/dist/esm/utils/pglite.js +80 -0
- package/dist/esm/utils/pglite.js.map +1 -0
- package/dist/esm/utils/port.js +30 -0
- package/dist/esm/utils/port.js.map +1 -0
- package/dist/esm/utils/print.js +23 -0
- package/dist/esm/utils/print.js.map +1 -0
- package/dist/esm/utils/promiseAllSettledWithThrow.js +19 -0
- package/dist/esm/utils/promiseAllSettledWithThrow.js.map +1 -0
- package/dist/esm/utils/promiseWithResolvers.js +13 -0
- package/dist/esm/utils/promiseWithResolvers.js.map +1 -0
- package/dist/esm/utils/queue.js +150 -0
- package/dist/esm/utils/queue.js.map +1 -0
- package/dist/esm/utils/range.js +8 -0
- package/dist/esm/utils/range.js.map +1 -0
- package/dist/esm/utils/result.js +10 -0
- package/dist/esm/utils/result.js.map +1 -0
- package/dist/esm/utils/sql-parse.js +1326 -0
- package/dist/esm/utils/sql-parse.js.map +1 -0
- package/dist/esm/utils/timer.js +9 -0
- package/dist/esm/utils/timer.js.map +1 -0
- package/dist/esm/utils/truncate.js +15 -0
- package/dist/esm/utils/truncate.js.map +1 -0
- package/dist/esm/utils/wait.js +10 -0
- package/dist/esm/utils/wait.js.map +1 -0
- package/dist/esm/utils/zipper.js +67 -0
- package/dist/esm/utils/zipper.js.map +1 -0
- package/dist/types/bin/commands/codegen.d.ts +5 -0
- package/dist/types/bin/commands/codegen.d.ts.map +1 -0
- package/dist/types/bin/commands/createViews.d.ts +8 -0
- package/dist/types/bin/commands/createViews.d.ts.map +1 -0
- package/dist/types/bin/commands/dev.d.ts +5 -0
- package/dist/types/bin/commands/dev.d.ts.map +1 -0
- package/dist/types/bin/commands/list.d.ts +5 -0
- package/dist/types/bin/commands/list.d.ts.map +1 -0
- package/dist/types/bin/commands/prune.d.ts +5 -0
- package/dist/types/bin/commands/prune.d.ts.map +1 -0
- package/dist/types/bin/commands/serve.d.ts +5 -0
- package/dist/types/bin/commands/serve.d.ts.map +1 -0
- package/dist/types/bin/commands/start.d.ts +19 -0
- package/dist/types/bin/commands/start.d.ts.map +1 -0
- package/dist/types/bin/isolatedController.d.ts +13 -0
- package/dist/types/bin/isolatedController.d.ts.map +1 -0
- package/dist/types/bin/isolatedWorker.d.ts +9 -0
- package/dist/types/bin/isolatedWorker.d.ts.map +1 -0
- package/dist/types/bin/ponder.d.ts +37 -0
- package/dist/types/bin/ponder.d.ts.map +1 -0
- package/dist/types/bin/utils/codegen.d.ts +6 -0
- package/dist/types/bin/utils/codegen.d.ts.map +1 -0
- package/dist/types/bin/utils/exit.d.ts +10 -0
- package/dist/types/bin/utils/exit.d.ts.map +1 -0
- package/dist/types/build/config.d.ts +97 -0
- package/dist/types/build/config.d.ts.map +1 -0
- package/dist/types/build/factory.d.ts +15 -0
- package/dist/types/build/factory.d.ts.map +1 -0
- package/dist/types/build/index.d.ts +84 -0
- package/dist/types/build/index.d.ts.map +1 -0
- package/dist/types/build/plugin.d.ts +4 -0
- package/dist/types/build/plugin.d.ts.map +1 -0
- package/dist/types/build/pre.d.ts +29 -0
- package/dist/types/build/pre.d.ts.map +1 -0
- package/dist/types/build/schema.d.ts +20 -0
- package/dist/types/build/schema.d.ts.map +1 -0
- package/dist/types/build/stacktrace.d.ts +13 -0
- package/dist/types/build/stacktrace.d.ts.map +1 -0
- package/dist/types/client/index.d.ts +27 -0
- package/dist/types/client/index.d.ts.map +1 -0
- package/dist/types/config/address.d.ts +34 -0
- package/dist/types/config/address.d.ts.map +1 -0
- package/dist/types/config/eventFilter.d.ts +18 -0
- package/dist/types/config/eventFilter.d.ts.map +1 -0
- package/dist/types/config/index.d.ts +144 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/config/utilityTypes.d.ts +43 -0
- package/dist/types/config/utilityTypes.d.ts.map +1 -0
- package/dist/types/database/actions.d.ts +99 -0
- package/dist/types/database/actions.d.ts.map +1 -0
- package/dist/types/database/index.d.ts +493 -0
- package/dist/types/database/index.d.ts.map +1 -0
- package/dist/types/database/queryBuilder.d.ts +65 -0
- package/dist/types/database/queryBuilder.d.ts.map +1 -0
- package/dist/types/drizzle/bigint.d.ts +25 -0
- package/dist/types/drizzle/bigint.d.ts.map +1 -0
- package/dist/types/drizzle/bytes.d.ts +31 -0
- package/dist/types/drizzle/bytes.d.ts.map +1 -0
- package/dist/types/drizzle/hex.d.ts +25 -0
- package/dist/types/drizzle/hex.d.ts.map +1 -0
- package/dist/types/drizzle/index.d.ts +6 -0
- package/dist/types/drizzle/index.d.ts.map +1 -0
- package/dist/types/drizzle/json.d.ts +51 -0
- package/dist/types/drizzle/json.d.ts.map +1 -0
- package/dist/types/drizzle/kit/index.d.ts +187 -0
- package/dist/types/drizzle/kit/index.d.ts.map +1 -0
- package/dist/types/drizzle/onchain.d.ts +298 -0
- package/dist/types/drizzle/onchain.d.ts.map +1 -0
- package/dist/types/drizzle/text.d.ts +29 -0
- package/dist/types/drizzle/text.d.ts.map +1 -0
- package/dist/types/graphql/graphiql.html.d.ts +2 -0
- package/dist/types/graphql/graphiql.html.d.ts.map +1 -0
- package/dist/types/graphql/index.d.ts +12 -0
- package/dist/types/graphql/index.d.ts.map +1 -0
- package/dist/types/graphql/json.d.ts +3 -0
- package/dist/types/graphql/json.d.ts.map +1 -0
- package/dist/types/graphql/middleware.d.ts +29 -0
- package/dist/types/graphql/middleware.d.ts.map +1 -0
- package/dist/types/index.d.ts +23 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/indexing/addStackTrace.d.ts +3 -0
- package/dist/types/indexing/addStackTrace.d.ts.map +1 -0
- package/dist/types/indexing/client.d.ts +154 -0
- package/dist/types/indexing/client.d.ts.map +1 -0
- package/dist/types/indexing/index.d.ts +72 -0
- package/dist/types/indexing/index.d.ts.map +1 -0
- package/dist/types/indexing/profile.d.ts +16 -0
- package/dist/types/indexing/profile.d.ts.map +1 -0
- package/dist/types/indexing-store/cache.d.ts +115 -0
- package/dist/types/indexing-store/cache.d.ts.map +1 -0
- package/dist/types/indexing-store/index.d.ts +24 -0
- package/dist/types/indexing-store/index.d.ts.map +1 -0
- package/dist/types/indexing-store/profile.d.ts +7 -0
- package/dist/types/indexing-store/profile.d.ts.map +1 -0
- package/dist/types/indexing-store/utils.d.ts +19 -0
- package/dist/types/indexing-store/utils.d.ts.map +1 -0
- package/dist/types/internal/common.d.ts +15 -0
- package/dist/types/internal/common.d.ts.map +1 -0
- package/dist/types/internal/errors.d.ts +101 -0
- package/dist/types/internal/errors.d.ts.map +1 -0
- package/dist/types/internal/logger.d.ts +37 -0
- package/dist/types/internal/logger.d.ts.map +1 -0
- package/dist/types/internal/metrics.d.ts +120 -0
- package/dist/types/internal/metrics.d.ts.map +1 -0
- package/dist/types/internal/options.d.ts +62 -0
- package/dist/types/internal/options.d.ts.map +1 -0
- package/dist/types/internal/shutdown.d.ts +8 -0
- package/dist/types/internal/shutdown.d.ts.map +1 -0
- package/dist/types/internal/telemetry.d.ts +43 -0
- package/dist/types/internal/telemetry.d.ts.map +1 -0
- package/dist/types/internal/types.d.ts +443 -0
- package/dist/types/internal/types.d.ts.map +1 -0
- package/dist/types/rpc/actions.d.ts +360 -0
- package/dist/types/rpc/actions.d.ts.map +1 -0
- package/dist/types/rpc/http.d.ts +17 -0
- package/dist/types/rpc/http.d.ts.map +1 -0
- package/dist/types/rpc/index.d.ts +43 -0
- package/dist/types/rpc/index.d.ts.map +1 -0
- package/dist/types/runtime/events.d.ts +40 -0
- package/dist/types/runtime/events.d.ts.map +1 -0
- package/dist/types/runtime/filter.d.ts +96 -0
- package/dist/types/runtime/filter.d.ts.map +1 -0
- package/dist/types/runtime/fragments.d.ts +30 -0
- package/dist/types/runtime/fragments.d.ts.map +1 -0
- package/dist/types/runtime/historical.d.ts +123 -0
- package/dist/types/runtime/historical.d.ts.map +1 -0
- package/dist/types/runtime/index.d.ts +89 -0
- package/dist/types/runtime/index.d.ts.map +1 -0
- package/dist/types/runtime/init.d.ts +28 -0
- package/dist/types/runtime/init.d.ts.map +1 -0
- package/dist/types/runtime/isolated.d.ts +14 -0
- package/dist/types/runtime/isolated.d.ts.map +1 -0
- package/dist/types/runtime/multichain.d.ts +13 -0
- package/dist/types/runtime/multichain.d.ts.map +1 -0
- package/dist/types/runtime/omnichain.d.ts +23 -0
- package/dist/types/runtime/omnichain.d.ts.map +1 -0
- package/dist/types/runtime/realtime.d.ts +93 -0
- package/dist/types/runtime/realtime.d.ts.map +1 -0
- package/dist/types/server/error.d.ts +5 -0
- package/dist/types/server/error.d.ts.map +1 -0
- package/dist/types/server/index.d.ts +13 -0
- package/dist/types/server/index.d.ts.map +1 -0
- package/dist/types/sync-historical/index.d.ts +36 -0
- package/dist/types/sync-historical/index.d.ts.map +1 -0
- package/dist/types/sync-realtime/bloom.d.ts +18 -0
- package/dist/types/sync-realtime/bloom.d.ts.map +1 -0
- package/dist/types/sync-realtime/index.d.ts +48 -0
- package/dist/types/sync-realtime/index.d.ts.map +1 -0
- package/dist/types/sync-store/encode.d.ts +25 -0
- package/dist/types/sync-store/encode.d.ts.map +1 -0
- package/dist/types/sync-store/index.d.ts +135 -0
- package/dist/types/sync-store/index.d.ts.map +1 -0
- package/dist/types/sync-store/migrations.d.ts +8 -0
- package/dist/types/sync-store/migrations.d.ts.map +1 -0
- package/dist/types/sync-store/schema.d.ts +1828 -0
- package/dist/types/sync-store/schema.d.ts.map +1 -0
- package/dist/types/types/db.d.ts +213 -0
- package/dist/types/types/db.d.ts.map +1 -0
- package/dist/types/types/eth.d.ts +196 -0
- package/dist/types/types/eth.d.ts.map +1 -0
- package/dist/types/types/utils.d.ts +38 -0
- package/dist/types/types/utils.d.ts.map +1 -0
- package/dist/types/types/virtual.d.ts +99 -0
- package/dist/types/types/virtual.d.ts.map +1 -0
- package/dist/types/ui/app.d.ts +22 -0
- package/dist/types/ui/app.d.ts.map +1 -0
- package/dist/types/ui/index.d.ts +5 -0
- package/dist/types/ui/index.d.ts.map +1 -0
- package/dist/types/ui/patch.d.ts +7 -0
- package/dist/types/ui/patch.d.ts.map +1 -0
- package/dist/types/utils/abi.d.ts +23 -0
- package/dist/types/utils/abi.d.ts.map +1 -0
- package/dist/types/utils/bigint.d.ts +15 -0
- package/dist/types/utils/bigint.d.ts.map +1 -0
- package/dist/types/utils/chains.d.ts +42 -0
- package/dist/types/utils/chains.d.ts.map +1 -0
- package/dist/types/utils/checkpoint.d.ts +52 -0
- package/dist/types/utils/checkpoint.d.ts.map +1 -0
- package/dist/types/utils/chunk.d.ts +2 -0
- package/dist/types/utils/chunk.d.ts.map +1 -0
- package/dist/types/utils/copy.d.ts +16 -0
- package/dist/types/utils/copy.d.ts.map +1 -0
- package/dist/types/utils/date.d.ts +7 -0
- package/dist/types/utils/date.d.ts.map +1 -0
- package/dist/types/utils/debug.d.ts +105 -0
- package/dist/types/utils/debug.d.ts.map +1 -0
- package/dist/types/utils/decodeAbiParameters.d.ts +28 -0
- package/dist/types/utils/decodeAbiParameters.d.ts.map +1 -0
- package/dist/types/utils/decodeEventLog.d.ts +12 -0
- package/dist/types/utils/decodeEventLog.d.ts.map +1 -0
- package/dist/types/utils/dedupe.d.ts +20 -0
- package/dist/types/utils/dedupe.d.ts.map +1 -0
- package/dist/types/utils/duplicates.d.ts +7 -0
- package/dist/types/utils/duplicates.d.ts.map +1 -0
- package/dist/types/utils/estimate.d.ts +11 -0
- package/dist/types/utils/estimate.d.ts.map +1 -0
- package/dist/types/utils/finality.d.ts +12 -0
- package/dist/types/utils/finality.d.ts.map +1 -0
- package/dist/types/utils/format.d.ts +3 -0
- package/dist/types/utils/format.d.ts.map +1 -0
- package/dist/types/utils/generators.d.ts +42 -0
- package/dist/types/utils/generators.d.ts.map +1 -0
- package/dist/types/utils/hash.d.ts +11 -0
- package/dist/types/utils/hash.d.ts.map +1 -0
- package/dist/types/utils/interval.d.ts +53 -0
- package/dist/types/utils/interval.d.ts.map +1 -0
- package/dist/types/utils/lowercase.d.ts +5 -0
- package/dist/types/utils/lowercase.d.ts.map +1 -0
- package/dist/types/utils/mutex.d.ts +5 -0
- package/dist/types/utils/mutex.d.ts.map +1 -0
- package/dist/types/utils/never.d.ts +2 -0
- package/dist/types/utils/never.d.ts.map +1 -0
- package/dist/types/utils/offset.d.ts +8 -0
- package/dist/types/utils/offset.d.ts.map +1 -0
- package/dist/types/utils/order.d.ts +2 -0
- package/dist/types/utils/order.d.ts.map +1 -0
- package/dist/types/utils/partition.d.ts +22 -0
- package/dist/types/utils/partition.d.ts.map +1 -0
- package/dist/types/utils/pg.d.ts +8 -0
- package/dist/types/utils/pg.d.ts.map +1 -0
- package/dist/types/utils/pglite.d.ts +25 -0
- package/dist/types/utils/pglite.d.ts.map +1 -0
- package/dist/types/utils/port.d.ts +5 -0
- package/dist/types/utils/port.d.ts.map +1 -0
- package/dist/types/utils/print.d.ts +2 -0
- package/dist/types/utils/print.d.ts.map +1 -0
- package/dist/types/utils/promiseAllSettledWithThrow.d.ts +8 -0
- package/dist/types/utils/promiseAllSettledWithThrow.d.ts.map +1 -0
- package/dist/types/utils/promiseWithResolvers.d.ts +10 -0
- package/dist/types/utils/promiseWithResolvers.d.ts.map +1 -0
- package/dist/types/utils/queue.d.ts +33 -0
- package/dist/types/utils/queue.d.ts.map +1 -0
- package/dist/types/utils/range.d.ts +8 -0
- package/dist/types/utils/range.d.ts.map +1 -0
- package/dist/types/utils/result.d.ts +17 -0
- package/dist/types/utils/result.d.ts.map +1 -0
- package/dist/types/utils/sql-parse.d.ts +21 -0
- package/dist/types/utils/sql-parse.d.ts.map +1 -0
- package/dist/types/utils/timer.d.ts +6 -0
- package/dist/types/utils/timer.d.ts.map +1 -0
- package/dist/types/utils/truncate.d.ts +9 -0
- package/dist/types/utils/truncate.d.ts.map +1 -0
- package/dist/types/utils/wait.d.ts +6 -0
- package/dist/types/utils/wait.d.ts.map +1 -0
- package/dist/types/utils/zipper.d.ts +36 -0
- package/dist/types/utils/zipper.d.ts.map +1 -0
- package/package.json +114 -0
- package/src/bin/commands/codegen.ts +56 -0
- package/src/bin/commands/createViews.ts +318 -0
- package/src/bin/commands/dev.ts +490 -0
- package/src/bin/commands/list.ts +208 -0
- package/src/bin/commands/prune.ts +322 -0
- package/src/bin/commands/serve.ts +236 -0
- package/src/bin/commands/start.ts +319 -0
- package/src/bin/isolatedController.ts +300 -0
- package/src/bin/isolatedWorker.ts +192 -0
- package/src/bin/ponder.ts +208 -0
- package/src/bin/utils/codegen.ts +32 -0
- package/src/bin/utils/exit.ts +112 -0
- package/src/build/config.ts +1141 -0
- package/src/build/factory.ts +147 -0
- package/src/build/index.ts +790 -0
- package/src/build/plugin.ts +58 -0
- package/src/build/pre.ts +114 -0
- package/src/build/schema.ts +358 -0
- package/src/build/stacktrace.ts +137 -0
- package/src/client/index.ts +551 -0
- package/src/config/address.ts +45 -0
- package/src/config/eventFilter.ts +33 -0
- package/src/config/index.ts +240 -0
- package/src/config/utilityTypes.ts +152 -0
- package/src/database/actions.ts +873 -0
- package/src/database/index.ts +1029 -0
- package/src/database/queryBuilder.ts +537 -0
- package/src/drizzle/bigint.ts +57 -0
- package/src/drizzle/bytes.ts +68 -0
- package/src/drizzle/hex.ts +58 -0
- package/src/drizzle/index.ts +40 -0
- package/src/drizzle/json.ts +159 -0
- package/src/drizzle/kit/index.ts +1348 -0
- package/src/drizzle/onchain.ts +476 -0
- package/src/drizzle/text.ts +77 -0
- package/src/graphql/graphiql.html.ts +59 -0
- package/src/graphql/index.ts +1351 -0
- package/src/graphql/json.ts +62 -0
- package/src/graphql/middleware.ts +115 -0
- package/src/index.ts +139 -0
- package/src/indexing/addStackTrace.ts +69 -0
- package/src/indexing/client.ts +1184 -0
- package/src/indexing/index.ts +976 -0
- package/src/indexing/profile.ts +771 -0
- package/src/indexing-store/cache.ts +1057 -0
- package/src/indexing-store/index.ts +628 -0
- package/src/indexing-store/profile.ts +557 -0
- package/src/indexing-store/utils.ts +162 -0
- package/src/internal/common.ts +15 -0
- package/src/internal/errors.ts +228 -0
- package/src/internal/logger.ts +252 -0
- package/src/internal/metrics.ts +1030 -0
- package/src/internal/options.ts +130 -0
- package/src/internal/shutdown.ts +32 -0
- package/src/internal/telemetry.ts +303 -0
- package/src/internal/types.ts +611 -0
- package/src/rpc/actions.ts +1344 -0
- package/src/rpc/http.ts +164 -0
- package/src/rpc/index.ts +959 -0
- package/src/runtime/events.ts +875 -0
- package/src/runtime/filter.ts +705 -0
- package/src/runtime/fragments.ts +674 -0
- package/src/runtime/historical.ts +1522 -0
- package/src/runtime/index.ts +569 -0
- package/src/runtime/init.ts +49 -0
- package/src/runtime/isolated.ts +775 -0
- package/src/runtime/multichain.ts +860 -0
- package/src/runtime/omnichain.ts +920 -0
- package/src/runtime/realtime.ts +1164 -0
- package/src/server/error.ts +68 -0
- package/src/server/index.ts +173 -0
- package/src/sync-historical/index.ts +1065 -0
- package/src/sync-realtime/bloom.ts +102 -0
- package/src/sync-realtime/index.ts +1304 -0
- package/src/sync-store/encode.ts +153 -0
- package/src/sync-store/index.ts +1633 -0
- package/src/sync-store/migrations.ts +1801 -0
- package/src/sync-store/schema.ts +248 -0
- package/src/types/db.ts +292 -0
- package/src/types/eth.ts +216 -0
- package/src/types/utils.ts +47 -0
- package/src/types/virtual.ts +244 -0
- package/src/types.d.ts +38 -0
- package/src/ui/app.ts +207 -0
- package/src/ui/index.ts +37 -0
- package/src/ui/patch.ts +204 -0
- package/src/utils/abi.ts +103 -0
- package/src/utils/bigint.ts +41 -0
- package/src/utils/chains.ts +22 -0
- package/src/utils/checkpoint.ts +203 -0
- package/src/utils/chunk.ts +7 -0
- package/src/utils/copy.ts +151 -0
- package/src/utils/date.ts +26 -0
- package/src/utils/debug.ts +110 -0
- package/src/utils/decodeAbiParameters.ts +428 -0
- package/src/utils/decodeEventLog.ts +100 -0
- package/src/utils/dedupe.ts +32 -0
- package/src/utils/duplicates.ts +19 -0
- package/src/utils/estimate.ts +27 -0
- package/src/utils/finality.ts +40 -0
- package/src/utils/format.ts +22 -0
- package/src/utils/generators.ts +157 -0
- package/src/utils/hash.ts +22 -0
- package/src/utils/interval.ts +212 -0
- package/src/utils/lowercase.ts +6 -0
- package/src/utils/mutex.ts +33 -0
- package/src/utils/never.ts +3 -0
- package/src/utils/offset.ts +133 -0
- package/src/utils/order.ts +16 -0
- package/src/utils/partition.ts +53 -0
- package/src/utils/pg.ts +230 -0
- package/src/utils/pglite.ts +97 -0
- package/src/utils/port.ts +34 -0
- package/src/utils/print.ts +31 -0
- package/src/utils/promiseAllSettledWithThrow.ts +27 -0
- package/src/utils/promiseWithResolvers.ts +20 -0
- package/src/utils/queue.ts +258 -0
- package/src/utils/range.ts +8 -0
- package/src/utils/result.ts +26 -0
- package/src/utils/sql-parse.ts +1477 -0
- package/src/utils/timer.ts +8 -0
- package/src/utils/truncate.ts +15 -0
- package/src/utils/wait.ts +8 -0
- package/src/utils/zipper.ts +80 -0
|
@@ -0,0 +1,1057 @@
|
|
|
1
|
+
import type { QB } from "@/database/queryBuilder.js";
|
|
2
|
+
import { getPrimaryKeyColumns } from "@/drizzle/index.js";
|
|
3
|
+
import { getColumnCasing } from "@/drizzle/kit/index.js";
|
|
4
|
+
import { getPartitionName } from "@/drizzle/onchain.js";
|
|
5
|
+
import { addErrorMeta, toErrorMeta } from "@/indexing/index.js";
|
|
6
|
+
import type { Common } from "@/internal/common.js";
|
|
7
|
+
import {
|
|
8
|
+
CopyFlushError,
|
|
9
|
+
DelayedInsertError,
|
|
10
|
+
ShutdownError,
|
|
11
|
+
} from "@/internal/errors.js";
|
|
12
|
+
import type {
|
|
13
|
+
CrashRecoveryCheckpoint,
|
|
14
|
+
Event,
|
|
15
|
+
SchemaBuild,
|
|
16
|
+
} from "@/internal/types.js";
|
|
17
|
+
import { dedupe } from "@/utils/dedupe.js";
|
|
18
|
+
import { prettyPrint } from "@/utils/print.js";
|
|
19
|
+
import { promiseAllSettledWithThrow } from "@/utils/promiseAllSettledWithThrow.js";
|
|
20
|
+
import { startClock } from "@/utils/timer.js";
|
|
21
|
+
import {
|
|
22
|
+
type Table,
|
|
23
|
+
getTableColumns,
|
|
24
|
+
getTableName,
|
|
25
|
+
isTable,
|
|
26
|
+
or,
|
|
27
|
+
sql,
|
|
28
|
+
} from "drizzle-orm";
|
|
29
|
+
import { getTableConfig } from "drizzle-orm/pg-core";
|
|
30
|
+
import copy from "pg-copy-streams";
|
|
31
|
+
import {
|
|
32
|
+
getProfilePatternKey,
|
|
33
|
+
recordProfilePattern,
|
|
34
|
+
recoverProfilePattern,
|
|
35
|
+
} from "./profile.js";
|
|
36
|
+
import {
|
|
37
|
+
getCacheKey,
|
|
38
|
+
getPrimaryKeyCache,
|
|
39
|
+
getWhereCondition,
|
|
40
|
+
normalizeRow,
|
|
41
|
+
} from "./utils.js";
|
|
42
|
+
|
|
43
|
+
export type IndexingCache = {
|
|
44
|
+
/**
|
|
45
|
+
* Returns true if the cache has an entry for `table` with `key`.
|
|
46
|
+
*/
|
|
47
|
+
has: (params: { table: Table; key: object }) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Returns the entry for `table` with `key`.
|
|
50
|
+
*/
|
|
51
|
+
get: (params: { table: Table; key: object }) =>
|
|
52
|
+
| Row
|
|
53
|
+
| null
|
|
54
|
+
| Promise<Row | null>;
|
|
55
|
+
/**
|
|
56
|
+
* Sets the entry for `table` with `key` to `row`.
|
|
57
|
+
*/
|
|
58
|
+
set: (params: {
|
|
59
|
+
table: Table;
|
|
60
|
+
key: object;
|
|
61
|
+
row: Row;
|
|
62
|
+
isUpdate: boolean;
|
|
63
|
+
}) => Row;
|
|
64
|
+
/**
|
|
65
|
+
* Deletes the entry for `table` with `key`.
|
|
66
|
+
*/
|
|
67
|
+
delete: (params: { table: Table; key: object }) => boolean | Promise<boolean>;
|
|
68
|
+
/**
|
|
69
|
+
* Writes all temporary data to the database.
|
|
70
|
+
*
|
|
71
|
+
* @param params.tableNames - If provided, only flush the tables in the set.
|
|
72
|
+
*/
|
|
73
|
+
flush: (params?: { tableNames?: Set<string> }) => Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Predict and load rows that will be accessed in the next event batch.
|
|
76
|
+
*/
|
|
77
|
+
prefetch: (params: { events: Event[] }) => Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Marks the cache as incomplete.
|
|
80
|
+
*/
|
|
81
|
+
invalidate: () => void;
|
|
82
|
+
/**
|
|
83
|
+
* Deletes all entries from the cache.
|
|
84
|
+
*/
|
|
85
|
+
clear: () => void;
|
|
86
|
+
event: Event | undefined;
|
|
87
|
+
qb: QB;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const SAMPLING_RATE = 10;
|
|
91
|
+
const PREDICTION_THRESHOLD = 0.25;
|
|
92
|
+
const LOW_BATCH_THRESHOLD = 20;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Database row.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* {
|
|
99
|
+
* "owner": "0x123",
|
|
100
|
+
* "spender": "0x456",
|
|
101
|
+
* "amount": 100n,
|
|
102
|
+
* }
|
|
103
|
+
*/
|
|
104
|
+
export type Row = { [key: string]: unknown };
|
|
105
|
+
/**
|
|
106
|
+
* Serialized primary key values for uniquely identifying a database row.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* "0x123_0x456"
|
|
110
|
+
*/
|
|
111
|
+
type CacheKey = string;
|
|
112
|
+
/**
|
|
113
|
+
* Event name.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* "Erc20:Transfer"
|
|
117
|
+
*
|
|
118
|
+
* @example
|
|
119
|
+
* "Erc20.mint()"
|
|
120
|
+
*/
|
|
121
|
+
type EventName = string;
|
|
122
|
+
/**
|
|
123
|
+
* Recorded database access pattern.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* {
|
|
127
|
+
* "owner": ["args", "from"],
|
|
128
|
+
* "spender": ["log", "address"],
|
|
129
|
+
* }
|
|
130
|
+
*/
|
|
131
|
+
export type ProfilePattern = {
|
|
132
|
+
[key: string]:
|
|
133
|
+
| {
|
|
134
|
+
type: "derived";
|
|
135
|
+
value: string[];
|
|
136
|
+
fn?: (value: unknown) => unknown;
|
|
137
|
+
}
|
|
138
|
+
| {
|
|
139
|
+
type: "delimeter";
|
|
140
|
+
values: { value: string[]; fn?: (value: unknown) => unknown }[];
|
|
141
|
+
delimiter: string;
|
|
142
|
+
};
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Serialized for uniquely identifying a {@link ProfilePattern}.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* "{
|
|
150
|
+
* "owner": ["args", "from"],
|
|
151
|
+
* "spender": ["log", "address"],
|
|
152
|
+
* }"
|
|
153
|
+
*/
|
|
154
|
+
type ProfileKey = string;
|
|
155
|
+
/**
|
|
156
|
+
* Cache of database rows.
|
|
157
|
+
*/
|
|
158
|
+
type Cache = Map<
|
|
159
|
+
Table,
|
|
160
|
+
{
|
|
161
|
+
cache: Map<CacheKey, Row | null>;
|
|
162
|
+
/** Cached keys that were prefetched. */
|
|
163
|
+
prefetched: Set<CacheKey>;
|
|
164
|
+
/** Cached keys that were not prefetched but were accessed anyway. */
|
|
165
|
+
spillover: Set<CacheKey>;
|
|
166
|
+
/** `true` if the cache completely mirrors the database. */
|
|
167
|
+
isCacheComplete: boolean;
|
|
168
|
+
/**
|
|
169
|
+
* Estimated size of the cache in bytes.
|
|
170
|
+
*
|
|
171
|
+
* Note: this stops getting updated once `isCacheComplete = false`.
|
|
172
|
+
*/
|
|
173
|
+
bytes: number;
|
|
174
|
+
/** Number of times `get` missed the cached and read from the database. */
|
|
175
|
+
diskReads: number;
|
|
176
|
+
}
|
|
177
|
+
>;
|
|
178
|
+
/**
|
|
179
|
+
* Buffer of database rows that will be flushed to the database.
|
|
180
|
+
*/
|
|
181
|
+
type Buffer = Map<
|
|
182
|
+
Table,
|
|
183
|
+
Map<
|
|
184
|
+
CacheKey,
|
|
185
|
+
{
|
|
186
|
+
row: Row;
|
|
187
|
+
metadata: { event: Event | undefined };
|
|
188
|
+
}
|
|
189
|
+
>
|
|
190
|
+
>;
|
|
191
|
+
/**
|
|
192
|
+
* Metadata about database access patterns for each event.
|
|
193
|
+
*/
|
|
194
|
+
type Profile = Map<
|
|
195
|
+
EventName,
|
|
196
|
+
Map<Table, Map<ProfileKey, { pattern: ProfilePattern; count: number }>>
|
|
197
|
+
>;
|
|
198
|
+
|
|
199
|
+
const getBytes = (value: unknown) => {
|
|
200
|
+
let size = 0;
|
|
201
|
+
|
|
202
|
+
if (typeof value === "number") {
|
|
203
|
+
size += 8;
|
|
204
|
+
} else if (typeof value === "string") {
|
|
205
|
+
size += 2 * value.length;
|
|
206
|
+
} else if (typeof value === "boolean") {
|
|
207
|
+
size += 4;
|
|
208
|
+
} else if (typeof value === "bigint") {
|
|
209
|
+
size += 48;
|
|
210
|
+
} else if (value === null || value === undefined) {
|
|
211
|
+
size += 8;
|
|
212
|
+
} else if (Array.isArray(value)) {
|
|
213
|
+
size += 24; // NodeJs object overhead (3 * 8 bytes)
|
|
214
|
+
|
|
215
|
+
for (const e of value) {
|
|
216
|
+
// value + 8 bytes for the key
|
|
217
|
+
size += getBytes(e) + 8;
|
|
218
|
+
}
|
|
219
|
+
} else {
|
|
220
|
+
size += 24; // NodeJs object overhead (3 * 8 bytes)
|
|
221
|
+
for (const col of Object.values(value)) {
|
|
222
|
+
// value + 8 bytes for the key
|
|
223
|
+
size += getBytes(col) + 8;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return size;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const ESCAPE_REGEX = /([\\\b\f\n\r\t\v])/g;
|
|
231
|
+
|
|
232
|
+
export const getCopyText = (table: Table, rows: Row[]) => {
|
|
233
|
+
let result = "";
|
|
234
|
+
const columns = Object.entries(getTableColumns(table));
|
|
235
|
+
for (let i = 0; i < rows.length; i++) {
|
|
236
|
+
const isLastRow = i === rows.length - 1;
|
|
237
|
+
const row = rows[i]!;
|
|
238
|
+
for (let j = 0; j < columns.length; j++) {
|
|
239
|
+
const isLastColumn = j === columns.length - 1;
|
|
240
|
+
const [columnName, column] = columns[j]!;
|
|
241
|
+
let value = row[columnName];
|
|
242
|
+
if (isLastColumn) {
|
|
243
|
+
if (value === null || value === undefined) {
|
|
244
|
+
result += "\\N";
|
|
245
|
+
} else {
|
|
246
|
+
if (column.mapToDriverValue !== undefined) {
|
|
247
|
+
value = column.mapToDriverValue(value);
|
|
248
|
+
if (value === null || value === undefined) {
|
|
249
|
+
result += "\\N";
|
|
250
|
+
} else {
|
|
251
|
+
result += `${String(value).replace(ESCAPE_REGEX, "\\$1")}`;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
} else {
|
|
256
|
+
if (value === null || value === undefined) {
|
|
257
|
+
result += "\\N\t";
|
|
258
|
+
} else {
|
|
259
|
+
if (column.mapToDriverValue !== undefined) {
|
|
260
|
+
value = column.mapToDriverValue(value);
|
|
261
|
+
}
|
|
262
|
+
if (value === null || value === undefined) {
|
|
263
|
+
result += "\\N\t";
|
|
264
|
+
} else {
|
|
265
|
+
result += `${String(value).replace(ESCAPE_REGEX, "\\$1")}\t`;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (isLastRow === false) {
|
|
271
|
+
result += "\n";
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
return result;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
export const getCopyHelper = (qb: QB, chainId?: number) => {
|
|
279
|
+
if (qb.$dialect === "pglite") {
|
|
280
|
+
return async (table: Table, text: string, includeSchema = true) => {
|
|
281
|
+
const target = includeSchema
|
|
282
|
+
? `"${getTableConfig(table).schema ?? "public"}"."${chainId === undefined ? getTableName(table) : getPartitionName(table, chainId)}"`
|
|
283
|
+
: `"${chainId === undefined ? getTableName(table) : getPartitionName(table, chainId)}"`;
|
|
284
|
+
await qb.$client
|
|
285
|
+
.query(`COPY ${target} FROM '/dev/blob'`, [], {
|
|
286
|
+
blob: new Blob([text]),
|
|
287
|
+
})
|
|
288
|
+
// Note: `TransactionError` is applied because the query
|
|
289
|
+
// uses the low-level `$client.query` method.
|
|
290
|
+
.catch((error) => {
|
|
291
|
+
throw new CopyFlushError(error.message);
|
|
292
|
+
});
|
|
293
|
+
};
|
|
294
|
+
} else {
|
|
295
|
+
return async (table: Table, text: string, includeSchema = true) => {
|
|
296
|
+
const target = includeSchema
|
|
297
|
+
? `"${getTableConfig(table).schema ?? "public"}"."${chainId === undefined ? getTableName(table) : getPartitionName(table, chainId)}"`
|
|
298
|
+
: `"${chainId === undefined ? getTableName(table) : getPartitionName(table, chainId)}"`;
|
|
299
|
+
const copyStream = qb.$client.query(
|
|
300
|
+
copy.from(`COPY ${target} FROM STDIN`),
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
await new Promise((resolve, reject) => {
|
|
304
|
+
copyStream.on("finish", resolve);
|
|
305
|
+
copyStream.on("error", reject);
|
|
306
|
+
|
|
307
|
+
copyStream.write(text);
|
|
308
|
+
copyStream.end();
|
|
309
|
+
}).catch((error) => {
|
|
310
|
+
throw new CopyFlushError(error.message);
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export const recoverBatchError = async <T>(
|
|
317
|
+
values: T[],
|
|
318
|
+
callback: (values: T[]) => Promise<unknown>,
|
|
319
|
+
): Promise<
|
|
320
|
+
{ status: "success" } | { status: "error"; error: Error; value: T }
|
|
321
|
+
> => {
|
|
322
|
+
try {
|
|
323
|
+
await callback(values);
|
|
324
|
+
return { status: "success" };
|
|
325
|
+
} catch (error) {
|
|
326
|
+
if (values.length === 1) {
|
|
327
|
+
return { status: "error", error: error as Error, value: values[0]! };
|
|
328
|
+
}
|
|
329
|
+
const left = values.slice(0, Math.floor(values.length / 2));
|
|
330
|
+
const right = values.slice(Math.floor(values.length / 2));
|
|
331
|
+
const resultLeft = await recoverBatchError(left, callback);
|
|
332
|
+
if (resultLeft.status === "error") {
|
|
333
|
+
return resultLeft;
|
|
334
|
+
}
|
|
335
|
+
const resultRight = await recoverBatchError(right, callback);
|
|
336
|
+
if (resultRight.status === "error") {
|
|
337
|
+
return resultRight;
|
|
338
|
+
}
|
|
339
|
+
return { status: "success" };
|
|
340
|
+
}
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
export const createIndexingCache = ({
|
|
344
|
+
common,
|
|
345
|
+
schemaBuild: { schema },
|
|
346
|
+
crashRecoveryCheckpoint,
|
|
347
|
+
eventCount,
|
|
348
|
+
chainId,
|
|
349
|
+
}: {
|
|
350
|
+
common: Common;
|
|
351
|
+
schemaBuild: Pick<SchemaBuild, "schema">;
|
|
352
|
+
crashRecoveryCheckpoint: CrashRecoveryCheckpoint;
|
|
353
|
+
eventCount: { [eventName: string]: number };
|
|
354
|
+
chainId?: number;
|
|
355
|
+
}): IndexingCache => {
|
|
356
|
+
let event: Event | undefined;
|
|
357
|
+
let qb: QB = undefined!;
|
|
358
|
+
|
|
359
|
+
const cache: Cache = new Map();
|
|
360
|
+
const insertBuffer: Buffer = new Map();
|
|
361
|
+
const updateBuffer: Buffer = new Map();
|
|
362
|
+
/** Profiling data about access patterns for each event. */
|
|
363
|
+
const profile: Profile = new Map();
|
|
364
|
+
|
|
365
|
+
const tables = Object.values(schema).filter(isTable);
|
|
366
|
+
const primaryKeyCache = getPrimaryKeyCache(tables);
|
|
367
|
+
|
|
368
|
+
for (const table of tables) {
|
|
369
|
+
cache.set(table, {
|
|
370
|
+
cache: new Map(),
|
|
371
|
+
prefetched: new Set(),
|
|
372
|
+
spillover: new Set(),
|
|
373
|
+
isCacheComplete: crashRecoveryCheckpoint === undefined,
|
|
374
|
+
bytes: 0,
|
|
375
|
+
diskReads: 0,
|
|
376
|
+
});
|
|
377
|
+
insertBuffer.set(table, new Map());
|
|
378
|
+
updateBuffer.set(table, new Map());
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
has({ table, key }) {
|
|
383
|
+
if (cache.get(table)!.isCacheComplete) return true;
|
|
384
|
+
const ck = getCacheKey(table, key, primaryKeyCache);
|
|
385
|
+
|
|
386
|
+
return (
|
|
387
|
+
cache.get(table)!.cache.has(ck) ??
|
|
388
|
+
insertBuffer.get(table)!.has(ck) ??
|
|
389
|
+
updateBuffer.get(table)!.has(ck)
|
|
390
|
+
);
|
|
391
|
+
},
|
|
392
|
+
async get({ table, key }) {
|
|
393
|
+
if (
|
|
394
|
+
event &&
|
|
395
|
+
eventCount[event.eventCallback.name]! % SAMPLING_RATE === 1
|
|
396
|
+
) {
|
|
397
|
+
if (profile.has(event.eventCallback.name) === false) {
|
|
398
|
+
profile.set(event.eventCallback.name, new Map());
|
|
399
|
+
for (const table of tables) {
|
|
400
|
+
profile.get(event.eventCallback.name)!.set(table, new Map());
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const pattern = recordProfilePattern(
|
|
405
|
+
event,
|
|
406
|
+
table,
|
|
407
|
+
key,
|
|
408
|
+
Array.from(
|
|
409
|
+
profile.get(event.eventCallback.name)!.get(table)!.values(),
|
|
410
|
+
).map(({ pattern }) => pattern),
|
|
411
|
+
primaryKeyCache,
|
|
412
|
+
);
|
|
413
|
+
if (pattern) {
|
|
414
|
+
const key = getProfilePatternKey(pattern);
|
|
415
|
+
if (profile.get(event.eventCallback.name)!.get(table)!.has(key)) {
|
|
416
|
+
profile.get(event.eventCallback.name)!.get(table)!.get(key)!
|
|
417
|
+
.count++;
|
|
418
|
+
} else {
|
|
419
|
+
profile
|
|
420
|
+
.get(event.eventCallback.name)!
|
|
421
|
+
.get(table)!
|
|
422
|
+
.set(key, { pattern, count: 1 });
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
const ck = getCacheKey(table, key, primaryKeyCache);
|
|
428
|
+
// Note: order is important, it is an invariant that update entries
|
|
429
|
+
// are prioritized over insert entries
|
|
430
|
+
const bufferEntry =
|
|
431
|
+
updateBuffer.get(table)!.get(ck) ?? insertBuffer.get(table)!.get(ck);
|
|
432
|
+
|
|
433
|
+
if (bufferEntry) {
|
|
434
|
+
common.metrics.ponder_indexing_cache_requests_total.inc({
|
|
435
|
+
table: getTableName(table),
|
|
436
|
+
type: cache.get(table)!.isCacheComplete ? "complete" : "hit",
|
|
437
|
+
});
|
|
438
|
+
return bufferEntry.row;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const entry = cache.get(table)!.cache.get(ck);
|
|
442
|
+
|
|
443
|
+
if (entry !== undefined) {
|
|
444
|
+
if (
|
|
445
|
+
cache.get(table)!.prefetched.has(ck) === false &&
|
|
446
|
+
cache.get(table)!.isCacheComplete === false
|
|
447
|
+
) {
|
|
448
|
+
cache.get(table)!.spillover.add(ck);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
common.metrics.ponder_indexing_cache_requests_total.inc({
|
|
452
|
+
table: getTableName(table),
|
|
453
|
+
type: cache.get(table)!.isCacheComplete ? "complete" : "hit",
|
|
454
|
+
});
|
|
455
|
+
return entry;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
cache.get(table)!.diskReads++;
|
|
459
|
+
|
|
460
|
+
if (cache.get(table)!.isCacheComplete) {
|
|
461
|
+
common.metrics.ponder_indexing_cache_requests_total.inc({
|
|
462
|
+
table: getTableName(table),
|
|
463
|
+
type: "complete",
|
|
464
|
+
});
|
|
465
|
+
return null;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
cache.get(table)!.spillover.add(ck);
|
|
469
|
+
|
|
470
|
+
common.metrics.ponder_indexing_cache_requests_total.inc({
|
|
471
|
+
table: getTableName(table),
|
|
472
|
+
type: "miss",
|
|
473
|
+
});
|
|
474
|
+
|
|
475
|
+
const endClock = startClock();
|
|
476
|
+
|
|
477
|
+
const result = await qb
|
|
478
|
+
.wrap((db) =>
|
|
479
|
+
db.select().from(table).where(getWhereCondition(table, key)),
|
|
480
|
+
)
|
|
481
|
+
.then((res) => (res.length === 0 ? null : res[0]!))
|
|
482
|
+
.then((row) => {
|
|
483
|
+
cache.get(table)!.cache.set(ck, row);
|
|
484
|
+
|
|
485
|
+
// Note: the size is not recorded because it is not possible
|
|
486
|
+
// to miss the cache when in the "full in-memory" mode
|
|
487
|
+
|
|
488
|
+
return row;
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
common.metrics.ponder_indexing_cache_query_duration.observe(
|
|
492
|
+
{
|
|
493
|
+
table: getTableName(table),
|
|
494
|
+
method: "find",
|
|
495
|
+
},
|
|
496
|
+
endClock(),
|
|
497
|
+
);
|
|
498
|
+
return result;
|
|
499
|
+
},
|
|
500
|
+
set({ table, key, row: _row, isUpdate }) {
|
|
501
|
+
const row = normalizeRow(table, _row, isUpdate);
|
|
502
|
+
const ck = getCacheKey(table, key, primaryKeyCache);
|
|
503
|
+
|
|
504
|
+
if (isUpdate) {
|
|
505
|
+
updateBuffer.get(table)!.set(ck, { row, metadata: { event } });
|
|
506
|
+
} else {
|
|
507
|
+
insertBuffer.get(table)!.set(ck, { row, metadata: { event } });
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
return row;
|
|
511
|
+
},
|
|
512
|
+
async delete({ table, key }) {
|
|
513
|
+
const ck = getCacheKey(table, key, primaryKeyCache);
|
|
514
|
+
|
|
515
|
+
const inInsertBuffer = insertBuffer.get(table)!.delete(ck);
|
|
516
|
+
const inUpdateBuffer = updateBuffer.get(table)!.delete(ck);
|
|
517
|
+
|
|
518
|
+
cache.get(table)!.cache.delete(ck);
|
|
519
|
+
|
|
520
|
+
const inDb = await qb
|
|
521
|
+
.wrap((db) =>
|
|
522
|
+
db.delete(table).where(getWhereCondition(table, key)).returning(),
|
|
523
|
+
)
|
|
524
|
+
.then((result) => result.length > 0);
|
|
525
|
+
|
|
526
|
+
return inInsertBuffer || inUpdateBuffer || inDb;
|
|
527
|
+
},
|
|
528
|
+
async flush({ tableNames } = {}) {
|
|
529
|
+
const context = {
|
|
530
|
+
logger: common.logger.child({ action: "flush_database_rows" }),
|
|
531
|
+
};
|
|
532
|
+
const flushEndClock = startClock();
|
|
533
|
+
|
|
534
|
+
const copy = getCopyHelper(qb, chainId);
|
|
535
|
+
|
|
536
|
+
const flushTable = async (table: Table) => {
|
|
537
|
+
const shouldRecordBytes = cache.get(table)!.isCacheComplete;
|
|
538
|
+
if (
|
|
539
|
+
tableNames !== undefined &&
|
|
540
|
+
tableNames.has(getTableName(table)) === false
|
|
541
|
+
) {
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
const tableCache = cache.get(table)!;
|
|
546
|
+
|
|
547
|
+
const target =
|
|
548
|
+
chainId === undefined
|
|
549
|
+
? getTableName(table)
|
|
550
|
+
: getPartitionName(table, chainId);
|
|
551
|
+
|
|
552
|
+
const insertValues = Array.from(insertBuffer.get(table)!.values());
|
|
553
|
+
const updateValues = Array.from(updateBuffer.get(table)!.values());
|
|
554
|
+
|
|
555
|
+
if (insertValues.length > 0) {
|
|
556
|
+
const endClock = startClock();
|
|
557
|
+
|
|
558
|
+
if (insertValues.length > LOW_BATCH_THRESHOLD) {
|
|
559
|
+
const text = getCopyText(
|
|
560
|
+
table,
|
|
561
|
+
insertValues.map(({ row }) => row),
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
await new Promise(setImmediate);
|
|
565
|
+
|
|
566
|
+
await copy(table, text);
|
|
567
|
+
} else {
|
|
568
|
+
await qb.wrap(
|
|
569
|
+
(db) =>
|
|
570
|
+
db.insert(table).values(insertValues.map(({ row }) => row)),
|
|
571
|
+
context,
|
|
572
|
+
);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
common.metrics.ponder_indexing_cache_query_duration.observe(
|
|
576
|
+
{
|
|
577
|
+
table: getTableName(table),
|
|
578
|
+
method: "flush",
|
|
579
|
+
},
|
|
580
|
+
endClock(),
|
|
581
|
+
);
|
|
582
|
+
|
|
583
|
+
let bytes = 0;
|
|
584
|
+
for (const [key, entry] of insertBuffer.get(table)!) {
|
|
585
|
+
if (shouldRecordBytes && tableCache.cache.has(key) === false) {
|
|
586
|
+
bytes += getBytes(entry.row) + getBytes(key);
|
|
587
|
+
}
|
|
588
|
+
tableCache.cache.set(key, entry.row);
|
|
589
|
+
}
|
|
590
|
+
tableCache.bytes += bytes;
|
|
591
|
+
insertBuffer.get(table)!.clear();
|
|
592
|
+
|
|
593
|
+
await new Promise(setImmediate);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
if (updateValues.length > 0) {
|
|
597
|
+
const primaryKeys = getPrimaryKeyColumns(table);
|
|
598
|
+
|
|
599
|
+
const endClock = startClock();
|
|
600
|
+
|
|
601
|
+
if (updateValues.length > LOW_BATCH_THRESHOLD) {
|
|
602
|
+
// Steps for flushing "update" entries:
|
|
603
|
+
// 1. Create temp table
|
|
604
|
+
// 2. Copy into temp table
|
|
605
|
+
// 3. Update target table with data from temp
|
|
606
|
+
|
|
607
|
+
const createTempTableQuery = `
|
|
608
|
+
CREATE TEMP TABLE IF NOT EXISTS "${target}"
|
|
609
|
+
AS SELECT * FROM "${getTableConfig(table).schema ?? "public"}"."${target}"
|
|
610
|
+
WITH NO DATA;`;
|
|
611
|
+
|
|
612
|
+
const updateQuery = `
|
|
613
|
+
UPDATE "${getTableConfig(table).schema ?? "public"}"."${target}" as target
|
|
614
|
+
SET ${Object.values(getTableColumns(table))
|
|
615
|
+
.map(
|
|
616
|
+
(column) =>
|
|
617
|
+
`"${getColumnCasing(
|
|
618
|
+
column,
|
|
619
|
+
"snake_case",
|
|
620
|
+
)}" = source."${getColumnCasing(column, "snake_case")}"`,
|
|
621
|
+
)
|
|
622
|
+
.join(",\n")}
|
|
623
|
+
FROM "${target}" source
|
|
624
|
+
WHERE ${primaryKeys
|
|
625
|
+
.map(({ sql }) => `target."${sql}" = source."${sql}"`)
|
|
626
|
+
.join(" AND ")};`;
|
|
627
|
+
|
|
628
|
+
await qb.wrap((db) => db.execute(createTempTableQuery), context);
|
|
629
|
+
|
|
630
|
+
const text = getCopyText(
|
|
631
|
+
table,
|
|
632
|
+
updateValues.map(({ row }) => row),
|
|
633
|
+
);
|
|
634
|
+
await new Promise(setImmediate);
|
|
635
|
+
|
|
636
|
+
await copy(table, text, false);
|
|
637
|
+
await qb.wrap((db) => db.execute(updateQuery), context);
|
|
638
|
+
await qb.wrap(
|
|
639
|
+
(db) => db.execute(`TRUNCATE TABLE "${target}"`),
|
|
640
|
+
context,
|
|
641
|
+
);
|
|
642
|
+
} else {
|
|
643
|
+
await qb.wrap(
|
|
644
|
+
(db) =>
|
|
645
|
+
db
|
|
646
|
+
.insert(table)
|
|
647
|
+
.values(updateValues.map(({ row }) => row))
|
|
648
|
+
.onConflictDoUpdate({
|
|
649
|
+
// @ts-ignore
|
|
650
|
+
target: primaryKeys.map(({ js }) => table[js]!),
|
|
651
|
+
set: Object.fromEntries(
|
|
652
|
+
Object.entries(getTableColumns(table)).map(
|
|
653
|
+
([columnName, column]) => [
|
|
654
|
+
columnName,
|
|
655
|
+
sql.raw(
|
|
656
|
+
`excluded."${getColumnCasing(column, "snake_case")}"`,
|
|
657
|
+
),
|
|
658
|
+
],
|
|
659
|
+
),
|
|
660
|
+
),
|
|
661
|
+
}),
|
|
662
|
+
context,
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
common.metrics.ponder_indexing_cache_query_duration.observe(
|
|
667
|
+
{
|
|
668
|
+
table: getTableName(table),
|
|
669
|
+
method: "flush",
|
|
670
|
+
},
|
|
671
|
+
endClock(),
|
|
672
|
+
);
|
|
673
|
+
|
|
674
|
+
let bytes = 0;
|
|
675
|
+
for (const [key, entry] of updateBuffer.get(table)!) {
|
|
676
|
+
if (shouldRecordBytes && tableCache.cache.has(key) === false) {
|
|
677
|
+
bytes += getBytes(entry.row) + getBytes(key);
|
|
678
|
+
}
|
|
679
|
+
tableCache.cache.set(key, entry.row);
|
|
680
|
+
}
|
|
681
|
+
tableCache.bytes += bytes;
|
|
682
|
+
updateBuffer.get(table)!.clear();
|
|
683
|
+
|
|
684
|
+
await new Promise(setImmediate);
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
if (insertValues.length > 0 || updateValues.length > 0) {
|
|
688
|
+
common.logger.debug({
|
|
689
|
+
msg: "Wrote database rows",
|
|
690
|
+
table: target,
|
|
691
|
+
row_count: insertValues.length + updateValues.length,
|
|
692
|
+
duration: flushEndClock(),
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
|
|
697
|
+
try {
|
|
698
|
+
if (qb.$dialect === "postgres") {
|
|
699
|
+
await qb.wrap((db) => db.execute("SAVEPOINT flush"), context);
|
|
700
|
+
|
|
701
|
+
await promiseAllSettledWithThrow(
|
|
702
|
+
Array.from(cache.keys()).map(flushTable),
|
|
703
|
+
);
|
|
704
|
+
|
|
705
|
+
await qb.wrap((db) => db.execute("RELEASE flush"), context);
|
|
706
|
+
} else {
|
|
707
|
+
// Note: pglite must run sequentially
|
|
708
|
+
for (const table of cache.keys()) {
|
|
709
|
+
await flushTable(table);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
} catch (_error) {
|
|
713
|
+
let error = _error as Error;
|
|
714
|
+
if (error instanceof ShutdownError || qb.$dialect === "pglite") {
|
|
715
|
+
throw error;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
// Note `isFlushRetry` is true when the previous flush failed. When `isFlushRetry` is false, this
|
|
719
|
+
// function takes an optimized fast path, with support for small batch sizes. PGlite always takes
|
|
720
|
+
// the fast path because it doesn't support delayed insert errors.
|
|
721
|
+
|
|
722
|
+
await qb.wrap((db) => db.execute("ROLLBACK to flush"), context);
|
|
723
|
+
|
|
724
|
+
for (const table of cache.keys()) {
|
|
725
|
+
if (
|
|
726
|
+
tableNames !== undefined &&
|
|
727
|
+
tableNames.has(getTableName(table)) === false
|
|
728
|
+
) {
|
|
729
|
+
continue;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
const target =
|
|
733
|
+
chainId === undefined
|
|
734
|
+
? getTableName(table)
|
|
735
|
+
: getPartitionName(table, chainId);
|
|
736
|
+
|
|
737
|
+
const insertValues = Array.from(insertBuffer.get(table)!.values());
|
|
738
|
+
const updateValues = Array.from(updateBuffer.get(table)!.values());
|
|
739
|
+
|
|
740
|
+
if (insertValues.length > 0) {
|
|
741
|
+
const endClock = startClock();
|
|
742
|
+
|
|
743
|
+
await qb.wrap((db) => db.execute("SAVEPOINT flush"), context);
|
|
744
|
+
|
|
745
|
+
const result = await recoverBatchError(
|
|
746
|
+
insertValues,
|
|
747
|
+
async (values) => {
|
|
748
|
+
await qb.wrap((db) => db.execute("ROLLBACK to flush"), context);
|
|
749
|
+
const text = getCopyText(
|
|
750
|
+
table,
|
|
751
|
+
values.map(({ row }) => row),
|
|
752
|
+
);
|
|
753
|
+
await copy(table, text);
|
|
754
|
+
|
|
755
|
+
await qb.wrap((db) => db.execute("SAVEPOINT flush"), context);
|
|
756
|
+
},
|
|
757
|
+
);
|
|
758
|
+
|
|
759
|
+
if (result.status === "error") {
|
|
760
|
+
error = new DelayedInsertError(result.error.message);
|
|
761
|
+
error.stack = undefined;
|
|
762
|
+
|
|
763
|
+
addErrorMeta(
|
|
764
|
+
error,
|
|
765
|
+
`db.insert arguments:\n${prettyPrint(result.value.row)}`,
|
|
766
|
+
);
|
|
767
|
+
|
|
768
|
+
if (result.value.metadata.event) {
|
|
769
|
+
addErrorMeta(error, toErrorMeta(result.value.metadata.event));
|
|
770
|
+
|
|
771
|
+
common.logger.warn({
|
|
772
|
+
msg: "Failed to write cached database rows",
|
|
773
|
+
event: result.value.metadata.event.eventCallback.name,
|
|
774
|
+
type: "insert",
|
|
775
|
+
table: getTableName(table),
|
|
776
|
+
row_count: insertValues.length,
|
|
777
|
+
duration: endClock(),
|
|
778
|
+
error,
|
|
779
|
+
});
|
|
780
|
+
} else {
|
|
781
|
+
common.logger.warn({
|
|
782
|
+
msg: "Failed to write cached database rows",
|
|
783
|
+
type: "insert",
|
|
784
|
+
table: getTableName(table),
|
|
785
|
+
row_count: insertValues.length,
|
|
786
|
+
duration: endClock(),
|
|
787
|
+
error,
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
throw error;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
if (updateValues.length > 0) {
|
|
796
|
+
// Steps for flushing "update" entries:
|
|
797
|
+
// 1. Create temp table
|
|
798
|
+
// 2. Copy into temp table
|
|
799
|
+
// 3. Update target table with data from temp
|
|
800
|
+
|
|
801
|
+
const createTempTableQuery = `
|
|
802
|
+
CREATE TEMP TABLE IF NOT EXISTS "${target}"
|
|
803
|
+
AS SELECT * FROM "${getTableConfig(table).schema ?? "public"}"."${target}"
|
|
804
|
+
WITH NO DATA;
|
|
805
|
+
`;
|
|
806
|
+
|
|
807
|
+
const endClock = startClock();
|
|
808
|
+
|
|
809
|
+
await qb.wrap((db) => db.execute(createTempTableQuery), context);
|
|
810
|
+
await qb.wrap((db) => db.execute("SAVEPOINT flush"), context);
|
|
811
|
+
|
|
812
|
+
const result = await recoverBatchError(
|
|
813
|
+
updateValues,
|
|
814
|
+
async (values) => {
|
|
815
|
+
await qb.wrap((db) => db.execute("ROLLBACK to flush"), context);
|
|
816
|
+
const text = getCopyText(
|
|
817
|
+
table,
|
|
818
|
+
values.map(({ row }) => row),
|
|
819
|
+
);
|
|
820
|
+
await copy(table, text, false);
|
|
821
|
+
|
|
822
|
+
await qb.wrap((db) => db.execute("SAVEPOINT flush"), context);
|
|
823
|
+
},
|
|
824
|
+
);
|
|
825
|
+
|
|
826
|
+
await qb.wrap(
|
|
827
|
+
(db) => db.execute(`TRUNCATE TABLE "${target}"`),
|
|
828
|
+
context,
|
|
829
|
+
);
|
|
830
|
+
|
|
831
|
+
if (result.status === "error") {
|
|
832
|
+
error = new DelayedInsertError(result.error.message);
|
|
833
|
+
error.stack = undefined;
|
|
834
|
+
|
|
835
|
+
addErrorMeta(
|
|
836
|
+
error,
|
|
837
|
+
`db.update arguments:\n${prettyPrint(result.value.row)}`,
|
|
838
|
+
);
|
|
839
|
+
|
|
840
|
+
if (result.value.metadata.event) {
|
|
841
|
+
addErrorMeta(error, toErrorMeta(result.value.metadata.event));
|
|
842
|
+
|
|
843
|
+
common.logger.warn({
|
|
844
|
+
msg: "Failed to write cached database rows",
|
|
845
|
+
event: result.value.metadata.event.eventCallback.name,
|
|
846
|
+
type: "update",
|
|
847
|
+
table: getTableName(table),
|
|
848
|
+
row_count: updateValues.length,
|
|
849
|
+
duration: endClock(),
|
|
850
|
+
error,
|
|
851
|
+
});
|
|
852
|
+
} else {
|
|
853
|
+
common.logger.warn({
|
|
854
|
+
msg: "Failed to write cached database rows",
|
|
855
|
+
type: "update",
|
|
856
|
+
table: getTableName(table),
|
|
857
|
+
row_count: updateValues.length,
|
|
858
|
+
duration: endClock(),
|
|
859
|
+
error,
|
|
860
|
+
});
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
throw error;
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// Note: if we weren't able to find the exact row that caused the error,
|
|
869
|
+
// throw the original error.
|
|
870
|
+
throw error;
|
|
871
|
+
}
|
|
872
|
+
},
|
|
873
|
+
async prefetch({ events }) {
|
|
874
|
+
const context = {
|
|
875
|
+
logger: common.logger.child({ action: "prefetch_database_rows" }),
|
|
876
|
+
};
|
|
877
|
+
|
|
878
|
+
let totalBytes = 0;
|
|
879
|
+
for (const table of tables) {
|
|
880
|
+
totalBytes += cache.get(table)!.bytes;
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
// If data from the cache needs to be evicted, start with the
|
|
884
|
+
// table with the least disk reads.
|
|
885
|
+
|
|
886
|
+
if (totalBytes > common.options.indexingCacheMaxBytes) {
|
|
887
|
+
for (const table of tables.sort(
|
|
888
|
+
(a, b) => cache.get(a)!.diskReads - cache.get(b)!.diskReads,
|
|
889
|
+
)) {
|
|
890
|
+
if (cache.get(table)!.isCacheComplete === false) continue;
|
|
891
|
+
|
|
892
|
+
totalBytes -= cache.get(table)!.bytes;
|
|
893
|
+
|
|
894
|
+
cache.get(table)!.bytes = 0;
|
|
895
|
+
cache.get(table)!.cache.clear();
|
|
896
|
+
cache.get(table)!.isCacheComplete = false;
|
|
897
|
+
// Note: spillover is not cleared because it is an invariant
|
|
898
|
+
// it is empty
|
|
899
|
+
|
|
900
|
+
common.logger.debug({
|
|
901
|
+
msg: "Evicted cached database rows",
|
|
902
|
+
table: getTableName(table),
|
|
903
|
+
row_count: cache.get(table)!.cache.size,
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
if (totalBytes < common.options.indexingCacheMaxBytes) break;
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
|
|
910
|
+
if (tables.every((table) => cache.get(table)!.isCacheComplete)) {
|
|
911
|
+
return;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
// Use historical accesses + next event batch to determine which
|
|
915
|
+
// rows are going to be accessed, and preload them into the cache.
|
|
916
|
+
|
|
917
|
+
const prediction = new Map<Table, Map<CacheKey, Row>>();
|
|
918
|
+
|
|
919
|
+
for (const table of tables) {
|
|
920
|
+
prediction.set(table, new Map());
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
for (const event of events) {
|
|
924
|
+
if (profile.has(event.eventCallback.name)) {
|
|
925
|
+
for (const table of tables) {
|
|
926
|
+
if (cache.get(table)!.isCacheComplete) continue;
|
|
927
|
+
for (const [, { count, pattern }] of profile
|
|
928
|
+
.get(event.eventCallback.name)!
|
|
929
|
+
.get(table)!) {
|
|
930
|
+
// Expected value of times the prediction will be used.
|
|
931
|
+
const ev =
|
|
932
|
+
(count * SAMPLING_RATE) / eventCount[event.eventCallback.name]!;
|
|
933
|
+
if (ev > PREDICTION_THRESHOLD) {
|
|
934
|
+
const row = recoverProfilePattern(pattern, event);
|
|
935
|
+
const key = getCacheKey(table, row, primaryKeyCache);
|
|
936
|
+
prediction.get(table)!.set(key, row);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
for (const [table, tableCache] of cache) {
|
|
944
|
+
if (cache.get(table)!.isCacheComplete) continue;
|
|
945
|
+
for (const key of tableCache.cache.keys()) {
|
|
946
|
+
if (
|
|
947
|
+
tableCache.spillover.has(key) ||
|
|
948
|
+
prediction.get(table)!.has(key)
|
|
949
|
+
) {
|
|
950
|
+
prediction.get(table)!.delete(key);
|
|
951
|
+
} else {
|
|
952
|
+
tableCache.cache.delete(key);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
for (const table of tables) {
|
|
958
|
+
cache.get(table)!.spillover.clear();
|
|
959
|
+
cache.get(table)!.prefetched.clear();
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
for (const [table, tablePredictions] of prediction) {
|
|
963
|
+
common.metrics.ponder_indexing_cache_requests_total.inc(
|
|
964
|
+
{
|
|
965
|
+
table: getTableName(table),
|
|
966
|
+
type: "prefetch",
|
|
967
|
+
},
|
|
968
|
+
tablePredictions.size,
|
|
969
|
+
);
|
|
970
|
+
}
|
|
971
|
+
|
|
972
|
+
await Promise.all(
|
|
973
|
+
Array.from(prediction.entries())
|
|
974
|
+
.filter(([, tablePredictions]) => tablePredictions.size > 0)
|
|
975
|
+
.map(async ([table, tablePredictions]) => {
|
|
976
|
+
for (const [key] of tablePredictions) {
|
|
977
|
+
cache.get(table)!.prefetched.add(key);
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
const conditions = dedupe(
|
|
981
|
+
Array.from(tablePredictions),
|
|
982
|
+
([key]) => key,
|
|
983
|
+
).map(([, prediction]) => getWhereCondition(table, prediction));
|
|
984
|
+
|
|
985
|
+
if (conditions.length === 0) return;
|
|
986
|
+
const endClock = startClock();
|
|
987
|
+
|
|
988
|
+
await qb
|
|
989
|
+
.wrap(
|
|
990
|
+
(db) =>
|
|
991
|
+
db
|
|
992
|
+
.select()
|
|
993
|
+
.from(table)
|
|
994
|
+
.where(or(...conditions)),
|
|
995
|
+
context,
|
|
996
|
+
)
|
|
997
|
+
.then((results) => {
|
|
998
|
+
const resultsPerKey = new Map<CacheKey, Row>();
|
|
999
|
+
for (const result of results) {
|
|
1000
|
+
const ck = getCacheKey(table, result, primaryKeyCache);
|
|
1001
|
+
resultsPerKey.set(ck, result);
|
|
1002
|
+
}
|
|
1003
|
+
|
|
1004
|
+
const tableCache = cache.get(table)!;
|
|
1005
|
+
for (const key of tablePredictions.keys()) {
|
|
1006
|
+
if (resultsPerKey.has(key)) {
|
|
1007
|
+
tableCache.cache.set(key, resultsPerKey.get(key)!);
|
|
1008
|
+
} else {
|
|
1009
|
+
tableCache.cache.set(key, null);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
common.logger.debug({
|
|
1014
|
+
msg: "Pre-queried database rows",
|
|
1015
|
+
table: getTableName(table),
|
|
1016
|
+
row_count: results.length,
|
|
1017
|
+
duration: endClock(),
|
|
1018
|
+
});
|
|
1019
|
+
});
|
|
1020
|
+
|
|
1021
|
+
common.metrics.ponder_indexing_cache_query_duration.observe(
|
|
1022
|
+
{
|
|
1023
|
+
table: getTableName(table),
|
|
1024
|
+
method: "load",
|
|
1025
|
+
},
|
|
1026
|
+
endClock(),
|
|
1027
|
+
);
|
|
1028
|
+
}),
|
|
1029
|
+
);
|
|
1030
|
+
},
|
|
1031
|
+
invalidate() {
|
|
1032
|
+
for (const tableCache of cache.values()) {
|
|
1033
|
+
tableCache.isCacheComplete = false;
|
|
1034
|
+
}
|
|
1035
|
+
},
|
|
1036
|
+
clear() {
|
|
1037
|
+
for (const tableCache of cache.values()) {
|
|
1038
|
+
tableCache.cache.clear();
|
|
1039
|
+
tableCache.spillover.clear();
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
for (const tableBuffer of insertBuffer.values()) {
|
|
1043
|
+
tableBuffer.clear();
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
for (const tableBuffer of updateBuffer.values()) {
|
|
1047
|
+
tableBuffer.clear();
|
|
1048
|
+
}
|
|
1049
|
+
},
|
|
1050
|
+
set event(_event: Event | undefined) {
|
|
1051
|
+
event = _event;
|
|
1052
|
+
},
|
|
1053
|
+
set qb(_qb: QB) {
|
|
1054
|
+
qb = _qb;
|
|
1055
|
+
},
|
|
1056
|
+
};
|
|
1057
|
+
};
|