Skip to content

Commit

Permalink
cleaner check
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew committed Jan 22, 2024
1 parent 51c2315 commit dcb727a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 79 deletions.
34 changes: 18 additions & 16 deletions examples/client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
db.fields().then((fields) => {
fields.map((field) => {
dbFields.add(field.fieldName);
fieldTypes[field.fieldName] = db.decodeType(field.fieldType);
fieldTypes[field.fieldName] = field.fieldType;
});

document.getElementById("fields").innerHTML = JSON.stringify(
Expand Down Expand Up @@ -88,33 +88,35 @@
return "Error: 'value' in 'where' clause is missing.";
}

const allowedRanks = fieldTypes[whereNode.key];
const keyType = fieldTypes[whereNode.key];

let valueRank;
let valueType;
const FIELD_TYPE_STRING = 1 << 0;
const FIELD_TYPE_NUMBER = 1 << 1;
const FIELD_TYPE_BOOLEAN = 1 << 4;
const FIELD_TYPE_NULL = 1 << 5;

if (whereNode.value === null) {
valueRank = 1;
valueType = "null";
if (keyType && FIELD_TYPE_NULL > 0) {
return `Error: 'key: ${whereNode.key} does not have type: null.`;
}
}
if (typeof whereNode.value === "boolean") {
valueRank = 2;
valueType = "boolean";
if (keyType && FIELD_TYPE_BOOLEAN > 0) {
return `Error: 'key: ${whereNode.key} does not have type: boolean.`;
}
}
if (
typeof whereNode.value === "number" ||
typeof whereNode.value === "bigint"
) {
valueRank = 3;
valueType = "number or bigint";
if (keyType && FIELD_TYPE_NUMBER) {
return `Error: 'key: ${whereNode.key} does not have type: number or bigint.`;
}
}
if (typeof whereNode.value === "string") {
valueRank = 4;
valueType = "string";
}

if (!allowedRanks.has(valueRank)) {
return `Error: 'key: ${whereNode.key} does not have type: ${valueType}.`;
if (keyType && FIELD_TYPE_STRING) {
return `Error: 'key: ${whereNode.key} does not have type: string.`;
}
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ export type Query<T extends Schema> = {
orderBy?: OrderBy<T>[];
};

// This record maps from fieldRank -> Bitmask
export const typeRankToBits: Record<number, bigint> = {
4: BigInt(1), // FieldTypeString = 1 << 0
3: BigInt(2), // FieldTypeNumber = 1 << 1
//'Object': BigInt(4), // FieldTypeObject = 1 << 2
//'Array': BigInt(8), // FieldTypeArray = 1 << 3
2: BigInt(16), // FieldTypeBoolean = 1 << 4
1: BigInt(32), // FieldTypeNull = 1 << 5
};

function parseIgnoringSuffix(x: string) {
// TODO: implement a proper parser.
try {
Expand Down Expand Up @@ -213,17 +203,4 @@ export class Database<T extends Schema> {
}
}
}

// given a bitmask, we decode and return the types
decodeType(bitmask: bigint): Set<number> {
let decodedRanks = new Set<number>();

for (const [fieldRank, bitValue] of Object.entries(typeRankToBits)) {
if ((bitmask & bitValue) !== BigInt(0)) {
decodedRanks.add(Number(fieldRank));
}
}

return decodedRanks;
}
}
40 changes: 0 additions & 40 deletions src/tests/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,43 +94,3 @@ describe("test query relation", () => {
expect(results).toEqual([25]);
});
});

describe("test type bitmask decoding", () => {
let mockDataFile: jest.Mocked<DataFile>;
let mockIndexFile: jest.Mocked<VersionedIndexFile<any>>;
let database: Database<any>;
beforeEach(() => {
(DataFile.forUrl as jest.Mock).mockReturnValue({
get: jest.fn().mockResolvedValue("mocked response"),
});
mockDataFile = DataFile.forUrl(
"http://example.com/data"
) as jest.Mocked<DataFile>;

mockIndexFile = {
indexFileHeader: jest.fn(),
indexHeaders: jest.fn(),
indexRecord: jest.fn(),
dataRecord: jest.fn(),
} as jest.Mocked<VersionedIndexFile<any>>;

// instantiate a Database object with given mocked data file and index file
database = Database.forDataFileAndIndexFile(mockDataFile, mockIndexFile);
});

it("it should return the correct field rank from fieldtype", async () => {
const testCases = [
{ fieldType: BigInt(34), expected: new Set<number>([1, 3]) },
{ fieldType: BigInt(1), expected: new Set<number>([4]) },
{ fieldType: BigInt(2), expected: new Set<number>([3]) },
{ fieldType: BigInt(16), expected: new Set<number>([2]) },
{ fieldType: BigInt(32), expected: new Set<number>([1]) },
{ fieldType: BigInt(33), expected: new Set<number>([1, 4]) },
];

testCases.forEach(({ fieldType, expected }) => {
const result = database.decodeType(fieldType);
expect(result).toEqual(expected);
});
});
});

0 comments on commit dcb727a

Please sign in to comment.