Skip to content

Commit

Permalink
replace custom function with size
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Sep 23, 2023
1 parent 1d3adb8 commit 7893115
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 31 deletions.
6 changes: 0 additions & 6 deletions packages/common/src/getByteLength.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ export * from "./resourceIdToHex";
export * from "./resourceTypes";
export * from "./spliceHex";
export * from "./transportObserver";
export * from "./getByteLength";
11 changes: 3 additions & 8 deletions packages/store-sync/src/postgres/postgresStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, PublicClient, concatHex } from "viem";
import { Hex, PublicClient, concatHex, size } from "viem";
import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core";
import { eq, inArray } from "drizzle-orm";
import { buildTable } from "./buildTable";
Expand All @@ -7,7 +7,7 @@ import { debug } from "./debug";
import { buildInternalTables } from "./buildInternalTables";
import { getTables } from "./getTables";
import { schemaVersion } from "./schemaVersion";
import { getByteLength, hexToResourceId, spliceHex } from "@latticexyz/common";
import { hexToResourceId, spliceHex } from "@latticexyz/common";
import { setupTables } from "./setupTables";
import { getTableKey } from "./getTableKey";
import { StorageAdapter, StorageAdapterBlock } from "../common";
Expand Down Expand Up @@ -132,12 +132,7 @@ export async function postgresStorage<TConfig extends StoreConfig = StoreConfig>
// TODO: verify that this returns what we expect (doesn't error/undefined on no record)
const previousValue = (await tx.select().from(sqlTable).where(eq(sqlTable.__key, uniqueKey)).execute())[0];
const previousStaticData = (previousValue?.__staticData as Hex) ?? "0x";
const newStaticData = spliceHex(
previousStaticData,
log.args.start,
getByteLength(log.args.data),
log.args.data
);
const newStaticData = spliceHex(previousStaticData, log.args.start, size(log.args.data), log.args.data);
const newValue = decodeValueArgs(table.valueSchema, {
staticData: newStaticData,
encodedLengths: (previousValue?.__encodedLengths as Hex) ?? "0x",
Expand Down
11 changes: 3 additions & 8 deletions packages/store-sync/src/recs/recsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { debug } from "./debug";
import { World as RecsWorld, getComponentValue, hasComponent, removeComponent, setComponent } from "@latticexyz/recs";
import { defineInternalComponents } from "./defineInternalComponents";
import { getTableEntity } from "./getTableEntity";
import { getByteLength, hexToResourceId, spliceHex } from "@latticexyz/common";
import { hexToResourceId, spliceHex } from "@latticexyz/common";
import { decodeValueArgs } from "@latticexyz/protocol-parser";
import { Hex } from "viem";
import { Hex, size } from "viem";
import { isTableRegistrationLog } from "../isTableRegistrationLog";
import { logToTable } from "../logToTable";
import { hexKeyTupleToEntity } from "./hexKeyTupleToEntity";
Expand Down Expand Up @@ -98,12 +98,7 @@ export function recsStorage<TConfig extends StoreConfig = StoreConfig>({
// TODO: add tests that this works when no record had been set before
const previousValue = getComponentValue(component, entity);
const previousStaticData = (previousValue?.__staticData as Hex) ?? "0x";
const newStaticData = spliceHex(
previousStaticData,
log.args.start,
getByteLength(log.args.data),
log.args.data
);
const newStaticData = spliceHex(previousStaticData, log.args.start, size(log.args.data), log.args.data);
const newValue = decodeValueArgs(table.valueSchema, {
staticData: newStaticData,
encodedLengths: (previousValue?.__encodedLengths as Hex) ?? "0x",
Expand Down
11 changes: 3 additions & 8 deletions packages/store-sync/src/sqlite/sqliteStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, PublicClient, concatHex, getAddress } from "viem";
import { Hex, PublicClient, concatHex, getAddress, size } from "viem";
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { and, eq, sql } from "drizzle-orm";
import { sqliteTableToSql } from "./sqliteTableToSql";
Expand All @@ -12,7 +12,7 @@ import { schemaVersion } from "./schemaVersion";
import { StorageAdapter } from "../common";
import { isTableRegistrationLog } from "../isTableRegistrationLog";
import { logToTable } from "../logToTable";
import { getByteLength, hexToResourceId, spliceHex } from "@latticexyz/common";
import { hexToResourceId, spliceHex } from "@latticexyz/common";
import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser";

// TODO: upgrade drizzle and use async sqlite interface for consistency
Expand Down Expand Up @@ -131,12 +131,7 @@ export async function sqliteStorage<TConfig extends StoreConfig = StoreConfig>({
// TODO: verify that this returns what we expect (doesn't error/undefined on no record)
const previousValue = (await tx.select().from(sqlTable).where(eq(sqlTable.__key, uniqueKey)).execute())[0];
const previousStaticData = (previousValue?.__staticData as Hex) ?? "0x";
const newStaticData = spliceHex(
previousStaticData,
log.args.start,
getByteLength(log.args.data),
log.args.data
);
const newStaticData = spliceHex(previousStaticData, log.args.start, size(log.args.data), log.args.data);
const newValue = decodeValueArgs(table.valueSchema, {
staticData: newStaticData,
encodedLengths: (previousValue?.__encodedLengths as Hex) ?? "0x",
Expand Down

0 comments on commit 7893115

Please sign in to comment.