Skip to content

Commit

Permalink
Merge branch 'main' into logger
Browse files Browse the repository at this point in the history
  • Loading branch information
friendlymatthew authored Jan 24, 2024
2 parents 311c77c + 55ed423 commit 164db53
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 24 additions & 0 deletions examples/client/server.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
Expand Down
32 changes: 31 additions & 1 deletion src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -182,6 +207,7 @@ export class Database<T extends Schema> {
);
// group the field ranges by the field name and merge them into single ranges.
const fieldRangeMap = new Map<keyof T, [number, number]>();

for (const [key, value] of fieldRanges) {
const existing = fieldRangeMap.get(key);
if (existing) {
Expand All @@ -207,6 +233,8 @@ export class Database<T extends Schema> {
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.
Expand All @@ -219,6 +247,8 @@ export class Database<T extends Schema> {
const dataRecord = await this.indexFile.dataRecord(
indexRecord.dataNumber
);

console.log(`Data record: `, dataRecord);
const dataFieldValue = parseIgnoringSuffix(
await this.dataFile.get(
dataRecord.startByteOffset,
Expand Down
19 changes: 18 additions & 1 deletion src/tests/database.test.ts
Original file line number Diff line number Diff line change
@@ -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 "..";
Expand Down Expand Up @@ -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)
})

})
})

0 comments on commit 164db53

Please sign in to comment.