private void compact() { // 未完成的compact文件 // 检查是否有需要合并的block, 容量小于50% List<DataBlock> readonlyBlocks = dataBlockEngine.getReadonlyBlocks(); // 合并这两个block }
public boolean isFull() { return fileHeader.isFull(); }
public void removeData(StoreTxLogPosition txLogPosition, long fromIndex, int length) { // 减少存活数就可以了, 物理删除滞后 fileHeader.getAliveNum().decrementAndGet(); synchronized (this) { this.lastTxLogPosition = txLogPosition; } }
public V getValue(IndexItem<K> index) { try { DataBlock dataBlock = NAME_BLOCK_MAP.get(index.getFileId()); if (dataBlock == null) { return null; } byte[] data = dataBlock.readData(index.getFromIndex(), index.getLength()); UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(data); DataEntry<K, V> dataEntry = serializer.deserialize(is, new TypeReference<DataEntry<K, V>>() { }.getType()); return dataEntry.getValue(); } catch (Exception e) { throw new DBException("Read data error: " + e.getMessage(), e); } }
/** * 写数据 */ private DataAppendResult append(StoreTxLogPosition storeTxLogPosition, byte[] dataBytes) throws IOException { DataBlock writeBlock = getWriteDataBlock(); try { return writeBlock.append(storeTxLogPosition, dataBytes); } catch (CapacityNotEnoughException e) { if (!readonlyBlocks.contains(writeBlock)) { readonlyBlocks.add(writeBlock); } writableBlocks.remove(writeBlock); return append(storeTxLogPosition, dataBytes); } }
public DataBlock(String fileName, StoreConfig storeConfig) throws IOException { this.fileName = fileName; this.fileId = Long.parseLong(fileName.substring(0, fileName.lastIndexOf("."))); this.file = new File(storeConfig.getDataPath(), fileName); this.storeConfig = storeConfig; this.fileSize = storeConfig.getDataBlockFileSize(); this.fileHeader = new DataBlockFileHeader(); this.flushDataService = new FlushDataService(); init(); }
public static <K> IndexItem<K> convertToIndex(K key, DataAppendResult result) { IndexItem<K> index = new IndexItem<K>(); index.setKey(key); index.setFileId(result.getFileId()); index.setFromIndex(result.getFromIndex()); index.setLength(result.getLength()); return index; }
public StoreTxLogPosition getLastTxLogPosition() { return new StoreTxLogPosition(fileHeader.getStoreTxLogRecordId()); }
/** * 删除某个键值对(逻辑删除),物理删除滞后 */ public void remove(StoreTxLogPosition storeTxLogPosition, IndexItem<K> index) { DataBlock dataBlock = NAME_BLOCK_MAP.get(index.getFileId()); if (dataBlock == null) { return; } dataBlock.removeData(storeTxLogPosition, index.getFromIndex(), index.getLength()); }
public V get(K key) { // 1. 从缓存中获取 V value = dataCache.get(key); if (value != null) { return value; } IndexItem<K> indexItem = index.getIndexItem(key); if (indexItem == null) { return null; } // 2. 从Data文件读取 return dataBlockEngine.getValue(indexItem); }
public V getValue(IndexItem<K> index) { try { DataBlock dataBlock = NAME_BLOCK_MAP.get(index.getFileId()); if (dataBlock == null) { return null; } byte[] data = dataBlock.readData(index.getFromIndex(), index.getLength()); UnsafeByteArrayInputStream is = new UnsafeByteArrayInputStream(data); DataEntry<K, V> dataEntry = serializer.deserialize(is, new TypeReference<DataEntry<K, V>>() { }.getType()); return dataEntry.getValue(); } catch (Exception e) { throw new DBException("Read data error: " + e.getMessage(), e); } }
/** * 写数据 */ private DataAppendResult append(StoreTxLogPosition storeTxLogPosition, byte[] dataBytes) throws IOException { DataBlock writeBlock = getWriteDataBlock(); try { return writeBlock.append(storeTxLogPosition, dataBytes); } catch (CapacityNotEnoughException e) { if (!readonlyBlocks.contains(writeBlock)) { readonlyBlocks.add(writeBlock); } writableBlocks.remove(writeBlock); return append(storeTxLogPosition, dataBytes); } }
public DataBlock(String fileName, StoreConfig storeConfig) throws IOException { this.fileName = fileName; this.fileId = Long.parseLong(fileName.substring(0, fileName.lastIndexOf("."))); this.file = new File(storeConfig.getDataPath(), fileName); this.storeConfig = storeConfig; this.fileSize = storeConfig.getDataBlockFileSize(); this.fileHeader = new DataBlockFileHeader(); this.flushDataService = new FlushDataService(); init(); }
public static <K> IndexItem<K> convertToIndex(K key, DataAppendResult result) { IndexItem<K> index = new IndexItem<K>(); index.setKey(key); index.setFileId(result.getFileId()); index.setFromIndex(result.getFromIndex()); index.setLength(result.getLength()); return index; }
private void compact() { // 未完成的compact文件 // 检查是否有需要合并的block, 容量小于50% List<DataBlock> readonlyBlocks = dataBlockEngine.getReadonlyBlocks(); // 合并这两个block }
public boolean isFull() { return fileHeader.isFull(); }
public void removeData(StoreTxLogPosition txLogPosition, long fromIndex, int length) { // 减少存活数就可以了, 物理删除滞后 fileHeader.getAliveNum().decrementAndGet(); synchronized (this) { this.lastTxLogPosition = txLogPosition; } }
public StoreTxLogPosition getLastTxLogPosition() { return new StoreTxLogPosition(fileHeader.getStoreTxLogRecordId()); }
/** * 删除某个键值对(逻辑删除),物理删除滞后 */ public void remove(StoreTxLogPosition storeTxLogPosition, IndexItem<K> index) { DataBlock dataBlock = NAME_BLOCK_MAP.get(index.getFileId()); if (dataBlock == null) { return; } dataBlock.removeData(storeTxLogPosition, index.getFromIndex(), index.getLength()); }
public V get(K key) { // 1. 从缓存中获取 V value = dataCache.get(key); if (value != null) { return value; } IndexItem<K> indexItem = index.getIndexItem(key); if (indexItem == null) { return null; } // 2. 从Data文件读取 return dataBlockEngine.getValue(indexItem); }