package mydb;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class App {
public static void main(String[] args) {
Driver driver = new Driver();
driver.init(Paths.get("C:\\tempDir\\"),
Arrays.asList(new Field("pk1","int",true,false,0),
new Field("pk2","String",true,false,200),
new Field("f0","String",false,true,3000),
new Field("f1","String",false,true,3000),
new Field("f2","String",false,true,2000),
new Field("f3","String",false,true,3000),
new Field("f4","String",false,true,3000),
new Field("f5","String",false,true,3000),
new Field("f6","String",false,true,3000),
new Field("f7","String",false,true,3000),
new Field("f8","String",false,true,300),
new Field("f9","String",false,true,3000),
new Field("f10","String",false,true,1000),
new Field("f11","String",false,true,3000),
new Field("f12","String",false,true,3000),
new Field("f13","String",false,true,3000),
new Field("f14","String",false,true,3000),
new Field("f15","String",false,true,3000),
new Field("f16","String",false,true,5000),
new Field("f17","String",false,true,3000),
new Field("f18","String",false,true,3000),
new Field("f19","String",false,true,1000)
));
for (int i = 0; i < 10*10; i++) {
List<Object> oneRecord = new ArrayList<>();
oneRecord.add(i);oneRecord.add("helloworld"+(i+89));
for (int j = 0; j < 20; j++) {
oneRecord.add(getRecord(i,j,3000));
}
driver.insert(oneRecord);
}
int maxdelete = 10*1;
for (int i = 0; i < maxdelete; i++) {
HashMap<String, Object> condition = new HashMap<>();
condition.put("pk1", i+8);
driver.delete(condition);
int j = (23+i*5)%maxdelete;
condition.put("f"+j, getRecord(i,j,3000));
driver.delete(condition);
}
HashMap<String, Object> condition = new HashMap<>();
condition.put("pk1", 11);
List<Object> oneRecord = driver.query(condition);
System.out.println(oneRecord.size());
}
private static Object getRecord(int i,int j,int maxlength){
StringBuffer sb = new StringBuffer(3000);
sb.append(""+(234+i)).append("iowepfjiasd;skja;fj;asdfsmdlml;asdfk;alsdkjf;asdjf;asdfas");
sb.append(sb.toString());
sb.append(sb.toString());
sb.append(sb.toString());
sb.append(sb.toString());
sb.append(sb.toString());
sb.append(sb.toString());
// System.out.println(sb.substring(0, maxlength-5).length());
return sb.toString();
}
}
class Field{
public Field(String name, String type, boolean isPK, boolean isNull, int maxlength) {
this.name = name;
this.type = type;
this.isPK = isPK;
this.isNull = isNull;
this.maxlength = maxlength;
}
int index;
String name;
String type;
boolean isPK;
boolean isNull;
int maxlength;
}
class Driver{
private List<List<Object>> data = new LinkedList<>();
private List<Field> fields;
private HashMap<String,Field> fiedsMap = new HashMap<>();
public void init(Path path,List<Field> fields) {
this.fields = fields;
for (int i = 0; i < fields.size(); i++) {
fields.get(i).index = i;
this.fiedsMap.put(fields.get(i).name, fields.get(i));
}
}
public void insert(List<Object> oneRecord) {
data.add(oneRecord);
}
public void delete(HashMap<String, Object> condition) {
for (Iterator iterator = data.iterator(); iterator.hasNext();) {
List<Object> oneRecord = (List<Object>) iterator.next();
if(isMatch(condition, oneRecord) ){
iterator.remove();
System.out.println("rm " + oneRecord.get(0));
}
}
}
public List<Object> query(HashMap<String, Object> condition) {
List<Object> r = new ArrayList<>();
for (List<Object> oneRecord : data) {
if(isMatch(condition, oneRecord)){
r.add( oneRecord);
}
}
return r;
}
private boolean isMatch(HashMap<String, Object> condition, List<Object> oneRecord) {
for(String key: condition.keySet() ){
Field f = this.fiedsMap.get(key);
if(f==null || !oneRecord.get(f.index).equals(condition.get(key)) ){
return false;
}
}
return true;
}
}
mydb
最新推荐文章于 2023-10-04 23:04:56 发布