andcastillo / fdp2017 Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
public class AttrType {
public static final int INTEGER = 11;
public static final int FLOAT = 12;
public static final int STRING = 13;
public static final int COLNAME = 21;
public static final int FIELDNO = 22;
// --------------------------------------------------------------------------
/**
/**
if (obj instanceof Integer) {
return INTEGER;
}
if (obj instanceof Float) {
return FLOAT;
}
if (obj instanceof String) {
return STRING;
}
// otherwise, unknown type
throw new IllegalArgumentException("Unknown AttrType "
+ obj.getClass().getName());
} // public static int getType(Object obj)
/**
switch (value) {
case INTEGER:
return "INTEGER";
case FLOAT:
return "FLOAT";
case STRING:
return "STRING";
case COLNAME:
return "COLNAME";
case FIELDNO:
return "FIELDNO";
} // switch
return ("Unexpected AttrType " + value);
} // public static String toString(int value)
} // public class AttrType
Add IndexScan class to provide the functionality of scanning available indexed tables.
I've changed the file format a little, and I updated the README to reflect these changes, and added a database example.
Se realiza la actualizacion en la clase DataBase para poder ejecutar consultas en formato JSON : 02d4ed7
https://github.com/andcastillo/fdp2017/blob/merge/db/src/main/java/org/db/core/DataBase.java
Tambien realice un ejemplo que convierte unos nodos creados a JSON (para entender el formato de entrada), y posteriormente ese String en JSON es usado como consulta de entrada para probar la base de datos, actualmente funciona correctamente, otros compañeros podrían probar y notificar si algo se encuentra fallando.
Clase Main de Prueba:
https://github.com/andcastillo/fdp2017/blob/merge/db/src/test/java/org/db/TestMain.java
Actualizacion de la clase seqScan con respecto a los esquemas cargados en la clase DataBase, esto afecto Projection y RemoveRepeted, se corrigió correctamente y se actualizaron las clases de pruebas.
Este método debe solicitar el nombre de la tabla y el de al columna a indexar. De acuerdo con la definición del proyecto el enfoque a seguir es:
los datos numéricos se deben indexar usando árboles y los datos de texto con tablas Hash
Interfaz que debe ser implementada por los Operadores de la BD (Selección, Proyección, Eliminacón de repetidos, etc).
Esta interfaz debe declarar funciones para aplicar el operador, especificar y establecer el tipo de operador.
Caso :
SELECT * FROM table WHERE condition
Donde condition : columnId=value
La funcion que realiza la busqueda de la condicion es
// Selection Scan
String SelectionScan = "condition";
Int Column = 2
while (scanner.hasNext()) {
List<String> line = parseLine(scanner.nextLine());
String scanValue = line.get(Column);
if ( SelectionScan.equals(scanValue) ) {
printLine(line);
}
}
Su implementacion completa tomando los datos de un csv ubicado en data/example.csv
con la estructura [id(int), string, Bool, Double]
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CSVUtils {
private static final char DEFAULT_SEPARATOR = ',';
private static final char DEFAULT_QUOTE = '"';
public static void main(String[] args) throws Exception {
String csvFile = "data/example.csv";
Scanner scanner = new Scanner(new File(csvFile));
// Selection Scan
String indexScan = "true";
while (scanner.hasNext()) {
List<String> line = parseLine(scanner.nextLine());
String scanValue = line.get(2);
if ( indexScan.equals(scanValue) ) {
printLine(line);
}
}
scanner.close();
}
public static List<String> parseLine(String cvsLine) {
return parseLine(cvsLine, DEFAULT_SEPARATOR, DEFAULT_QUOTE);
}
public static void printLine(List<String> line) {
System.out.println("id="+ line.get(0)+ " b= " + line.get(1) + ", c= " + line.get(2) + " , x=" + line.get(3));
}
public static void selectFromLine(List<String> line, int[] t) {
String select = "";
//
for (int i = 0; i < t.length; i++) {
select = select + line.get(i) + " ";
}
System.out.println(select);
}
public static List<String> parseLine(String cvsLine, char separators) {
return parseLine(cvsLine, separators, DEFAULT_QUOTE);
}
public static List<String> parseLine(String cvsLine, char separators, char customQuote) {
List<String> result = new ArrayList<>();
//if empty, return!
if (cvsLine == null && cvsLine.isEmpty()) {
return result;
}
if (customQuote == ' ') {
customQuote = DEFAULT_QUOTE;
}
if (separators == ' ') {
separators = DEFAULT_SEPARATOR;
}
StringBuffer curVal = new StringBuffer();
boolean inQuotes = false;
boolean startCollectChar = false;
boolean doubleQuotesInColumn = false;
char[] chars = cvsLine.toCharArray();
for (char ch : chars) {
if (inQuotes) {
startCollectChar = true;
if (ch == customQuote) {
inQuotes = false;
doubleQuotesInColumn = false;
} else {
//Fixed : allow "" in custom quote enclosed
if (ch == '\"') {
if (!doubleQuotesInColumn) {
curVal.append(ch);
doubleQuotesInColumn = true;
}
} else {
curVal.append(ch);
}
}
} else {
if (ch == customQuote) {
inQuotes = true;
//Fixed : allow "" in empty quote enclosed
if (chars[0] != '"' && customQuote == '\"') {
curVal.append('"');
}
//double quotes in column will hit this!
if (startCollectChar) {
curVal.append('"');
}
} else if (ch == separators) {
result.add(curVal.toString());
curVal = new StringBuffer();
startCollectChar = false;
} else if (ch == '\r') {
//ignore LF characters
continue;
} else if (ch == '\n') {
//the end, break!
break;
} else {
curVal.append(ch);
}
}
}
result.add(curVal.toString());
return result;
}
}
Para soportar indexScan es necesario recorrer el BTree siguiendo un orden establecido, mantener el nodo acutal y ofrecer métodos que permitan realizar next() y consultar hasNext(), para esto se puede implementar la Interfaz Iterator
Se crea la clase Schema
984942c
Creo que mas adelante sera necesario modificarlo puesto que cuando se lee una fila de un bloque no se sabe el nombre de la columna puesto que dependen de su orden (Revisar).
Con la clase Schema planteada anteriormente se crea la funcion LoadSchemas para cargar los esquemas de toda la base de datos al iniciar la app
279d253
Se ponen unos ejemplos en el Main para acceder a los datos de los esquemas cargados
24711eb
En caso de que esten de acuerdo podriamos hacer un Pull requests en la rama Master confirmar.
import global.AttrType;
/**
Each tuple has a schema that defines the logical view of the raw bytes; it
describes the types, lengths, offsets, and names of a tuple's fields.
*/
public class Schema {
/** Minimum column width for output. */
public static final int MIN_WIDTH = 10;
// --------------------------------------------------------------------------
/** Attribute types of the fields. */
protected int[] types;
/** Variable lengths of the fields. */
protected int[] lengths;
/** Relative offsets of the fields. */
protected int[] offsets;
/** Column names of the fields. */
protected String[] names;
// --------------------------------------------------------------------------
public Schema clone() {
Schema ret = new Schema(names.length);
for (int i = 0; i < names.length; i++) {
ret.types[i] = types[i];
ret.lengths[i] = lengths[i];
ret.offsets[i] = offsets[i];
ret.names[i] = names[i];
}
return ret;
}
/**
/**
Sets the type, length, and name for the given field; this automatically
calculates its offset, provided that fields are set in ascending order.
*/
public void initField(int fldno, int type, int length, String name) {
// save the type, length, and name
types[fldno] = type;
lengths[fldno] = length;
names[fldno] = name;
// calculate the relative offset
if (fldno > 0) {
offsets[fldno] = offsets[fldno - 1] + lengths[fldno - 1];
} else {
offsets[fldno] = 0;
}
} // public void initField(int fldno, int type, int length, String name)
/**
number of this field to set
contains the info to copy
number of the field to copy
*/
public void initField(int fldno, Schema schema, int srcno) {
initField(fldno, schema.types[srcno], schema.lengths[srcno],
schema.names[srcno]);
}
/**
the left schema
the right schema
*/
public static Schema join(Schema s1, Schema s2) {
// construct the new schema
int s1cnt = s1.getCount();
int s2cnt = s2.getCount();
Schema schema = new Schema(s1cnt + s2cnt);
// copy all fields from s1 and s2
int fldno = 0;
for (int i = 0; i < s1cnt; i++) {
schema.initField(fldno++, s1, i);
}
for (int i = 0; i < s2cnt; i++) {
schema.initField(fldno++, s2, i);
}
// return the resulting schema
return schema;
} // public static Schema join(Schema s1, Schema s2)
// --------------------------------------------------------------------------
/**
/**
/**
/**
/**
/**
/**
/**
Prints the schema (i.e. the first lines of typical query output).
*/
public void print() {
// print and space the column names
int len = 0;
for (int i = 0; i < types.length; i++) {
// print the column name
System.out.print(names[i]);
// figure out the padding
int collen = Math.max(names[i].length(), MIN_WIDTH);
if (types[i] == AttrType.STRING) {
collen = Math.max(lengths[i], collen);
}
len += collen;
// pad the output to the field length
for (int j = 0; j < collen - names[i].length(); j++) {
System.out.print(' ');
}
} // for
// print the line separator
System.out.println();
for (int i = 0; i < len; i++) {
System.out.print('-');
}
System.out.println();
} // public void print()
} // public class Schema
Acabo de hacer un merge de las ramas activas y un refactoring de la organización del proyecto. He empaquetado todo y he creado un proyecto maven lo que facilita usar librerias. Pero veo que aquí todos son hard core.
Para el caso de indexado con Hash: permitir filtrar con el operador =
Para el caso de indexado con Btree: permitr filtrar con = y con rangos (>, <)
El archivo que fue mezclado en la rama merge bajo este commit contiene un error de sintaxis y una marca #EOF al final del mismo. Esto no permite compilar
In order to use index scan the needed tables should be indexed
Revisar que se retorne lo requerido en las soluciones
Consula 1:
https://github.com/andcastillo/fdp2017/blob/final/db/src/main/java/org/db/MainPregunta1.java
Consulta 2:
https://github.com/andcastillo/fdp2017/blob/final/db/src/main/java/org/db/MainPregunta2.java
Consulta 3:
https://github.com/andcastillo/fdp2017/blob/final/db/src/main/java/org/db/MainPregunta3.java
Tener en cuenta que en cada compilación se deben borra las tablas temporales creadas.
Algo crítico en este momento es tener una clase DataBase. Esta clase debe ser la encargada de cargar los datos y crear los índices. Luego debe ser la clase que debe recibir un query string y ejecutar la consulta. Ej
DataBase db = new DataBase("myDatabase"); //Por ejemplo. Eso debe cargar la base de datos que està en data/myDatabase
luego:
Object result = db.query("SELECT x, y, FROM A");
Result puede ser un string con el nombre de la tabla resultado, o un apuntador a folder que contiene la tabla resultado.
List<String> tablas = new ArrayList<>();
tablas.add("A");
List<String> attr = new ArrayList<>();
attr.add("b");
attr.add("id");
attr.add("x");
Node node = new Node();
node.setTableInput(tablas);
node.setParameters(attr);
node.setType("Projection");
IOperator op = new Projection();
String result= op.apply(node);
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.