Skip to content

Commit

Permalink
Merge branch 'bogdan-devel' of https://github.com/8VM41/DMD_DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Warchant committed Nov 22, 2015
2 parents 8ca09ac + dd13909 commit 1dc5535
Show file tree
Hide file tree
Showing 17 changed files with 528 additions and 427 deletions.
4 changes: 2 additions & 2 deletions dbms/SQL/SQLParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static String processQuery(String query) throws SQLError {
}
}
int[] array = indexList.stream().mapToInt(i->i).toArray();
rows.add(new Row(line, array));
//rows.add(new Row(line, array));
}
}
catch (ClassCastException e) {
Expand All @@ -82,7 +82,7 @@ public static String processQuery(String query) throws SQLError {
}
}
int[] array = indexList.stream().mapToInt(i->i).toArray();
rows.add(new Row(line, array));
//rows.add(new Row(line, array));
}
catch (ClassCastException d) {
throw new SQLError("Insert cannot be empty");
Expand Down
12 changes: 12 additions & 0 deletions dbms/Server/Cursor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package Server;

import java.util.concurrent.ConcurrentNavigableMap;

/**
* @author Bogdan Vaneev
* Innopolis University
* 11/22/2015
*/
public class Cursor {
ConcurrentNavigableMap<Object[], Object[]> table;
}
27 changes: 19 additions & 8 deletions dbms/Server/DBServer.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package Server;

import core.exceptions.RecordStatus;
import core.exceptions.SQLError;
import core.descriptive.TableSchema;
import core.managers.DBManager;
import org.mapdb.DB;
import org.mapdb.DBMaker;

import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.Map;

public class DBServer {
public static DBManager SMDB;
public static DB db;
public static Map<String, TableSchema> tables;

public static void main(String[] args) throws IOException {

Expand All @@ -18,20 +23,26 @@ public static void main(String[] args) throws IOException {

try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
System.out.println("Running server on port " + portNumber);
SMDB = new DBManager("sm.db");

db = DBMaker.fileDB(new File("sm.db"))
.transactionDisable()
.cacheLRUEnable()
.make();

db.commit();

tables = db.hashMapCreate("tables").valueSerializer(TableSchema.tableSchemaSerializer()).makeOrGet();

System.out.println("Ready");
while (listening) {
new DBServerThread(serverSocket.accept()).start();
new Server.DBServerThread(serverSocket.accept()).start();
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
} catch (SQLError sqlError) {
sqlError.printStackTrace();

} catch (Exception e) {
e.printStackTrace();
} catch (RecordStatus recordStatus) {
recordStatus.printStackTrace();
}
}
}
16 changes: 12 additions & 4 deletions dbms/Server/DBServerThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ public void run() {
) {
String inputLine, outputLine = "";

out.println("SMDB v.0.1-pre-alpha Connection ready");
out.println("CONNECTION_OK");

while ((inputLine = in.readLine()) != null) {

if (inputLine.equals("Bye")) {
DBServer.db.commit();
DBServer.db.close();
break;
}



try {
outputLine = DBServer.SMDB.processQuery(inputLine);
outputLine = Server.DBServer.SMDB.processQuery(inputLine);
out.println(outputLine);
} catch (SQLError e) {
out.println();
Expand All @@ -49,8 +58,7 @@ public void run() {
recordStatus.printStackTrace();
}

if (outputLine.equals("Bye"))
break;

}
socket.close();
} catch (IOException e) {
Expand Down
2 changes: 1 addition & 1 deletion dbms/Server/Daemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static private void doProcessing() {
System.out.println("Running server on port " + portNumber);
System.out.println("Ready");
while (listening) {
new DBServerThread(serverSocket.accept()).start();
new Server.DBServerThread(serverSocket.accept()).start();
}
} catch (IOException e) {
System.err.println("Could not listen on port " + portNumber);
Expand Down
97 changes: 41 additions & 56 deletions dbms/core/descriptive/Attribute.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package core.descriptive;

import core.exceptions.SQLError;
import core.util.Misc;
import org.mapdb.Serializer;

import java.nio.ByteBuffer;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

/**
* @author Bogdan Vaneev
* Innopolis University
* 10/23/2015
*/
public class Attribute {
public class Attribute implements Serializable {
/**
* FLAGS
**/
Expand Down Expand Up @@ -107,24 +110,31 @@ public static String fromEnumCaster(byte value) {
}
}

public static Attribute deserialize(ByteBuffer b) {
Attribute a = new Attribute();
a.name = Misc.parseStr(b);
a.type = b.get();
a.flags = b.get();
public static Serializer attributeSerializer() {
return new Serializer<Attribute>() {
@Override
public void serialize(DataOutput dataOutput, Attribute o) throws IOException {
dataOutput.writeUTF(o.name);
dataOutput.write(o.type);
dataOutput.write(o.flags);

if (a.hasFlag(F_DV)) {
a.defaultValue = Misc.parseBytes(b);
}
if (o.hasFlag(F_FK))
dataOutput.writeUTF(o.fk);
}

if (a.hasFlag(F_FK)) {
a.fk = Misc.parseStr(b);
}
@Override
public Attribute deserialize(DataInput dataInput, int i) throws IOException {
Attribute a = new Attribute();
a.name = dataInput.readUTF();
a.type = dataInput.readByte();
a.flags = dataInput.readByte();

if (a.hasFlag(F_UQ))
a.rootpage = b.getInt();
if (a.hasFlag(F_FK))
a.fk = dataInput.readUTF();

return a;
return a;
}
};
}

public static int getDataType(String type) {
Expand All @@ -145,56 +155,31 @@ public static byte getConstraintType(String constraint) {
else return -1;
}

public void setRootpage(int root) {
this.rootpage = root;
public String getName() {
return this.name;
}

public ByteBuffer serialize() {
byte[] nameb = this.name.getBytes();
byte[] fkb;

int size = 2 + nameb.length + 1 + 1 + (this.hasFlag(F_UQ) ? 4 : 0);

if (this.hasFlag(F_FK)) {
fkb = this.fk.getBytes();
size += 2 + fkb.length;
}

if (this.hasFlag(F_DV)) {
size += 2 + defaultValue.length;
public void setName(String n) throws SQLError {
if (n.matches("[a-z0-9A-Z_\\-]{1,24}")) {
this.name = n;
} else {
throw new SQLError("Incorrect attribute name");
}
}

ByteBuffer buf = ByteBuffer.allocate(size);

Misc.addStr(buf, name);
buf.put(type);
buf.put(flags);

if (this.hasFlag(F_DV))
Misc.addBytes(buf, defaultValue);

if (this.hasFlag(F_FK))
Misc.addStr(buf, fk);

if (this.hasFlag(F_UQ))
buf.putInt(rootpage);
public Serializer getSerializer() {
if (type == T_FLOAT) return Serializer.FLOAT;
if (type == T_INT) return Serializer.INTEGER;
if (type == T_TEXT) return Serializer.STRING;
if (type == T_SHORT) return Serializer.SHORT;

buf.flip();
return buf;
return Serializer.BYTE_ARRAY;
}

public void removeFlag(byte flag) {
if (this.hasFlag(flag)) this.flags ^= flag;
}

public void setName(String n) throws SQLError {
if (n.matches("[a-z0-9A-Z_\\-]{1,24}")) {
this.name = n;
} else {
throw new SQLError("Incorrect attribute name");
}
}

public void setType(byte type) throws SQLError {
if (type >= 1 && type <= 5) this.type = type;
else throw new SQLError("Wrong type!");
Expand Down
16 changes: 0 additions & 16 deletions dbms/core/descriptive/Pair.java

This file was deleted.

42 changes: 0 additions & 42 deletions dbms/core/descriptive/Pointer.java

This file was deleted.

Loading

0 comments on commit 1dc5535

Please sign in to comment.