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

org.bdware.doip.sbyterepo.AutoSplitByteArrayStorage Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.sbyterepo;

import org.bdware.doip.RocksDBUtil;

import java.io.ByteArrayOutputStream;

public class AutoSplitByteArrayStorage {
    RocksDBUtil doTable;

    public AutoSplitByteArrayStorage(RocksDBUtil table) {
        doTable = table;
    }

    public byte[] readDOBytes(String doid, long offset, long length) {
        //split to 1024*1024 trunc
        long offsetPos = offset / (1024L * 1024L);
        ByteArrayOutputStream bo = new ByteArrayOutputStream();
        int readPos = (int) (offset - offsetPos * 1024L * 1024L);
        int readedCount = 0;
        for (long i = offsetPos; i * 1024L * 1024L <= offset + length; i++) {
            String key = doid + "/" + i;
            byte[] data = doTable.getAsByte(key);
            if (data == null || data.length == 0) {
                break;
            }
            int dataleft = data.length - readPos;
            int len = dataleft;
            if (dataleft >= length - readedCount) {
                len = (int) (length - readedCount);
            }
            bo.write(data, readPos, len);
            readPos = 0;
            readedCount += len;
        }
        return bo.toByteArray();
    }

    final static int buffSize = 1024 * 1024;

    public void updateDOBytes(String doid, long offset, long length, byte[] data) {
        //split to 1024*1024 trunc
        int offsetPos = (int) (offset / (buffSize));
        for (long i = 0; i < offsetPos; i++) {
            String key = doid + "/" + i;
            if (doTable.getAsByte(key) == null)
                doTable.put(key, new byte[buffSize]);
        }
        int writePos = (int) (offset - offsetPos * buffSize);
        int readPos = 0;
        for (long i = offsetPos; i * buffSize < offset + length; i++) {
            String key = doid + "/" + i;
            byte[] toUpdate = doTable.getAsByte(key);
            if (toUpdate == null) {
                if (length - readPos < buffSize)
                    toUpdate = new byte[(int) (length - readPos)];
                else
                    toUpdate = new byte[buffSize];
            }
            //try to append
            if (toUpdate.length < buffSize) {
                //space is not enough
                if (toUpdate.length - writePos < length - readPos) {
                    byte[] appended;
                    if (length - readPos < 1024 * 1024 - writePos) {
                        appended = new byte[(int) (writePos + length - readPos)];
                    } else {
                        appended = new byte[1024 * 1024];
                    }
                    System.arraycopy(toUpdate, 0, appended, 0, toUpdate.length);
                    toUpdate = appended;
                }
            }
            int space = toUpdate.length - writePos;
            int len;
            if (space >= length - readPos) {
                len = (int) (length - readPos);
            } else {
                len = space;
            }
            System.arraycopy(data, readPos, toUpdate, writePos, len);
            writePos = 0;
            readPos += len;
            doTable.put(key, toUpdate);
        }
    }

    public void delete(String doid) {
        try {
            for (int i = 0; ; i++) {
                String key = doid + "/" + i;
                byte[] data = doTable.getAsByte(key);
                if (data != null && data.length > 0)
                    doTable.delete(key);
                else break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean has(String doid) {
        try {
            byte[] data = doTable.getAsByte(doid + "/" + 0);
            return data != null && data.length > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public long size(String doid) {
        try {
            long size = 0;
            for (int i = 0; ; i++) {
                byte[] data = doTable.getAsByte(doid + "/" + i);
                if (data != null && data.length > 0) {
                    size += data.length;
                } else {
                    break;
                }
            }
            if (size == 0) return -1;
            return size;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy