📄 api.java
字号:
import java.io.*;
public class API {
String reason;
IndexManager indexmanager;
RecordManager recordmanager;
Catalog catalog;
public API(IndexManager im, RecordManager rm, Catalog cl) {
indexmanager = im;
recordmanager = rm;
catalog = cl;
}
public boolean createTable(Interpreter cmd){
File table = new File(cmd.table);
if(table.exists()){
reason = new String("创建表的文件已存在");
return false;
}
try{
table.createNewFile();
RandomAccessFile rantable = new RandomAccessFile(table, "rw");
rantable.writeLong(0); //总的记录个数
rantable.writeLong(0); //块的个数
int rlenght=cmd.attnumber+1;
for(int i=0;i<cmd.attnumber;i++)
rlenght=rlenght+cmd.attlength[i];
if(rlenght < 9)
rlenght = 9;
rantable.writeInt(rlenght);//每个记录的长度
rantable.writeInt(4096/rlenght);//每个块多少个记录
rantable.writeLong(1024);//第一个空位置地址
rantable.close();
}catch(Exception e){
reason = new String("建立表文件失败");
return false;
}
cmd.mean = 3;
cmd.indexname = cmd.primarykey+"index";
cmd.uniqueatt = cmd.primarykey;
return indexmanager.createIndex(cmd);
}
public boolean dropTable(Interpreter cmd){
File table = new File(cmd.table);
if(!table.exists()){
reason = new String("要删除的表不存在");
return false;
}
for(int i=0; i<cmd.attnumber; i++){
if(cmd.attindex[i]!=null){
new File(cmd.attindex[i]).delete();
recordmanager.deleteBlock(cmd.attindex[i]);
}
}
recordmanager.deleteBlock(cmd.table);
return table.delete();
}
public boolean createIndex(Interpreter cmd){
return indexmanager.createIndex(cmd);
}
public boolean deleteIndex(Interpreter cmd){
File index = new File(cmd.indexname);
if(!index.exists()){
reason = new String("该索引的文件不存在");
return false;
}
recordmanager.deleteBlock(cmd.indexname);
return index.delete();
}
public boolean select(Interpreter cmd){
return recordmanager.getRecord(cmd);
}
public boolean insert(Interpreter cmd){
boolean a;
if(a=recordmanager.insertRecord(cmd))
catalog.addrecord(cmd.table);
return a;
}
public boolean delete(Interpreter cmd){
int a =recordmanager.deleteRecord(cmd);
long c;
if(a == -2){
c= catalog.gettablerecord(cmd.table);
catalog.deleteIndexfromTable(cmd.table);
for(int i=0; i<cmd.attnumber; i++)
if(cmd.attindex[i]!=null)
catalog.deleteIndexfromTable(cmd.attindex[i]);
System.out.println("成功删除记录"+c+"项");
return true;
}
if(a>0){
catalog.subrecord(cmd.table, a);
System.out.println("成功删除记录"+a+"项");
return true;
}
System.out.println("满足此删除条件的项不存在");
return true;
}
public boolean quit(Interpreter cmd){
return recordmanager.quit();
}
public boolean exefile(Interpreter cmd){
try{
BufferedReader exef = new BufferedReader(new FileReader(cmd.table));
String com;
com = exef.readLine();
String command = new String("");
while(com!=null){
int pos=0;
while(com!=null){
pos = com.indexOf(";");
if(pos>=0){
command = command+"\n"+com.substring(0,pos+1);
System.out.println(command.trim());
cmd.init(command);
if(catalog.test(cmd))
exec(cmd);
if(pos!=com.length()-1){
command = com.substring(pos,com.length());
}
else
command = new String("");
break;
}
command = command + "\n"+com;
com = exef.readLine();
}
com = exef.readLine();
}
exef.close();
}catch(Exception e){
System.out.println(e);
}
return true;
}
public boolean exec(Interpreter cmd){
switch(cmd.mean){
case 0:
return false;
case 1:{
if(createTable(cmd))
return true;
else{
System.out.println("API failed "+reason);
return false;
}
}
case 2:{
if(dropTable(cmd))
return true;
else
return false;
}
case 3:{
if(createIndex(cmd))
return true;
else
return false;
}
case 4:{
if(deleteIndex(cmd))
return true;
else
return false;
}
case 5:{
if(select(cmd))
return true;
else
return false;
}
case 6:{
if(insert(cmd))
return true;
else
return false;
}
case 7:{
if(delete(cmd))
return true;
else
return false;
}
case 8:{
if(quit(cmd))
return true;
else
return false;
}
case 9:{
if(exefile(cmd))
return true;
else
return false;
}
default:
return false;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -