From 9e4e4b842dbfa952bfc22df8e5838b4c27c9f084 Mon Sep 17 00:00:00 2001 From: Bob Florian Date: Thu, 22 Mar 2012 18:10:10 -0400 Subject: [PATCH] Integration test fixes. --- CassandraOrmGrailsPlugin.groovy | 9 ++- .../cassandra/orm/CassandraOrmService.groovy | 13 ++-- .../cassandra/OrmPersistenceMethods.groovy | 60 ------------------- .../cassandra/mapping/DataMapping.groovy | 9 ++- .../OrmUtility.groovy} | 22 +++---- .../test/MockPersistenceMethods.groovy | 4 +- .../plugins/cassandra/test/orm/Car.groovy | 33 ---------- .../cassandra/test/orm/Conference.groovy | 35 ----------- .../plugins/cassandra/test/orm/Course.groovy | 33 ---------- .../plugins/cassandra/test/orm/Grade.groovy | 40 ------------- .../cassandra/test/orm/Household.groovy | 43 ------------- .../plugins/cassandra/test/orm/School.groovy | 35 ----------- .../plugins/cassandra/test/orm/Student.groovy | 39 ------------ .../cassandra/test/orm/Transcript.groovy | 38 ------------ 14 files changed, 34 insertions(+), 379 deletions(-) delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/OrmPersistenceMethods.groovy rename src/groovy/com/reachlocal/grails/plugins/cassandra/{test/orm/Person.groovy => mapping/OrmUtility.groovy} (67%) delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Car.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Conference.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Course.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Grade.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Household.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/School.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Student.groovy delete mode 100644 src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Transcript.groovy diff --git a/CassandraOrmGrailsPlugin.groovy b/CassandraOrmGrailsPlugin.groovy index eb32def..26995c1 100644 --- a/CassandraOrmGrailsPlugin.groovy +++ b/CassandraOrmGrailsPlugin.groovy @@ -1,3 +1,5 @@ +import com.reachlocal.grails.plugins.cassandra.mapping.OrmUtility + /* * Copyright 2012 ReachLocal Inc. * @@ -47,7 +49,12 @@ Brief description of the plugin. } def doWithDynamicMethods = { ctx -> - // TODO Implement registering dynamic methods to classes (optional) + application.allClasses.each {clazz -> + if (OrmUtility.isMappedClass(clazz)) { + log.info "Mapping ${clazz.name} for Casssandra ORM" + OrmUtility.addDynamicMethods(clazz, ctx) + } + } } def doWithApplicationContext = { applicationContext -> diff --git a/grails-app/services/com/reachlocal/grails/plugins/cassandra/orm/CassandraOrmService.groovy b/grails-app/services/com/reachlocal/grails/plugins/cassandra/orm/CassandraOrmService.groovy index da207e0..a210845 100644 --- a/grails-app/services/com/reachlocal/grails/plugins/cassandra/orm/CassandraOrmService.groovy +++ b/grails-app/services/com/reachlocal/grails/plugins/cassandra/orm/CassandraOrmService.groovy @@ -19,22 +19,25 @@ package com.reachlocal.grails.plugins.cassandra.orm import org.springframework.beans.factory.InitializingBean import org.codehaus.groovy.grails.commons.ConfigurationHolder import com.reachlocal.grails.plugins.cassandra.mapping.DataMapping +import org.springframework.context.ApplicationContextAware +import org.springframework.context.ApplicationContext /** * @author: Bob Florian */ -class CassandraOrmService implements InitializingBean +class CassandraOrmService implements InitializingBean, ApplicationContextAware { boolean transactional = false - def applicationContext + + ApplicationContext applicationContext def ormClientServiceName = ConfigurationHolder.config?.cassandra?.ormClientServiceName ?: "astyanaxService" - + def client def persistence def mapping - - void afterPropertiesSet () + + void afterPropertiesSet () { client = applicationContext.getBean(ormClientServiceName) persistence = client.orm diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/OrmPersistenceMethods.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/OrmPersistenceMethods.groovy deleted file mode 100644 index 0c4620d..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/OrmPersistenceMethods.groovy +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra - -/** - * @author: Bob Florian - */ -interface OrmPersistenceMethods -{ - def columnFamily(String name); - - def getRow(Object client, Object columnFamily, Object rowKey); - - def getRows(Object client, Object columnFamily, Collection rowKeys); - - def getRowsColumnSlice(Object client, Object columnFamily, Collection rowKeys, Collection columnNames); - - def getRowsWithEqualityIndex(client, columnFamily, properties, max); - - def getColumnRange(Object client, Object columnFamily, Object rowKey, Object start, Object finish, Boolean reversed, Integer max); - - def getColumnSlice(Object client, Object columnFamily, Object rowKey, Collection columnNames); - - def getColumn(Object client, Object columnFamily, Object rowKey, Object columnName); - - def prepareMutationBatch(client); - - void deleteColumn(mutationBatch, columnFamily, rowKey, columnName); - - void putColumns(mutationBatch, columnFamily, rowKey, columnMap); - - void deleteRow(mutationBatch, columnFamily, rowKey); - - def execute(mutationBatch); - - def getRow(rows, key); - - def getColumn(row, name); - - def name(column); - - String stringValue(column); - - byte[] byteArrayValue(column); -} - diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/DataMapping.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/DataMapping.groovy index 6ad1d5d..21eec54 100644 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/DataMapping.groovy +++ b/src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/DataMapping.groovy @@ -18,7 +18,6 @@ package com.reachlocal.grails.plugins.cassandra.mapping import grails.converters.JSON import java.nio.ByteBuffer -import com.reachlocal.grails.plugins.cassandra.OrmPersistenceMethods /** * @author: Bob Florian @@ -28,8 +27,9 @@ class DataMapping static final CLASS_NAME_KEY = '_class_name_' static final KEY_SUFFIX = InstanceMethods.KEY_SUFFIX static final DIRTY_SUFFIX = InstanceMethods.DIRTY_SUFFIX + static final GLOBAL_TRANSIENTS = ["class","id","cassandra","indexColumnFamily","columnFamily","metaClass","keySpace"] as Set - OrmPersistenceMethods persistence + def persistence def dataProperties(data) { @@ -48,13 +48,16 @@ class DataMapping map[CLASS_NAME_KEY] = clazz.getName() data.metaClass.properties.each() { if (!it.name.endsWith(DIRTY_SUFFIX)) { + def prop = data.getProperty(it.name) if (prop != null && it.getter && !it.getter.isStatic() && !transients.contains(it.name) && + !GLOBAL_TRANSIENTS.contains(it.name) && !hasMany[it.name]) { + println it.name if (MappingUtils.isMappedClass(prop.class)) { map["${it.name}${KEY_SUFFIX}"] = prop.id } @@ -124,7 +127,7 @@ class DataMapping def newObject(cols) { def obj = null - if (cols.size()) { + if (cols) { def className = persistence.stringValue(persistence.getColumn(cols, CLASS_NAME_KEY)) def asClass = Class.forName(className, false, DataMapping.class.classLoader) obj = asClass.newInstance() diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Person.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/OrmUtility.groovy similarity index 67% rename from src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Person.groovy rename to src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/OrmUtility.groovy index 5c8ebbd..50f123f 100644 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Person.groovy +++ b/src/groovy/com/reachlocal/grails/plugins/cassandra/mapping/OrmUtility.groovy @@ -14,21 +14,21 @@ * limitations under the License. */ -package com.reachlocal.grails.plugins.cassandra.test.orm +package com.reachlocal.grails.plugins.cassandra.mapping /** * @author: Bob Florian */ -class Person +class OrmUtility { - String emailAddress - String firstName - String lastName - Car firstCar - Car secondCar + static void addDynamicMethods(clazz, ctx) + { + ClassMethods.addDynamicOrmMethods(clazz, ctx) + InstanceMethods.addDynamicOrmMethods(clazz, ctx) + } - static cassandraMapping = [ - primaryKey: 'emailAddress', - explicitIndexes: ["firstCar","secondCar"] - ] + static boolean isMappedClass(clazz) + { + MappingUtils.isMappedClass(clazz) + } } diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/MockPersistenceMethods.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/MockPersistenceMethods.groovy index 7309b9e..b868bf5 100644 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/MockPersistenceMethods.groovy +++ b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/MockPersistenceMethods.groovy @@ -16,12 +16,10 @@ package com.reachlocal.grails.plugins.cassandra.test -import com.reachlocal.grails.plugins.cassandra.OrmPersistenceMethods - /** * @author: Bob Florian */ -class MockPersistenceMethods implements OrmPersistenceMethods +class MockPersistenceMethods { def CLASSES = [ MockUser_CFO: 'com.reachlocal.grails.plugins.cassandra.test.orm.User', diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Car.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Car.groovy deleted file mode 100644 index c748389..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Car.groovy +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author: Bob Florian - */ -class Car -{ - String uuid - String make - String model - String color - Integer year - - static cassandraMapping = [ - primaryKey: 'uuid' - ] -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Conference.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Conference.groovy deleted file mode 100644 index af54525..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Conference.groovy +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author: Bob Florian - */ -class Conference -{ - String uuid = UUID.timeUUID().toString() - Date date - String title - - static hasMany = [households : Household, schools:School] - List households - List schools - - static cassandraMapping = [ - primaryKey : ["uuid"] - ] -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Course.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Course.groovy deleted file mode 100644 index ca1d42f..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Course.groovy +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author Bob Florian - */ -class Course -{ - String title - String description - - List students - static hasMany = [students: Student] - - static cassandraMapping =[ - primaryKey: ['title'] - ] -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Grade.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Grade.groovy deleted file mode 100644 index 99018bb..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Grade.groovy +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author: Bob Florian - */ -class Grade -{ - Date date - Float pointValue - Transcript transcript - Course course - static belongsTo = [transcript: Transcript, course: Course] - - static transients = ["timestamp"] - - static cassandraMapping = [ - primaryKey : ["transcript","timestamp","course"] - ] - - Long getTimestamp() - { - return date.time - } -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Household.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Household.groovy deleted file mode 100644 index 429499c..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Household.groovy +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author Bob Florian - */ -class Household -{ - String emailAddress - String address1 - String address2 - String city - String state - String zip - String phone - Integer age - List occupations - List students - Boolean ptaMember - - static hasMany = [students: Student] - - static cassandraMapping = [ - primaryKey: 'emailAddress', - secondaryIndexes: ['city','state','zip'], - explicitIndexes: ['phone'] - ] -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/School.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/School.groovy deleted file mode 100644 index c9e77e0..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/School.groovy +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author: Bob Florian - */ -class School -{ - String name - Integer minGrade - Integer maxGrade - - static hasMany =[transcripts: Transcript, conferences: Conference] - List transcripts - List conferences - - static cassandraMapping =[ - primaryIndex: ['name'] - ] -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Student.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Student.groovy deleted file mode 100644 index f33fe27..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Student.groovy +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author Bob Florian - */ -class Student -{ - String uuid = UUID.timeUUID().toString() - String firstName - String lastName - Integer age - - static belongsTo = [household: Household] - Household household - - static hasMany = [courses: Course] - List courses - - static cassandraMapping =[ - primaryKey: 'uuid', - secondaryIndexes: ['age'] - ] -} diff --git a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Transcript.groovy b/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Transcript.groovy deleted file mode 100644 index b65c6ff..0000000 --- a/src/groovy/com/reachlocal/grails/plugins/cassandra/test/orm/Transcript.groovy +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2012 ReachLocal Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.reachlocal.grails.plugins.cassandra.test.orm - -/** - * @author: Bob Florian - */ -class Transcript -{ - String uuid = UUID.randomUUID().toString() - Student student - TranscriptStatus status = TranscriptStatus.CURRENT - School school - static belongsTo = [school:School, student:Student] - - List grades - static hasMany = [grades: Grade] - - static cassandraMapping = [ - primaryKey : "uuid" - ] -} - -enum TranscriptStatus {CURRENT, CLOSED} \ No newline at end of file