-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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.apache.hugegraph.store.buffer; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
|
||
public class ByteBufferAllocator { | ||
// size of each Buffer | ||
final int capacity; | ||
// max num of Buffers | ||
final int maxCount; | ||
final BlockingQueue<ByteBuffer> freeQueue = new LinkedBlockingQueue<>(); | ||
Check warning on line 32 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L32
|
||
// current num of Buffers in queue | ||
AtomicInteger totalCount; | ||
|
||
public ByteBufferAllocator(int cap, int count) { | ||
this.capacity = cap; | ||
this.maxCount = count; | ||
this.totalCount = new AtomicInteger(0); | ||
} | ||
Check warning on line 40 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L36-L40
|
||
|
||
public ByteBuffer get() throws InterruptedException { | ||
ByteBuffer buffer = null; | ||
Check warning on line 43 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L43
|
||
while (buffer == null) { | ||
if (freeQueue.size() > 0) { | ||
buffer = freeQueue.poll(); | ||
Check warning on line 46 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L46
|
||
} else if (totalCount.get() < maxCount) { | ||
buffer = ByteBuffer.allocate(capacity); | ||
totalCount.incrementAndGet(); | ||
Check warning on line 49 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L48-L49
|
||
} else { | ||
buffer = freeQueue.poll(1, TimeUnit.SECONDS); | ||
Check warning on line 51 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L51
|
||
} | ||
} | ||
return buffer; | ||
Check warning on line 54 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L54
|
||
} | ||
|
||
public void release(ByteBuffer buffer) { | ||
if (freeQueue.size() < maxCount) { | ||
buffer.clear(); | ||
freeQueue.add(buffer); | ||
Check warning on line 60 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L59-L60
|
||
} | ||
} | ||
Check warning on line 62 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java Codecov / codecov/patchhugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L62
|
||
} |