Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 17 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 18 | import org.onosproject.core.IdBlock; |
| 19 | import org.onosproject.core.IdBlockStore; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.osgi.service.component.annotations.Component; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 21 | |
| 22 | import java.util.Map; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | import java.util.concurrent.atomic.AtomicLong; |
| 25 | |
| 26 | /** |
| 27 | * Simple implementation of id block store. |
| 28 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 29 | @Component(immediate = true, service = IdBlockStore.class) |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 30 | public class SimpleIdBlockStore implements IdBlockStore { |
| 31 | |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 32 | private static final long DEFAULT_BLOCK_SIZE = 0x1000L; |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 33 | |
| 34 | private final Map<String, AtomicLong> topicBlocks = new ConcurrentHashMap<>(); |
| 35 | |
| 36 | @Override |
| 37 | public synchronized IdBlock getIdBlock(String topic) { |
| 38 | AtomicLong blockGenerator = topicBlocks.get(topic); |
| 39 | if (blockGenerator == null) { |
| 40 | blockGenerator = new AtomicLong(0); |
| 41 | topicBlocks.put(topic, blockGenerator); |
| 42 | } |
| 43 | Long blockBase = blockGenerator.getAndAdd(DEFAULT_BLOCK_SIZE); |
| 44 | return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE); |
| 45 | } |
| 46 | } |