From: Stephane Gamard Date: Mon, 28 Apr 2014 11:02:29 +0000 (+0200) Subject: SONAR-5237 - moved DB part of stack to sonar-core X-Git-Tag: 4.4-RC1~1379 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5214de8cc260b47b130d863ddeba87b7d204239b;p=sonarqube.git SONAR-5237 - moved DB part of stack to sonar-core --- diff --git a/sonar-core/src/main/java/org/sonar/core/cluster/LocalNonBlockingWorkQueue.java b/sonar-core/src/main/java/org/sonar/core/cluster/LocalNonBlockingWorkQueue.java new file mode 100644 index 00000000000..25b10c399ec --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/cluster/LocalNonBlockingWorkQueue.java @@ -0,0 +1,94 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.cluster; + +import org.sonar.core.cluster.WorkQueue; + +import java.io.Serializable; +import java.util.Map; +import java.util.Queue; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentLinkedQueue; + +public class LocalNonBlockingWorkQueue implements WorkQueue{ + + private final static int WORKQUEUE_INITIAL_CAPACITY = 20; + + private ConcurrentHashMap> index; + private ConcurrentHashMap> update; + private ConcurrentHashMap> delete; + + public LocalNonBlockingWorkQueue(){ + this.index = new ConcurrentHashMap>(WORKQUEUE_INITIAL_CAPACITY); + this.update = new ConcurrentHashMap>(WORKQUEUE_INITIAL_CAPACITY); + this.delete = new ConcurrentHashMap>(WORKQUEUE_INITIAL_CAPACITY); + } + + private Integer enqueue(Map> map, String indexName, Serializable key){ + if(!map.containsKey(indexName)){ + map.put(indexName, new ConcurrentLinkedQueue()); + } + map.get(indexName).offer(key); + return 0; + } + + private Object dequeue(Map> map, String indexName){ + return (map.containsKey(indexName))? + map.get(indexName).poll(): + null; + } + + @Override + public Integer enqueInsert(String indexName, Serializable key) { + return this.enqueue(index, indexName, key); + } + + @Override + public Integer enqueUpdate(String indexName, Serializable key) { + return this.enqueue(update, indexName, key); + } + + @Override + public Integer enqueDelete(String indexName, Serializable key) { + return this.enqueue(delete, indexName, key); + } + + @Override + public Object dequeInsert(String indexName) { + return this.dequeue(index, indexName); + } + + @Override + public Object dequeUpdate(String indexName) { + return this.dequeue(update, indexName); + } + + @Override + public Object dequeDelete(String indexName) { + return this.dequeue(delete, indexName); + } + + @Override + public Status getStatus(Integer workId) { + // TODO Auto-generated method stub + return null; + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/cluster/WorkQueue.java b/sonar-core/src/main/java/org/sonar/core/cluster/WorkQueue.java new file mode 100644 index 00000000000..e3790f642b9 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/cluster/WorkQueue.java @@ -0,0 +1,43 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.cluster; + +import java.io.Serializable; + +public interface WorkQueue { + + Integer enqueInsert(String indexName, Serializable key); + + Integer enqueUpdate(String indexName, Serializable key); + + Integer enqueDelete(String indexName, Serializable key); + + Object dequeInsert(String indexName); + + Object dequeUpdate(String indexName); + + Object dequeDelete(String indexName); + + Status getStatus(Integer workId); + + interface Status { + + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/db/BaseDao.java b/sonar-core/src/main/java/org/sonar/core/db/BaseDao.java new file mode 100644 index 00000000000..e10bbf6cfdb --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/db/BaseDao.java @@ -0,0 +1,113 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.db; + +import org.apache.ibatis.session.SqlSession; +import org.sonar.core.cluster.WorkQueue; +import org.sonar.core.persistence.MyBatis; + +import java.io.Serializable; + +public abstract class BaseDao, K extends Serializable, M extends Dao> + implements Dao { + + protected MyBatis mybatis; + private WorkQueue queue; + + protected BaseDao(WorkQueue workQueue, MyBatis myBatis) { + this.mybatis = myBatis; + this.queue = workQueue; + } + + protected abstract String getIndexName(); + + protected abstract Class getMapperClass(); + + private M getMapper(SqlSession session) { + return session.getMapper(getMapperClass()); + } + + protected void enqueInsert(K key) { + this.queue.enqueInsert(this.getIndexName(), key); + } + + protected void enqueUpdate(K key) { + this.queue.enqueUpdate(this.getIndexName(), key); + } + + protected void enqueDelete(K key) { + this.queue.enqueDelete(this.getIndexName(), key); + } + + protected MyBatis getMyBatis(){ + return this.mybatis; + } + + @Override + @SuppressWarnings("unchecked") + public E getByKey(K key) { + E item = null; + SqlSession session = getMyBatis().openSession(); + item = getMapper(session).getByKey(key); + MyBatis.closeQuietly(session); + return item; + } + + @Override + public void update(E item) { + SqlSession session = getMyBatis().openSession(); + try { + getMapper(session).update(item); + session.commit(); + this.enqueUpdate(item.getKey()); + } finally { + MyBatis.closeQuietly(session); + } + } + + @Override + public void insert(E item) { + SqlSession session = getMyBatis().openSession(); + try { + getMapper(session).insert(item); + session.commit(); + this.enqueInsert(item.getKey()); + } finally { + MyBatis.closeQuietly(session); + } + } + + @Override + public void delete(E item) { + this.deleteByKey(item.getKey()); + } + + @Override + public void deleteByKey(K key) { + SqlSession session = getMyBatis().openSession(); + try { + getMapper(session).deleteByKey(key); + session.commit(); + } finally { + MyBatis.closeQuietly(session); + this.enqueDelete(key); + } + } +} diff --git a/sonar-core/src/main/java/org/sonar/core/db/Dao.java b/sonar-core/src/main/java/org/sonar/core/db/Dao.java new file mode 100644 index 00000000000..458d2b703e2 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/db/Dao.java @@ -0,0 +1,39 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.db; + +import java.io.Serializable; +import java.util.Collection; + +public interface Dao, K extends Serializable> { + + public E getByKey(K key); + + public void update(E item); + + public void insert(E item); + + public void delete(E item); + + public void deleteByKey(K key); + + public Collection insertsSince(Long timestamp); + +} diff --git a/sonar-core/src/main/java/org/sonar/core/db/Dto.java b/sonar-core/src/main/java/org/sonar/core/db/Dto.java new file mode 100644 index 00000000000..2ac53d71222 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/db/Dto.java @@ -0,0 +1,28 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.db; + +import java.io.Serializable; + +public interface Dto { + + K getKey(); + +} diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleConstants.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleConstants.java new file mode 100644 index 00000000000..636a6dcd67c --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleConstants.java @@ -0,0 +1,26 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.rule; + +public interface RuleConstants { + + public static final String INDEX_NAME = "rules"; + public static final String ES_TYPE = "rule"; +} diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java index 3b5677fc9b9..1c87168d8b5 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDao.java @@ -24,6 +24,8 @@ import org.apache.ibatis.session.SqlSession; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; import org.sonar.api.rule.RuleKey; +import org.sonar.core.cluster.WorkQueue; +import org.sonar.core.db.BaseDao; import org.sonar.core.persistence.MyBatis; import javax.annotation.CheckForNull; @@ -33,12 +35,22 @@ import java.util.List; import static com.google.common.collect.Lists.newArrayList; -public class RuleDao implements BatchComponent, ServerComponent { +public class RuleDao extends BaseDao + implements BatchComponent, ServerComponent { - private MyBatis mybatis; - public RuleDao(MyBatis mybatis) { - this.mybatis = mybatis; + public RuleDao(MyBatis mybatis, WorkQueue queue) { + super(queue, mybatis); + } + + @Override + protected String getIndexName() { + return RuleConstants.INDEX_NAME; + } + + @Override + protected Class getMapperClass() { + return RuleMapper.class; } public List selectAll() { @@ -131,29 +143,18 @@ public class RuleDao implements BatchComponent, ServerComponent { getMapper(session).update(rule); } - public void update(RuleDto rule) { - SqlSession session = mybatis.openSession(); - try { - update(rule, session); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - } +// public RuleDto update(RuleDto rule) { +// return super.update(rule); +// } +// +// public RuleDto insert(RuleDto ruleToInsert) { +// return super.insert(ruleToInsert); +// } public void insert(RuleDto ruleToInsert, SqlSession session) { getMapper(session).insert(ruleToInsert); } - public void insert(RuleDto ruleToInsert) { - SqlSession session = mybatis.openSession(); - try { - insert(ruleToInsert, session); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - } - } public void insert(Collection rules) { SqlSession session = mybatis.openBatchSession(); @@ -307,4 +308,10 @@ public class RuleDao implements BatchComponent, ServerComponent { } return dtos; } + + @Override + public Collection insertsSince(Long timestamp) { + // TODO Auto-generated method stub + return null; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java index 8bb8f4e38b5..78dd41c99a5 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleDto.java @@ -19,6 +19,9 @@ */ package org.sonar.core.rule; +import org.sonar.api.rule.RuleKey; + +import org.sonar.core.db.Dto; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import org.apache.commons.lang.builder.ReflectionToStringBuilder; @@ -30,7 +33,7 @@ import javax.annotation.Nullable; import java.util.Date; -public final class RuleDto { +public final class RuleDto implements Dto{ public static final Integer DISABLED_CHARACTERISTIC_ID = -1; @@ -61,6 +64,12 @@ public final class RuleDto { private Date createdAt; private Date updatedAt; + @Override + public RuleKey getKey() { + return RuleKey.of(this.getRepositoryKey(), this.getRuleKey()); + } + + @Deprecated public Integer getId() { return id; } diff --git a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java index e31b14e04aa..536071017eb 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/RuleMapper.java @@ -21,10 +21,12 @@ package org.sonar.core.rule; import org.apache.ibatis.annotations.Param; import org.sonar.api.rule.RuleKey; +import org.sonar.core.db.Dao; import java.util.List; -public interface RuleMapper { +public interface RuleMapper extends Dao{ + List selectAll(); List selectEnablesAndNonManual(); diff --git a/sonar-core/src/test/java/org/sonar/core/cluster/LocalNonBlockingWorkQueueTest.java b/sonar-core/src/test/java/org/sonar/core/cluster/LocalNonBlockingWorkQueueTest.java new file mode 100644 index 00000000000..c352f14cb44 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/cluster/LocalNonBlockingWorkQueueTest.java @@ -0,0 +1,138 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.cluster; + +import org.sonar.core.cluster.LocalNonBlockingWorkQueue; + +import org.junit.Test; + +import java.io.Serializable; +import java.util.Map; + +import static org.fest.assertions.Assertions.assertThat; + + +public class LocalNonBlockingWorkQueueTest { + + private static final String WORKING_INDEX = "working_index"; + private static final String NON_WORKING_INDEX = "non_working_index"; + + @Test + public void test_insert_queue(){ + LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); + + assertThat(queue.dequeInsert(WORKING_INDEX)).isNull(); + assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); + + queue.enqueInsert(WORKING_INDEX, new Integer(0)); + assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); + + Object dequeued = queue.dequeInsert(WORKING_INDEX); + assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); + assertThat(queue.dequeInsert(WORKING_INDEX)).isNull(); + + assertThat(dequeued).isEqualTo(new Integer(0)); + } + + @Test + public void test_update_queue(){ + LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); + + assertThat(queue.dequeUpdate(WORKING_INDEX)).isNull(); + assertThat(queue.dequeUpdate(NON_WORKING_INDEX)).isNull(); + + queue.enqueUpdate(WORKING_INDEX, new Integer(0)); + assertThat(queue.dequeUpdate(NON_WORKING_INDEX)).isNull(); + + Object dequeued = queue.dequeUpdate(WORKING_INDEX); + assertThat(queue.dequeUpdate(NON_WORKING_INDEX)).isNull(); + assertThat(queue.dequeUpdate(WORKING_INDEX)).isNull(); + + assertThat(dequeued).isEqualTo(new Integer(0)); + } + + @Test + public void test_delete_queue(){ + LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); + + assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); + assertThat(queue.dequeDelete(NON_WORKING_INDEX)).isNull(); + + queue.enqueDelete(WORKING_INDEX, new Integer(0)); + assertThat(queue.dequeDelete(NON_WORKING_INDEX)).isNull(); + + Object dequeued = queue.dequeDelete(WORKING_INDEX); + assertThat(queue.dequeDelete(NON_WORKING_INDEX)).isNull(); + assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); + + assertThat(dequeued).isEqualTo(new Integer(0)); + } + + @Test + public void test_enque_seralizable_object(){ + + LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); + + class NonSerializable implements Serializable{ + private Object var1; + private Map objs; + } + + NonSerializable nonSer = new NonSerializable(); + assertThat(queue.enqueInsert(WORKING_INDEX, nonSer)).isNotNull(); + + Object dequeued = queue.dequeInsert(WORKING_INDEX); + assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); + + assertThat(dequeued).isNotNull(); + assertThat(dequeued.getClass()).isEqualTo(NonSerializable.class); + } + + @Test + public void test_under_queue_capacity(){ + LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); + + for(int i = 0; i < 10; i++){ + assertThat(queue.enqueDelete(WORKING_INDEX, i)).isNotNull(); + } + + for(int i = 0; i < 10; i++){ + assertThat(queue.dequeDelete(WORKING_INDEX)).isNotNull(); + } + assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); + + } + + @Test + public void test_over_queue_capacity(){ + LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); + + for(int i = 0; i < 100; i++){ + assertThat(queue.enqueDelete(WORKING_INDEX, i)).isNotNull(); + } + + for(int i = 0; i < 100; i++){ + assertThat(queue.dequeDelete(WORKING_INDEX)).isNotNull(); + } + assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); + + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java index 6386962c054..8b6aaf45095 100644 --- a/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java +++ b/sonar-core/src/test/java/org/sonar/core/rule/RuleDaoTest.java @@ -19,6 +19,8 @@ */ package org.sonar.core.rule; +import org.sonar.core.cluster.WorkQueue; + import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; @@ -34,6 +36,8 @@ import org.sonar.core.persistence.AbstractDaoTestCase; import java.util.List; +import static org.mockito.Mockito.mock; + import static com.google.common.collect.Lists.newArrayList; import static org.fest.assertions.Assertions.assertThat; @@ -43,7 +47,8 @@ public class RuleDaoTest extends AbstractDaoTestCase { @Before public void createDao() throws Exception { - dao = new RuleDao(getMyBatis()); + WorkQueue queue = mock(WorkQueue.class); + dao = new RuleDao(getMyBatis(), queue); } @Test diff --git a/sonar-server/src/main/java/org/sonar/server/cluster/LocalNonBlockingWorkQueue.java b/sonar-server/src/main/java/org/sonar/server/cluster/LocalNonBlockingWorkQueue.java deleted file mode 100644 index c3c53600c41..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/cluster/LocalNonBlockingWorkQueue.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.sonar.server.cluster; - -import java.io.Serializable; -import java.util.Map; -import java.util.Queue; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentLinkedQueue; - -public class LocalNonBlockingWorkQueue implements WorkQueue{ - - private final static int WORKQUEUE_INITIAL_CAPACITY = 20; - - private ConcurrentHashMap> index; - private ConcurrentHashMap> update; - private ConcurrentHashMap> delete; - - public LocalNonBlockingWorkQueue(){ - this.index = new ConcurrentHashMap>(WORKQUEUE_INITIAL_CAPACITY); - this.update = new ConcurrentHashMap>(WORKQUEUE_INITIAL_CAPACITY); - this.delete = new ConcurrentHashMap>(WORKQUEUE_INITIAL_CAPACITY); - } - - private Integer enqueue(Map> map, String indexName, Serializable key){ - if(!map.containsKey(indexName)){ - map.put(indexName, new ConcurrentLinkedQueue()); - } - map.get(indexName).offer(key); - return 0; - } - - private Object dequeue(Map> map, String indexName){ - return (map.containsKey(indexName))? - map.get(indexName).poll(): - null; - } - - @Override - public Integer enqueInsert(String indexName, Serializable key) { - return this.enqueue(index, indexName, key); - } - - @Override - public Integer enqueUpdate(String indexName, Serializable key) { - return this.enqueue(update, indexName, key); - } - - @Override - public Integer enqueDelete(String indexName, Serializable key) { - return this.enqueue(delete, indexName, key); - } - - @Override - public Object dequeInsert(String indexName) { - return this.dequeue(index, indexName); - } - - @Override - public Object dequeUpdate(String indexName) { - return this.dequeue(update, indexName); - } - - @Override - public Object dequeDelete(String indexName) { - return this.dequeue(delete, indexName); - } - - @Override - public Status getStatus(Integer workId) { - // TODO Auto-generated method stub - return null; - } - -} diff --git a/sonar-server/src/main/java/org/sonar/server/cluster/WorkQueue.java b/sonar-server/src/main/java/org/sonar/server/cluster/WorkQueue.java deleted file mode 100644 index 79c3b520dc9..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/cluster/WorkQueue.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.cluster; - -import java.io.Serializable; - -public interface WorkQueue { - - Integer enqueInsert(String indexName, Serializable key); - - Integer enqueUpdate(String indexName, Serializable key); - - Integer enqueDelete(String indexName, Serializable key); - - Object dequeInsert(String indexName); - - Object dequeUpdate(String indexName); - - Object dequeDelete(String indexName); - - Status getStatus(Integer workId); - - interface Status { - - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java b/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java deleted file mode 100644 index 21a2270ef63..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.db; - -import org.apache.ibatis.session.SqlSession; -import org.sonar.core.persistence.MyBatis; -import org.sonar.server.cluster.WorkQueue; - -import java.io.Serializable; - -public abstract class BaseDao, K extends Serializable> implements Dao { - - private MyBatis myBatis; - private WorkQueue workQueue; - - protected BaseDao(WorkQueue workQueue, MyBatis myBatis) { - this.myBatis = myBatis; - this.workQueue = workQueue; - } - - protected abstract String getIndexName(); - - protected void enqueInsert(K key) { - this.workQueue.enqueInsert(this.getIndexName(), key); - } - - protected void enqueUpdate(K key) { - this.workQueue.enqueUpdate(this.getIndexName(), key); - } - - protected void enqueDelete(K key) { - this.workQueue.enqueDelete(this.getIndexName(), key); - } - - protected MyBatis getMyBatis(){ - return this.myBatis; - } - - @Override - @SuppressWarnings("unchecked") - public E getByKey(K key) { - E item = null; - SqlSession session = getMyBatis().openSession(); - item = (E) session.getMapper(this.getClass()).getByKey(key); - MyBatis.closeQuietly(session); - return item; - } - - @Override - public E update(E item) { - SqlSession session = getMyBatis().openSession(); - E result = null; - try { - result = (E) session.getMapper(this.getClass()).update(item); - session.commit(); - } finally { - this.enqueUpdate(item.getKey()); - MyBatis.closeQuietly(session); - return result; - } - } - - @Override - public E insert(E item) { - SqlSession session = getMyBatis().openSession(); - E result = null; - try { - result = (E) session.getMapper(this.getClass()).insert(item); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - this.enqueInsert(item.getKey()); - return result; - } - } - - @Override - public void delete(E item) { - this.deleteByKey(item.getKey()); - } - - @Override - public void deleteByKey(K key) { - SqlSession session = getMyBatis().openSession(); - try { - session.getMapper(this.getClass()).deleteByKey(key); - session.commit(); - } finally { - MyBatis.closeQuietly(session); - this.enqueDelete(key); - } - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/db/Dao.java b/sonar-server/src/main/java/org/sonar/server/db/Dao.java deleted file mode 100644 index e3fad2e59f5..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/db/Dao.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.db; - -import java.io.Serializable; -import java.util.Collection; - -public interface Dao, K extends Serializable> { - - public E getByKey(K key); - - public E update(E item); - - public E insert(E item); - - public void delete(E item); - - public void deleteByKey(K key); - - public Collection insertsSince(Long timestamp); - -} diff --git a/sonar-server/src/main/java/org/sonar/server/db/Dto.java b/sonar-server/src/main/java/org/sonar/server/db/Dto.java deleted file mode 100644 index 483a276f175..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/db/Dto.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.db; - -import java.io.Serializable; - -public interface Dto { - - K getKey(); - -} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleConstants.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleConstants.java deleted file mode 100644 index 98abd6f7979..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleConstants.java +++ /dev/null @@ -1,7 +0,0 @@ -package org.sonar.server.rule2; - -public interface RuleConstants { - - public static final String INDEX_NAME = "rules"; - public static final String ES_TYPE = "rule"; -} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java deleted file mode 100644 index 3bc9875c398..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDao.java +++ /dev/null @@ -1,26 +0,0 @@ -package org.sonar.server.rule2; - -import java.util.Collection; - -import org.sonar.core.persistence.MyBatis; -import org.sonar.server.cluster.WorkQueue; -import org.sonar.api.rule.RuleKey; -import org.sonar.server.db.BaseDao; - -public class RuleDao extends BaseDao { - - protected RuleDao(WorkQueue workQueue, MyBatis myBatis) { - super(workQueue, myBatis); - } - - @Override - protected String getIndexName() { - return RuleConstants.INDEX_NAME; - } - - @Override - public Collection insertsSince(Long timestamp) { - // TODO Auto-generated method stub - return null; - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDto.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleDto.java deleted file mode 100644 index 621de3ae268..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleDto.java +++ /dev/null @@ -1,344 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.rule2; - -import org.sonar.api.rule.RuleKey; - -import org.sonar.core.rule.SeverityUtil; -import org.sonar.server.db.Dto; -import org.apache.commons.lang.builder.EqualsBuilder; -import org.apache.commons.lang.builder.HashCodeBuilder; -import org.apache.commons.lang.builder.ReflectionToStringBuilder; -import org.apache.commons.lang.builder.ToStringStyle; -import org.sonar.check.Cardinality; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import java.util.Date; - -public final class RuleDto implements Dto { - - public static final Integer DISABLED_CHARACTERISTIC_ID = -1; - - private String repositoryKey; - private String ruleKey; - private String description; - private String status; - private String name; - private String configKey; - private Integer severity; - private Cardinality cardinality; - private String language; - private Integer parentId; - private String noteData; - private String noteUserLogin; - private Date noteCreatedAt; - private Date noteUpdatedAt; - private Integer subCharacteristicId; - private Integer defaultSubCharacteristicId; - private String remediationFunction; - private String defaultRemediationFunction; - private String remediationCoefficient; - private String defaultRemediationCoefficient; - private String remediationOffset; - private String defaultRemediationOffset; - private String effortToFixDescription; - private Date createdAt; - private Date updatedAt; - - @Override - public RuleKey getKey() { - return RuleKey.of(this.getRepositoryKey(), this.getRuleKey()); - } - - public String getRepositoryKey() { - return repositoryKey; - } - - public RuleDto setRepositoryKey(String repositoryKey) { - this.repositoryKey = repositoryKey; - return this; - } - - public String getRuleKey() { - return ruleKey; - } - - public RuleDto setRuleKey(String ruleKey) { - this.ruleKey = ruleKey; - return this; - } - - public String getDescription() { - return description; - } - - public RuleDto setDescription(String description) { - this.description = description; - return this; - } - - public String getStatus() { - return status; - } - - public RuleDto setStatus(String status) { - this.status = status; - return this; - } - - public String getName() { - return name; - } - - public RuleDto setName(String name) { - this.name = name; - return this; - } - - public String getConfigKey() { - return configKey; - } - - public RuleDto setConfigKey(String configKey) { - this.configKey = configKey; - return this; - } - - public Integer getSeverity() { - return severity; - } - - public String getSeverityString() { - return SeverityUtil.getSeverityFromOrdinal(severity); - } - - public RuleDto setSeverity(String severity) { - this.severity = SeverityUtil.getOrdinalFromSeverity(severity); - return this; - } - - public RuleDto setSeverity(Integer severity) { - this.severity = severity; - return this; - } - - - public Cardinality getCardinality() { - return cardinality; - } - - public RuleDto setCardinality(Cardinality cardinality) { - this.cardinality = cardinality; - return this; - } - - public String getLanguage() { - return language; - } - - public RuleDto setLanguage(String language) { - this.language = language; - return this; - } - - @CheckForNull - public Integer getParentId() { - return parentId; - } - - public RuleDto setParentId(@Nullable Integer parentId) { - this.parentId = parentId; - return this; - } - - public String getNoteData() { - return noteData; - } - - public RuleDto setNoteData(String noteData) { - this.noteData = noteData; - return this; - } - - public String getNoteUserLogin() { - return noteUserLogin; - } - - public RuleDto setNoteUserLogin(String noteUserLogin) { - this.noteUserLogin = noteUserLogin; - return this; - } - - public Date getNoteCreatedAt() { - return noteCreatedAt; - } - - public RuleDto setNoteCreatedAt(Date noteCreatedAt) { - this.noteCreatedAt = noteCreatedAt; - return this; - } - - public Date getNoteUpdatedAt() { - return noteUpdatedAt; - } - - public RuleDto setNoteUpdatedAt(Date noteUpdatedAt) { - this.noteUpdatedAt = noteUpdatedAt; - return this; - } - - @CheckForNull - public Integer getSubCharacteristicId() { - return subCharacteristicId; - } - - public RuleDto setSubCharacteristicId(@Nullable Integer subCharacteristicId) { - this.subCharacteristicId = subCharacteristicId; - return this; - } - - @CheckForNull - public Integer getDefaultSubCharacteristicId() { - return defaultSubCharacteristicId; - } - - public RuleDto setDefaultSubCharacteristicId(@Nullable Integer defaultSubCharacteristicId) { - this.defaultSubCharacteristicId = defaultSubCharacteristicId; - return this; - } - - @CheckForNull - public String getRemediationFunction() { - return remediationFunction; - } - - public RuleDto setRemediationFunction(@Nullable String remediationFunction) { - this.remediationFunction = remediationFunction; - return this; - } - - @CheckForNull - public String getDefaultRemediationFunction() { - return defaultRemediationFunction; - } - - public RuleDto setDefaultRemediationFunction(@Nullable String defaultRemediationFunction) { - this.defaultRemediationFunction = defaultRemediationFunction; - return this; - } - - @CheckForNull - public String getRemediationCoefficient() { - return remediationCoefficient; - } - - public RuleDto setRemediationCoefficient(@Nullable String remediationCoefficient) { - this.remediationCoefficient = remediationCoefficient; - return this; - } - - @CheckForNull - public String getDefaultRemediationCoefficient() { - return defaultRemediationCoefficient; - } - - public RuleDto setDefaultRemediationCoefficient(@Nullable String defaultRemediationCoefficient) { - this.defaultRemediationCoefficient = defaultRemediationCoefficient; - return this; - } - - @CheckForNull - public String getRemediationOffset() { - return remediationOffset; - } - - public RuleDto setRemediationOffset(@Nullable String remediationOffset) { - this.remediationOffset = remediationOffset; - return this; - } - - @CheckForNull - public String getDefaultRemediationOffset() { - return defaultRemediationOffset; - } - - public RuleDto setDefaultRemediationOffset(@Nullable String defaultRemediationOffset) { - this.defaultRemediationOffset = defaultRemediationOffset; - return this; - } - - @CheckForNull - public String getEffortToFixDescription() { - return effortToFixDescription; - } - - public RuleDto setEffortToFixDescription(@Nullable String effortToFixDescription) { - this.effortToFixDescription = effortToFixDescription; - return this; - } - - public Date getCreatedAt() { - return createdAt; - } - - public RuleDto setCreatedAt(Date createdAt) { - this.createdAt = createdAt; - return this; - } - - public Date getUpdatedAt() { - return updatedAt; - } - - public RuleDto setUpdatedAt(Date updatedAt) { - this.updatedAt = updatedAt; - return this; - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof RuleDto)) { - return false; - } - if (this == obj) { - return true; - } - RuleDto other = (RuleDto) obj; - return new EqualsBuilder() - .append(repositoryKey, other.getRepositoryKey()) - .append(ruleKey, other.getRuleKey()) - .isEquals(); - } - - @Override - public int hashCode() { - return new HashCodeBuilder(17, 37) - .append(repositoryKey) - .append(ruleKey) - .toHashCode(); - } - - @Override - public String toString() { - return new ReflectionToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).toString(); - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java index 89ccff40a50..865ba63d1bd 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleImpl.java @@ -1,3 +1,22 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.sonar.server.rule2; import org.sonar.api.rule.Severity; diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java index d7bda890e89..e30f7e29ec1 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleIndex.java @@ -1,17 +1,37 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.sonar.server.rule2; import org.elasticsearch.common.settings.ImmutableSettings; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.rule.RuleKey; -import org.sonar.core.persistence.MyBatis; +import org.sonar.core.cluster.WorkQueue; import org.sonar.core.profiling.Profiling; -import org.sonar.server.cluster.WorkQueue; +import org.sonar.core.rule.RuleConstants; +import org.sonar.core.rule.RuleDao; +import org.sonar.core.db.Dao; import org.sonar.server.search.BaseIndex; import java.io.IOException; @@ -25,8 +45,8 @@ public class RuleIndex extends BaseIndex { private static final Logger LOG = LoggerFactory.getLogger(RuleIndex.class); - public RuleIndex(WorkQueue workQueue, RuleDao dao, Profiling profiling) { - super(workQueue, dao, profiling); + public RuleIndex(WorkQueue queue, RuleDao dao, Profiling profiling) { + super(queue, dao, profiling); } @Override diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleQuery.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleQuery.java index 2a7cc349988..d526c2839dd 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleQuery.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleQuery.java @@ -1,3 +1,22 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.sonar.server.rule2; public class RuleQuery { diff --git a/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java b/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java index a9087216fd8..dfd0835e3cf 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java +++ b/sonar-server/src/main/java/org/sonar/server/rule2/RuleService.java @@ -20,6 +20,8 @@ package org.sonar.server.rule2; import org.sonar.api.rule.RuleKey; +import org.sonar.core.rule.RuleDao; +import org.sonar.core.rule.RuleDto; import org.sonar.server.search.Hit; import javax.annotation.CheckForNull; diff --git a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java index 9eaf598196e..1d6529238e0 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/BaseIndex.java @@ -33,12 +33,11 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.core.persistence.MyBatis; +import org.sonar.core.cluster.WorkQueue; +import org.sonar.core.db.Dao; import org.sonar.core.profiling.Profiling; import org.sonar.core.profiling.Profiling.Level; import org.sonar.core.profiling.StopWatch; -import org.sonar.server.cluster.WorkQueue; -import org.sonar.server.db.Dao; import java.io.Serializable; import java.util.Collection; diff --git a/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java b/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java index 0547675b535..de2d4bc0d9f 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java +++ b/sonar-server/src/main/java/org/sonar/server/search/IndexSynchronizer.java @@ -19,9 +19,10 @@ */ package org.sonar.server.search; +import org.sonar.core.cluster.WorkQueue; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.server.cluster.WorkQueue; import java.io.Serializable; diff --git a/sonar-server/src/test/java/org/sonar/server/cluster/LocalNonBlockingWorkQueueTest.java b/sonar-server/src/test/java/org/sonar/server/cluster/LocalNonBlockingWorkQueueTest.java deleted file mode 100644 index 117db387a9a..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/cluster/LocalNonBlockingWorkQueueTest.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2014 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * SonarQube is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.cluster; - -import org.junit.Test; - -import java.io.Serializable; -import java.util.Map; - -import static org.fest.assertions.Assertions.assertThat; - - -public class LocalNonBlockingWorkQueueTest { - - private static final String WORKING_INDEX = "working_index"; - private static final String NON_WORKING_INDEX = "non_working_index"; - - @Test - public void test_insert_queue(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - - assertThat(queue.dequeInsert(WORKING_INDEX)).isNull(); - assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); - - queue.enqueInsert(WORKING_INDEX, new Integer(0)); - assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); - - Object dequeued = queue.dequeInsert(WORKING_INDEX); - assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); - assertThat(queue.dequeInsert(WORKING_INDEX)).isNull(); - - assertThat(dequeued).isEqualTo(new Integer(0)); - } - - @Test - public void test_update_queue(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - - assertThat(queue.dequeUpdate(WORKING_INDEX)).isNull(); - assertThat(queue.dequeUpdate(NON_WORKING_INDEX)).isNull(); - - queue.enqueUpdate(WORKING_INDEX, new Integer(0)); - assertThat(queue.dequeUpdate(NON_WORKING_INDEX)).isNull(); - - Object dequeued = queue.dequeUpdate(WORKING_INDEX); - assertThat(queue.dequeUpdate(NON_WORKING_INDEX)).isNull(); - assertThat(queue.dequeUpdate(WORKING_INDEX)).isNull(); - - assertThat(dequeued).isEqualTo(new Integer(0)); - } - - @Test - public void test_delete_queue(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - - assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); - assertThat(queue.dequeDelete(NON_WORKING_INDEX)).isNull(); - - queue.enqueDelete(WORKING_INDEX, new Integer(0)); - assertThat(queue.dequeDelete(NON_WORKING_INDEX)).isNull(); - - Object dequeued = queue.dequeDelete(WORKING_INDEX); - assertThat(queue.dequeDelete(NON_WORKING_INDEX)).isNull(); - assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); - - assertThat(dequeued).isEqualTo(new Integer(0)); - } - - @Test - public void test_enque_seralizable_object(){ - - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - - class NonSerializable implements Serializable{ - private Object var1; - private Map objs; - } - - NonSerializable nonSer = new NonSerializable(); - assertThat(queue.enqueInsert(WORKING_INDEX, nonSer)).isNotNull(); - - Object dequeued = queue.dequeInsert(WORKING_INDEX); - assertThat(queue.dequeInsert(NON_WORKING_INDEX)).isNull(); - - assertThat(dequeued).isNotNull(); - assertThat(dequeued.getClass()).isEqualTo(NonSerializable.class); - } - - @Test - public void test_under_queue_capacity(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - - for(int i = 0; i < 10; i++){ - assertThat(queue.enqueDelete(WORKING_INDEX, i)).isNotNull(); - } - - for(int i = 0; i < 10; i++){ - assertThat(queue.dequeDelete(WORKING_INDEX)).isNotNull(); - } - assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); - - } - - @Test - public void test_over_queue_capacity(){ - LocalNonBlockingWorkQueue queue = new LocalNonBlockingWorkQueue(); - - for(int i = 0; i < 100; i++){ - assertThat(queue.enqueDelete(WORKING_INDEX, i)).isNotNull(); - } - - for(int i = 0; i < 100; i++){ - assertThat(queue.dequeDelete(WORKING_INDEX)).isNotNull(); - } - assertThat(queue.dequeDelete(WORKING_INDEX)).isNull(); - - } - -} diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index 01c35e4d405..6707ea13852 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -34,6 +34,8 @@ import org.sonar.api.server.rule.RulesDefinition; import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.System2; +import org.sonar.core.cluster.LocalNonBlockingWorkQueue; +import org.sonar.core.cluster.WorkQueue; import org.sonar.core.persistence.AbstractDaoTestCase; import org.sonar.core.persistence.MyBatis; import org.sonar.core.qualityprofile.db.ActiveRuleDao; @@ -50,7 +52,11 @@ import java.util.Date; import static org.fest.assertions.Assertions.assertThat; import static org.fest.assertions.Fail.fail; -import static org.mockito.Mockito.*; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class RegisterRulesTest extends AbstractDaoTestCase { @@ -85,14 +91,16 @@ public class RegisterRulesTest extends AbstractDaoTestCase { ActiveRuleDao activeRuleDao; CharacteristicDao characteristicDao; System2 system; + WorkQueue queue; Date date = DateUtils.parseDateTime("2014-03-17T19:10:03+0100"); @Before public void before() { system = mock(System2.class); + queue = mock(WorkQueue.class); when(system.now()).thenReturn(date.getTime()); myBatis = getMyBatis(); - ruleDao = new RuleDao(myBatis); + ruleDao = new RuleDao(myBatis, queue); ruleTagDao = new RuleTagDao(myBatis); activeRuleDao = new ActiveRuleDao(myBatis); ruleTagOperations = new RuleTagOperations(ruleTagDao, esRuleTags); diff --git a/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java b/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java index a1e21f995b8..f356961ddef 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule2/RuleIndexTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.rule2; -import org.sonar.server.cluster.LocalNonBlockingWorkQueue; +import org.sonar.core.cluster.LocalNonBlockingWorkQueue; import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchNode; import com.github.tlrx.elasticsearch.test.support.junit.runners.ElasticsearchRunner; diff --git a/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java b/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java index 0f75176d5e1..46232717b59 100644 --- a/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java +++ b/sonar-server/src/test/java/org/sonar/server/search/BaseIndexTest.java @@ -19,8 +19,6 @@ */ package org.sonar.server.search; -import org.sonar.server.cluster.LocalNonBlockingWorkQueue; - import com.github.tlrx.elasticsearch.test.annotations.ElasticsearchNode; import com.github.tlrx.elasticsearch.test.support.junit.runners.ElasticsearchRunner; import org.elasticsearch.client.transport.NoNodeAvailableException; @@ -32,6 +30,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.sonar.api.config.Settings; +import org.sonar.core.cluster.LocalNonBlockingWorkQueue; import org.sonar.core.profiling.Profiling; import java.io.Serializable;