All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.ikm.tinkar.collection.SpinedIntLongArrayMap Maven / Gradle / Ivy

There is a newer version: 1.79.0
Show newest version
/*
 * Copyright © 2015 Integrated Knowledge Management ([email protected])
 *
 * 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 dev.ikm.tinkar.collection;

import dev.ikm.tinkar.collection.store.IntLongArrayStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceArray;

public class SpinedIntLongArrayMap extends SpinedIntObjectMap {

    private static final Logger LOG = LoggerFactory.getLogger(SpinedIntLongArrayMap.class);
    private final IntLongArrayStore intLongArrayStore;

    public SpinedIntLongArrayMap(IntLongArrayStore intLongArrayStore) {
        super(intLongArrayStore.getSpineCount());
        this.intLongArrayStore = intLongArrayStore;
    }

    public int sizeOnDisk() {
        return intLongArrayStore.sizeOnDisk();
    }

    public int memoryInUse() {
        AtomicInteger sizeInBytes = new AtomicInteger();
        sizeInBytes.addAndGet(((spineSize * 8) * getSpineCount()));
        forEachSpine((AtomicReferenceArray spine, int spineIndex) -> {
            for (int i = 0; i < spine.length(); i++) {
                long[] value = spine.get(i);
                sizeInBytes.addAndGet(value.length + 4); // 4 bytes = integer length of the array of array length.
            }
        });
        return sizeInBytes.get();
    }


    protected AtomicReferenceArray readSpine(int spineIndex) {
        Optional> optionalSpine = this.intLongArrayStore.get(spineIndex);
        if (optionalSpine.isPresent()) {
            return optionalSpine.get();
        }
        return new AtomicReferenceArray<>(spineSize);
    }

    public boolean write() {

        try {
            fileSemaphore.acquireUninterruptibly();
            this.intLongArrayStore.writeSpineCount(getSpineCount());
            return forEachChangedSpine((AtomicReferenceArray spine, int spineIndex) -> {
                this.intLongArrayStore.put(spineIndex, spine);
            });
        } finally {
            fileSemaphore.release();
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy