Skip to content

Commit

Permalink
feat: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
pomelo-nwu committed Sep 26, 2024
1 parent bb15889 commit 3363fcb
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 46 deletions.
1 change: 1 addition & 0 deletions examples/kuzu-graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@graphscope/studio-importor": "workspace:*",
"@graphscope/studio-query": "workspace:*",
"@kuzu/kuzu-wasm": "^1.0.4",
"localforage": "^1.10.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-highlight-words": "^0.20.0",
Expand Down
6 changes: 5 additions & 1 deletion examples/kuzu-graph/src/components/import-data/save.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useState } from 'react';
import { SaveOutlined } from '@ant-design/icons';
import { useContext as useModeling } from '@graphscope/studio-importor';
import { createGraph, importGraph } from './services';
import localforage from 'localforage';

import { useNavigate } from 'react-router-dom';

Expand Down Expand Up @@ -38,7 +39,10 @@ const SaveModeling: React.FunctionComponent<SaveModelingProps> = props => {
edges: schema.edges,
});
//@ts-ignore
await importGraph(csvFiles);

const csvFiles = (await localforage.getItem('DRAFT_GRAPH_FILES')) as File[];
const res = await importGraph(csvFiles);
console.log('res', res);

setState(preState => {
return {
Expand Down
5 changes: 1 addition & 4 deletions examples/kuzu-graph/src/components/import-data/services.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const createGraph = async (params: { nodes: any[]; edges: any[] }, graph_

const kuzuDriver = await getDriver();

const createSchema = 'CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name))';
await kuzuDriver.createSchema(schema);

const insertQuery = "CREATE (u:User {name: 'Alice', age: 35})";
Expand All @@ -36,9 +35,7 @@ export const createGraph = async (params: { nodes: any[]; edges: any[] }, graph_
export const importGraph = async (csvFiles: File[]) => {
console.log('csvFiles', csvFiles);
const kuzuDriver = await getDriver();

await kuzuDriver.initializeGraph(csvFiles);
return true;
return await kuzuDriver.loadGraph(csvFiles);
};

export function transform(params) {
Expand Down
142 changes: 101 additions & 41 deletions examples/kuzu-graph/src/kuzu-javascript-driver/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { default as Kuzu } from '@kuzu/kuzu-wasm';

interface IKuzuResult {
Database: () => Promise<any>;
Database: (params: any) => Promise<any>;
Connection: (db: any) => Promise<any>;
FS: Promise<any>;
execute: (query: string) => Promise<any>;
Expand All @@ -16,7 +16,6 @@ interface IKuzuConnection {
close: () => Promise<void>;
}


export class KuzuDriver {
private kuzu: typeof Kuzu;
private db: IKuzuDatabase | null;
Expand All @@ -32,22 +31,22 @@ export class KuzuDriver {

async initialize(): Promise<void> {
const result: IKuzuResult = await this.kuzu();
this.db = await result.Database();
//@ts-ignore
this.db = await result.Database('test', 0, 10, false, false, 4194304 * 16 * 4);
this.conn = await result.Connection(this.db);
this.FS = result.FS;

this.FS?.mkdir('data');
}

async executeQuery(queries) {
let result_list: (string)[] = [];
let result_list: string[] = [];
if (queries instanceof Array) {
for (const query of queries) {
let partial_result = await this.executeSingleQuery(query);
result_list = result_list.concat(partial_result);
}
}
else {
} else {
let partial_result = await this.executeSingleQuery(queries);
result_list = result_list.concat(partial_result);
}
Expand All @@ -56,25 +55,25 @@ export class KuzuDriver {
}

async executeSingleQuery(query) {
let result_list: (string)[] = [];
let result_list: string[] = [];
try {
let query_list = query.split(';');
query_list.forEach((query) => {

query_list.forEach(query => {
query = query.trim();
if (query != '') {
if (query.startsWith('#') || query.startsWith('//')) return;
console.log("execute: ", query);
console.log('execute: ', query);
const query_result = this.conn?.execute(query);
if (typeof query_result === 'undefined') {
result_list.push("");
}
else {
result_list.push('');
} else {
result_list.push(query_result.toString());
}
}
});
} catch (error) {
console.log("execute error");
console.log('execute error');
}

return result_list;
Expand All @@ -84,15 +83,15 @@ export class KuzuDriver {
const input_type_upper = input_type.toUpperCase();
switch (input_type_upper) {
case 'DT_STRING':
return 'STRING';
return 'STRING';
case 'DT_DOUBLE':
return 'DOUBLE';
return 'DOUBLE';
case 'DT_SIGNED_INT32':
return 'INT32';
return 'INT32';
case 'DT_SIGNED_INT64':
return 'INT64';
return 'INT64';
default:
return 'STRING';
return 'STRING';
}
}

Expand All @@ -104,13 +103,15 @@ export class KuzuDriver {
const { name, type, primaryKey } = property;
return `${name} ${this.typeConvert(type)}`;
});
const primary_keys = properties.map(property => {
const { name, type, primaryKey } = property;
if (primaryKey) {
return `PRIMARY KEY (${name})`
}
return null
}).filter(key => key !== null);
const primary_keys = properties
.map(property => {
const { name, type, primaryKey } = property;
if (primaryKey) {
return `PRIMARY KEY (${name})`;
}
return null;
})
.filter(key => key !== null);
const final_property_scripts = property_scripts.concat(primary_keys);
return `CREATE NODE TABLE ${label} (${final_property_scripts.join(', ')});`;
});
Expand All @@ -120,25 +121,49 @@ export class KuzuDriver {
const { name, type } = property;
return `${name} ${this.typeConvert(type)}`;
});
return `CREATE REL TABLE ${label} FROM ${source} TO ${target} (${property_scripts.join(', ')});`;
if (property_scripts.length === 0) {
return `CREATE REL TABLE ${label} (FROM ${source} TO ${target});`;
}
return `CREATE REL TABLE ${label} (FROM ${source} TO ${target} , ${property_scripts.join(', ')});`;
});

// console.log(node_scripts.join('; ') + '; ' + edge_scripts.join('; '));
// console.log(node_scripts[0]);
/** execute */
let result: any[] = [];

// let full_query = node_scripts.join('; ') + '; ' + edge_scripts.join('; ') + ';';
// const createResult = await this.conn?.execute(full_query);
const createNodeResult = await this.executeQuery(node_scripts);
console.log('Schema created: ', createNodeResult);
for (let i = 0; i < node_scripts.length; i++) {
console.log('create nodes script: ', node_scripts[i]);
const res = await this.conn?.execute(node_scripts[i]);
result.push(res.toString());
}
for (let i = 0; i < edge_scripts.length; i++) {
console.log('create edges script: ', edge_scripts[i]);
const res = await this.conn?.execute(edge_scripts[i]);
result.push(res.toString());
}

const createEdgeResult = await this.executeQuery(edge_scripts);
console.log('Schema created: ', createEdgeResult);
console.log('Schema created: ', result);
}

async initializeGraph(data_files: File[]): Promise<void> {
async uploadCsvFile(file: File): Promise<any> {
if (file) {
const reader = new FileReader();
reader.onload = async e => {
//@ts-ignore
var fileData = new Uint8Array(e.target.result); // transfer to Uint8Array
var fileName = file.name; // get path from user
var filePath = 'data/' + fileName;
var label_name = file.name.split('.')[0];
await this.FS.writeFile(filePath, fileData);
const res = await this.conn?.execute(`COPY ${label_name} FROM "${filePath}" (HEADER=true, DELIM="|");`);
console.log('File uploaded successfully!', filePath, res.toString());
};
reader.readAsArrayBuffer(file);
}
}
async loadGraph(data_files: File[]): Promise<any> {
for (const file of data_files) {
console.log(file,this.FS)
await this.uploadCsvFile(file);
}
return true;
}

async insertData(query: string): Promise<void> {
Expand All @@ -149,11 +174,46 @@ export class KuzuDriver {

async queryData(query: string): Promise<any> {
console.time('Query cost');
const queryResult = await this.executeQuery(query);
// const queryResult = await this.conn?.execute(query);
// const queryResult = await this.executeQuery(query);
const queryResult = await this.conn?.execute(query);
console.timeEnd('Query cost');
console.log('Query result: ', queryResult);
return queryResult;
try {
const data = JSON.parse(queryResult.table.toString());
console.log('data', data);
const nodes: any[] = [];
const edges: any[] = [];
data.map(item => {
//@ts-check
const { n, e } = item;

if (n) {
const { id, _LABEL, _ID, ...others } = n;
nodes.push({
id,
label: _LABEL,
properties: {
...others,
},
});
}
if (e) {
const { id, _LABEL, _ID, ...others } = n;
edges.push({
id,
label: _LABEL,
properties: {
...others,
},
});
}
});
return { nodes, edges };
} catch (error) {
return {
nodes: [],
edges: [],
};
}
}

async close(): Promise<void> {
Expand Down

0 comments on commit 3363fcb

Please sign in to comment.