Permalink
Browse files

...

  • Loading branch information...
1 parent caff57f commit f2277f9eb60b14a7c290cfee353aebc51d65b70b @leonlee committed Dec 23, 2012
Showing with 259 additions and 36 deletions.
  1. +3 −2 modules/sys-auth/src/main/groovy/org/riderzen/ogs/auth/RegisterProcessor.groovy
  2. +1 −1 platform/common/src/main/groovy/org/riderzen/ogs/common/Address.groovy
  3. +1 −6 platform/common/src/main/groovy/org/riderzen/ogs/common/BaseModel.groovy
  4. +30 −0 platform/common/src/main/groovy/org/riderzen/ogs/common/DataStoreHelper.groovy
  5. +9 −0 platform/common/src/main/groovy/org/riderzen/ogs/common/DataStoreOperation.groovy
  6. +5 −0 platform/common/src/main/groovy/org/riderzen/ogs/common/Tools.groovy
  7. +4 −0 platform/data-store/build.gradle
  8. +4 −0 platform/data-store/gradle.properties
  9. +8 −0 platform/data-store/src/main/groovy/org/riderzen/ogs/ds/CacheHelper.groovy
  10. +17 −0 platform/data-store/src/main/groovy/org/riderzen/ogs/ds/CreateOperation.groovy
  11. +8 −3 .../groovy/org/riderzen/ogs/common → data-store/src/main/groovy/org/riderzen/ogs/ds}/DBHelper.groovy
  12. +11 −0 platform/data-store/src/main/groovy/org/riderzen/ogs/ds/DataPoolException.groovy
  13. +81 −0 platform/data-store/src/main/groovy/org/riderzen/ogs/ds/DataSourceHolder.groovy
  14. +24 −0 platform/data-store/src/main/resources/config.json
  15. +45 −0 platform/data-store/src/main/resources/data-store.groovy
  16. +3 −0 platform/data-store/src/main/resources/mod.json
  17. +0 −1 platform/gsql-persistor/gradle.properties
  18. +0 −15 platform/gsql-persistor/src/main/resources/gsql-persistor.groovy
  19. +0 −3 platform/gsql-persistor/src/main/resources/mod.json
  20. +4 −4 platform/protocol/src/main/resources/protocol-parser.groovy
  21. +1 −1 settings.gradle
