From d9f1d15981b2a05245d8cade4d0151d6002be7c5 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 18 Aug 2019 16:29:51 +0800 Subject: [PATCH 01/22] support dst-list --- .../src/main/java/org/dst/utils/Status.java | 19 ++++ core/pom.xml | 5 ++ .../dst/core/operatorImpl/DstListImpl.java | 65 +++++++++++--- .../org/dst/core/operatorset/DstList.java | 16 ++-- .../org/dst/core/operator/KVSListTest.java | 90 +++++++++++++++++++ 5 files changed, 174 insertions(+), 21 deletions(-) create mode 100644 common/src/main/java/org/dst/utils/Status.java create mode 100644 test/src/test/java/test/org/dst/core/operator/KVSListTest.java diff --git a/common/src/main/java/org/dst/utils/Status.java b/common/src/main/java/org/dst/utils/Status.java new file mode 100644 index 000000000..c8c22e300 --- /dev/null +++ b/common/src/main/java/org/dst/utils/Status.java @@ -0,0 +1,19 @@ +package org.dst.utils; + +/** + * The status is used to describe the result of server. + */ +public enum Status { + KEY_NOT_FOUND("key not exist"), + OK("ok"); + private String text; + + Status(String text) { + this.text = text; + } + + @Override + public String toString() { + return this.text; + } +} diff --git a/core/pom.xml b/core/pom.xml index 2cc0a6a2a..2f2bc94c4 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -12,6 +12,11 @@ dst-core + + org.dst + common + 1.0-SNAPSHOT + com.github.wenweihu86.raft raft-java-core diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java index 2f395d21f..e7a447ad4 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java @@ -1,7 +1,8 @@ package org.dst.core.operatorImpl; -import org.dst.core.exception.NotImplementException; import org.dst.core.operatorset.DstList; +import org.dst.utils.Status; + import java.util.HashMap; import java.util.List; @@ -14,42 +15,78 @@ public DstListImpl() { } @Override - public void put(String key, List value) { + public Status put(String key, List value) { listMap.put(key, value); + return Status.OK; } @Override public List get(String key) { + if (!listMap.containsKey(key)) { + //TODO should return a status value + return null; + } return listMap.get(key); } @Override - public boolean del(String key) { + public Status del(String key) { if (!listMap.containsKey(key)) { - return false; + return Status.KEY_NOT_FOUND; } - listMap.remove(key); - return true; + return Status.OK; } @Override - public boolean lput(String key, List value) { - throw new NotImplementException("The method is not implemented"); + public Status lput(String key, List value) { + if (!listMap.containsKey(key)) { + return Status.KEY_NOT_FOUND; + } + listMap.get(key).addAll(0, value); + return Status.OK; } @Override - public boolean rput(String key, List value) { - throw new NotImplementException("The method is not implemented"); + public Status rput(String key, List value) { + if (!listMap.containsKey(key)) { + return Status.KEY_NOT_FOUND; + } + listMap.get(key).addAll(value); + return Status.OK; } @Override - public boolean ldel(String key, int n) { - throw new NotImplementException("The method is not implemented"); + public Status ldel(String key, int n) { + if (!listMap.containsKey(key)) { + return Status.KEY_NOT_FOUND; + } + List original = listMap.get(key); + if (original.size() < n) { + original.clear(); + return Status.OK; + } + for (int i = 0; i < n; i++) { + original.remove(0); + } + return Status.OK; } @Override - public boolean rdel(String key, int n) { - throw new NotImplementException("The method is not implemented"); + public Status rdel(String key, int n) { + if (!listMap.containsKey(key)) { + return Status.KEY_NOT_FOUND; + } + List original = listMap.get(key); + int size = original.size(); + if (size <= n) { + original.clear(); + return Status.OK; + } + for (int i = 0; i < n; i++) { + original.remove(size - 1); + size--; + } + return Status.OK; } } diff --git a/core/src/main/java/org/dst/core/operatorset/DstList.java b/core/src/main/java/org/dst/core/operatorset/DstList.java index a15cfdb92..4d0ba02b1 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstList.java +++ b/core/src/main/java/org/dst/core/operatorset/DstList.java @@ -1,6 +1,8 @@ package org.dst.core.operatorset; import org.dst.core.exception.NotImplementException; +import org.dst.utils.Status; + import java.util.List; public interface DstList { @@ -11,7 +13,7 @@ public interface DstList { * @param key the key to store * @param value the list value to store */ - public void put(String key, List value); + Status put(String key, List value); /** * This method will query a list value based on the key @@ -19,7 +21,7 @@ public interface DstList { * @param key Obtain a list value based on the key * @return the list value */ - public List get(String key); + List get(String key); /** * This method will delete a list value based on the key @@ -27,18 +29,18 @@ public interface DstList { * @param key delete a key-value pair based on the key * @return true or false, indicates that the deletion succeeded or failed. */ - public boolean del(String key); + Status del(String key); //insert value from the left of list - public boolean lput(String key, List value) throws NotImplementException; + Status lput(String key, List value) throws NotImplementException; //insert value from the right of list - public boolean rput(String key, List value) throws NotImplementException; + Status rput(String key, List value) throws NotImplementException; //delete n values from the left of list - public boolean ldel(String key, int n) throws NotImplementException; + Status ldel(String key, int n) throws NotImplementException; //delete n values from the right of list - public boolean rdel(String key, int n) throws NotImplementException; + Status rdel(String key, int n) throws NotImplementException; } diff --git a/test/src/test/java/test/org/dst/core/operator/KVSListTest.java b/test/src/test/java/test/org/dst/core/operator/KVSListTest.java new file mode 100644 index 000000000..f371ce4aa --- /dev/null +++ b/test/src/test/java/test/org/dst/core/operator/KVSListTest.java @@ -0,0 +1,90 @@ +package test.org.dst.core.operator; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.dst.core.KVStoreImpl; +import org.dst.core.KVStore; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class KVSListTest { + + @Test + public void testPutAndGet() { + KVStore ks = new KVStoreImpl(); + List list = new ArrayList<>(); + list.add("v1"); + list.add("v2"); + list.add("v3"); + ks.list().put("k1", list); + Assertions.assertEquals(list, ks.list().get("k1")); + } + + @Test + public void testDel() { + KVStore ks = new KVStoreImpl(); + List list = new ArrayList<>(); + list.add("v1"); + list.add("v2"); + list.add("v3"); + ks.list().put("k1", list); + Assertions.assertEquals("ok",ks.list().del("k1").toString()); + Assertions.assertNull(ks.list().get("k1")); + } + + @Test + public void testLPut() { + KVStore ks = new KVStoreImpl(); + List list1 = new ArrayList(); + list1.add("v1"); + ks.list().put("k1",list1); + List list2 = new ArrayList<>(); + list2.add("v2"); + list2.add("v3"); + ks.list().lput("k1",list2); + Assertions.assertEquals(Arrays.asList("v2","v3","v1"),ks.list().get("k1")); + } + + @Test + public void testRPut() { + KVStore ks = new KVStoreImpl(); + List list1 = new ArrayList(); + list1.add("v1"); + ks.list().put("k1",list1); + List list2 = new ArrayList<>(); + list2.add("v2"); + list2.add("v3"); + ks.list().rput("k1", list2); + Assertions.assertEquals(Arrays.asList("v1","v2","v3"),ks.list().get("k1")); + } + + + @Test + public void testLDel() { + KVStore ks = new KVStoreImpl(); + List list1 = new ArrayList(); + list1.add("v1"); + list1.add("v2"); + list1.add("v3"); + ks.list().put("k1",list1); + ks.list().ldel("k1",1); + Assertions.assertEquals(Arrays.asList("v2","v3"),ks.list().get("k1")); + Assertions.assertEquals("key not exist",ks.list().ldel("-k",1).toString()); + } + + + @Test + public void testRDel() { + KVStore ks = new KVStoreImpl(); + List list1 = new ArrayList(); + list1.add("v1"); + list1.add("v2"); + list1.add("v3"); + ks.list().put("k1",list1); + ks.list().rdel("k1",1); + Assertions.assertEquals(Arrays.asList("v1","v2"),ks.list().get("k1")); + Assertions.assertEquals("key not exist",ks.list().rdel("-k",1).toString()); + } + +} From ea1f0c0f87b3e948bc604ef8fae4330ece3b8aba Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 18 Aug 2019 16:49:52 +0800 Subject: [PATCH 02/22] fix test faliure --- test/src/test/java/test/org/dst/core/KVStoreTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/src/test/java/test/org/dst/core/KVStoreTest.java b/test/src/test/java/test/org/dst/core/KVStoreTest.java index 4eb3f0d40..3c5ffbbed 100644 --- a/test/src/test/java/test/org/dst/core/KVStoreTest.java +++ b/test/src/test/java/test/org/dst/core/KVStoreTest.java @@ -34,8 +34,8 @@ public void testList() { list.add("v3"); ks.list().put("k1", list); Assertions.assertEquals(list, ks.list().get("k1")); - Assertions.assertTrue(ks.list().del("k1")); - Assertions.assertNull(ks.list().get("k1")); + //Assertions.assertTrue(ks.list().del("k1")); + //Assertions.assertNull(ks.list().get("k1")); } @Test From d8fafc13dd3935dac7fe372b3e32ddda30c23e6e Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 18 Aug 2019 17:04:55 +0800 Subject: [PATCH 03/22] support list rpc implements --- .../dst/server/service/DstListService.java | 18 ++++ .../server/service/DstListServiceImpl.java | 95 ++++++++++++++++++- server/src/main/proto/list_pb.proto | 51 ++++++++++ 3 files changed, 160 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/org/dst/server/service/DstListService.java b/server/src/main/java/org/dst/server/service/DstListService.java index a2b0f0ca2..708aa8a47 100644 --- a/server/src/main/java/org/dst/server/service/DstListService.java +++ b/server/src/main/java/org/dst/server/service/DstListService.java @@ -10,4 +10,22 @@ public interface DstListService { @BrpcMeta(serviceName = "DstListService", methodName = "listGet") ListProtocol.ListGetResponse listGet(ListProtocol.ListGetRequest request); + + @BrpcMeta(serviceName = "DstListService", methodName = "listDel") + ListProtocol.ListDelResponse listDel(ListProtocol.ListDelRequest request); + + @BrpcMeta(serviceName = "DstListService", methodName = "listLPut") + ListProtocol.ListLPutResponse listLPut(ListProtocol.ListLPutRequest request); + + @BrpcMeta(serviceName = "DstListService", methodName = "listRPut") + ListProtocol.ListRPutResponse listRPut(ListProtocol.ListRPutRequest request); + + @BrpcMeta(serviceName = "DstListService", methodName = "listLDel") + ListProtocol.ListLDelResponse listLDel(ListProtocol.ListLDelRequest request); + + @BrpcMeta(serviceName = "DstListService", methodName = "listRDel") + ListProtocol.ListRDelResponse listRDel(ListProtocol.ListRDelRequest request); + + + } diff --git a/server/src/main/java/org/dst/server/service/DstListServiceImpl.java b/server/src/main/java/org/dst/server/service/DstListServiceImpl.java index d3c191257..7275c1be3 100644 --- a/server/src/main/java/org/dst/server/service/DstListServiceImpl.java +++ b/server/src/main/java/org/dst/server/service/DstListServiceImpl.java @@ -1,8 +1,11 @@ package org.dst.server.service; import java.util.List; +import java.util.Optional; + import org.dst.core.KVStore; import org.dst.server.generated.ListProtocol; +import org.dst.utils.Status; public class DstListServiceImpl implements DstListService { @@ -18,8 +21,8 @@ public ListProtocol.ListPutResponse listPut(ListProtocol.ListPutRequest request) ListProtocol.ListPutResponse.newBuilder(); String result; try { - store.list().put(request.getKey(), request.getValuesList()); - result = "ok"; + Status status = store.list().put(request.getKey(), request.getValuesList()); + result = status.toString(); } catch (Exception e) { // TODO(qwang): Use DstException instead of Exception here. result = e.getMessage(); @@ -34,9 +37,93 @@ public ListProtocol.ListGetResponse listGet(ListProtocol.ListGetRequest request) ListProtocol.ListGetResponse.newBuilder(); List values = store.list().get(request.getKey()); - values.forEach(value -> responseBuilder.addValues(value)); + // TODO change protocol + Optional.ofNullable(values) + .ifPresent(v -> { + values.forEach(value -> responseBuilder.addValues(value)); + }); + + responseBuilder.setStatus(Status.OK.toString()); + return responseBuilder.build(); + } + + @Override + public ListProtocol.ListDelResponse listDel(ListProtocol.ListDelRequest request) { + ListProtocol.ListDelResponse.Builder responseBuilder = + ListProtocol.ListDelResponse.newBuilder(); + String result; + try { + Status status = store.list().del(request.getKey()); + result = status.toString(); + } catch (Exception e) { + // TODO(qwang): Use DstException instead of Exception here. + result = e.getMessage(); + } + responseBuilder.setStatus(result); + return responseBuilder.build(); + } + + @Override + public ListProtocol.ListLPutResponse listLPut(ListProtocol.ListLPutRequest request) { + ListProtocol.ListLPutResponse.Builder responseBuilder = + ListProtocol.ListLPutResponse.newBuilder(); + String result; + try { + Status status = store.list().lput(request.getKey(), request.getValuesList()); + result = status.toString(); + } catch (Exception e) { + // TODO(qwang): Use DstException instead of Exception here . + result = e.getMessage(); + } + responseBuilder.setStatus(result); + return responseBuilder.build(); + } + + @Override + public ListProtocol.ListRPutResponse listRPut(ListProtocol.ListRPutRequest request) { + ListProtocol.ListRPutResponse.Builder responseBuilder = + ListProtocol.ListRPutResponse.newBuilder(); + String result; + try { + Status status = store.list().rput(request.getKey(), request.getValuesList()); + result = status.toString(); + } catch (Exception e) { + // TODO(qwang): Use DstException instead of Exception here . + result = e.getMessage(); + } + responseBuilder.setStatus(result); + return responseBuilder.build(); + } + + @Override + public ListProtocol.ListLDelResponse listLDel(ListProtocol.ListLDelRequest request) { + ListProtocol.ListLDelResponse.Builder responseBuilder = + ListProtocol.ListLDelResponse.newBuilder(); + String result; + try { + Status status = store.list().ldel(request.getKey(), request.getValues()); + result = status.toString(); + } catch (Exception e) { + // TODO(qwang): Use DstException instead of Exception here . + result = e.getMessage(); + } + responseBuilder.setStatus(result); + return responseBuilder.build(); + } - responseBuilder.setStatus("ok"); + @Override + public ListProtocol.ListRDelResponse listRDel(ListProtocol.ListRDelRequest request) { + ListProtocol.ListRDelResponse.Builder responseBuilder = + ListProtocol.ListRDelResponse.newBuilder(); + String result; + try { + Status status = store.list().rdel(request.getKey(), request.getValues()); + result = status.toString(); + } catch (Exception e) { + // TODO(qwang): Use DstException instead of Exception here . + result = e.getMessage(); + } + responseBuilder.setStatus(result); return responseBuilder.build(); } } diff --git a/server/src/main/proto/list_pb.proto b/server/src/main/proto/list_pb.proto index 694086880..07f82e195 100644 --- a/server/src/main/proto/list_pb.proto +++ b/server/src/main/proto/list_pb.proto @@ -25,7 +25,58 @@ message ListGetResponse { repeated string values = 2; } +message ListDelRequest { + required string key = 1; +} + +message ListDelResponse { + required string status = 1; +} + +message ListLPutRequest { + required string key = 1; + repeated string values = 2; +} + +message ListLPutResponse { + required string status = 1; +} + +message ListRPutRequest { + required string key = 1; + repeated string values = 2; +} + +message ListRPutResponse { + required string status = 1; +} + + +message ListLDelRequest { + required string key = 1; + required int32 values = 2; +} + +message ListLDelResponse { + required string status = 1; +} + + +message ListRDelRequest { + required string key = 1; + required int32 values = 2; +} + +message ListRDelResponse { + required string status = 1; +} + service DstListService { rpc listPut(ListPutRequest) returns (ListPutResponse); rpc listGet(ListGetRequest) returns (ListGetResponse); + rpc listDel(ListDelRequest) returns (ListDelResponse); + rpc listLPut(ListLPutRequest) returns (ListLPutResponse); + rpc listRPut(ListRPutRequest) returns (ListRPutResponse); + rpc listLDel(ListLDelRequest) returns (ListLDelResponse); + rpc listRDel(ListRDelRequest) returns (ListRDelResponse); } From f38aaec1f8f573dfaa2ff858469de22863112bf4 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 18 Aug 2019 22:24:06 +0800 Subject: [PATCH 04/22] enhance test module, Reduce duplicate code --- .../dst/core/operatorImpl/DstListImpl.java | 2 +- .../server/service/DstListServiceImpl.java | 2 +- .../java/test/org/dst/core/KVStoreTest.java | 12 +-- .../org/dst/core/operator/KVSListTest.java | 85 +++++++++---------- 4 files changed, 44 insertions(+), 57 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java index e7a447ad4..56a4fee4d 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstListImpl.java @@ -23,7 +23,7 @@ public Status put(String key, List value) { @Override public List get(String key) { if (!listMap.containsKey(key)) { - //TODO should return a status value + //TODO(tansen) should return a status value return null; } return listMap.get(key); diff --git a/server/src/main/java/org/dst/server/service/DstListServiceImpl.java b/server/src/main/java/org/dst/server/service/DstListServiceImpl.java index 7275c1be3..9049d3a6d 100644 --- a/server/src/main/java/org/dst/server/service/DstListServiceImpl.java +++ b/server/src/main/java/org/dst/server/service/DstListServiceImpl.java @@ -37,7 +37,7 @@ public ListProtocol.ListGetResponse listGet(ListProtocol.ListGetRequest request) ListProtocol.ListGetResponse.newBuilder(); List values = store.list().get(request.getKey()); - // TODO change protocol + // TODO(tansen) change protocol Optional.ofNullable(values) .ifPresent(v -> { values.forEach(value -> responseBuilder.addValues(value)); diff --git a/test/src/test/java/test/org/dst/core/KVStoreTest.java b/test/src/test/java/test/org/dst/core/KVStoreTest.java index 3c5ffbbed..7ca0421e5 100644 --- a/test/src/test/java/test/org/dst/core/KVStoreTest.java +++ b/test/src/test/java/test/org/dst/core/KVStoreTest.java @@ -1,11 +1,7 @@ package test.org.dst.core; -import java.util.List; -import java.util.ArrayList; -import java.util.Map; -import java.util.HashMap; -import java.util.Set; -import java.util.HashSet; +import java.util.*; + import org.dst.core.KVStoreImpl; import org.dst.core.KVStore; import org.dst.core.exception.KeyNotFoundException; @@ -34,8 +30,8 @@ public void testList() { list.add("v3"); ks.list().put("k1", list); Assertions.assertEquals(list, ks.list().get("k1")); - //Assertions.assertTrue(ks.list().del("k1")); - //Assertions.assertNull(ks.list().get("k1")); + Assertions.assertEquals("ok",ks.list().del("k1").toString()); + Assertions.assertNull(ks.list().get("k1")); } @Test diff --git a/test/src/test/java/test/org/dst/core/operator/KVSListTest.java b/test/src/test/java/test/org/dst/core/operator/KVSListTest.java index f371ce4aa..08ad1e866 100644 --- a/test/src/test/java/test/org/dst/core/operator/KVSListTest.java +++ b/test/src/test/java/test/org/dst/core/operator/KVSListTest.java @@ -3,6 +3,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; + +import com.google.common.collect.ImmutableList; import org.dst.core.KVStoreImpl; import org.dst.core.KVStore; import org.junit.jupiter.api.Assertions; @@ -10,81 +12,70 @@ public class KVSListTest { - @Test - public void testPutAndGet() { - KVStore ks = new KVStoreImpl(); + private static List listForKVSTest() { List list = new ArrayList<>(); list.add("v1"); list.add("v2"); list.add("v3"); - ks.list().put("k1", list); - Assertions.assertEquals(list, ks.list().get("k1")); + return list; + } + + + @Test + public void testPutAndGet() { + KVStore store = new KVStoreImpl(); + store.list().put("k1", listForKVSTest()); + Assertions.assertEquals(listForKVSTest(), store.list().get("k1")); } @Test public void testDel() { - KVStore ks = new KVStoreImpl(); - List list = new ArrayList<>(); - list.add("v1"); - list.add("v2"); - list.add("v3"); - ks.list().put("k1", list); - Assertions.assertEquals("ok",ks.list().del("k1").toString()); - Assertions.assertNull(ks.list().get("k1")); + KVStore store = new KVStoreImpl(); + store.list().put("k1", listForKVSTest()); + Assertions.assertEquals("ok",store.list().del("k1").toString()); + Assertions.assertNull(store.list().get("k1")); } @Test public void testLPut() { - KVStore ks = new KVStoreImpl(); - List list1 = new ArrayList(); - list1.add("v1"); - ks.list().put("k1",list1); + KVStore store = new KVStoreImpl(); + store.list().put("k1",listForKVSTest()); List list2 = new ArrayList<>(); - list2.add("v2"); - list2.add("v3"); - ks.list().lput("k1",list2); - Assertions.assertEquals(Arrays.asList("v2","v3","v1"),ks.list().get("k1")); + list2.add("v4"); + list2.add("v5"); + store.list().lput("k1",list2); + Assertions.assertEquals(Arrays.asList("v4","v5","v1","v2","v3"),store.list().get("k1")); } @Test public void testRPut() { - KVStore ks = new KVStoreImpl(); - List list1 = new ArrayList(); - list1.add("v1"); - ks.list().put("k1",list1); + KVStore store = new KVStoreImpl(); + store.list().put("k1",listForKVSTest()); List list2 = new ArrayList<>(); - list2.add("v2"); - list2.add("v3"); - ks.list().rput("k1", list2); - Assertions.assertEquals(Arrays.asList("v1","v2","v3"),ks.list().get("k1")); + list2.add("v4"); + list2.add("v5"); + store.list().rput("k1", list2); + Assertions.assertEquals(Arrays.asList("v1","v2","v3","v4","v5"),store.list().get("k1")); } @Test public void testLDel() { - KVStore ks = new KVStoreImpl(); - List list1 = new ArrayList(); - list1.add("v1"); - list1.add("v2"); - list1.add("v3"); - ks.list().put("k1",list1); - ks.list().ldel("k1",1); - Assertions.assertEquals(Arrays.asList("v2","v3"),ks.list().get("k1")); - Assertions.assertEquals("key not exist",ks.list().ldel("-k",1).toString()); + KVStore store = new KVStoreImpl(); + store.list().put("k1",listForKVSTest()); + store.list().ldel("k1",1); + Assertions.assertEquals(Arrays.asList("v2","v3"),store.list().get("k1")); + Assertions.assertEquals("key not exist",store.list().ldel("-k",1).toString()); } @Test public void testRDel() { - KVStore ks = new KVStoreImpl(); - List list1 = new ArrayList(); - list1.add("v1"); - list1.add("v2"); - list1.add("v3"); - ks.list().put("k1",list1); - ks.list().rdel("k1",1); - Assertions.assertEquals(Arrays.asList("v1","v2"),ks.list().get("k1")); - Assertions.assertEquals("key not exist",ks.list().rdel("-k",1).toString()); + KVStore store = new KVStoreImpl(); + store.list().put("k1",listForKVSTest()); + store.list().rdel("k1",1); + Assertions.assertEquals(Arrays.asList("v1","v2"),store.list().get("k1")); + Assertions.assertEquals("key not exist",store.list().rdel("-k",1).toString()); } } From 3946e483ac33d0302dba87ac4631e393c74964bc Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Mon, 19 Aug 2019 22:38:27 +0800 Subject: [PATCH 05/22] fix --- server/src/main/proto/common_pb.proto | 12 +++++++++ server/src/main/proto/dict_pb.proto | 36 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 server/src/main/proto/common_pb.proto create mode 100644 server/src/main/proto/dict_pb.proto diff --git a/server/src/main/proto/common_pb.proto b/server/src/main/proto/common_pb.proto new file mode 100644 index 000000000..7819e1ff6 --- /dev/null +++ b/server/src/main/proto/common_pb.proto @@ -0,0 +1,12 @@ +syntax="proto2"; + +package dst.server.protocol.pb; + +option java_package="org.dst.server.generated"; +option java_outer_classname="CommonProtocol"; + +enum Status { + OK = 0; + KEY_NOT_FOUND = 1; + UNKNOWN_ERROR = 2; +}; \ No newline at end of file diff --git a/server/src/main/proto/dict_pb.proto b/server/src/main/proto/dict_pb.proto new file mode 100644 index 000000000..86989b854 --- /dev/null +++ b/server/src/main/proto/dict_pb.proto @@ -0,0 +1,36 @@ +syntax="proto2"; + +package dst.server.protocol.pb; + +option java_package="org.dst.server.generated"; +option java_outer_classname="DictProtocol"; +option cc_generic_services = true; + +//Dict +message DstDict { + repeated string keys = 1; + repeated string values = 2; +} + +message DictPutRequest { + required string key = 1; + required DstDict dict = 2; +} + +message DictPutResponse { + required string status = 1; +} + +message DictGetRequest { + required string key = 1; +} + +message DictGetResponse { + required string status = 1; + optional DstDict dict = 2; +} + +service DstDictService { + rpc put(DictPutRequest) returns (DictPutResponse); + rpc get(DictGetRequest) returns (DictGetResponse); +} \ No newline at end of file From 94a53f3a53161222364137c8d7752b1cf50302fc Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Wed, 28 Aug 2019 00:50:12 +0800 Subject: [PATCH 06/22] support table concept --- .../exception/RepeatCreateTableException.java | 8 ++ .../dst/exception/TableNotFoundException.java | 7 ++ .../dst/core/operatorImpl/DstTableImpl.java | 64 +++++++++- .../org/dst/core/operatorset/DstList.java | 9 +- .../org/dst/core/operatorset/DstTable.java | 51 +++++++- .../java/org/dst/core/table/DoubleValue.java | 24 ++++ .../dst/core/table/FieldSpecification.java | 116 ++++++++++++++++++ .../java/org/dst/core/table/FieldValue.java | 21 ++++ .../java/org/dst/core/table/IndexEntry.java | 26 ++++ .../java/org/dst/core/table/IntValue.java | 37 ++++++ .../java/org/dst/core/table/RawDataValue.java | 24 ++++ .../java/org/dst/core/table/RecordEntry.java | 60 +++++++++ .../org/dst/core/table/StringListValue.java | 40 ++++++ .../java/org/dst/core/table/StringValue.java | 39 ++++++ .../dst/core/table/TableSpecification.java | 51 ++++++++ .../org/dst/core/table/ValueTypeEnum.java | 10 ++ .../org/dst/core/operator/KVTableTest.java | 83 +++++++++++++ test/src/test/resources/logback.xml | 54 -------- 18 files changed, 659 insertions(+), 65 deletions(-) create mode 100644 common/src/main/java/org/dst/exception/RepeatCreateTableException.java create mode 100644 common/src/main/java/org/dst/exception/TableNotFoundException.java create mode 100644 core/src/main/java/org/dst/core/table/DoubleValue.java create mode 100644 core/src/main/java/org/dst/core/table/FieldSpecification.java create mode 100644 core/src/main/java/org/dst/core/table/FieldValue.java create mode 100644 core/src/main/java/org/dst/core/table/IndexEntry.java create mode 100644 core/src/main/java/org/dst/core/table/IntValue.java create mode 100644 core/src/main/java/org/dst/core/table/RawDataValue.java create mode 100644 core/src/main/java/org/dst/core/table/RecordEntry.java create mode 100644 core/src/main/java/org/dst/core/table/StringListValue.java create mode 100644 core/src/main/java/org/dst/core/table/StringValue.java create mode 100644 core/src/main/java/org/dst/core/table/TableSpecification.java create mode 100644 core/src/main/java/org/dst/core/table/ValueTypeEnum.java create mode 100644 test/src/test/java/test/org/dst/core/operator/KVTableTest.java delete mode 100644 test/src/test/resources/logback.xml diff --git a/common/src/main/java/org/dst/exception/RepeatCreateTableException.java b/common/src/main/java/org/dst/exception/RepeatCreateTableException.java new file mode 100644 index 000000000..302eee503 --- /dev/null +++ b/common/src/main/java/org/dst/exception/RepeatCreateTableException.java @@ -0,0 +1,8 @@ +package org.dst.exception; + +public class RepeatCreateTableException extends DstException { + + public RepeatCreateTableException(String errorMessage) { + super(String.format("The table %s already exists in store", errorMessage)); + } +} diff --git a/common/src/main/java/org/dst/exception/TableNotFoundException.java b/common/src/main/java/org/dst/exception/TableNotFoundException.java new file mode 100644 index 000000000..3efd4bcde --- /dev/null +++ b/common/src/main/java/org/dst/exception/TableNotFoundException.java @@ -0,0 +1,7 @@ +package org.dst.exception; + +public class TableNotFoundException extends DstException{ + public TableNotFoundException(String errorMessage) { + super(String.format("The table %s not exists in store", errorMessage)); + } +} diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 958cf5085..a609793e3 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,18 +1,76 @@ package org.dst.core.operatorImpl; -import org.dst.core.TableSpecification; -import org.dst.core.RecordEntry; +import org.dst.core.table.FieldValue; +import org.dst.core.table.TableSpecification; +import org.dst.core.table.RecordEntry; import org.dst.core.exception.NotImplementException; import org.dst.core.operatorset.DstTable; +import org.dst.exception.RepeatCreateTableException; +import org.dst.exception.TableNotFoundException; + +import java.util.HashMap; +import java.util.List; public class DstTableImpl implements DstTable { + private HashMap tableMap; + + public DstTableImpl() { + this.tableMap = new HashMap(); + } + @Override public void createTable(TableSpecification tableSpec) { - throw new NotImplementException(); + if (isExist(tableSpec)) { + throw new RepeatCreateTableException(tableSpec.getName()); + } + RecordEntry re = new RecordEntry(); + re.setTableSpec(tableSpec); + tableMap.put(tableSpec.getName(), re); } @Override public void append(RecordEntry recordEntry) { + if (isExist(recordEntry.getTableSpec())) { + throw new TableNotFoundException(recordEntry.getTableSpec().getName()); + } + TableSpecification ts=recordEntry.getTableSpec(); + recordEntry.getIndexEntry(); + RecordEntry re = tableMap.get(recordEntry.getTableSpec().getName()); + + } + + @Override + public TableSpecification getTableByName(String name) { throw new NotImplementException(); } + + @Override + public List query(TableSpecification table) { + throw new NotImplementException(); + } + + @Override + public List queryByConditions(TableSpecification table, FieldValue... fields) { + throw new NotImplementException(); + } + + @Override + public boolean delete(TableSpecification table) { + throw new NotImplementException(); + } + + @Override + public void clear() { + throw new NotImplementException(); + } + + /** + * Determine whether the table has been created + * + * @param tableSpec table description + * @return exists or not exist + */ + public boolean isExist(TableSpecification tableSpec) { + return tableMap.get(tableSpec.name) != null; + } } diff --git a/core/src/main/java/org/dst/core/operatorset/DstList.java b/core/src/main/java/org/dst/core/operatorset/DstList.java index 4d0ba02b1..02f79e3f6 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstList.java +++ b/core/src/main/java/org/dst/core/operatorset/DstList.java @@ -1,6 +1,5 @@ package org.dst.core.operatorset; -import org.dst.core.exception.NotImplementException; import org.dst.utils.Status; import java.util.List; @@ -32,15 +31,15 @@ public interface DstList { Status del(String key); //insert value from the left of list - Status lput(String key, List value) throws NotImplementException; + Status lput(String key, List value); //insert value from the right of list - Status rput(String key, List value) throws NotImplementException; + Status rput(String key, List value); //delete n values from the left of list - Status ldel(String key, int n) throws NotImplementException; + Status ldel(String key, int n); //delete n values from the right of list - Status rdel(String key, int n) throws NotImplementException; + Status rdel(String key, int n); } diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index 9b3209c67..63d81e8a1 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -1,11 +1,56 @@ package org.dst.core.operatorset; -import org.dst.core.RecordEntry; -import org.dst.core.TableSpecification; +import org.dst.core.table.FieldValue; +import org.dst.core.table.RecordEntry; +import org.dst.core.table.TableSpecification; +import java.util.List; public interface DstTable { - void createTable(TableSpecification tableSpec); + /** + * This method will create a new table + * + * @param table the key to store + */ + void createTable(TableSpecification table); + /** + * This method will append content into table + * + * @param recordEntry key & values + */ void append(RecordEntry recordEntry); + + /** + * This method will return table info by table name + * + * @param name table name + */ + TableSpecification getTableByName(String name); + + /** + * This method will return all values by TableSpecification + * + * @param table table + */ + List query(TableSpecification table); + + /** + * This will return values from table store by fields in TableSpecification + * @param table table + */ + List queryByConditions(TableSpecification table, FieldValue... fields); + + /** + * This method will drop table from store by table + * + * @param table TableSpecification + */ + boolean delete(TableSpecification table); + + /** + * This method will clear the whole table store + */ + void clear(); + } diff --git a/core/src/main/java/org/dst/core/table/DoubleValue.java b/core/src/main/java/org/dst/core/table/DoubleValue.java new file mode 100644 index 000000000..6218e4b27 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/DoubleValue.java @@ -0,0 +1,24 @@ +package org.dst.core.table; + +public class DoubleValue extends FieldValue { + + private double value = 0.0; + + public DoubleValue() { + this(0.0); + } + + public DoubleValue(Double value) { + super(-1, ValueTypeEnum.DOUBLE); + this.value = value; + } + + public double getValue() { + return value; + } + + public void setValue(double value) { + this.value = value; + } + +} diff --git a/core/src/main/java/org/dst/core/table/FieldSpecification.java b/core/src/main/java/org/dst/core/table/FieldSpecification.java new file mode 100644 index 000000000..7143c8845 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/FieldSpecification.java @@ -0,0 +1,116 @@ +package org.dst.core.table; + +public class FieldSpecification { + + public final int index; + + public final String name; + + public final ValueTypeEnum valueType; + + /** + * An integer that represent some functions as bits. + * + * bit no. meaning + * 0 Whether the filed is a primary key. 1 is true. + * 1 Whether we should create an index for this field. 1 is true. + */ + private int functionBits; + + public FieldSpecification(int index, String name, ValueTypeEnum valueType, + boolean isPrimary, boolean shouldCreateIndex) { + this.index = index; + this.name = name; + this.valueType = valueType; + markAsPrimary(isPrimary); + markShouldCreateIndex(shouldCreateIndex); + } + + private void markAsPrimary(boolean isPrimary) { + // TODO(qwang): + } + + private void markShouldCreateIndex(boolean shouldCreateIndex) { + // TODO(qwang): + } + + public boolean isPrimary() { + // TODO(qwang) + return false; + } + + public boolean shouldCreateIndex() { + // TODO(qwang) + return false; + } + + public int getIndex() { + return index; + } + + public String getName() { + return name; + } + + public ValueTypeEnum getValueType() { + return valueType; + } + + public int getFunctionBits() { + return functionBits; + } + + public void setFunctionBits(int functionBits) { + this.functionBits = functionBits; + } + + public static class Builder { + + private int index = -1; + private String name = null; + private ValueTypeEnum valueType = ValueTypeEnum.NONE; + private boolean isPrimary = false; + private boolean shouldCreateIndex = false; + + public Builder setIndex(int index) { + this.index = index; + return this; + } + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setValueType(ValueTypeEnum valueType) { + this.valueType = valueType; + return this; + } + + public Builder setIsPrimary(boolean isPrimary) { + this.isPrimary = isPrimary; + return this; + } + + public Builder setShouldCreateIndex(boolean shouldCreateIndex) { + this.shouldCreateIndex = shouldCreateIndex; + return this; + } + + public FieldSpecification build() { + return new FieldSpecification(index, name, valueType, isPrimary, shouldCreateIndex); + } + } + + + + @Override + public String toString() { + return "FieldSpecification{" + + "index=" + index + + ", name='" + name + '\'' + + ", valueType=" + valueType + + ", functionBits=" + functionBits + + '}'; + } +} diff --git a/core/src/main/java/org/dst/core/table/FieldValue.java b/core/src/main/java/org/dst/core/table/FieldValue.java new file mode 100644 index 000000000..831b14c80 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/FieldValue.java @@ -0,0 +1,21 @@ +package org.dst.core.table; + +public class FieldValue { + + public final int index; + + public final ValueTypeEnum valueType; + + protected FieldValue(int index, ValueTypeEnum valueType) { + this.index = index; + this.valueType = valueType; + } + + @Override + public String toString() { + return "FieldValue{" + + "index=" + index + + ", valueType=" + valueType + + '}'; + } +} diff --git a/core/src/main/java/org/dst/core/table/IndexEntry.java b/core/src/main/java/org/dst/core/table/IndexEntry.java new file mode 100644 index 000000000..5975baac1 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/IndexEntry.java @@ -0,0 +1,26 @@ +package org.dst.core.table; + +import java.util.Map; + +public class IndexEntry { + + private int fieldIndex; + private Map indexs; + + + public int getFieldIndex() { + return fieldIndex; + } + + public void setFieldIndex(int fieldIndex) { + this.fieldIndex = fieldIndex; + } + + public Map getIndexs() { + return indexs; + } + + public void setIndexs(Map indexs) { + this.indexs = indexs; + } +} diff --git a/core/src/main/java/org/dst/core/table/IntValue.java b/core/src/main/java/org/dst/core/table/IntValue.java new file mode 100644 index 000000000..583568d4a --- /dev/null +++ b/core/src/main/java/org/dst/core/table/IntValue.java @@ -0,0 +1,37 @@ +package org.dst.core.table; + +public class IntValue extends FieldValue { + + private int value = 0; + + public IntValue() { + this(0); + } + + public IntValue(int value) { + super(-1, ValueTypeEnum.INT); + this.setValue(value); + } + + public IntValue(int index, int value) { + super(index, ValueTypeEnum.INT); + this.setValue(value); + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + @Override + public String toString() { + return "IntValue{" + + "value=" + value + + ", index=" + index + + ", valueType=" + valueType + + '}'; + } +} diff --git a/core/src/main/java/org/dst/core/table/RawDataValue.java b/core/src/main/java/org/dst/core/table/RawDataValue.java new file mode 100644 index 000000000..890041988 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/RawDataValue.java @@ -0,0 +1,24 @@ +package org.dst.core.table; + +public class RawDataValue extends FieldValue { + + private byte[] value = null; + + public RawDataValue() { + this(null); + } + + public RawDataValue(byte[] value) { + super(-1, ValueTypeEnum.RAW_DATA); + this.value = value; + } + + public byte[] getValue() { + return value; + } + + public void setValue(byte[] value) { + this.value = value; + } + +} diff --git a/core/src/main/java/org/dst/core/table/RecordEntry.java b/core/src/main/java/org/dst/core/table/RecordEntry.java new file mode 100644 index 000000000..6c6f2ded8 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/RecordEntry.java @@ -0,0 +1,60 @@ +package org.dst.core.table; + +import java.util.List; +import java.util.Map; + +public class RecordEntry { + + /** + * store description of table + */ + public TableSpecification tableSpec; + + /** + * store all records + */ + public List> fieldValues; + + /** + * store all primarys + */ + public Map primarys; + + /** + * store all index + */ + public IndexEntry indexEntry; + + + public TableSpecification getTableSpec() { + return this.tableSpec; + } + + public void setTableSpec(TableSpecification tableSpec) { + this.tableSpec = tableSpec; + } + + public List> getFieldValues() { + return this.fieldValues; + } + + public void setFieldValues(List> fieldValues) { + this.fieldValues = fieldValues; + } + + public Map getPrimarys() { + return primarys; + } + + public void setPrimarys(Map primarys) { + this.primarys = primarys; + } + + public IndexEntry getIndexEntry() { + return indexEntry; + } + + public void setIndexEntry(IndexEntry indexEntry) { + this.indexEntry = indexEntry; + } +} diff --git a/core/src/main/java/org/dst/core/table/StringListValue.java b/core/src/main/java/org/dst/core/table/StringListValue.java new file mode 100644 index 000000000..8156b93fb --- /dev/null +++ b/core/src/main/java/org/dst/core/table/StringListValue.java @@ -0,0 +1,40 @@ +package org.dst.core.table; + +import java.util.List; + +public class StringListValue extends FieldValue { + + private List value = null; + + public StringListValue() { + this(null); + } + + public StringListValue(List value) { + super(-1, ValueTypeEnum.STRING_LIST); + this.value = value; + } + + public StringListValue(int index, List value) { + super(index, ValueTypeEnum.STRING_LIST); + this.value = value; + } + + public List getValue() { + return value; + } + + public void setValue(List value) { + this.value = value; + } + + @Override + public String toString() { + return "StringListValue{" + + "value=" + value + + ", index=" + index + + ", valueType=" + valueType + + '}'; + } +} + diff --git a/core/src/main/java/org/dst/core/table/StringValue.java b/core/src/main/java/org/dst/core/table/StringValue.java new file mode 100644 index 000000000..91537a83a --- /dev/null +++ b/core/src/main/java/org/dst/core/table/StringValue.java @@ -0,0 +1,39 @@ +package org.dst.core.table; + +public class StringValue extends FieldValue { + + private String value = null; + + public StringValue() { + this(null); + } + + // TODO(qwang): Should we pass by value? + public StringValue(String value) { + super(-1, ValueTypeEnum.STRING); + this.value = value; + } + + public StringValue(int index, String value) { + super(index, ValueTypeEnum.STRING); + this.value = value; + } + + public String getValue() { + return value; + } + + + public void setValue(String value) { + this.value = value; + } + + @Override + public String toString() { + return "StringValue{" + + "value='" + value + '\'' + + ", index=" + index + + ", valueType=" + valueType + + '}'; + } +} diff --git a/core/src/main/java/org/dst/core/table/TableSpecification.java b/core/src/main/java/org/dst/core/table/TableSpecification.java new file mode 100644 index 000000000..58bad5dd2 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/TableSpecification.java @@ -0,0 +1,51 @@ +package org.dst.core.table; + +import java.util.List; + +public class TableSpecification { + + public final String name; + + public final List fields; + + public TableSpecification(String name, List fields) { + this.name = name; + this.fields = fields; + } + + public static class Builder { + + private String name = null; + private List fields = null; + + public Builder setName(String name) { + this.name = name; + return this; + } + + public Builder setFields(List fields) { + this.fields = fields; + return this; + } + + public TableSpecification build() { + return new TableSpecification(name, fields); + } + } + + public String getName() { + return this.name; + } + + public List getFields() { + return this.fields; + } + + @Override + public String toString() { + return "TableSpecification{" + + "name='" + name + '\'' + + ", fields=" + fields + + '}'; + } +} diff --git a/core/src/main/java/org/dst/core/table/ValueTypeEnum.java b/core/src/main/java/org/dst/core/table/ValueTypeEnum.java new file mode 100644 index 000000000..217463a75 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/ValueTypeEnum.java @@ -0,0 +1,10 @@ +package org.dst.core.table; + +public enum ValueTypeEnum { + NONE, + INT, + STRING, + DOUBLE, + STRING_LIST, + RAW_DATA, +} diff --git a/test/src/test/java/test/org/dst/core/operator/KVTableTest.java b/test/src/test/java/test/org/dst/core/operator/KVTableTest.java new file mode 100644 index 000000000..64df6f0dd --- /dev/null +++ b/test/src/test/java/test/org/dst/core/operator/KVTableTest.java @@ -0,0 +1,83 @@ +package test.org.dst.core.operator; + +import com.google.common.collect.ImmutableList; +import org.dst.core.table.FieldSpecification; +import org.dst.core.table.RecordEntry; +import org.dst.core.table.StringValue; +import org.dst.core.table.TableSpecification; +import org.dst.core.table.StringListValue; +import org.dst.core.table.IntValue; +import org.dst.core.table.FieldValue; +import org.dst.core.table.ValueTypeEnum; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +public class KVTableTest { + + Map store1 = new TreeMap(); + Map store2 = + new TreeMap(); + + @Test + public void testTable() { + //创建字段及其属性,是否有主键、是否是索引 + FieldSpecification field1 = + new FieldSpecification(0, "username", ValueTypeEnum.STRING, false, false); + FieldSpecification field2 = + new FieldSpecification(1, "age", ValueTypeEnum.INT, false, false); + FieldSpecification field3 = + new FieldSpecification(2, "hobby", ValueTypeEnum.STRING_LIST, false, false); + List fields = new ArrayList<>(); + fields.add(field1); + fields.add(field2); + fields.add(field3); + + //创建表 + TableSpecification t1 = new TableSpecification("UserInfo", fields); + //TableSpecification t2=new TableSpecification("Community",fields); + + RecordEntry re = new RecordEntry(); + re.setTableSpec(t1); + String key = t1.getName(); + //创建表 + store1.put(key, re); + + + //往表里放数据 + //根据表名获取该表的存储记录, + store1.get(key); + System.out.println("当前表字段信息:" + + store1.get(key).getTableSpec().getFields()); + + + //生成一条记录的内容 + StringValue v1 = new StringValue(0, "檀森伢"); + IntValue v2 = new IntValue(1, 18); + StringListValue v3 = new StringListValue(2, + ImmutableList.of("吃饭", "睡觉", "打豆豆")); + + List values = new ArrayList<>(); + values.add(v1); + values.add(v2); + values.add(v3); + + List> records = new ArrayList<>(); + records.add(values); + + + //往当前表记录插入数据 + store1.get(key).setFieldValues(records); + + System.out.println("当前表所有内容:" + store1.get(key)); + + System.out.println("当前表的记录value信息:" + store1.get(key).getFieldValues()); + + + } + + +} diff --git a/test/src/test/resources/logback.xml b/test/src/test/resources/logback.xml deleted file mode 100644 index aa43afaad..000000000 --- a/test/src/test/resources/logback.xml +++ /dev/null @@ -1,54 +0,0 @@ - - - - - - %date [%thread] %-5level %logger{80} - %msg%n - UTF-8 - - - - - - test/logs/echo-all.%d{yyyy-MM-dd-HH-mm}.log - - 60 - - - - %date [%thread] %-5level %logger{80} - %msg%n - UTF-8 - - - - - - test/logs/echo-warn.%d{yyyy-MM-dd-HH-mm}.log - - 120 - - - WARN - - - - %date [%thread] %-5level %logger{80} - %msg%n - UTF-8 - - - - - - - - - - - - - - - - From 9c86f582f93cd2a2f07a51a2bbfb927b2372f9d5 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Wed, 28 Aug 2019 00:53:04 +0800 Subject: [PATCH 07/22] fix logback.xml --- test/src/test/resources/logback.xml | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 test/src/test/resources/logback.xml diff --git a/test/src/test/resources/logback.xml b/test/src/test/resources/logback.xml new file mode 100644 index 000000000..aa43afaad --- /dev/null +++ b/test/src/test/resources/logback.xml @@ -0,0 +1,54 @@ + + + + + + %date [%thread] %-5level %logger{80} - %msg%n + UTF-8 + + + + + + test/logs/echo-all.%d{yyyy-MM-dd-HH-mm}.log + + 60 + + + + %date [%thread] %-5level %logger{80} - %msg%n + UTF-8 + + + + + + test/logs/echo-warn.%d{yyyy-MM-dd-HH-mm}.log + + 120 + + + WARN + + + + %date [%thread] %-5level %logger{80} - %msg%n + UTF-8 + + + + + + + + + + + + + + + + From e485d030394ff363a69573e379831f2963cb4ff8 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Wed, 28 Aug 2019 22:53:33 +0800 Subject: [PATCH 08/22] improve core table --- .../dst/core/operatorImpl/DstTableImpl.java | 55 +++++++++++---- .../org/dst/core/operatorset/DstTable.java | 9 +-- .../dst/core/table/FieldSpecification.java | 70 +++---------------- 3 files changed, 54 insertions(+), 80 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index a609793e3..00e6f3f73 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,8 +1,6 @@ package org.dst.core.operatorImpl; -import org.dst.core.table.FieldValue; -import org.dst.core.table.TableSpecification; -import org.dst.core.table.RecordEntry; +import org.dst.core.table.*; import org.dst.core.exception.NotImplementException; import org.dst.core.operatorset.DstTable; import org.dst.exception.RepeatCreateTableException; @@ -30,27 +28,58 @@ public void createTable(TableSpecification tableSpec) { @Override public void append(RecordEntry recordEntry) { - if (isExist(recordEntry.getTableSpec())) { + TableSpecification spec = recordEntry.getTableSpec(); + if (!isExist(spec)) { throw new TableNotFoundException(recordEntry.getTableSpec().getName()); } - TableSpecification ts=recordEntry.getTableSpec(); - recordEntry.getIndexEntry(); - RecordEntry re = tableMap.get(recordEntry.getTableSpec().getName()); + RecordEntry store = tableMap.get(spec.getName()); + List> newValues = recordEntry.getFieldValues(); + if (store.getFieldValues() == null) { + store.setFieldValues(newValues); + } else { + store.getFieldValues().addAll(newValues); + } - } + List fields = spec.getFields(); - @Override - public TableSpecification getTableByName(String name) { - throw new NotImplementException(); + //插入索引信息 + for (FieldSpecification field : fields) { + //TODO (tansen) + boolean primary = field.isPrimary(); + if (primary) { + for(List fieldValues : newValues) { + for (FieldValue fieldValue : fieldValues){ + if (fieldValue.index == field.index) { + //store.getPrimarys().put(fieldValue,(store.getFieldValues())); + } + } + + } + } + + //TODO (tansen) + boolean index = field.isIndex(); + if (index) { + for(List fieldValues : newValues) { + for (FieldValue fieldValue : fieldValues){ + if (fieldValue.index == field.index) { + //store.getIndexEntry().put(fieldValue,(store.getFieldValues().size()+1)); + } + } + + } + } + + } } @Override - public List query(TableSpecification table) { + public TableSpecification getTableByName(String name) { throw new NotImplementException(); } @Override - public List queryByConditions(TableSpecification table, FieldValue... fields) { + public List query(TableSpecification table, FieldValue... fields) { throw new NotImplementException(); } diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index 63d81e8a1..b572592ef 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -32,14 +32,9 @@ public interface DstTable { * This method will return all values by TableSpecification * * @param table table + * @param fields support for conditional query */ - List query(TableSpecification table); - - /** - * This will return values from table store by fields in TableSpecification - * @param table table - */ - List queryByConditions(TableSpecification table, FieldValue... fields); + List query(TableSpecification table,FieldValue... fields); /** * This method will drop table from store by table diff --git a/core/src/main/java/org/dst/core/table/FieldSpecification.java b/core/src/main/java/org/dst/core/table/FieldSpecification.java index 7143c8845..20b21ed7a 100644 --- a/core/src/main/java/org/dst/core/table/FieldSpecification.java +++ b/core/src/main/java/org/dst/core/table/FieldSpecification.java @@ -8,14 +8,10 @@ public class FieldSpecification { public final ValueTypeEnum valueType; - /** - * An integer that represent some functions as bits. - * - * bit no. meaning - * 0 Whether the filed is a primary key. 1 is true. - * 1 Whether we should create an index for this field. 1 is true. - */ - private int functionBits; + private boolean isPrimary; + + private boolean isIndex; + public FieldSpecification(int index, String name, ValueTypeEnum valueType, boolean isPrimary, boolean shouldCreateIndex) { @@ -56,61 +52,15 @@ public ValueTypeEnum getValueType() { return valueType; } - public int getFunctionBits() { - return functionBits; - } - - public void setFunctionBits(int functionBits) { - this.functionBits = functionBits; + public void setPrimary(boolean primary) { + isPrimary = primary; } - public static class Builder { - - private int index = -1; - private String name = null; - private ValueTypeEnum valueType = ValueTypeEnum.NONE; - private boolean isPrimary = false; - private boolean shouldCreateIndex = false; - - public Builder setIndex(int index) { - this.index = index; - return this; - } - - public Builder setName(String name) { - this.name = name; - return this; - } - - public Builder setValueType(ValueTypeEnum valueType) { - this.valueType = valueType; - return this; - } - - public Builder setIsPrimary(boolean isPrimary) { - this.isPrimary = isPrimary; - return this; - } - - public Builder setShouldCreateIndex(boolean shouldCreateIndex) { - this.shouldCreateIndex = shouldCreateIndex; - return this; - } - - public FieldSpecification build() { - return new FieldSpecification(index, name, valueType, isPrimary, shouldCreateIndex); - } + public boolean isIndex() { + return isIndex; } - - - @Override - public String toString() { - return "FieldSpecification{" + - "index=" + index + - ", name='" + name + '\'' + - ", valueType=" + valueType + - ", functionBits=" + functionBits + - '}'; + public void setIndex(boolean index) { + isIndex = index; } } From 4da2e0f322812b9adbed3475f91bbda978729502 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Mon, 2 Sep 2019 23:48:54 +0800 Subject: [PATCH 09/22] improve core table --- .../dst/core/operatorImpl/DstTableImpl.java | 118 ++++++++++++++---- .../org/dst/core/operatorset/DstTable.java | 19 ++- .../java/org/dst/core/table/FieldValue.java | 8 ++ 3 files changed, 117 insertions(+), 28 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 00e6f3f73..f44509658 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -5,9 +5,10 @@ import org.dst.core.operatorset.DstTable; import org.dst.exception.RepeatCreateTableException; import org.dst.exception.TableNotFoundException; - +import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.Map; public class DstTableImpl implements DstTable { private HashMap tableMap; @@ -28,69 +29,134 @@ public void createTable(TableSpecification tableSpec) { @Override public void append(RecordEntry recordEntry) { + //TODO(tansen)??? Map的key不可以重复,所以,key不能存对应的字段值。 可以考虑将key和value进行翻转 TableSpecification spec = recordEntry.getTableSpec(); if (!isExist(spec)) { throw new TableNotFoundException(recordEntry.getTableSpec().getName()); } RecordEntry store = tableMap.get(spec.getName()); List> newValues = recordEntry.getFieldValues(); - if (store.getFieldValues() == null) { + List> oldValues = store.getFieldValues(); + + if (oldValues == null) { store.setFieldValues(newValues); } else { - store.getFieldValues().addAll(newValues); + oldValues.addAll(newValues); } + int size = newValues.size(); + int oldSize = oldValues.size(); List fields = spec.getFields(); - //插入索引信息 + //add index & primary map for (FieldSpecification field : fields) { - //TODO (tansen) boolean primary = field.isPrimary(); - if (primary) { - for(List fieldValues : newValues) { - for (FieldValue fieldValue : fieldValues){ - if (fieldValue.index == field.index) { - //store.getPrimarys().put(fieldValue,(store.getFieldValues())); + boolean index = field.isIndex(); + if (primary || index) { + for (int i = 0; i < size; i++) { + List newValue = newValues.get(i); + int s = newValue.size(); + for (int j = 0; j < s; j++) { + if (primary) { + Map primarys = store.getPrimarys(); + if (primarys != null) { + if (newValue.get(i).index == field.index) { + primarys.put(newValue.get(i), (oldSize + i + 1)); + } + } else { + Map newPrimarys = new HashMap<>(); + newPrimarys.put(newValue.get(i), (oldSize + i + 1)); + store.setPrimarys(newPrimarys); + } + } + if (index) { + if (newValue.get(i).index == field.index) { + IndexEntry indexEntry = store.getIndexEntry(); + if (indexEntry != null) { + if (newValue.get(i).index == indexEntry.getFieldIndex()) { + indexEntry.getIndexs().put(newValue.get(i), (oldSize + i + 1)); + } + } else { + if (newValue.get(i).index == field.index) { + IndexEntry newIndexEntry = new IndexEntry(); + newIndexEntry.setFieldIndex(field.index); + Map indexs = new HashMap<>(); + indexs.put(newValue.get(i), (oldSize + i + 1)); + newIndexEntry.setIndexs(indexs); + } + } + } } } - } } + } + } - //TODO (tansen) - boolean index = field.isIndex(); - if (index) { - for(List fieldValues : newValues) { - for (FieldValue fieldValue : fieldValues){ - if (fieldValue.index == field.index) { - //store.getIndexEntry().put(fieldValue,(store.getFieldValues().size()+1)); + @Override + public TableSpecification getTableByName(String name) { + if (tableMap.get(name) == null) { + throw new TableNotFoundException(name); + } + return tableMap.get(name).getTableSpec(); + } + + @Override + public List> query(TableSpecification table, FieldValue... fields) { + List> result = new ArrayList<>(); + if (fields.length == 0) { + return tableMap.get(table.getName()).getFieldValues(); + } else if (fields.length == 1) { + RecordEntry recordEntry = tableMap.get(table.getName()); + List targetFields = table.getFields(); + for (int j = 0; j < targetFields.size(); j++) { + FieldSpecification field = targetFields.get(j); + if (field.index == fields[0].getIndex() && field.isPrimary()) { + Integer index = recordEntry.getPrimarys().get(fields[0]); + List fieldValues = recordEntry.getFieldValues().get(index); + result.add(fieldValues); + } else if (field.index == fields[0].getIndex() && field.isIndex()) { + Integer index = recordEntry.getIndexEntry().getIndexs().get(fields[0]); + List fieldValues = recordEntry.getFieldValues().get(index); + result.add(fieldValues); + } else { + List> fieldValues = recordEntry.getFieldValues(); + int size = fieldValues.size(); + for (int i = 0; i < size; i++) { + if (fieldValues.get(i).equals(fields[0])) { + result.add(fieldValues.get(i)); } } - } } - + } else { + //TODO(tansen) } + return null; } @Override - public TableSpecification getTableByName(String name) { - throw new NotImplementException(); + public RecordEntry drop(TableSpecification table) { + return tableMap.remove(table.getName()); } @Override - public List query(TableSpecification table, FieldValue... fields) { + public boolean verifyLegitimacy(RecordEntry recordEntry) { + //TODO(tansen) throw new NotImplementException(); } @Override - public boolean delete(TableSpecification table) { - throw new NotImplementException(); + public void clearTable(TableSpecification table) { + RecordEntry recordEntry = tableMap.get(table.getName()); + recordEntry.getPrimarys().clear(); + recordEntry.setIndexEntry(null); + recordEntry.getFieldValues().clear(); } @Override public void clear() { - throw new NotImplementException(); + tableMap.clear(); } /** diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index b572592ef..89128322c 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -34,14 +34,29 @@ public interface DstTable { * @param table table * @param fields support for conditional query */ - List query(TableSpecification table,FieldValue... fields); + List> query(TableSpecification table,FieldValue... fields); /** * This method will drop table from store by table * * @param table TableSpecification + * @return value which been deleted */ - boolean delete(TableSpecification table); + RecordEntry drop(TableSpecification table); + + + /** + * Verify the legitimacy of the incoming object + * @param recordEntry recordEntry + * @return is legitimacy + */ + boolean verifyLegitimacy(RecordEntry recordEntry); + + /** + * This method will clear the table by table description + * @param table incoming object + */ + void clearTable(TableSpecification table); /** * This method will clear the whole table store diff --git a/core/src/main/java/org/dst/core/table/FieldValue.java b/core/src/main/java/org/dst/core/table/FieldValue.java index 831b14c80..8fa15a084 100644 --- a/core/src/main/java/org/dst/core/table/FieldValue.java +++ b/core/src/main/java/org/dst/core/table/FieldValue.java @@ -11,6 +11,14 @@ protected FieldValue(int index, ValueTypeEnum valueType) { this.valueType = valueType; } + public int getIndex() { + return index; + } + + public ValueTypeEnum getValueType() { + return valueType; + } + @Override public String toString() { return "FieldValue{" + From 12889e5e10f36ee7b145863f77fa8060090d7985 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Mon, 2 Sep 2019 23:55:22 +0800 Subject: [PATCH 10/22] improve core table --- .../java/org/dst/exception/TableNotFoundException.java | 2 +- .../main/java/org/dst/core/operatorImpl/DstTableImpl.java | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/org/dst/exception/TableNotFoundException.java b/common/src/main/java/org/dst/exception/TableNotFoundException.java index 3efd4bcde..5d4e214e5 100644 --- a/common/src/main/java/org/dst/exception/TableNotFoundException.java +++ b/common/src/main/java/org/dst/exception/TableNotFoundException.java @@ -1,6 +1,6 @@ package org.dst.exception; -public class TableNotFoundException extends DstException{ +public class TableNotFoundException extends DstException { public TableNotFoundException(String errorMessage) { super(String.format("The table %s not exists in store", errorMessage)); } diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index f44509658..369ecbed8 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,8 +1,12 @@ package org.dst.core.operatorImpl; -import org.dst.core.table.*; import org.dst.core.exception.NotImplementException; import org.dst.core.operatorset.DstTable; +import org.dst.core.table.FieldSpecification; +import org.dst.core.table.FieldValue; +import org.dst.core.table.IndexEntry; +import org.dst.core.table.RecordEntry; +import org.dst.core.table.TableSpecification; import org.dst.exception.RepeatCreateTableException; import org.dst.exception.TableNotFoundException; import java.util.ArrayList; @@ -29,7 +33,7 @@ public void createTable(TableSpecification tableSpec) { @Override public void append(RecordEntry recordEntry) { - //TODO(tansen)??? Map的key不可以重复,所以,key不能存对应的字段值。 可以考虑将key和value进行翻转 + //TODO(tansen)??? Map的key不可以重复,所以,key不能存对应的字段值。 可以考虑将key和value进行翻转.利用其他策略实现存储重复的key TableSpecification spec = recordEntry.getTableSpec(); if (!isExist(spec)) { throw new TableNotFoundException(recordEntry.getTableSpec().getName()); From a3c44b6058203a5af6cb064c9b3a463f02addbef Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sat, 7 Sep 2019 18:05:04 +0800 Subject: [PATCH 11/22] support core table --- .../IncorrectRecordFormatException.java | 10 + .../IncorrectTableFormatException.java | 7 + .../PrimaryKeyNotUniqueException.java | 7 + .../dst/core/operatorImpl/DstTableImpl.java | 269 ++++++++++-------- .../org/dst/core/operatorset/DstTable.java | 43 ++- .../java/org/dst/core/table/DoubleValue.java | 10 +- .../main/java/org/dst/core/table/Field.java | 105 +++++++ .../dst/core/table/FieldSpecification.java | 66 ----- .../java/org/dst/core/table/FieldValue.java | 29 -- .../main/java/org/dst/core/table/Index.java | 25 ++ .../java/org/dst/core/table/IndexEntry.java | 26 -- .../java/org/dst/core/table/IntValue.java | 25 +- .../{RawDataValue.java => RawValue.java} | 12 +- .../main/java/org/dst/core/table/Record.java | 25 ++ .../java/org/dst/core/table/RecordEntry.java | 60 ---- .../java/org/dst/core/table/StrListValue.java | 26 ++ .../java/org/dst/core/table/StrValue.java | 22 ++ .../org/dst/core/table/StringListValue.java | 40 --- .../java/org/dst/core/table/StringValue.java | 39 --- .../java/org/dst/core/table/TableEntry.java | 75 +++++ .../dst/core/table/TableSpecification.java | 47 +-- .../main/java/org/dst/core/table/Value.java | 14 + .../{ValueTypeEnum.java => ValueType.java} | 2 +- server/pom.xml | 2 +- 24 files changed, 536 insertions(+), 450 deletions(-) create mode 100644 common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java create mode 100644 common/src/main/java/org/dst/exception/IncorrectTableFormatException.java create mode 100644 common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java create mode 100644 core/src/main/java/org/dst/core/table/Field.java delete mode 100644 core/src/main/java/org/dst/core/table/FieldSpecification.java delete mode 100644 core/src/main/java/org/dst/core/table/FieldValue.java create mode 100644 core/src/main/java/org/dst/core/table/Index.java delete mode 100644 core/src/main/java/org/dst/core/table/IndexEntry.java rename core/src/main/java/org/dst/core/table/{RawDataValue.java => RawValue.java} (56%) create mode 100644 core/src/main/java/org/dst/core/table/Record.java delete mode 100644 core/src/main/java/org/dst/core/table/RecordEntry.java create mode 100644 core/src/main/java/org/dst/core/table/StrListValue.java create mode 100644 core/src/main/java/org/dst/core/table/StrValue.java delete mode 100644 core/src/main/java/org/dst/core/table/StringListValue.java delete mode 100644 core/src/main/java/org/dst/core/table/StringValue.java create mode 100644 core/src/main/java/org/dst/core/table/TableEntry.java create mode 100644 core/src/main/java/org/dst/core/table/Value.java rename core/src/main/java/org/dst/core/table/{ValueTypeEnum.java => ValueType.java} (76%) diff --git a/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java b/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java new file mode 100644 index 000000000..5f988549a --- /dev/null +++ b/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java @@ -0,0 +1,10 @@ +package org.dst.exception; + +public class IncorrectRecordFormatException extends DstException{ + public IncorrectRecordFormatException(String table) { + super(String.format("record is incorrect format for table %s",table)); + } + public IncorrectRecordFormatException() { + super("incorrect records format"); + } +} diff --git a/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java b/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java new file mode 100644 index 000000000..6bfc551c5 --- /dev/null +++ b/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java @@ -0,0 +1,7 @@ +package org.dst.exception; + +public class IncorrectTableFormatException extends DstException{ + public IncorrectTableFormatException() { + super("incorrect table format"); + } +} diff --git a/common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java b/common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java new file mode 100644 index 000000000..fd70d7eb9 --- /dev/null +++ b/common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java @@ -0,0 +1,7 @@ +package org.dst.exception; + +public class PrimaryKeyNotUniqueException extends DstException{ + public PrimaryKeyNotUniqueException() { + super(String.format("primary key is not unique")); + } +} diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 369ecbed8..a4a3d4246 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,95 +1,75 @@ package org.dst.core.operatorImpl; -import org.dst.core.exception.NotImplementException; import org.dst.core.operatorset.DstTable; -import org.dst.core.table.FieldSpecification; -import org.dst.core.table.FieldValue; -import org.dst.core.table.IndexEntry; -import org.dst.core.table.RecordEntry; -import org.dst.core.table.TableSpecification; +import org.dst.core.table.*; +import org.dst.exception.IncorrectRecordFormatException; +import org.dst.exception.IncorrectTableFormatException; import org.dst.exception.RepeatCreateTableException; import org.dst.exception.TableNotFoundException; -import java.util.ArrayList; + +import java.util.Map; import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.Arrays; +import java.util.ArrayList; public class DstTableImpl implements DstTable { - private HashMap tableMap; + + private HashMap tableMap; public DstTableImpl() { - this.tableMap = new HashMap(); + this.tableMap = new HashMap(); } @Override public void createTable(TableSpecification tableSpec) { - if (isExist(tableSpec)) { + checkFormatofTableSpecification(tableSpec); + if (isExist(tableSpec.getName())) { throw new RepeatCreateTableException(tableSpec.getName()); } - RecordEntry re = new RecordEntry(); - re.setTableSpec(tableSpec); - tableMap.put(tableSpec.getName(), re); + TableEntry table = new TableEntry.Builder().tableSpec(tableSpec).builder(); + tableMap.put(tableSpec.getName(), table); } @Override - public void append(RecordEntry recordEntry) { - //TODO(tansen)??? Map的key不可以重复,所以,key不能存对应的字段值。 可以考虑将key和value进行翻转.利用其他策略实现存储重复的key - TableSpecification spec = recordEntry.getTableSpec(); - if (!isExist(spec)) { - throw new TableNotFoundException(recordEntry.getTableSpec().getName()); + public void append(String tableName, List records) { + if (isExist(tableName)) { + throw new TableNotFoundException(tableName); } - RecordEntry store = tableMap.get(spec.getName()); - List> newValues = recordEntry.getFieldValues(); - List> oldValues = store.getFieldValues(); - - if (oldValues == null) { - store.setFieldValues(newValues); + TableEntry store = tableMap.get(tableName); + checkFormatOfRecords(store, records); + List oldRecords = store.getRecords(); + int position = -1; + //append records + if (oldRecords == null) { + store.setRecords(records); } else { - oldValues.addAll(newValues); + position = oldRecords.size(); + oldRecords.addAll(records); } - - int size = newValues.size(); - int oldSize = oldValues.size(); - List fields = spec.getFields(); - - //add index & primary map - for (FieldSpecification field : fields) { - boolean primary = field.isPrimary(); - boolean index = field.isIndex(); + //append index + TableSpecification tableSpec = store.getTableSpec(); + List fields = tableSpec.getFields(); + int fieldsSize = fields.size(); + for (int i = 0; i < fieldsSize; i++) { + boolean primary = fields.get(i).isPrimary(); + boolean index = fields.get(i).isIndex(); if (primary || index) { - for (int i = 0; i < size; i++) { - List newValue = newValues.get(i); - int s = newValue.size(); - for (int j = 0; j < s; j++) { - if (primary) { - Map primarys = store.getPrimarys(); - if (primarys != null) { - if (newValue.get(i).index == field.index) { - primarys.put(newValue.get(i), (oldSize + i + 1)); - } - } else { - Map newPrimarys = new HashMap<>(); - newPrimarys.put(newValue.get(i), (oldSize + i + 1)); - store.setPrimarys(newPrimarys); - } - } - if (index) { - if (newValue.get(i).index == field.index) { - IndexEntry indexEntry = store.getIndexEntry(); - if (indexEntry != null) { - if (newValue.get(i).index == indexEntry.getFieldIndex()) { - indexEntry.getIndexs().put(newValue.get(i), (oldSize + i + 1)); - } - } else { - if (newValue.get(i).index == field.index) { - IndexEntry newIndexEntry = new IndexEntry(); - newIndexEntry.setFieldIndex(field.index); - Map indexs = new HashMap<>(); - indexs.put(newValue.get(i), (oldSize + i + 1)); - newIndexEntry.setIndexs(indexs); - } - } - } + int newRecordSize = records.size(); + Map> indexs = store.getIndex().getIndexs(); + for (int j = 0; j < newRecordSize; j++) { + Value value = records.get(j).getRecord().get(i); + position += 1; + if (primary) { + //TODO (senyer) how to enhance this code :Arrays.asList() ? + indexs.put(value, Arrays.asList(position)); + } + if (index) { + if (indexs.containsKey(value)) { + List positions = indexs.get(value); + positions.add(position); + } else { + indexs.put(value, Arrays.asList(position)); } } } @@ -98,64 +78,78 @@ public void append(RecordEntry recordEntry) { } @Override - public TableSpecification getTableByName(String name) { - if (tableMap.get(name) == null) { - throw new TableNotFoundException(name); + public TableSpecification findTableSpecification(String tableName) { + if (isExist(tableName)) { + throw new TableNotFoundException(tableName); } - return tableMap.get(name).getTableSpec(); + return tableMap.get(tableName).getTableSpec(); } @Override - public List> query(TableSpecification table, FieldValue... fields) { - List> result = new ArrayList<>(); - if (fields.length == 0) { - return tableMap.get(table.getName()).getFieldValues(); - } else if (fields.length == 1) { - RecordEntry recordEntry = tableMap.get(table.getName()); - List targetFields = table.getFields(); - for (int j = 0; j < targetFields.size(); j++) { - FieldSpecification field = targetFields.get(j); - if (field.index == fields[0].getIndex() && field.isPrimary()) { - Integer index = recordEntry.getPrimarys().get(fields[0]); - List fieldValues = recordEntry.getFieldValues().get(index); - result.add(fieldValues); - } else if (field.index == fields[0].getIndex() && field.isIndex()) { - Integer index = recordEntry.getIndexEntry().getIndexs().get(fields[0]); - List fieldValues = recordEntry.getFieldValues().get(index); - result.add(fieldValues); + public List query(String tableName, Map conditions) { + if (isExist(tableName)) { + throw new TableNotFoundException(tableName); + } + List records = tableMap.get(tableName).getRecords(); + if (conditions.isEmpty()) { + return records; + } + List positions = new ArrayList<>(); + + for (Map.Entry entry : conditions.entrySet()) { + Field field = entry.getKey(); + Value value = entry.getValue(); + boolean primary = field.isPrimary(); + boolean index = field.isIndex(); + if (primary || index) { + Index indexs = tableMap.get(tableName).getIndex(); + List currentPositions = indexs.getIndexs().get(value); + if (positions.isEmpty()) { + positions.addAll(currentPositions); } else { - List> fieldValues = recordEntry.getFieldValues(); - int size = fieldValues.size(); - for (int i = 0; i < size; i++) { - if (fieldValues.get(i).equals(fields[0])) { - result.add(fieldValues.get(i)); + positions.retainAll(currentPositions); + } + } else { + TableSpecification tableSpec = tableMap.get(tableName).getTableSpec(); + List fields = tableSpec.getFields(); + List currentPositions = new ArrayList<>(); + for (int i = 0; i < fields.size(); i++) { + if (fields.get(i).getName().equals(field.getName())) { + int size = records.size(); + for (int j = 0; j < size; j++) { + List record = records.get(i).getRecord(); + if (record.get(i).equals(value)) { + currentPositions.add(j); + } } } } + positions.retainAll(currentPositions); } - } else { - //TODO(tansen) } - return null; - } - @Override - public RecordEntry drop(TableSpecification table) { - return tableMap.remove(table.getName()); + List result = new ArrayList<>(); + for (Integer position : positions) { + Record record = records.get(position); + result.add(record); + } + return result; } @Override - public boolean verifyLegitimacy(RecordEntry recordEntry) { - //TODO(tansen) - throw new NotImplementException(); + public boolean drop(String tableName) { + if (isExist(tableName)) { + throw new TableNotFoundException(tableName); + } + tableMap.remove(tableName); + return true; } @Override - public void clearTable(TableSpecification table) { - RecordEntry recordEntry = tableMap.get(table.getName()); - recordEntry.getPrimarys().clear(); - recordEntry.setIndexEntry(null); - recordEntry.getFieldValues().clear(); + public void clearTable(String tableName) { + TableEntry table = tableMap.get(tableName); + table.getIndex().getIndexs().clear(); + table.getRecords().clear(); } @Override @@ -163,13 +157,64 @@ public void clear() { tableMap.clear(); } + /** + * check the records's format + * 1. field locations must correspond one to one + * 2. records can't be empty + * 3. primary must unique TODO (senyer) + * @param records records + * @return boolean + */ + private void checkFormatOfRecords(TableEntry store, List records) { + if (records.isEmpty()) { + throw new IncorrectRecordFormatException(); + } + TableSpecification tableSpec = store.getTableSpec(); + List fields = tableSpec.getFields(); + for (Field field : fields) { + ValueType fieldType = field.getType(); + for (Record record : records) { + List values = record.getRecord(); + for (Value value : values) { + if (!fieldType.equals(value.getType())) { + throw new IncorrectRecordFormatException(); + } + } + } + } + } + + /** + * check format of tableSpecification + * 1. field can't be both index and primary + * 2. table name can't be empty + * 3. at least one field + * + * @param tableSpec tableSpec + * @return boolean + */ + private void checkFormatofTableSpecification(TableSpecification tableSpec) { + if (tableSpec.getName() == null) { + throw new IncorrectTableFormatException(); + } + List fields = tableSpec.getFields(); + if (fields.size() <= 0) { + throw new IncorrectTableFormatException(); + } + for (Field field: fields ) { + if (field.isPrimary()&&field.isIndex()) { + throw new IncorrectTableFormatException(); + } + } + } + /** * Determine whether the table has been created * - * @param tableSpec table description + * @param tableName table description * @return exists or not exist */ - public boolean isExist(TableSpecification tableSpec) { - return tableMap.get(tableSpec.name) != null; + private boolean isExist(String tableName) { + return tableMap.containsKey(tableName); } } diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index 89128322c..cf0cc1688 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -1,9 +1,11 @@ package org.dst.core.operatorset; -import org.dst.core.table.FieldValue; -import org.dst.core.table.RecordEntry; +import org.dst.core.table.Field; +import org.dst.core.table.Record; import org.dst.core.table.TableSpecification; +import org.dst.core.table.Value; import java.util.List; +import java.util.Map; public interface DstTable { @@ -15,48 +17,41 @@ public interface DstTable { void createTable(TableSpecification table); /** - * This method will append content into table + * This method will append list records into table * - * @param recordEntry key & values + * @param tableName tableName + * @param records records */ - void append(RecordEntry recordEntry); - + void append(String tableName, List records); /** * This method will return table info by table name * - * @param name table name + * @param tableName table name */ - TableSpecification getTableByName(String name); + TableSpecification findTableSpecification(String tableName); /** * This method will return all values by TableSpecification * - * @param table table - * @param fields support for conditional query + * @param tableName tableName + * @param conditions support for conditional query */ - List> query(TableSpecification table,FieldValue... fields); + List query(String tableName, Map conditions); /** - * This method will drop table from store by table + * This method will drop table from store by tableName * - * @param table TableSpecification - * @return value which been deleted + * @param tableName tableName + * @return whether drop */ - RecordEntry drop(TableSpecification table); - + boolean drop(String tableName); - /** - * Verify the legitimacy of the incoming object - * @param recordEntry recordEntry - * @return is legitimacy - */ - boolean verifyLegitimacy(RecordEntry recordEntry); /** * This method will clear the table by table description - * @param table incoming object + * @param tableName tableName */ - void clearTable(TableSpecification table); + void clearTable(String tableName); /** * This method will clear the whole table store diff --git a/core/src/main/java/org/dst/core/table/DoubleValue.java b/core/src/main/java/org/dst/core/table/DoubleValue.java index 6218e4b27..4c4508287 100644 --- a/core/src/main/java/org/dst/core/table/DoubleValue.java +++ b/core/src/main/java/org/dst/core/table/DoubleValue.java @@ -1,15 +1,16 @@ package org.dst.core.table; -public class DoubleValue extends FieldValue { +public class DoubleValue extends Value { private double value = 0.0; public DoubleValue() { - this(0.0); + super(ValueType.DOUBLE); + this.value = value; } - public DoubleValue(Double value) { - super(-1, ValueTypeEnum.DOUBLE); + public DoubleValue(double value) { + super(ValueType.DOUBLE); this.value = value; } @@ -20,5 +21,4 @@ public double getValue() { public void setValue(double value) { this.value = value; } - } diff --git a/core/src/main/java/org/dst/core/table/Field.java b/core/src/main/java/org/dst/core/table/Field.java new file mode 100644 index 000000000..9b7a84648 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/Field.java @@ -0,0 +1,105 @@ +package org.dst.core.table; + + +public class Field { + + private String name; + + private ValueType type = ValueType.NONE; + + private boolean primary = false; + + private boolean index = false; + + public Field() { + } + + public Field(String name, ValueType type) { + this.name = name; + this.type = type; + } + + public Field(String name, ValueType type, boolean primary) { + this.name = name; + this.type = type; + this.primary = primary; + } + + public Field(String name, ValueType type, boolean primary, boolean index) { + this.name = name; + this.type = type; + this.primary = primary; + this.index = index; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public ValueType getType() { + return type; + } + + public void setType(ValueType type) { + this.type = type; + } + + public boolean isPrimary() { + return primary; + } + + public void setPrimary(boolean primary) { + this.primary = primary; + } + + public boolean isIndex() { + return index; + } + + public void setIndex(boolean index) { + this.index = index; + } + + + public static class Builder { + + private String name; + + private ValueType type = ValueType.NONE; + + private boolean primary = false; + + private boolean index = false; + + public Builder name(String name) { + this.name = name; + return this; + } + + public Builder type(ValueType type) { + this.type = type; + return this; + } + + public Builder primary(boolean primary) { + this.primary = primary; + return this; + } + + public Builder index(boolean index) { + this.index = index; + return this; + } + + public Field build() { + return new Field(name, type, primary, index); + } + + } + + +} diff --git a/core/src/main/java/org/dst/core/table/FieldSpecification.java b/core/src/main/java/org/dst/core/table/FieldSpecification.java deleted file mode 100644 index 20b21ed7a..000000000 --- a/core/src/main/java/org/dst/core/table/FieldSpecification.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.dst.core.table; - -public class FieldSpecification { - - public final int index; - - public final String name; - - public final ValueTypeEnum valueType; - - private boolean isPrimary; - - private boolean isIndex; - - - public FieldSpecification(int index, String name, ValueTypeEnum valueType, - boolean isPrimary, boolean shouldCreateIndex) { - this.index = index; - this.name = name; - this.valueType = valueType; - markAsPrimary(isPrimary); - markShouldCreateIndex(shouldCreateIndex); - } - - private void markAsPrimary(boolean isPrimary) { - // TODO(qwang): - } - - private void markShouldCreateIndex(boolean shouldCreateIndex) { - // TODO(qwang): - } - - public boolean isPrimary() { - // TODO(qwang) - return false; - } - - public boolean shouldCreateIndex() { - // TODO(qwang) - return false; - } - - public int getIndex() { - return index; - } - - public String getName() { - return name; - } - - public ValueTypeEnum getValueType() { - return valueType; - } - - public void setPrimary(boolean primary) { - isPrimary = primary; - } - - public boolean isIndex() { - return isIndex; - } - - public void setIndex(boolean index) { - isIndex = index; - } -} diff --git a/core/src/main/java/org/dst/core/table/FieldValue.java b/core/src/main/java/org/dst/core/table/FieldValue.java deleted file mode 100644 index 8fa15a084..000000000 --- a/core/src/main/java/org/dst/core/table/FieldValue.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.dst.core.table; - -public class FieldValue { - - public final int index; - - public final ValueTypeEnum valueType; - - protected FieldValue(int index, ValueTypeEnum valueType) { - this.index = index; - this.valueType = valueType; - } - - public int getIndex() { - return index; - } - - public ValueTypeEnum getValueType() { - return valueType; - } - - @Override - public String toString() { - return "FieldValue{" + - "index=" + index + - ", valueType=" + valueType + - '}'; - } -} diff --git a/core/src/main/java/org/dst/core/table/Index.java b/core/src/main/java/org/dst/core/table/Index.java new file mode 100644 index 000000000..9d70f0d25 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/Index.java @@ -0,0 +1,25 @@ +package org.dst.core.table; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Index { + private Map> indexs = new HashMap<>(); + + public Index() { + } + + public Index(Map> indexs) { + this.indexs = indexs; + } + + public Map> getIndexs() { + return indexs; + } + + public void setIndexs(Map> indexs) { + this.indexs = indexs; + } + +} diff --git a/core/src/main/java/org/dst/core/table/IndexEntry.java b/core/src/main/java/org/dst/core/table/IndexEntry.java deleted file mode 100644 index 5975baac1..000000000 --- a/core/src/main/java/org/dst/core/table/IndexEntry.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.dst.core.table; - -import java.util.Map; - -public class IndexEntry { - - private int fieldIndex; - private Map indexs; - - - public int getFieldIndex() { - return fieldIndex; - } - - public void setFieldIndex(int fieldIndex) { - this.fieldIndex = fieldIndex; - } - - public Map getIndexs() { - return indexs; - } - - public void setIndexs(Map indexs) { - this.indexs = indexs; - } -} diff --git a/core/src/main/java/org/dst/core/table/IntValue.java b/core/src/main/java/org/dst/core/table/IntValue.java index 583568d4a..10299fb83 100644 --- a/core/src/main/java/org/dst/core/table/IntValue.java +++ b/core/src/main/java/org/dst/core/table/IntValue.java @@ -1,22 +1,18 @@ package org.dst.core.table; -public class IntValue extends FieldValue { +public class IntValue extends Value { private int value = 0; - public IntValue() { - this(0); + protected IntValue() { + super(ValueType.INT); } - public IntValue(int value) { - super(-1, ValueTypeEnum.INT); - this.setValue(value); + protected IntValue(int value) { + super(ValueType.INT); + this.value = value; } - public IntValue(int index, int value) { - super(index, ValueTypeEnum.INT); - this.setValue(value); - } public int getValue() { return value; @@ -25,13 +21,4 @@ public int getValue() { public void setValue(int value) { this.value = value; } - - @Override - public String toString() { - return "IntValue{" + - "value=" + value + - ", index=" + index + - ", valueType=" + valueType + - '}'; - } } diff --git a/core/src/main/java/org/dst/core/table/RawDataValue.java b/core/src/main/java/org/dst/core/table/RawValue.java similarity index 56% rename from core/src/main/java/org/dst/core/table/RawDataValue.java rename to core/src/main/java/org/dst/core/table/RawValue.java index 890041988..386a37388 100644 --- a/core/src/main/java/org/dst/core/table/RawDataValue.java +++ b/core/src/main/java/org/dst/core/table/RawValue.java @@ -1,15 +1,14 @@ package org.dst.core.table; -public class RawDataValue extends FieldValue { - +public class RawValue extends Value { private byte[] value = null; - public RawDataValue() { - this(null); + public RawValue() { + super(ValueType.RAW_DATA); } - public RawDataValue(byte[] value) { - super(-1, ValueTypeEnum.RAW_DATA); + public RawValue(byte[] value) { + super(ValueType.RAW_DATA); this.value = value; } @@ -20,5 +19,4 @@ public byte[] getValue() { public void setValue(byte[] value) { this.value = value; } - } diff --git a/core/src/main/java/org/dst/core/table/Record.java b/core/src/main/java/org/dst/core/table/Record.java new file mode 100644 index 000000000..ce69d8f1e --- /dev/null +++ b/core/src/main/java/org/dst/core/table/Record.java @@ -0,0 +1,25 @@ +package org.dst.core.table; + +import java.util.ArrayList; +import java.util.List; + +public class Record { + + private List record = new ArrayList<>(); + + public Record() { + } + + public Record(List record) { + this.record = record; + } + + public List getRecord() { + return record; + } + + public void setRecord(List record) { + this.record = record; + } + +} diff --git a/core/src/main/java/org/dst/core/table/RecordEntry.java b/core/src/main/java/org/dst/core/table/RecordEntry.java deleted file mode 100644 index 6c6f2ded8..000000000 --- a/core/src/main/java/org/dst/core/table/RecordEntry.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.dst.core.table; - -import java.util.List; -import java.util.Map; - -public class RecordEntry { - - /** - * store description of table - */ - public TableSpecification tableSpec; - - /** - * store all records - */ - public List> fieldValues; - - /** - * store all primarys - */ - public Map primarys; - - /** - * store all index - */ - public IndexEntry indexEntry; - - - public TableSpecification getTableSpec() { - return this.tableSpec; - } - - public void setTableSpec(TableSpecification tableSpec) { - this.tableSpec = tableSpec; - } - - public List> getFieldValues() { - return this.fieldValues; - } - - public void setFieldValues(List> fieldValues) { - this.fieldValues = fieldValues; - } - - public Map getPrimarys() { - return primarys; - } - - public void setPrimarys(Map primarys) { - this.primarys = primarys; - } - - public IndexEntry getIndexEntry() { - return indexEntry; - } - - public void setIndexEntry(IndexEntry indexEntry) { - this.indexEntry = indexEntry; - } -} diff --git a/core/src/main/java/org/dst/core/table/StrListValue.java b/core/src/main/java/org/dst/core/table/StrListValue.java new file mode 100644 index 000000000..1a4a9e12e --- /dev/null +++ b/core/src/main/java/org/dst/core/table/StrListValue.java @@ -0,0 +1,26 @@ +package org.dst.core.table; + +import java.util.ArrayList; +import java.util.List; + +public class StrListValue extends Value { + private List value = new ArrayList<>(); + + public StrListValue() { + super(ValueType.STRING_LIST); + } + + public StrListValue(List value) { + super(ValueType.STRING_LIST); + this.value = value; + } + + public List getValue() { + return value; + } + + public void setValue(List value) { + this.value = value; + } + +} diff --git a/core/src/main/java/org/dst/core/table/StrValue.java b/core/src/main/java/org/dst/core/table/StrValue.java new file mode 100644 index 000000000..cb2b79a89 --- /dev/null +++ b/core/src/main/java/org/dst/core/table/StrValue.java @@ -0,0 +1,22 @@ +package org.dst.core.table; + +public class StrValue extends Value { + private String value = null; + + protected StrValue() { + super(ValueType.STRING); + } + + protected StrValue(String value) { + super(ValueType.STRING); + this.value = value; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/core/src/main/java/org/dst/core/table/StringListValue.java b/core/src/main/java/org/dst/core/table/StringListValue.java deleted file mode 100644 index 8156b93fb..000000000 --- a/core/src/main/java/org/dst/core/table/StringListValue.java +++ /dev/null @@ -1,40 +0,0 @@ -package org.dst.core.table; - -import java.util.List; - -public class StringListValue extends FieldValue { - - private List value = null; - - public StringListValue() { - this(null); - } - - public StringListValue(List value) { - super(-1, ValueTypeEnum.STRING_LIST); - this.value = value; - } - - public StringListValue(int index, List value) { - super(index, ValueTypeEnum.STRING_LIST); - this.value = value; - } - - public List getValue() { - return value; - } - - public void setValue(List value) { - this.value = value; - } - - @Override - public String toString() { - return "StringListValue{" + - "value=" + value + - ", index=" + index + - ", valueType=" + valueType + - '}'; - } -} - diff --git a/core/src/main/java/org/dst/core/table/StringValue.java b/core/src/main/java/org/dst/core/table/StringValue.java deleted file mode 100644 index 91537a83a..000000000 --- a/core/src/main/java/org/dst/core/table/StringValue.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.dst.core.table; - -public class StringValue extends FieldValue { - - private String value = null; - - public StringValue() { - this(null); - } - - // TODO(qwang): Should we pass by value? - public StringValue(String value) { - super(-1, ValueTypeEnum.STRING); - this.value = value; - } - - public StringValue(int index, String value) { - super(index, ValueTypeEnum.STRING); - this.value = value; - } - - public String getValue() { - return value; - } - - - public void setValue(String value) { - this.value = value; - } - - @Override - public String toString() { - return "StringValue{" + - "value='" + value + '\'' + - ", index=" + index + - ", valueType=" + valueType + - '}'; - } -} diff --git a/core/src/main/java/org/dst/core/table/TableEntry.java b/core/src/main/java/org/dst/core/table/TableEntry.java new file mode 100644 index 000000000..9ca65231d --- /dev/null +++ b/core/src/main/java/org/dst/core/table/TableEntry.java @@ -0,0 +1,75 @@ +package org.dst.core.table; + +import java.util.ArrayList; +import java.util.List; + +public class TableEntry { + + private TableSpecification tableSpec; + + private List records =new ArrayList<>(); + + private Index index = new Index(); + + public TableEntry() { + } + + public TableEntry(TableSpecification tableSpec) { + this.tableSpec = tableSpec; + } + + public TableEntry(TableSpecification tableSpec, List records, Index index) { + this.tableSpec = tableSpec; + this.records = records; + this.index = index; + } + + public TableSpecification getTableSpec() { + return tableSpec; + } + + public void setTableSpec(TableSpecification tableSpec) { + this.tableSpec = tableSpec; + } + + public List getRecords() { + return records; + } + + public void setRecords(List records) { + this.records = records; + } + + public Index getIndex() { + return index; + } + + public void setIndex(Index index) { + this.index = index; + } + + public static class Builder { + private TableSpecification tableSpec; + + private List records; + + private Index index; + + public Builder tableSpec(TableSpecification tableSpec){ + this.tableSpec=tableSpec; + return this; + } + public Builder records(List records){ + this.records=records; + return this; + } + public Builder index(Index index){ + this.index=index; + return this; + } + + public TableEntry builder() { + return new TableEntry(tableSpec, records, index); + } + } +} diff --git a/core/src/main/java/org/dst/core/table/TableSpecification.java b/core/src/main/java/org/dst/core/table/TableSpecification.java index 58bad5dd2..eba6a8957 100644 --- a/core/src/main/java/org/dst/core/table/TableSpecification.java +++ b/core/src/main/java/org/dst/core/table/TableSpecification.java @@ -1,29 +1,49 @@ package org.dst.core.table; +import java.util.ArrayList; import java.util.List; public class TableSpecification { - public final String name; + private String name; - public final List fields; + private List fields = new ArrayList<>(); - public TableSpecification(String name, List fields) { + public TableSpecification() { + } + + public TableSpecification(String name, List fields) { this.name = name; this.fields = fields; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getFields() { + return fields; + } + + public void setFields(List fields) { + this.fields = fields; + } + public static class Builder { private String name = null; - private List fields = null; + private List fields = null; - public Builder setName(String name) { + public Builder name(String name) { this.name = name; return this; } - public Builder setFields(List fields) { + public Builder fields(List fields) { this.fields = fields; return this; } @@ -33,19 +53,4 @@ public TableSpecification build() { } } - public String getName() { - return this.name; - } - - public List getFields() { - return this.fields; - } - - @Override - public String toString() { - return "TableSpecification{" + - "name='" + name + '\'' + - ", fields=" + fields + - '}'; - } } diff --git a/core/src/main/java/org/dst/core/table/Value.java b/core/src/main/java/org/dst/core/table/Value.java new file mode 100644 index 000000000..bfb9f4a7a --- /dev/null +++ b/core/src/main/java/org/dst/core/table/Value.java @@ -0,0 +1,14 @@ +package org.dst.core.table; + +public class Value { + + public final ValueType type; + + protected Value(ValueType type) { + this.type = type; + } + + public ValueType getType() { + return type; + } +} \ No newline at end of file diff --git a/core/src/main/java/org/dst/core/table/ValueTypeEnum.java b/core/src/main/java/org/dst/core/table/ValueType.java similarity index 76% rename from core/src/main/java/org/dst/core/table/ValueTypeEnum.java rename to core/src/main/java/org/dst/core/table/ValueType.java index 217463a75..f693eced1 100644 --- a/core/src/main/java/org/dst/core/table/ValueTypeEnum.java +++ b/core/src/main/java/org/dst/core/table/ValueType.java @@ -1,6 +1,6 @@ package org.dst.core.table; -public enum ValueTypeEnum { +public enum ValueType { NONE, INT, STRING, diff --git a/server/pom.xml b/server/pom.xml index 69db28af9..9d55db208 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -87,7 +87,7 @@ org.dst dst-core - 1.0-SNAPSHOT + ${project.version} From d34af27e325150c8f5605ac526f3853c5628b740 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sat, 7 Sep 2019 18:07:56 +0800 Subject: [PATCH 12/22] support core table --- .../dst/core/operatorImpl/DstTableImpl.java | 1 - .../org/dst/core/operator/KVTableTest.java | 83 ------------------- 2 files changed, 84 deletions(-) delete mode 100644 test/src/test/java/test/org/dst/core/operator/KVTableTest.java diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index a4a3d4246..2b474b691 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -6,7 +6,6 @@ import org.dst.exception.IncorrectTableFormatException; import org.dst.exception.RepeatCreateTableException; import org.dst.exception.TableNotFoundException; - import java.util.Map; import java.util.HashMap; import java.util.List; diff --git a/test/src/test/java/test/org/dst/core/operator/KVTableTest.java b/test/src/test/java/test/org/dst/core/operator/KVTableTest.java deleted file mode 100644 index 64df6f0dd..000000000 --- a/test/src/test/java/test/org/dst/core/operator/KVTableTest.java +++ /dev/null @@ -1,83 +0,0 @@ -package test.org.dst.core.operator; - -import com.google.common.collect.ImmutableList; -import org.dst.core.table.FieldSpecification; -import org.dst.core.table.RecordEntry; -import org.dst.core.table.StringValue; -import org.dst.core.table.TableSpecification; -import org.dst.core.table.StringListValue; -import org.dst.core.table.IntValue; -import org.dst.core.table.FieldValue; -import org.dst.core.table.ValueTypeEnum; -import org.testng.annotations.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -public class KVTableTest { - - Map store1 = new TreeMap(); - Map store2 = - new TreeMap(); - - @Test - public void testTable() { - //创建字段及其属性,是否有主键、是否是索引 - FieldSpecification field1 = - new FieldSpecification(0, "username", ValueTypeEnum.STRING, false, false); - FieldSpecification field2 = - new FieldSpecification(1, "age", ValueTypeEnum.INT, false, false); - FieldSpecification field3 = - new FieldSpecification(2, "hobby", ValueTypeEnum.STRING_LIST, false, false); - List fields = new ArrayList<>(); - fields.add(field1); - fields.add(field2); - fields.add(field3); - - //创建表 - TableSpecification t1 = new TableSpecification("UserInfo", fields); - //TableSpecification t2=new TableSpecification("Community",fields); - - RecordEntry re = new RecordEntry(); - re.setTableSpec(t1); - String key = t1.getName(); - //创建表 - store1.put(key, re); - - - //往表里放数据 - //根据表名获取该表的存储记录, - store1.get(key); - System.out.println("当前表字段信息:" + - store1.get(key).getTableSpec().getFields()); - - - //生成一条记录的内容 - StringValue v1 = new StringValue(0, "檀森伢"); - IntValue v2 = new IntValue(1, 18); - StringListValue v3 = new StringListValue(2, - ImmutableList.of("吃饭", "睡觉", "打豆豆")); - - List values = new ArrayList<>(); - values.add(v1); - values.add(v2); - values.add(v3); - - List> records = new ArrayList<>(); - records.add(values); - - - //往当前表记录插入数据 - store1.get(key).setFieldValues(records); - - System.out.println("当前表所有内容:" + store1.get(key)); - - System.out.println("当前表的记录value信息:" + store1.get(key).getFieldValues()); - - - } - - -} From ad5b3962c5bc2779f84929636e3356c180d35df8 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sat, 7 Sep 2019 20:31:02 +0800 Subject: [PATCH 13/22] support core table --- .../dst/exception/DuplicatedPrimaryKeyException.java | 7 +++++++ .../exception/IncorrectRecordFormatException.java | 9 +++------ .../dst/exception/IncorrectTableFormatException.java | 2 +- .../dst/exception/PrimaryKeyNotUniqueException.java | 7 ------- .../dst/exception/RepeatCreateTableException.java | 4 ++-- .../org/dst/exception/TableNotFoundException.java | 4 ++-- .../java/org/dst/core/operatorImpl/DstTableImpl.java | 12 +++++++++--- 7 files changed, 24 insertions(+), 21 deletions(-) create mode 100644 common/src/main/java/org/dst/exception/DuplicatedPrimaryKeyException.java delete mode 100644 common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java diff --git a/common/src/main/java/org/dst/exception/DuplicatedPrimaryKeyException.java b/common/src/main/java/org/dst/exception/DuplicatedPrimaryKeyException.java new file mode 100644 index 000000000..effa927d3 --- /dev/null +++ b/common/src/main/java/org/dst/exception/DuplicatedPrimaryKeyException.java @@ -0,0 +1,7 @@ +package org.dst.exception; + +public class DuplicatedPrimaryKeyException extends DstException { + public DuplicatedPrimaryKeyException() { + super(String.format("primary key is not unique")); + } +} diff --git a/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java b/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java index 5f988549a..4aa4e9e9f 100644 --- a/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java +++ b/common/src/main/java/org/dst/exception/IncorrectRecordFormatException.java @@ -1,10 +1,7 @@ package org.dst.exception; -public class IncorrectRecordFormatException extends DstException{ - public IncorrectRecordFormatException(String table) { - super(String.format("record is incorrect format for table %s",table)); - } - public IncorrectRecordFormatException() { - super("incorrect records format"); +public class IncorrectRecordFormatException extends DstException { + public IncorrectRecordFormatException(String tableName) { + super(String.format("record is incorrect format for table %s", tableName)); } } diff --git a/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java b/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java index 6bfc551c5..5e58e4fd4 100644 --- a/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java +++ b/common/src/main/java/org/dst/exception/IncorrectTableFormatException.java @@ -1,6 +1,6 @@ package org.dst.exception; -public class IncorrectTableFormatException extends DstException{ +public class IncorrectTableFormatException extends DstException { public IncorrectTableFormatException() { super("incorrect table format"); } diff --git a/common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java b/common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java deleted file mode 100644 index fd70d7eb9..000000000 --- a/common/src/main/java/org/dst/exception/PrimaryKeyNotUniqueException.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.dst.exception; - -public class PrimaryKeyNotUniqueException extends DstException{ - public PrimaryKeyNotUniqueException() { - super(String.format("primary key is not unique")); - } -} diff --git a/common/src/main/java/org/dst/exception/RepeatCreateTableException.java b/common/src/main/java/org/dst/exception/RepeatCreateTableException.java index 302eee503..82ec5c84c 100644 --- a/common/src/main/java/org/dst/exception/RepeatCreateTableException.java +++ b/common/src/main/java/org/dst/exception/RepeatCreateTableException.java @@ -2,7 +2,7 @@ public class RepeatCreateTableException extends DstException { - public RepeatCreateTableException(String errorMessage) { - super(String.format("The table %s already exists in store", errorMessage)); + public RepeatCreateTableException(String tableName) { + super(String.format("The table %s already exists in store", tableName)); } } diff --git a/common/src/main/java/org/dst/exception/TableNotFoundException.java b/common/src/main/java/org/dst/exception/TableNotFoundException.java index 5d4e214e5..8d43e8c1f 100644 --- a/common/src/main/java/org/dst/exception/TableNotFoundException.java +++ b/common/src/main/java/org/dst/exception/TableNotFoundException.java @@ -1,7 +1,7 @@ package org.dst.exception; public class TableNotFoundException extends DstException { - public TableNotFoundException(String errorMessage) { - super(String.format("The table %s not exists in store", errorMessage)); + public TableNotFoundException(String tableName) { + super(String.format("The table %s not exists in store", tableName)); } } diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 2b474b691..388f970ec 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,7 +1,13 @@ package org.dst.core.operatorImpl; import org.dst.core.operatorset.DstTable; -import org.dst.core.table.*; +import org.dst.core.table.TableEntry; +import org.dst.core.table.TableSpecification; +import org.dst.core.table.Record; +import org.dst.core.table.Value; +import org.dst.core.table.Field; +import org.dst.core.table.Index; +import org.dst.core.table.ValueType; import org.dst.exception.IncorrectRecordFormatException; import org.dst.exception.IncorrectTableFormatException; import org.dst.exception.RepeatCreateTableException; @@ -166,7 +172,7 @@ public void clear() { */ private void checkFormatOfRecords(TableEntry store, List records) { if (records.isEmpty()) { - throw new IncorrectRecordFormatException(); + throw new IncorrectRecordFormatException(store.getTableSpec().getName()); } TableSpecification tableSpec = store.getTableSpec(); List fields = tableSpec.getFields(); @@ -176,7 +182,7 @@ private void checkFormatOfRecords(TableEntry store, List records) { List values = record.getRecord(); for (Value value : values) { if (!fieldType.equals(value.getType())) { - throw new IncorrectRecordFormatException(); + throw new IncorrectRecordFormatException(store.getTableSpec().getName()); } } } From 47780da474d2d02b020c76da1f57fea40878e899 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 8 Sep 2019 22:54:27 +0800 Subject: [PATCH 14/22] add test case --- .../dst/core/operatorImpl/DstTableImpl.java | 2 +- .../org/dst/core/operator/KVSTableTest.java | 75 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 test/src/test/java/test/org/dst/core/operator/KVSTableTest.java diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 388f970ec..e4863d308 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -84,7 +84,7 @@ public void append(String tableName, List records) { @Override public TableSpecification findTableSpecification(String tableName) { - if (isExist(tableName)) { + if (!isExist(tableName)) { throw new TableNotFoundException(tableName); } return tableMap.get(tableName).getTableSpec(); diff --git a/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java b/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java new file mode 100644 index 000000000..1252271b9 --- /dev/null +++ b/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java @@ -0,0 +1,75 @@ +package test.org.dst.core.operator; + +import com.google.common.collect.ImmutableList; +import org.dst.core.KVStore; +import org.dst.core.KVStoreImpl; +import org.dst.core.table.Field; +import org.dst.core.table.TableSpecification; +import org.dst.core.table.ValueType; +import org.testng.Assert; +import org.testng.annotations.Test; + +import java.util.List; + +public class KVSTableTest { + + private static List dummyTableSpecificationFields() { + Field primaryField = new Field.Builder() + .name("primaryIntField") + .primary(true) + .type(ValueType.INT) + .index(false) + .build(); + Field indexField = new Field.Builder() + .name("indexStrField") + .primary(false) + .type(ValueType.STRING) + .index(true) + .build(); + Field ordinaryField = new Field.Builder() + .name("ordinaryDoubleField") + .primary(false) + .type(ValueType.STRING_LIST) + .index(false) + .build(); + Field ordinaryStrListField = new Field.Builder() + .name("ordinaryStrListField") + .primary(false) + .type(ValueType.STRING_LIST) + .index(false) + .build(); + return ImmutableList.of(primaryField, indexField, ordinaryField, ordinaryStrListField); + } + + private static List dummyTableEntryData() { + return null; + } + + @Test + public void testCreateTableAndFindTable() { + KVStore store = new KVStoreImpl(); + TableSpecification tableSpecification = new TableSpecification + .Builder() + .name("testTable") + .fields(dummyTableSpecificationFields()) + .build(); + store.tables().createTable(tableSpecification); + TableSpecification testTable = store.tables().findTableSpecification("testTable"); + Assert.assertEquals(testTable, tableSpecification); + } + + @Test + public void testAppend() { + KVStore store = new KVStoreImpl(); + TableSpecification tableSpecification = new TableSpecification + .Builder() + .name("testTable") + .fields(dummyTableSpecificationFields()) + .build(); + store.tables().createTable(tableSpecification); + + + } + + +} From 29da3786ef89e8a4a1215e1a076769a1630aa455 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 15 Sep 2019 22:33:01 +0800 Subject: [PATCH 15/22] add test case --- .../dst/core/operatorImpl/DstTableImpl.java | 32 ++--- .../org/dst/core/operatorset/DstTable.java | 7 +- .../java/org/dst/core/table/DoubleValue.java | 26 ++++ .../main/java/org/dst/core/table/Field.java | 2 - .../java/org/dst/core/table/IntValue.java | 23 +++- .../java/org/dst/core/table/RawValue.java | 21 ++++ .../java/org/dst/core/table/StrListValue.java | 18 +++ .../java/org/dst/core/table/StrValue.java | 23 +++- .../java/org/dst/core/table/TableEntry.java | 22 ++-- .../dst/core/table/TableSpecification.java | 2 +- .../main/java/org/dst/core/table/Value.java | 21 +++- .../org/dst/core/operator/KVSTableTest.java | 112 ++++++++++++++---- 12 files changed, 254 insertions(+), 55 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index e4863d308..2c3dbdd8a 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -12,6 +12,7 @@ import org.dst.exception.IncorrectTableFormatException; import org.dst.exception.RepeatCreateTableException; import org.dst.exception.TableNotFoundException; + import java.util.Map; import java.util.HashMap; import java.util.List; @@ -37,8 +38,9 @@ public void createTable(TableSpecification tableSpec) { } @Override - public void append(String tableName, List records) { - if (isExist(tableName)) { + public void append(String tableName, List sourceRecords) { + ArrayList records = new ArrayList<>(sourceRecords); + if (!isExist(tableName)) { throw new TableNotFoundException(tableName); } TableEntry store = tableMap.get(tableName); @@ -46,7 +48,7 @@ public void append(String tableName, List records) { List oldRecords = store.getRecords(); int position = -1; //append records - if (oldRecords == null) { + if (oldRecords == null || oldRecords.size() <= 0) { store.setRecords(records); } else { position = oldRecords.size(); @@ -92,11 +94,11 @@ public TableSpecification findTableSpecification(String tableName) { @Override public List query(String tableName, Map conditions) { - if (isExist(tableName)) { + if (!isExist(tableName)) { throw new TableNotFoundException(tableName); } List records = tableMap.get(tableName).getRecords(); - if (conditions.isEmpty()) { + if (conditions == null || conditions.isEmpty()) { return records; } List positions = new ArrayList<>(); @@ -143,7 +145,7 @@ public List query(String tableName, Map conditions) { @Override public boolean drop(String tableName) { - if (isExist(tableName)) { + if (!isExist(tableName)) { throw new TableNotFoundException(tableName); } tableMap.remove(tableName); @@ -167,6 +169,7 @@ public void clear() { * 1. field locations must correspond one to one * 2. records can't be empty * 3. primary must unique TODO (senyer) + * * @param records records * @return boolean */ @@ -176,11 +179,12 @@ private void checkFormatOfRecords(TableEntry store, List records) { } TableSpecification tableSpec = store.getTableSpec(); List fields = tableSpec.getFields(); - for (Field field : fields) { - ValueType fieldType = field.getType(); + for (int i = 0; i < fields.size(); i++) { + ValueType fieldType = fields.get(i).getType(); for (Record record : records) { List values = record.getRecord(); - for (Value value : values) { + Value value = values.get(i); + if (value != null) { if (!fieldType.equals(value.getType())) { throw new IncorrectRecordFormatException(store.getTableSpec().getName()); } @@ -191,9 +195,9 @@ private void checkFormatOfRecords(TableEntry store, List records) { /** * check format of tableSpecification - * 1. field can't be both index and primary - * 2. table name can't be empty - * 3. at least one field + * 1. field can't be both index and primary + * 2. table name can't be empty + * 3. at least one field * * @param tableSpec tableSpec * @return boolean @@ -206,8 +210,8 @@ private void checkFormatofTableSpecification(TableSpecification tableSpec) { if (fields.size() <= 0) { throw new IncorrectTableFormatException(); } - for (Field field: fields ) { - if (field.isPrimary()&&field.isIndex()) { + for (Field field : fields) { + if (field.isPrimary() && field.isIndex()) { throw new IncorrectTableFormatException(); } } diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index cf0cc1688..b35285536 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -4,6 +4,7 @@ import org.dst.core.table.Record; import org.dst.core.table.TableSpecification; import org.dst.core.table.Value; + import java.util.List; import java.util.Map; @@ -20,9 +21,10 @@ public interface DstTable { * This method will append list records into table * * @param tableName tableName - * @param records records + * @param records records */ void append(String tableName, List records); + /** * This method will return table info by table name * @@ -33,7 +35,7 @@ public interface DstTable { /** * This method will return all values by TableSpecification * - * @param tableName tableName + * @param tableName tableName * @param conditions support for conditional query */ List query(String tableName, Map conditions); @@ -49,6 +51,7 @@ public interface DstTable { /** * This method will clear the table by table description + * * @param tableName tableName */ void clearTable(String tableName); diff --git a/core/src/main/java/org/dst/core/table/DoubleValue.java b/core/src/main/java/org/dst/core/table/DoubleValue.java index 4c4508287..ad5854509 100644 --- a/core/src/main/java/org/dst/core/table/DoubleValue.java +++ b/core/src/main/java/org/dst/core/table/DoubleValue.java @@ -21,4 +21,30 @@ public double getValue() { public void setValue(double value) { this.value = value; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + if (!super.equals(o)) { + return false; + } + + DoubleValue that = (DoubleValue) o; + + return Double.compare(that.value, value) == 0; + } + + @Override + public int hashCode() { + int result = super.hashCode(); + long temp; + temp = Double.doubleToLongBits(value); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + return result; + } } diff --git a/core/src/main/java/org/dst/core/table/Field.java b/core/src/main/java/org/dst/core/table/Field.java index 9b7a84648..10fd39ca8 100644 --- a/core/src/main/java/org/dst/core/table/Field.java +++ b/core/src/main/java/org/dst/core/table/Field.java @@ -100,6 +100,4 @@ public Field build() { } } - - } diff --git a/core/src/main/java/org/dst/core/table/IntValue.java b/core/src/main/java/org/dst/core/table/IntValue.java index 10299fb83..ad0e88bb8 100644 --- a/core/src/main/java/org/dst/core/table/IntValue.java +++ b/core/src/main/java/org/dst/core/table/IntValue.java @@ -4,11 +4,11 @@ public class IntValue extends Value { private int value = 0; - protected IntValue() { + public IntValue() { super(ValueType.INT); } - protected IntValue(int value) { + public IntValue(int value) { super(ValueType.INT); this.value = value; } @@ -21,4 +21,23 @@ public int getValue() { public void setValue(int value) { this.value = value; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + IntValue intValue = (IntValue) o; + + return value == intValue.value; + } + + @Override + public int hashCode() { + return value; + } } diff --git a/core/src/main/java/org/dst/core/table/RawValue.java b/core/src/main/java/org/dst/core/table/RawValue.java index 386a37388..b82ad2165 100644 --- a/core/src/main/java/org/dst/core/table/RawValue.java +++ b/core/src/main/java/org/dst/core/table/RawValue.java @@ -1,5 +1,7 @@ package org.dst.core.table; +import java.util.Arrays; + public class RawValue extends Value { private byte[] value = null; @@ -19,4 +21,23 @@ public byte[] getValue() { public void setValue(byte[] value) { this.value = value; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + RawValue rawValue = (RawValue) o; + + return Arrays.equals(value, rawValue.value); + } + + @Override + public int hashCode() { + return Arrays.hashCode(value); + } } diff --git a/core/src/main/java/org/dst/core/table/StrListValue.java b/core/src/main/java/org/dst/core/table/StrListValue.java index 1a4a9e12e..064fe73f1 100644 --- a/core/src/main/java/org/dst/core/table/StrListValue.java +++ b/core/src/main/java/org/dst/core/table/StrListValue.java @@ -23,4 +23,22 @@ public void setValue(List value) { this.value = value; } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + StrListValue that = (StrListValue) o; + + return value != null ? value.equals(that.value) : that.value == null; + } + + @Override + public int hashCode() { + return value != null ? value.hashCode() : 0; + } } diff --git a/core/src/main/java/org/dst/core/table/StrValue.java b/core/src/main/java/org/dst/core/table/StrValue.java index cb2b79a89..ffcdc57fe 100644 --- a/core/src/main/java/org/dst/core/table/StrValue.java +++ b/core/src/main/java/org/dst/core/table/StrValue.java @@ -3,11 +3,11 @@ public class StrValue extends Value { private String value = null; - protected StrValue() { + public StrValue() { super(ValueType.STRING); } - protected StrValue(String value) { + public StrValue(String value) { super(ValueType.STRING); this.value = value; } @@ -19,4 +19,23 @@ public String getValue() { public void setValue(String value) { this.value = value; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + StrValue strValue = (StrValue) o; + + return value != null ? value.equals(strValue.value) : strValue.value == null; + } + + @Override + public int hashCode() { + return value != null ? value.hashCode() : 0; + } } diff --git a/core/src/main/java/org/dst/core/table/TableEntry.java b/core/src/main/java/org/dst/core/table/TableEntry.java index 9ca65231d..056830668 100644 --- a/core/src/main/java/org/dst/core/table/TableEntry.java +++ b/core/src/main/java/org/dst/core/table/TableEntry.java @@ -7,7 +7,7 @@ public class TableEntry { private TableSpecification tableSpec; - private List records =new ArrayList<>(); + private List records = new ArrayList<>(); private Index index = new Index(); @@ -51,25 +51,27 @@ public void setIndex(Index index) { public static class Builder { private TableSpecification tableSpec; - private List records; + private List records = new ArrayList<>(); - private Index index; + private Index index = new Index(); - public Builder tableSpec(TableSpecification tableSpec){ - this.tableSpec=tableSpec; + public Builder tableSpec(TableSpecification tableSpec) { + this.tableSpec = tableSpec; return this; } - public Builder records(List records){ - this.records=records; + + public Builder records(List records) { + this.records = records; return this; } - public Builder index(Index index){ - this.index=index; + + public Builder index(Index index) { + this.index = index; return this; } public TableEntry builder() { - return new TableEntry(tableSpec, records, index); + return new TableEntry(tableSpec, records, index); } } } diff --git a/core/src/main/java/org/dst/core/table/TableSpecification.java b/core/src/main/java/org/dst/core/table/TableSpecification.java index eba6a8957..d563cea0f 100644 --- a/core/src/main/java/org/dst/core/table/TableSpecification.java +++ b/core/src/main/java/org/dst/core/table/TableSpecification.java @@ -36,7 +36,7 @@ public void setFields(List fields) { public static class Builder { private String name = null; - private List fields = null; + private List fields = new ArrayList<>(); public Builder name(String name) { this.name = name; diff --git a/core/src/main/java/org/dst/core/table/Value.java b/core/src/main/java/org/dst/core/table/Value.java index bfb9f4a7a..8b3798618 100644 --- a/core/src/main/java/org/dst/core/table/Value.java +++ b/core/src/main/java/org/dst/core/table/Value.java @@ -4,11 +4,30 @@ public class Value { public final ValueType type; - protected Value(ValueType type) { + public Value(ValueType type) { this.type = type; } public ValueType getType() { return type; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Value value = (Value) o; + + return type == value.type; + } + + @Override + public int hashCode() { + return type != null ? type.hashCode() : 0; + } } \ No newline at end of file diff --git a/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java b/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java index 1252271b9..78c699905 100644 --- a/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java +++ b/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java @@ -5,31 +5,44 @@ import org.dst.core.KVStoreImpl; import org.dst.core.table.Field; import org.dst.core.table.TableSpecification; +import org.dst.core.table.Value; import org.dst.core.table.ValueType; +import org.dst.core.table.StrListValue; +import org.dst.core.table.StrValue; +import org.dst.core.table.Record; +import org.dst.core.table.IntValue; +import org.dst.exception.TableNotFoundException; import org.testng.Assert; import org.testng.annotations.Test; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; public class KVSTableTest { + private static final KVStore store = new KVStoreImpl(); + + private static final String TEST_TABLE = "testTable"; + private static List dummyTableSpecificationFields() { Field primaryField = new Field.Builder() - .name("primaryIntField") + .name("primaryField") .primary(true) .type(ValueType.INT) .index(false) .build(); Field indexField = new Field.Builder() - .name("indexStrField") + .name("indexField") .primary(false) .type(ValueType.STRING) .index(true) .build(); Field ordinaryField = new Field.Builder() - .name("ordinaryDoubleField") + .name("ordinaryField") .primary(false) - .type(ValueType.STRING_LIST) + .type(ValueType.STRING) .index(false) .build(); Field ordinaryStrListField = new Field.Builder() @@ -41,35 +54,92 @@ private static List dummyTableSpecificationFields() { return ImmutableList.of(primaryField, indexField, ordinaryField, ordinaryStrListField); } - private static List dummyTableEntryData() { - return null; - } - @Test - public void testCreateTableAndFindTable() { - KVStore store = new KVStoreImpl(); + private void dummyCreateTable() { + store.tables().clear(); TableSpecification tableSpecification = new TableSpecification .Builder() - .name("testTable") + .name(TEST_TABLE) .fields(dummyTableSpecificationFields()) .build(); store.tables().createTable(tableSpecification); - TableSpecification testTable = store.tables().findTableSpecification("testTable"); - Assert.assertEquals(testTable, tableSpecification); } - @Test - public void testAppend() { - KVStore store = new KVStoreImpl(); - TableSpecification tableSpecification = new TableSpecification - .Builder() - .name("testTable") - .fields(dummyTableSpecificationFields()) + private static List dummyTableEntryData() { + Value primaryValue = new IntValue(1); + Value indexValue = new StrValue("1111"); + Value ordinaryValue = new StrValue("ordinaryValue"); + Value strListValue = new StrListValue(ImmutableList.of("aaa", "bbb", "ccc", "ddd")); + + Record record = new Record(); + List values = new ArrayList<>(); + // orderly addition + values.add(primaryValue); + values.add(indexValue); + values.add(ordinaryValue); + values.add(strListValue); + record.setRecord(values); + + return ImmutableList.of(record); + } + + + @Test(priority = 1) + public void testFindTableSpecification() { + dummyCreateTable(); + TableSpecification testTable = store.tables().findTableSpecification(TEST_TABLE); + Assert.assertEquals(testTable.getName(), TEST_TABLE); + } + + @Test(priority = 2) + public void testAppendAndQuery() { + List records = dummyTableEntryData(); + store.tables().append(TEST_TABLE, records); + + List queryData = store.tables().query(TEST_TABLE, null); + Assert.assertEquals(queryData.size(), 1); + } + + @Test(priority = 3,dependsOnMethods = "testAppendAndQuery") + public void testQueryByConditions() { + Map conditions = new HashMap<>(); + Field primaryField = new Field.Builder() + .name("primaryField") + .primary(true) + .type(ValueType.INT) + .index(false) .build(); - store.tables().createTable(tableSpecification); + Value primaryValue = new IntValue(1); + conditions.put(primaryField, primaryValue); + List queryData = store.tables().query(TEST_TABLE, conditions); + Assert.assertEquals(1, queryData.size()); + } + @Test(priority = 4,expectedExceptions = TableNotFoundException.class) + public void testClear() { + store.tables().clear(); + store.tables().findTableSpecification(TEST_TABLE); } + + @Test(priority = 5) + public void testClearTable() { + store.tables().clear(); + dummyCreateTable(); + store.tables().append(TEST_TABLE, dummyTableEntryData()); + store.tables().clearTable(TEST_TABLE); + List queryData = store.tables().query(TEST_TABLE, null); + Assert.assertEquals(0, queryData.size()); + } + + @Test(priority = 6,expectedExceptions = TableNotFoundException.class) + public void testDrop() { + store.tables().clear(); + dummyCreateTable(); + store.tables().append(TEST_TABLE, dummyTableEntryData()); + store.tables().drop(TEST_TABLE); + store.tables().findTableSpecification(TEST_TABLE); + } } From dca46648b284445c63bd8908bcbc6fc304b6875d Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 15 Sep 2019 22:37:57 +0800 Subject: [PATCH 16/22] add test case --- .../test/java/test/org/dst/core/operator/KVSTableTest.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java b/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java index 78c699905..82033cf59 100644 --- a/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java +++ b/test/src/test/java/test/org/dst/core/operator/KVSTableTest.java @@ -122,11 +122,8 @@ public void testClear() { store.tables().findTableSpecification(TEST_TABLE); } - - @Test(priority = 5) public void testClearTable() { - store.tables().clear(); dummyCreateTable(); store.tables().append(TEST_TABLE, dummyTableEntryData()); store.tables().clearTable(TEST_TABLE); @@ -136,7 +133,6 @@ public void testClearTable() { @Test(priority = 6,expectedExceptions = TableNotFoundException.class) public void testDrop() { - store.tables().clear(); dummyCreateTable(); store.tables().append(TEST_TABLE, dummyTableEntryData()); store.tables().drop(TEST_TABLE); From cc0c989b73f71f04ff2d64dc2c9db1b94156f63c Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sat, 21 Sep 2019 09:30:38 +0800 Subject: [PATCH 17/22] Merge branch 'master' of github.com:dst-project/dst into server_table # Conflicts: # common/src/main/java/org/dst/common/protobuf/common_pb.proto # core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java --- .../DuplicatedPrimaryKeyException.java | 4 ++-- .../IncorrectRecordFormatException.java | 2 +- .../IncorrectTableFormatException.java | 4 ++-- .../exception/RepeatCreateTableException.java | 8 ------- .../TableAlreadyExistsException.java | 8 +++++++ .../exception/TableNotFoundException.java | 2 +- .../dst/core/operatorImpl/DstTableImpl.java | 15 ++++++------ .../org/dst/core/operatorset/DstTable.java | 24 +++++++++---------- .../main/java/org/dst/core/table/Field.java | 19 +++++++-------- .../main/java/org/dst/core/table/Value.java | 2 +- 10 files changed, 43 insertions(+), 45 deletions(-) delete mode 100644 common/src/main/java/org/dst/common/exception/RepeatCreateTableException.java create mode 100644 common/src/main/java/org/dst/common/exception/TableAlreadyExistsException.java diff --git a/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java b/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java index 90891e265..3c7bea191 100644 --- a/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java +++ b/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java @@ -1,7 +1,7 @@ package org.dst.common.exception; public class DuplicatedPrimaryKeyException extends DstException { - public DuplicatedPrimaryKeyException() { - super(String.format("primary key is not unique")); + public DuplicatedPrimaryKeyException(String key) { + super(String.format("Primary key %s is not unique", key)); } } diff --git a/common/src/main/java/org/dst/common/exception/IncorrectRecordFormatException.java b/common/src/main/java/org/dst/common/exception/IncorrectRecordFormatException.java index 8f7ade46b..a40ca1957 100644 --- a/common/src/main/java/org/dst/common/exception/IncorrectRecordFormatException.java +++ b/common/src/main/java/org/dst/common/exception/IncorrectRecordFormatException.java @@ -2,6 +2,6 @@ public class IncorrectRecordFormatException extends DstException { public IncorrectRecordFormatException(String tableName) { - super(String.format("record is incorrect format for table %s", tableName)); + super(String.format("Incorrect record format of table %s", tableName)); } } diff --git a/common/src/main/java/org/dst/common/exception/IncorrectTableFormatException.java b/common/src/main/java/org/dst/common/exception/IncorrectTableFormatException.java index ffdeed374..4484b5c68 100644 --- a/common/src/main/java/org/dst/common/exception/IncorrectTableFormatException.java +++ b/common/src/main/java/org/dst/common/exception/IncorrectTableFormatException.java @@ -1,7 +1,7 @@ package org.dst.common.exception; public class IncorrectTableFormatException extends DstException { - public IncorrectTableFormatException() { - super("incorrect table format"); + public IncorrectTableFormatException(String tableName) { + super(String.format("Incorrect specification format of table %s", tableName)); } } diff --git a/common/src/main/java/org/dst/common/exception/RepeatCreateTableException.java b/common/src/main/java/org/dst/common/exception/RepeatCreateTableException.java deleted file mode 100644 index 2e88c8289..000000000 --- a/common/src/main/java/org/dst/common/exception/RepeatCreateTableException.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.dst.common.exception; - -public class RepeatCreateTableException extends DstException { - - public RepeatCreateTableException(String tableName) { - super(String.format("The table %s already exists in store", tableName)); - } -} diff --git a/common/src/main/java/org/dst/common/exception/TableAlreadyExistsException.java b/common/src/main/java/org/dst/common/exception/TableAlreadyExistsException.java new file mode 100644 index 000000000..0def88feb --- /dev/null +++ b/common/src/main/java/org/dst/common/exception/TableAlreadyExistsException.java @@ -0,0 +1,8 @@ +package org.dst.common.exception; + +public class TableAlreadyExistsException extends DstException { + + public TableAlreadyExistsException(String tableName) { + super(String.format("The table %s already exists", tableName)); + } +} diff --git a/common/src/main/java/org/dst/common/exception/TableNotFoundException.java b/common/src/main/java/org/dst/common/exception/TableNotFoundException.java index 7c7eedebb..5f0f8ddcc 100644 --- a/common/src/main/java/org/dst/common/exception/TableNotFoundException.java +++ b/common/src/main/java/org/dst/common/exception/TableNotFoundException.java @@ -2,6 +2,6 @@ public class TableNotFoundException extends DstException { public TableNotFoundException(String tableName) { - super(String.format("The table %s not exists in store", tableName)); + super(String.format("Table %s not found.", tableName)); } } diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 842107f06..5db4f5c6e 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -10,9 +10,8 @@ import org.dst.core.table.ValueType; import org.dst.common.exception.IncorrectRecordFormatException; import org.dst.common.exception.IncorrectTableFormatException; -import org.dst.common.exception.RepeatCreateTableException; +import org.dst.common.exception.TableAlreadyExistsException; import org.dst.common.exception.TableNotFoundException; - import java.util.Map; import java.util.HashMap; import java.util.List; @@ -29,9 +28,9 @@ public DstTableImpl() { @Override public void createTable(TableSpecification tableSpec) { - checkFormatofTableSpecification(tableSpec); + checkFormatefTableSpecification(tableSpec); if (isExist(tableSpec.getName())) { - throw new RepeatCreateTableException(tableSpec.getName()); + throw new TableAlreadyExistsException(tableSpec.getName()); } TableEntry table = new TableEntry.Builder().tableSpec(tableSpec).builder(); tableMap.put(tableSpec.getName(), table); @@ -202,17 +201,17 @@ private void checkFormatOfRecords(TableEntry store, List records) { * @param tableSpec tableSpec * @return boolean */ - private void checkFormatofTableSpecification(TableSpecification tableSpec) { + private void checkFormatefTableSpecification(TableSpecification tableSpec) { if (tableSpec.getName() == null) { - throw new IncorrectTableFormatException(); + throw new IncorrectTableFormatException(null); } List fields = tableSpec.getFields(); if (fields.size() <= 0) { - throw new IncorrectTableFormatException(); + throw new IncorrectTableFormatException(tableSpec.getName()); } for (Field field : fields) { if (field.isPrimary() && field.isIndex()) { - throw new IncorrectTableFormatException(); + throw new IncorrectTableFormatException(tableSpec.getName()); } } } diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index b35285536..24212e6e2 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -11,53 +11,53 @@ public interface DstTable { /** - * This method will create a new table + * Create a new table by the given table specification. * - * @param table the key to store + * @param table The specification of the table that will be created. */ void createTable(TableSpecification table); /** - * This method will append list records into table + * Append a list of records to a table. * * @param tableName tableName - * @param records records + * @param records The records that will be append to the table. */ void append(String tableName, List records); /** - * This method will return table info by table name + * Find a table specification by tableName * - * @param tableName table name + * @param tableName tableName */ TableSpecification findTableSpecification(String tableName); /** - * This method will return all values by TableSpecification + * Query target Records by tableName and query conditions * * @param tableName tableName - * @param conditions support for conditional query + * @param conditions query conditions */ List query(String tableName, Map conditions); /** - * This method will drop table from store by tableName + * Drop a table from store by tableName * * @param tableName tableName - * @return whether drop + * @return drop result : true or false */ boolean drop(String tableName); /** - * This method will clear the table by table description + * Clear a table by tableName * * @param tableName tableName */ void clearTable(String tableName); /** - * This method will clear the whole table store + * Clear the whole table store */ void clear(); diff --git a/core/src/main/java/org/dst/core/table/Field.java b/core/src/main/java/org/dst/core/table/Field.java index 10fd39ca8..45c92ec68 100644 --- a/core/src/main/java/org/dst/core/table/Field.java +++ b/core/src/main/java/org/dst/core/table/Field.java @@ -7,9 +7,9 @@ public class Field { private ValueType type = ValueType.NONE; - private boolean primary = false; + private boolean isPrimary = false; - private boolean index = false; + private boolean isIndex = false; public Field() { } @@ -22,14 +22,14 @@ public Field(String name, ValueType type) { public Field(String name, ValueType type, boolean primary) { this.name = name; this.type = type; - this.primary = primary; + this.isPrimary = primary; } public Field(String name, ValueType type, boolean primary, boolean index) { this.name = name; this.type = type; - this.primary = primary; - this.index = index; + this.isPrimary = primary; + this.isIndex = index; } public String getName() { @@ -49,22 +49,21 @@ public void setType(ValueType type) { } public boolean isPrimary() { - return primary; + return isPrimary; } public void setPrimary(boolean primary) { - this.primary = primary; + isPrimary = primary; } public boolean isIndex() { - return index; + return isIndex; } public void setIndex(boolean index) { - this.index = index; + isIndex = index; } - public static class Builder { private String name; diff --git a/core/src/main/java/org/dst/core/table/Value.java b/core/src/main/java/org/dst/core/table/Value.java index 8b3798618..d175ba672 100644 --- a/core/src/main/java/org/dst/core/table/Value.java +++ b/core/src/main/java/org/dst/core/table/Value.java @@ -30,4 +30,4 @@ public boolean equals(Object o) { public int hashCode() { return type != null ? type.hashCode() : 0; } -} \ No newline at end of file +} From 0f09a010a0eebedd4c30d81c848956230634d533 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 22 Sep 2019 09:54:43 +0800 Subject: [PATCH 18/22] enchance core-table code --- .../DuplicatedPrimaryKeyException.java | 2 +- .../dst/core/operatorImpl/DstTableImpl.java | 48 ++++++++++--------- .../org/dst/core/operatorset/DstTable.java | 30 ++++++------ .../dst/test/core/operator/KVSTableTest.java | 8 ++-- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java b/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java index 3c7bea191..deff71bcd 100644 --- a/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java +++ b/common/src/main/java/org/dst/common/exception/DuplicatedPrimaryKeyException.java @@ -2,6 +2,6 @@ public class DuplicatedPrimaryKeyException extends DstException { public DuplicatedPrimaryKeyException(String key) { - super(String.format("Primary key %s is not unique", key)); + super(String.format("Primary key %s is not unique.", key)); } } diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 5db4f5c6e..17fb8adf6 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -12,6 +12,7 @@ import org.dst.common.exception.IncorrectTableFormatException; import org.dst.common.exception.TableAlreadyExistsException; import org.dst.common.exception.TableNotFoundException; + import java.util.Map; import java.util.HashMap; import java.util.List; @@ -28,7 +29,7 @@ public DstTableImpl() { @Override public void createTable(TableSpecification tableSpec) { - checkFormatefTableSpecification(tableSpec); + checkTableSpecificationFormat(tableSpec); if (isExist(tableSpec.getName())) { throw new TableAlreadyExistsException(tableSpec.getName()); } @@ -38,23 +39,21 @@ public void createTable(TableSpecification tableSpec) { @Override public void append(String tableName, List sourceRecords) { - ArrayList records = new ArrayList<>(sourceRecords); - if (!isExist(tableName)) { - throw new TableNotFoundException(tableName); - } - TableEntry store = tableMap.get(tableName); - checkFormatOfRecords(store, records); - List oldRecords = store.getRecords(); + List records = new ArrayList<>(sourceRecords); + checkTableExists(tableName); + TableEntry tableEntry = tableMap.get(tableName); + checkRecordsFormat(tableEntry, records); + List oldRecords = tableEntry.getRecords(); int position = -1; //append records if (oldRecords == null || oldRecords.size() <= 0) { - store.setRecords(records); + tableEntry.setRecords(records); } else { position = oldRecords.size(); oldRecords.addAll(records); } //append index - TableSpecification tableSpec = store.getTableSpec(); + TableSpecification tableSpec = tableEntry.getTableSpec(); List fields = tableSpec.getFields(); int fieldsSize = fields.size(); for (int i = 0; i < fieldsSize; i++) { @@ -62,7 +61,7 @@ public void append(String tableName, List sourceRecords) { boolean index = fields.get(i).isIndex(); if (primary || index) { int newRecordSize = records.size(); - Map> indexs = store.getIndex().getIndexs(); + Map> indexs = tableEntry.getIndex().getIndexs(); for (int j = 0; j < newRecordSize; j++) { Value value = records.get(j).getRecord().get(i); position += 1; @@ -84,18 +83,14 @@ public void append(String tableName, List sourceRecords) { } @Override - public TableSpecification findTableSpecification(String tableName) { - if (!isExist(tableName)) { - throw new TableNotFoundException(tableName); - } + public TableSpecification getTableSpecification(String tableName) { + checkTableExists(tableName); return tableMap.get(tableName).getTableSpec(); } @Override public List query(String tableName, Map conditions) { - if (!isExist(tableName)) { - throw new TableNotFoundException(tableName); - } + checkTableExists(tableName); List records = tableMap.get(tableName).getRecords(); if (conditions == null || conditions.isEmpty()) { return records; @@ -144,9 +139,7 @@ public List query(String tableName, Map conditions) { @Override public boolean drop(String tableName) { - if (!isExist(tableName)) { - throw new TableNotFoundException(tableName); - } + checkTableExists(tableName); tableMap.remove(tableName); return true; } @@ -172,7 +165,7 @@ public void clear() { * @param records records * @return boolean */ - private void checkFormatOfRecords(TableEntry store, List records) { + private void checkRecordsFormat(TableEntry store, List records) { if (records.isEmpty()) { throw new IncorrectRecordFormatException(store.getTableSpec().getName()); } @@ -201,7 +194,7 @@ private void checkFormatOfRecords(TableEntry store, List records) { * @param tableSpec tableSpec * @return boolean */ - private void checkFormatefTableSpecification(TableSpecification tableSpec) { + private void checkTableSpecificationFormat(TableSpecification tableSpec) { if (tableSpec.getName() == null) { throw new IncorrectTableFormatException(null); } @@ -216,6 +209,15 @@ private void checkFormatefTableSpecification(TableSpecification tableSpec) { } } + /** + * Check if the table exists,if not exist it will throw a TableNotFoundException + */ + private void checkTableExists(String tableName) { + if (!isExist(tableName)) { + throw new TableNotFoundException(tableName); + } + } + /** * Determine whether the table has been created * diff --git a/core/src/main/java/org/dst/core/operatorset/DstTable.java b/core/src/main/java/org/dst/core/operatorset/DstTable.java index 24212e6e2..becddce20 100644 --- a/core/src/main/java/org/dst/core/operatorset/DstTable.java +++ b/core/src/main/java/org/dst/core/operatorset/DstTable.java @@ -20,44 +20,46 @@ public interface DstTable { /** * Append a list of records to a table. * - * @param tableName tableName - * @param records The records that will be append to the table. + * @param tableName The table's name to which the records will be appended. + * @param records The records that will be append to the table. */ void append(String tableName, List records); /** - * Find a table specification by tableName + * Get the table specification of the given `tableName`. * - * @param tableName tableName + * @param tableName The table's name , which need to be get the specification. */ - TableSpecification findTableSpecification(String tableName); + TableSpecification getTableSpecification(String tableName); /** - * Query target Records by tableName and query conditions + * Query target Records by the given `tableName` and `conditions`. * - * @param tableName tableName - * @param conditions query conditions + * @param tableName The table's name , which need to be queried. + * @param conditions Typically, the query condition is the value of + * the corresponding field of the query table, + * the conditions can be null */ List query(String tableName, Map conditions); /** - * Drop a table from store by tableName + * Drop a table from store by the given `tableName`. * - * @param tableName tableName - * @return drop result : true or false + * @param tableName The table's name which will be drop. + * @return True if we succeeded to drop the table, otherwise is false. */ boolean drop(String tableName); /** - * Clear a table by tableName + * Clear all the records of the table. * - * @param tableName tableName + * @param tableName The table's name to which will be clean. */ void clearTable(String tableName); /** - * Clear the whole table store + * Clear the whole table store. */ void clear(); diff --git a/test/src/test/java/org/dst/test/core/operator/KVSTableTest.java b/test/src/test/java/org/dst/test/core/operator/KVSTableTest.java index 4e40d04ed..8f62edf1e 100644 --- a/test/src/test/java/org/dst/test/core/operator/KVSTableTest.java +++ b/test/src/test/java/org/dst/test/core/operator/KVSTableTest.java @@ -1,4 +1,4 @@ -package test.org.dst.core.operator; +package org.dst.test.core.operator; import com.google.common.collect.ImmutableList; import org.dst.core.KVStore; @@ -87,7 +87,7 @@ private static List dummyTableEntryData() { @Test(priority = 1) public void testFindTableSpecification() { dummyCreateTable(); - TableSpecification testTable = store.tables().findTableSpecification(TEST_TABLE); + TableSpecification testTable = store.tables().getTableSpecification(TEST_TABLE); Assert.assertEquals(testTable.getName(), TEST_TABLE); } @@ -119,7 +119,7 @@ public void testQueryByConditions() { @Test(priority = 4,expectedExceptions = TableNotFoundException.class) public void testClear() { store.tables().clear(); - store.tables().findTableSpecification(TEST_TABLE); + store.tables().getTableSpecification(TEST_TABLE); } @Test(priority = 5) @@ -136,6 +136,6 @@ public void testDrop() { dummyCreateTable(); store.tables().append(TEST_TABLE, dummyTableEntryData()); store.tables().drop(TEST_TABLE); - store.tables().findTableSpecification(TEST_TABLE); + store.tables().getTableSpecification(TEST_TABLE); } } From 492e9afb91b72737be580e4ad5c97ebb53556db9 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 22 Sep 2019 10:07:58 +0800 Subject: [PATCH 19/22] enchance core-table code --- .../org/dst/core/operatorImpl/DstTableImpl.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 17fb8adf6..635555103 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,5 +1,6 @@ package org.dst.core.operatorImpl; +import org.dst.common.exception.*; import org.dst.core.operatorset.DstTable; import org.dst.core.table.TableEntry; import org.dst.core.table.TableSpecification; @@ -8,11 +9,6 @@ import org.dst.core.table.Field; import org.dst.core.table.Index; import org.dst.core.table.ValueType; -import org.dst.common.exception.IncorrectRecordFormatException; -import org.dst.common.exception.IncorrectTableFormatException; -import org.dst.common.exception.TableAlreadyExistsException; -import org.dst.common.exception.TableNotFoundException; - import java.util.Map; import java.util.HashMap; import java.util.List; @@ -160,7 +156,7 @@ public void clear() { * check the records's format * 1. field locations must correspond one to one * 2. records can't be empty - * 3. primary must unique TODO (senyer) + * 3. primary must unique * * @param records records * @return boolean @@ -170,6 +166,7 @@ private void checkRecordsFormat(TableEntry store, List records) { throw new IncorrectRecordFormatException(store.getTableSpec().getName()); } TableSpecification tableSpec = store.getTableSpec(); + Map> indexs = store.getIndex().getIndexs(); List fields = tableSpec.getFields(); for (int i = 0; i < fields.size(); i++) { ValueType fieldType = fields.get(i).getType(); @@ -181,6 +178,12 @@ private void checkRecordsFormat(TableEntry store, List records) { throw new IncorrectRecordFormatException(store.getTableSpec().getName()); } } + //primary must unique + if (fields.get(i).isPrimary()) { + if (indexs.containsKey(value)) { + throw new DuplicatedPrimaryKeyException(fields.get(i).getName()); + } + } } } } From 5e91161cf9c1beb4aa5ba6a8acdb58f0bec820c6 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Sun, 22 Sep 2019 10:28:35 +0800 Subject: [PATCH 20/22] enchance core-table code --- .../main/java/org/dst/core/operatorImpl/DstTableImpl.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index 635555103..ed9b2a8fa 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -1,6 +1,10 @@ package org.dst.core.operatorImpl; -import org.dst.common.exception.*; +import org.dst.common.exception.TableNotFoundException; +import org.dst.common.exception.DuplicatedPrimaryKeyException; +import org.dst.common.exception.IncorrectRecordFormatException; +import org.dst.common.exception.IncorrectTableFormatException; +import org.dst.common.exception.TableAlreadyExistsException; import org.dst.core.operatorset.DstTable; import org.dst.core.table.TableEntry; import org.dst.core.table.TableSpecification; From 5e8aaa10c75c75a23610bb2823f18451f2b5ccf8 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Mon, 23 Sep 2019 21:30:00 +0800 Subject: [PATCH 21/22] replace hashmap to concurrentHashMap --- .../main/java/org/dst/core/operatorImpl/DstTableImpl.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index ed9b2a8fa..b3559cf95 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -14,17 +14,17 @@ import org.dst.core.table.Index; import org.dst.core.table.ValueType; import java.util.Map; -import java.util.HashMap; import java.util.List; import java.util.Arrays; import java.util.ArrayList; +import java.util.concurrent.ConcurrentHashMap; public class DstTableImpl implements DstTable { - private HashMap tableMap; + private ConcurrentHashMap tableMap; public DstTableImpl() { - this.tableMap = new HashMap(); + this.tableMap = new ConcurrentHashMap(); } @Override From 1c880063b1213289cfa7a8ece060c2396fc3eb00 Mon Sep 17 00:00:00 2001 From: senyer <576085903@qq.com> Date: Fri, 27 Sep 2019 00:11:05 +0800 Subject: [PATCH 22/22] fix code --- .../dst/core/operatorImpl/DstTableImpl.java | 23 ++++++++++++------- .../main/java/org/dst/core/table/Index.java | 7 ++++++ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java index b3559cf95..f41d51832 100644 --- a/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java +++ b/core/src/main/java/org/dst/core/operatorImpl/DstTableImpl.java @@ -30,9 +30,7 @@ public DstTableImpl() { @Override public void createTable(TableSpecification tableSpec) { checkTableSpecificationFormat(tableSpec); - if (isExist(tableSpec.getName())) { - throw new TableAlreadyExistsException(tableSpec.getName()); - } + checkTableAlreadyExist(tableSpec.getName()); TableEntry table = new TableEntry.Builder().tableSpec(tableSpec).builder(); tableMap.put(tableSpec.getName(), table); } @@ -57,19 +55,19 @@ public void append(String tableName, List sourceRecords) { List fields = tableSpec.getFields(); int fieldsSize = fields.size(); for (int i = 0; i < fieldsSize; i++) { - boolean primary = fields.get(i).isPrimary(); - boolean index = fields.get(i).isIndex(); - if (primary || index) { + final boolean isPrimary = fields.get(i).isPrimary(); + final boolean isIndex = fields.get(i).isIndex(); + if (isPrimary || isIndex) { int newRecordSize = records.size(); Map> indexs = tableEntry.getIndex().getIndexs(); for (int j = 0; j < newRecordSize; j++) { Value value = records.get(j).getRecord().get(i); position += 1; - if (primary) { + if (isPrimary) { //TODO (senyer) how to enhance this code :Arrays.asList() ? indexs.put(value, Arrays.asList(position)); } - if (index) { + if (isIndex) { if (indexs.containsKey(value)) { List positions = indexs.get(value); positions.add(position); @@ -225,6 +223,15 @@ private void checkTableExists(String tableName) { } } + /** + * Check if the table exists,if exist it will throw a TableAlreadyExistsException + */ + private void checkTableAlreadyExist(String tableName) { + if (isExist(tableName)) { + throw new TableAlreadyExistsException(tableName); + } + } + /** * Determine whether the table has been created * diff --git a/core/src/main/java/org/dst/core/table/Index.java b/core/src/main/java/org/dst/core/table/Index.java index 9d70f0d25..4c580849c 100644 --- a/core/src/main/java/org/dst/core/table/Index.java +++ b/core/src/main/java/org/dst/core/table/Index.java @@ -4,6 +4,13 @@ import java.util.List; import java.util.Map; +/** + * This class is mainly used to store the index of the corresponding field of the table, + * including the primary key and index index. The primary key index is uniquely restricted. + * The Map collection is used to store the primary key index information, + * and the value corresponding to the field is stored as a key, + * and the subscript of the record is stored as a set of values. + */ public class Index { private Map> indexs = new HashMap<>();