From 55ed423556542bd391d63f61b0595b7294291263 Mon Sep 17 00:00:00 2001 From: Matthew <38759997+friendlymatthew@users.noreply.github.com> Date: Tue, 23 Jan 2024 18:32:17 -0500 Subject: [PATCH] Switch server (#60) * basic * add different server * switch to jsonl --- examples/README.md | 4 ++-- examples/client/server.go | 24 ++++++++++++++++++++++++ package.json | 3 +-- src/database.ts | 32 +++++++++++++++++++++++++++++++- src/tests/database.test.ts | 19 ++++++++++++++++++- 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 examples/client/server.go diff --git a/examples/README.md b/examples/README.md index f688f591..b93bbd7c 100644 --- a/examples/README.md +++ b/examples/README.md @@ -52,8 +52,8 @@ cp ../../dist/appendable.min.js.map ../client Then run the development server: ```sh -npm run serve:example +npm run example ``` -You should see the example built on http://192.168.1.157:8080 +You should see the example built on http://localhost:8080 diff --git a/examples/client/server.go b/examples/client/server.go new file mode 100644 index 00000000..97cc9aa2 --- /dev/null +++ b/examples/client/server.go @@ -0,0 +1,24 @@ +package main + +import ( + "log" + "net/http" +) + +func main() { + // Set the directory to serve + fs := http.FileServer(http.Dir("./")) + + // Handle all requests by serving a file of the same name + http.Handle("/", fs) + + // Define the port to listen on + port := "8080" + log.Printf("Listening on http://localhost:%s/", port) + + // Start the server + err := http.ListenAndServe(":"+port, nil) + if err != nil { + log.Fatal(err) + } +} diff --git a/package.json b/package.json index 44638d14..449b062c 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "build": "esbuild src/index.ts --bundle --minify --sourcemap --outfile=dist/appendable.min.js", "build-index": "go run cmd/main.go -jsonl examples/workspace/green_tripdata_2023-01.jsonl", - "serve:example": "cd examples/client && npx http-server", + "example": "cd examples/client && go run server.go", "test": "jest" }, "repository": { @@ -24,7 +24,6 @@ }, "devDependencies": { "@types/jest": "^29.5.11", - "http-server": "^14.1.1", "prettier": "^3.2.1", "ts-jest": "^29.1.1", "ts-node": "^10.9.2" diff --git a/src/database.ts b/src/database.ts index 905662b8..fa56b9e9 100644 --- a/src/database.ts +++ b/src/database.ts @@ -35,7 +35,6 @@ export function containsType(fieldType: bigint, desiredType: FieldType) { } function parseIgnoringSuffix(x: string, format: FormatType) { - console.log("parseSuffix: ", x); switch (format) { case FormatType.Jsonl: try { @@ -54,9 +53,35 @@ function parseIgnoringSuffix(x: string, format: FormatType) { return JSON.parse(x); case FormatType.Csv: + try { + console.log("parsing no error", parseCsvLine(x)); + return parseCsvLine(x); + } catch (error) { + console.log("registered as an error"); + let lastCompleteLine = findLastCompleteCsvLine(x); + console.log(lastCompleteLine); + return parseCsvLine(lastCompleteLine); + } } } +export function parseCsvLine(line: string) { + console.log("parsing csv: "); + let fields: string[] = line.split(","); + + fields.forEach((field) => { + if (field.length > 0) { + console.log("parsing: ", field); + return JSON.parse(field); + } + }); +} + +function findLastCompleteCsvLine(data: string) { + let lastNewlineIndex = data.lastIndexOf("\n"); + return lastNewlineIndex >= 0 ? data.slice(0, lastNewlineIndex) : data; +} + function fieldRank(token: any) { if (token === null) { return 1; @@ -182,6 +207,7 @@ export class Database { ); // group the field ranges by the field name and merge them into single ranges. const fieldRangeMap = new Map(); + for (const [key, value] of fieldRanges) { const existing = fieldRangeMap.get(key); if (existing) { @@ -207,6 +233,8 @@ export class Database { fieldRangesSorted.unshift(...fieldRangesSorted.splice(index, 1)); } } + + console.log("Field ranges: ", fieldRanges); // evaluate the field ranges in order. for (const [key, [start, end]] of fieldRangesSorted) { // check if the iteration order should be reversed. @@ -219,6 +247,8 @@ export class Database { const dataRecord = await this.indexFile.dataRecord( indexRecord.dataNumber ); + + console.log(`Data record: `, dataRecord); const dataFieldValue = parseIgnoringSuffix( await this.dataFile.get( dataRecord.startByteOffset, diff --git a/src/tests/database.test.ts b/src/tests/database.test.ts index 8a4914b1..a28e9955 100644 --- a/src/tests/database.test.ts +++ b/src/tests/database.test.ts @@ -1,4 +1,4 @@ -import { Database, FieldType, Query, containsType } from "../database"; +import { Database, FieldType, Query, containsType, parseCsvLine } from "../database"; import { DataFile } from "../data-file"; import { IndexFile, VersionedIndexFile } from "../index-file"; import { FormatType } from ".."; @@ -112,3 +112,20 @@ describe("test field type", () => { }); }); }); + + +describe("test parsing csv", () => { + + it("check csv parse", async() => { + const testCases = [ + { data: "151,1", expected: 151}, + { data: ",95,5", expected: 95} + ]; + + testCases.forEach(({ data, expected}) => { + let csv = parseCsvLine(data) + console.log(csv) + }) + + }) +}) \ No newline at end of file