@@ -1,8 +1,9 @@
package org.riderzen.ogs.auth
import org.riderzen.ogs.common.Address
+import DBHelper
+import org.riderzen.ogs.common.DataStoreOperation
import org.riderzen.ogs.common.EventHelper
-import org.riderzen.ogs.common.Tools
/**
* User: Leon Lee <mail.lgq@gmail.com>
@@ -15,7 +16,7 @@ class RegisterProcessor{
def user = User.populate(eh.param)
if (user) {
eh.newProcess(1)
- eh.eb.send(Address.platJdbc.val, user.toJson()) { message ->
+ eh.eb.send(Address.dataStore.val, [operation: DataStoreOperation.create, params: user.toJson()]) { message ->
eh.sendOK(user.toJson())
}
}
@@ -7,7 +7,7 @@ package org.riderzen.ogs.common
enum Address {
appProtocol("ogs.protocol"),
sysAuthRegister("ogs.mod.sys-auth.register"),
- platJdbc("ogs.plat.jdbc")
+ dataStore("ogs.plat.data-store")
public String val;
@@ -10,13 +10,11 @@ import net.sf.oval.constraint.NotNull
* Date: 12-12-7
*/
class BaseModel {
- @NotNull
Long id
@NotNull
Long createdOn
@NotNull
Long updatedOn
- @NotNull
Long vsn
static def transientAttributes = ['class', 'metaClass', 'transientAttributes', 'tableName']
@@ -41,10 +39,8 @@ class BaseModel {
def validate() {
if (!id) {
- id = DBHelper.nextId()
createdOn = Tools.currentTime()
updatedOn = createdOn
- vsn = 0
}
Validator validator = new Validator()
@@ -69,8 +65,7 @@ class BaseModel {
static GsonBuilder createBuilder() {
new GsonBuilder()
- .registerTypeAdapter()
- .serializeNulls()
+// .serializeNulls()
.excludeFieldsWithoutExposeAnnotation()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
}
@@ -0,0 +1,30 @@
+package org.riderzen.ogs.common
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-23
+ */
+class DataStoreHelper {
+ def static String generateInsertSql(model) {
+ StringBuilder buffer = new StringBuilder("insert into ")
+ buffer.append(model.tableName)
+ buffer.append(" (")
+ StringBuilder paramBuffer = new StringBuilder()
+ boolean first = true
+ for (String column : model.pAttributes().keySet()) {
+ if (first) {
+ first = false
+ paramBuffer.append("?")
+ } else {
+ buffer.append(", ")
+ paramBuffer.append(", ?")
+ }
+ buffer.append(column)
+ }
+ buffer.append(") values (")
+ buffer.append(paramBuffer.toString())
+ buffer.append(")")
+
+ buffer.toString()
+ }
+}
@@ -0,0 +1,9 @@
+package org.riderzen.ogs.common
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-23
+ */
+enum DataStoreOperation {
+ create, read, update, delete, count, readOne, createBatch, updateBatch, deleteBatch
+}
@@ -3,6 +3,7 @@ package org.riderzen.ogs.common
import hirondelle.date4j.DateTime
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.lang3.LocaleUtils
+import org.vertx.java.core.impl.Context
import org.vertx.java.core.logging.impl.LoggerFactory
/**
@@ -73,4 +74,8 @@ class Tools {
static def nextRid() {
UUID.randomUUID().toString();
}
+
+ static getLogger() {
+ Context.context.deploymentHandle.logger
+ }
}
@@ -0,0 +1,4 @@
+dependencies {
+ compile "com.jolbox:bonecp:${boneCPVsn}"
+ runtime "mysql:mysql-connector-java:${mysqlVsn}"
+}
@@ -0,0 +1,4 @@
+modulename=org.riderzen.ogs.platform.data-store
+
+boneCPVsn=0.8.0-rc1
+mysqlVsn=5.1.22
@@ -0,0 +1,8 @@
+package org.riderzen.ogs.ds
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-23
+ */
+class CacheHelper {
+}
@@ -0,0 +1,17 @@
+package org.riderzen.ogs.ds
+
+import groovy.sql.Sql
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-23
+ */
+class CreateOperation {
+ def params
+
+ def execute() {
+ if (params) {
+ DBHelper.save(params)
+ }
+ }
+}
@@ -1,6 +1,6 @@
-package org.riderzen.ogs.common
+package org.riderzen.ogs.ds
-import groovy.sql.Sql
+import org.riderzen.ogs.common.NoPropertyFoundException
import org.slf4j.LoggerFactory
/**
@@ -22,7 +22,12 @@ class DBHelper {
def sql = getShardSql(model)
def dataSet = sql.dateSet(tableName)
+ def id = nextId()
+ model.id = id as String
+ model.vsn = 0
dataSet.add(model.pAttributes())
+
+ id
}
static def batchSave(models) {
@@ -96,7 +101,7 @@ class DBHelper {
}
static def defaultSql() {
- return null;
+ return DataSourceHolder.defaultSql;
}
static long nextId() {
@@ -0,0 +1,11 @@
+package org.riderzen.ogs.ds
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-23
+ */
+class DataPoolException extends Exception {
+ DataPoolException(String s) {
+ super(s)
+ }
+}
@@ -0,0 +1,81 @@
+package org.riderzen.ogs.ds
+
+import com.jolbox.bonecp.BoneCP
+import com.jolbox.bonecp.BoneCPConfig
+import groovy.sql.Sql
+
+import java.util.concurrent.ConcurrentHashMap
+import java.util.concurrent.ConcurrentMap
+
+import static org.riderzen.ogs.common.Tools.logger
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-23
+ */
+class DataSourceHolder {
+ static DataSourceHolder instance;
+ static initialized = false
+ BoneCPConfig[] poolConfigs
+ BoneCP[] pools
+ ConcurrentMap connections
+ Range<Long>[] shardRanges
+
+ private DataSourceHolder(config) {
+ if (!config.shards) throw new DataPoolException("Invalid config ${config}")
+
+ logger.debug "initializing data source holder with config: $config"
+
+ poolConfigs = new BoneCPConfig[config.shards.length]
+ pools = new BoneCP[poolConfigs.length]
+ shardRanges = new Range<Long>[pools.length]
+
+ def i = 0
+ for (shard in config.shards) {
+ poolConfigs[i] = new BoneCPConfig(
+ jdbcUrl: shard.url,
+ username: shard.user,
+ password: shard.password,
+ partitionCount: shard.partitionCount,
+ maxConnectionsPerPartition: shard.maxConnectionsPerPartition,
+ minConnectionsPerPartition: shard.minConnectionsPerPartition,
+ acquireIncrement: shard.acquireIncrement
+ )
+
+ shardRanges[i] = (shard.range[0]..shard.range[1])
+ pools[i] = new BoneCP(poolConfigs[i])
+ i++
+ }
+ connections = new ConcurrentHashMap()
+
+ initialized = true
+ }
+
+ def static init(config) {
+ instance = new DataSourceHolder(config)
+ }
+
+ def static getInstance() {
+ if (!initialized) throw new DataPoolException("Non initialized.")
+ instance
+ }
+
+ def getDefaultSql() {
+ new Sql(pools[0].getConnection())
+ }
+
+ def getSql(id) {
+ if (!id) return getDefaultSql()
+
+ def sql = null
+ for (i in 0..shardRanges.length) {
+ if (shardRanges[i].contains(id)) {
+ sql = new Sql(pools[i].getConnection())
+ }
+ }
+
+ if (!sql) throw new DataPoolException("No shard found with id: ${id}")
+
+ return sql
+ }
+}
@@ -0,0 +1,24 @@
+{
+ "shards": [
+ {
+ "url": "jdbc:mysql://192.168.1.105:3306/ogs1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false",
+ "user": "root",
+ "password": "password",
+ "range": [0, 9999999],
+ "partitionCount": 4,
+ "maxConnectionsPerPartition": 50,
+ "minConnectionsPerPartition": 5,
+ "acquireIncrement": 10
+ },
+ {
+ "url": "jdbc:mysql://192.168.1.105:3306/ogs2?useUnicode=true&characterEncoding=utf8&autoReconnect=true&failOverReadOnly=false",
+ "user": "root",
+ "password": "password",
+ "range": [10000000, 19999999],
+ "partitionCount": 4,
+ "maxConnectionsPerPartition": 50,
+ "minConnectionsPerPartition": 5,
+ "acquireIncrement": 10
+ }
+ ]
+}
@@ -0,0 +1,45 @@
+import com.google.gson.Gson
+import org.msgpack.MessagePack
+import org.msgpack.packer.Packer
+import org.msgpack.unpacker.Unpacker
+import org.riderzen.ogs.common.Address
+import org.riderzen.ogs.common.DataStoreOperation
+import org.riderzen.ogs.ds.CreateOperation
+import org.riderzen.ogs.ds.DataSourceHolder
+import org.vertx.groovy.core.buffer.Buffer
+import org.vertx.groovy.core.eventbus.Message
+
+/**
+ * User: Leon Lee <mail.lgq@gmail.com>
+ * Date: 12-12-22
+ */
+def config = container.config
+def logger = container.logger
+def eb = vertx.eventBus
+
+logger.info "starting data-store..."
+config = config?: new Gson().fromJson(new File('config.json').text, Map.class)
+logger.info "config: $config"
+
+DataSourceHolder.init(config)
+
+eb.registerHandler(Address.dataStore.val) { Message message ->
+ DataStoreOperation operation = message.body.operation
+ String expression = message.body.expression
+ Map params = message.body.params
+
+ switch (operation) {
+ case DataStoreOperation.create:
+ def id = new CreateOperation(params: params).execute()
+ def result = id ? [error: false, id: id] : [error: "can't save"]
+ message.reply result
+ break;
+ case DataStoreOperation.read:
+ break;
+ case DataStoreOperation.update:
+ break;
+ case DataStoreOperation.delete:
+ break;
+ }
+
+}
@@ -0,0 +1,3 @@
+{
+ "main": "data-store.groovy"
+}
@@ -1 +0,0 @@
-modulename=org.riderzen.ogs.platform.gsql-persistor
@@ -1,15 +0,0 @@
-import com.google.gson.Gson
-import org.msgpack.MessagePack
-import org.msgpack.packer.Packer
-import org.msgpack.unpacker.Unpacker
-import org.vertx.groovy.core.buffer.Buffer
-
-/**
- * User: Leon Lee <mail.lgq@gmail.com>
- * Date: 12-12-22
- */
-def logger = container.logger
-def eb = vertx.eventBus
-def client = vertx.createNetClient()
-
-//eb.registerHandler(Address)
@@ -1,3 +0,0 @@
-{
- "main": "gsql-persistor.groovy"
-}
Oops, something went wrong.

0 comments on commit f2277f9

Please sign in to comment.