Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Added kson builder / pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongHwan committed Sep 18, 2019
1 parent e583044 commit bfc6a81
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ private enum ValueMode {
private HashMap<Class<?>, Field[]> cachedFields;

public KsonContext() {
this.valueStack = new FastStack<Object>();
this.modeStack = new FastStack<ValueMode>();
this.stringMaker = new StringMaker(100);
this(10, 100);
}

public KsonContext(int stackSize, int stringBufferSize) {
this.valueStack = new FastStack<Object>(stackSize);
this.modeStack = new FastStack<ValueMode>(stackSize);
this.stringMaker = new StringMaker(stringBufferSize);

this.objectStack = new FastStack<Object>();
this.ksonStack = new FastStack<KsonValue>();
this.objectStack = new FastStack<Object>(stackSize);
this.ksonStack = new FastStack<KsonValue>(stackSize);

this.registeredTransformers = new HashMap<Class<? extends Object>, Transformer<? extends Object>>();

Expand All @@ -62,6 +66,7 @@ public KsonContext() {
this.primaryObjects = new HashMap<Class<?>, HashMap<Object, Object>>();

this.cachedFields = new HashMap<Class<?>, Field[]>();


this.registeredTransformers.put(Date.class, new Transformer<Date>() {
@Override
Expand Down Expand Up @@ -145,6 +150,7 @@ public Object serialize(KsonContext ksonContext, HashMap<?, ?> value) {
return hashMap;
}
});

}

private Field[] getAccessibleFields(Class<?> clazz) {
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/realtimetech/kson/builder/KsonBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.realtimetech.kson.builder;

import java.util.HashMap;

import com.realtimetech.kson.KsonContext;
import com.realtimetech.kson.transform.Transformer;

public class KsonBuilder {
private HashMap<Class<?>, Transformer<?>> registeredTransformers;

private int stackSize;
private int stringBufferSize;

public KsonBuilder() {
this.registeredTransformers = new HashMap<Class<? extends Object>, Transformer<? extends Object>>();
this.stackSize = 10;
this.stringBufferSize = 100;
}

public KsonBuilder registerTransformer(Class<?> clazz, Transformer<?> preTransformer) {
this.registeredTransformers.put(clazz, preTransformer);

return this;
}

public int getStackSize() {
return stackSize;
}

public KsonBuilder setStackSize(int stackSize) {
this.stackSize = stackSize;

return this;
}

public int getStringBufferSize() {
return stringBufferSize;
}

public KsonBuilder setStringBufferSize(int stringBufferSize) {
this.stringBufferSize = stringBufferSize;

return this;
}

public KsonContext build() {
KsonContext ksonContext = new KsonContext(this.stackSize, this.stringBufferSize);

for (Class<?> clazz : this.registeredTransformers.keySet()) {
ksonContext.registerTransformer(clazz, this.registeredTransformers.get(clazz));
}

return ksonContext;
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/realtimetech/kson/pool/KsonPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.realtimetech.kson.pool;

import java.util.HashMap;

import com.realtimetech.kson.KsonContext;
import com.realtimetech.kson.builder.KsonBuilder;

public class KsonPool {
private HashMap<Thread, KsonContext> contextPools;

private KsonBuilder ksonBuilder;

public KsonPool(KsonBuilder ksonBuilder) {
this.contextPools = new HashMap<Thread, KsonContext>();

this.ksonBuilder = ksonBuilder;
}

public synchronized KsonContext get() {
Thread currentThread = Thread.currentThread();

if (!this.contextPools.containsKey(currentThread)) {
KsonContext freeContext = null;

for (Thread thread : this.contextPools.keySet()) {
if (!thread.isAlive()) {
freeContext = this.contextPools.get(thread);
this.contextPools.remove(thread);
break;
}
}

if (freeContext == null) {
freeContext = ksonBuilder.build();
}

this.contextPools.put(currentThread, freeContext);
}

return this.contextPools.get(currentThread);
}
}
39 changes: 38 additions & 1 deletion src/main/java/com/realtimetech/kson/test/MainTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,49 @@
package com.realtimetech.kson.test;

import java.io.IOException;

import com.realtimetech.kson.KsonContext;
import com.realtimetech.kson.builder.KsonBuilder;
import com.realtimetech.kson.element.KsonObject;
import com.realtimetech.kson.element.KsonValue;
import com.realtimetech.kson.pool.KsonPool;

public class MainTest {
@SuppressWarnings("unused")
public static void main(String[] args) throws Exception {
KsonContext ksonContext = new KsonContext();
KsonBuilder ksonBuilder = new KsonBuilder();
final KsonPool ksonPool = new KsonPool(ksonBuilder);

System.out.println("## Thread Starting");
Thread[] threads = new Thread[5];
for (int i = 0; i < 5; i++) {
threads[i] = new Thread(new Runnable() {
@Override
public void run() {
TestObject testObject = new TestObject(new Test(1234, "Yallo"));
KsonContext ksonContext = ksonPool.get();

System.out.println(" Context Start: " + ksonContext);
for (int i = 0; i < 100000; i++) {
try {
KsonValue fromObject = ksonContext.fromObject(testObject);
} catch (IllegalArgumentException | IllegalAccessException | IOException e) {
e.printStackTrace();
}
}
System.out.println(" Context Done: " + ksonContext);
}
});

threads[i].start();
Thread.sleep(1000);
}

for (int i = 0; i < 5; i++) {
threads[i].join();
}

KsonContext ksonContext = ksonBuilder.build();

{
String fieldTest;
Expand Down

0 comments on commit bfc6a81

Please sign in to comment.