Skip to content

Commit

Permalink
add ByteBufferAllocator
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengzna committed Mar 12, 2024
1 parent 07735c1 commit fb624c8
Showing 1 changed file with 63 additions and 0 deletions.
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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L32

Added line #L32 was not covered by tests
// 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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L36-L40

Added lines #L36 - L40 were not covered by tests

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L43

Added line #L43 was not covered by tests
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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L46

Added line #L46 was not covered by tests
} 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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L48-L49

Added lines #L48 - L49 were not covered by tests
} 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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L51

Added line #L51 was not covered by tests
}
}
return buffer;

Check warning on line 54 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L54

Added line #L54 was not covered by tests
}

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

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L59-L60

Added lines #L59 - L60 were not covered by tests
}
}

Check warning on line 62 in hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java

View check run for this annotation

Codecov / codecov/patch

hugegraph-store/hg-store-common/src/main/java/org/apache/hugegraph/store/buffer/ByteBufferAllocator.java#L62

Added line #L62 was not covered by tests
}

0 comments on commit fb624c8

Please sign in to comment.