Skip to content

Commit

Permalink
Many random changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sanketsarang committed Dec 1, 2016
1 parent 16b13c0 commit ea25cae
Show file tree
Hide file tree
Showing 102 changed files with 1,053 additions and 19,093 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
out
*.class
target
801 changes: 619 additions & 182 deletions .idea/workspace.xml

Large diffs are not rendered by default.

147 changes: 129 additions & 18 deletions src/main/java/com/blobcity/db/Db.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@
import com.blobcity.db.exceptions.InternalDbException;
import com.blobcity.db.search.Query;
import com.blobcity.db.search.StringUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
Expand All @@ -45,6 +41,7 @@ public abstract class Db {

private String collection = null;
private String ds = null;
private String _id = null; //primary key value holder, in case the sub class does not have this property

public Db() {
for (Annotation annotation : this.getClass().getAnnotations()) {
Expand All @@ -57,10 +54,6 @@ public Db() {
}
else{
ds = blobCityEntity.ds();
String dbName = Credentials.getInstance().getDb();
if(dbName.equals("dummy")) {
Credentials.getInstance().setDb(ds);
}
}

if (StringUtil.isEmpty(collection)) {
Expand Down Expand Up @@ -334,23 +327,136 @@ public static <T extends Db> Object execute(final Credentials credentials, final
throw new DbOperationException(response.getErrorCode(), response.getErrorCause());
}

public static void insertJsonData(final String collection, final JsonObject insertJson){
insertJsonData(Credentials.getInstance(), collection, insertJson);
public static void insertJson(final String collection, final JsonObject insertJson){
insertJson(Credentials.getInstance(), collection, Arrays.asList(new JsonObject[]{insertJson}));
}

public static void insertJson(final Credentials credentials, final String collection, final JsonObject insertJson){
insertJson(credentials, collection, Arrays.asList(new JsonObject[]{insertJson}));
}

public static void insertJson(final String collection, final List<JsonObject> jsonList) {
insertJson(Credentials.getInstance(), collection, jsonList);
}

public static void insertJson(final Credentials credentials, final String collection, final List<JsonObject> jsonList) {
if(collection == null || collection.isEmpty()) {
throw new InternalAdapterException("collection name must be specified");
}

if(jsonList == null || jsonList.isEmpty()) {
throw new InternalAdapterException("At-least one json required for a json insert operation");
}

JsonObject payloadJsonObject = new JsonObject();
JsonArray dataArray = new JsonArray();
for(JsonObject element : jsonList) {
dataArray.add(element);
}
payloadJsonObject.add(QueryConstants.DATA, dataArray);
payloadJsonObject.addProperty(QueryConstants.TYPE, "json");

final DbQueryResponse response = postStaticRequest(credentials, QueryType.INSERT, collection, payloadJsonObject);
reportIfError(response);
}

public static void insertCsv(final String collection, final String csvString) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertCsv(final Credentials credentials, final String collection, final String csvString) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertCsv(final String collection, final List<String> csvList) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertCsv(final Credentials credentials, final String collection, final List<String> csvList) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertXml(final String collection, final String xmlString) {
insertXml(Credentials.getInstance(), collection, Arrays.asList(new String[]{xmlString}));
}

public static void insertXml(final Credentials credentials, final String collection, final String xmlString) {
insertXml(credentials, collection, Arrays.asList(new String[]{xmlString}));
}

public static void insertJsonData(final Credentials credentials, final String collection, final JsonObject insertJson){
public static void insertXml(final String collection, final List<String> xmlList) {
insertXml(Credentials.getInstance(), collection, xmlList);
}

public static void insertXml(final Credentials credentials, final String collection, final List<String> xmlList) {
if(collection == null || collection.isEmpty()) {
throw new InternalAdapterException("collection name must be specified");
}

if(insertJson == null) {
throw new InternalAdapterException("json data to insert cannot be null");
if(xmlList == null || xmlList.isEmpty()) {
throw new InternalAdapterException("At-least one xml required for a xml insert operation");
}

JsonObject payloadJsonObject = new JsonObject();
JsonArray dataArray = new JsonArray();
for(String element : xmlList) {
dataArray.add(new JsonPrimitive(element));
}
payloadJsonObject.add(QueryConstants.DATA, dataArray);
payloadJsonObject.addProperty(QueryConstants.TYPE, "xml");

final DbQueryResponse response = postStaticRequest(credentials, QueryType.INSERT, collection, insertJson);
final DbQueryResponse response = postStaticRequest(credentials, QueryType.INSERT, collection, payloadJsonObject);
reportIfError(response);
}

public static void insertSql(final String collection, final String sqlString) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertSql(final Credentials credentials, final String collection, final String sqlString) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertSql(final String collection, final List<String> sqlList) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertSql(final Credentials credentials, final String collection, final List<String> sqlList) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertText(final String collection, final String text) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertText(final Credentials credentials, final String collection, final String text) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertText(final String collection, final List<String> text) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insertText(final Credentials credentials, final String collection, final List<String> text) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insert(final String collection, final String insertString) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insert(final Credentials credentials, final String collection, final String insertString) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insert(final String collection, final List<String> insertList) {
throw new UnsupportedOperationException("Not supported yet.");
}

public static void insert(final Credentials credentials, final List<String> insertList) {
throw new UnsupportedOperationException("Not supported yet.");
}

//TODO: Add support for inserting other data formats

/**
Expand Down Expand Up @@ -1193,7 +1299,12 @@ private DbQueryResponse postRequest(final Credentials credentials, QueryType que
break;
case INSERT:
case SAVE:
queryJson.add(QueryConstants.PAYLOAD, toJson());
JsonObject payloadJson = new JsonObject();
payloadJson.addProperty(QueryConstants.TYPE,"json");
JsonArray jsonArray = new JsonArray();
jsonArray.add(toJson());
payloadJson.add(QueryConstants.DATA, jsonArray);
queryJson.add(QueryConstants.PAYLOAD, payloadJson);
break;
default:
throw new InternalDbException("Attempting to executed unknown or unidentifed query");
Expand Down Expand Up @@ -1315,8 +1426,8 @@ public void save(final Credentials credentials) {
public boolean insert(final Credentials credentials) {
final DbQueryResponse response = postRequest(credentials, QueryType.INSERT);
if (response.isSuccessful()) {
final JsonElement payloadJson = response.getPayload();
fromJson(payloadJson.getAsJsonObject());
// final JsonElement payloadJson = response.getPayload();
// fromJson(payloadJson.getAsJsonObject());
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/blobcity/db/DbQueryResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Karun AB
*/
class DbQueryResponse {
class DbQueryResponse {

// Ack code
private final int ackCode;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/blobcity/db/QueryConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class QueryConstants {
public static final String PRIMARY_KEY = "pk";
public static final String USER = "username";
public static final String PASS = "password";
public static final String DATA = "data";
public static final String TYPE = "type";

/* Response data keys */
public static final String PAYLOAD = "p";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.blobcity.db.test.integration.data;

import com.blobcity.db.Db;
import com.blobcity.db.annotations.Entity;
import com.blobcity.db.config.Credentials;
import com.blobcity.db.enums.CollectionType;
import com.google.gson.JsonObject;
import org.junit.*;

import java.util.Arrays;

/**
* Created by sanketsarang on 18/11/16.
*/
public class EntityDataInsertIT {

@BeforeClass
public static void setUpClass() {
Credentials.init("localhost:10111","root","root","test");
Assert.assertTrue("Cannot perform data insert tests as datastore creation failed", Db.createDs("test"));
Assert.assertTrue("Cannot perform data insert tests as on-disk collection creation failed", Db.createCollection("disk", CollectionType.ON_DISK));
Assert.assertTrue("Cannot perform data insert tests as in-memory collection creation failed", Db.createCollection("mem", CollectionType.IN_MEMORY));
Assert.assertTrue("Cannot perform data insert tests as in-memory-non-durable collection creation failed", Db.createCollection("memnd", CollectionType.IN_MEMORY_NON_DURABLE));
}

@AfterClass
public static void tearDownClass() {
Assert.assertTrue("Failed to drop datastore after test completion. Other tests may fail.", Db.dropDs("test"));
Credentials.unInit();
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

@Test
public void insertEntityOnDisk() {
System.out.println("IT: insertEntityOnDisk");

Assert.assertTrue("Cannot perform data insert tests as on-disk collection creation failed", Db.createCollection("User", CollectionType.ON_DISK));
new User("name1","address1").insert();
}

@Test
public void insertJsonInMemory() {
System.out.println("IT: insertJsonInMemory - yet to be implemented");
}

@Test
public void insertJsonInMemoryND() {
System.out.println("IT: insertJsonInMemoryND - yet to be implemented");
}
}

@Entity(ds = "test", collection = "User")
class User extends Db {
private String name;
private String address;

public User(final String name, final String address) {
this.name = name;
this.address = address;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.blobcity.db.test.integration.data;

import com.blobcity.db.Db;
import com.blobcity.db.config.Credentials;
import com.blobcity.db.enums.CollectionType;
import com.google.gson.JsonObject;
import org.junit.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* Created by sanketsarang on 18/11/16.
*/
public class JSONDataInsertIT {

@BeforeClass
public static void setUpClass() {
Credentials.init("localhost:10111","root","root","test");
Assert.assertTrue("Cannot perform data insert tests as datastore creation failed", Db.createDs("test"));
Assert.assertTrue("Cannot perform data insert tests as on-disk collection creation failed", Db.createCollection("disk", CollectionType.ON_DISK));
Assert.assertTrue("Cannot perform data insert tests as in-memory collection creation failed", Db.createCollection("mem", CollectionType.IN_MEMORY));
Assert.assertTrue("Cannot perform data insert tests as in-memory-non-durable collection creation failed", Db.createCollection("memnd", CollectionType.IN_MEMORY_NON_DURABLE));
}

@AfterClass
public static void tearDownClass() {
Assert.assertTrue("Failed to drop datastore after test completion. Other tests may fail.", Db.dropDs("test"));
Credentials.unInit();
}

@Before
public void setUp() {
}

@After
public void tearDown() {
}

@Test
public void insertOnDisk() {
System.out.println("IT: insertOnDisk");

/* Insert first record */
JsonObject jsonObject1 = new JsonObject();
jsonObject1.addProperty("name","name1");
jsonObject1.addProperty("age",45);
Db.insertJson("disk", jsonObject1);

/* Insert second record */
JsonObject jsonObject2 = new JsonObject();
jsonObject2.addProperty("name","name1");
jsonObject2.addProperty("age",45);
jsonObject2.addProperty("address","address 1");
Db.insertJson("disk", jsonObject2);

/* Insert multiple records */
Db.insertJson("disk", Arrays.asList(new JsonObject[]{jsonObject1, jsonObject2}));
}

@Test
public void insertJsonInMemory() {
System.out.println("IT: insertJsonInMemory - yet to be implemented");
}

@Test
public void insertJsonInMemoryND() {
System.out.println("IT: insertJsonInMemoryND - yet to be implemented");
}
}
Loading

0 comments on commit ea25cae

Please sign in to comment.