9 changed files with 140 additions and 19 deletions
@ -0,0 +1,53 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sql; |
||||
|
|
||||
|
import com.google.common.util.concurrent.ListenableFuture; |
||||
|
import lombok.Data; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.concurrent.CopyOnWriteArrayList; |
||||
|
import java.util.function.Consumer; |
||||
|
import java.util.function.Function; |
||||
|
|
||||
|
@Slf4j |
||||
|
@Data |
||||
|
public class TbSqlBlockingQueueWrapper<E> { |
||||
|
private final CopyOnWriteArrayList<TbSqlBlockingQueue<E>> queues = new CopyOnWriteArrayList<>(); |
||||
|
private final TbSqlBlockingQueueParams params; |
||||
|
private ScheduledLogExecutorComponent logExecutor; |
||||
|
private final Function<E, Integer> hashCodeFunction; |
||||
|
private final int maxThreads; |
||||
|
|
||||
|
public void init(ScheduledLogExecutorComponent logExecutor, Consumer<List<E>> saveFunction) { |
||||
|
for (int i = 0; i < maxThreads; i++) { |
||||
|
TbSqlBlockingQueue<E> queue = new TbSqlBlockingQueue<>(params); |
||||
|
queues.add(queue); |
||||
|
queue.init(logExecutor, saveFunction, i); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public ListenableFuture<Void> add(E element) { |
||||
|
int hash = hashCodeFunction.apply(element); |
||||
|
int queueIndex = (hash & 0x7FFFFFFF) % maxThreads; |
||||
|
return queues.get(queueIndex).add(element); |
||||
|
} |
||||
|
|
||||
|
public void destroy() { |
||||
|
queues.forEach(TbSqlBlockingQueue::destroy); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,26 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2020 The Thingsboard Authors |
||||
|
* |
||||
|
* 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 org.thingsboard.server.dao.sqlts; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.UUID; |
||||
|
|
||||
|
@Data |
||||
|
public class TsKey { |
||||
|
private final UUID entityId; |
||||
|
private final String key; |
||||
|
} |
||||
Loading…
Reference in new issue