📄 datafilemanager.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Core License version 1 published by ozone-db.org.//// Copyright (C) 2003-@year@, Leo Mekenkamp. All rights reserved.//// $Id: DataFileManager.java,v 1.1.2.1 2004/04/10 10:06:51 per_nyfelt Exp $package org.ozoneDB.core.storage.gammaStore;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.util.Iterator;import java.util.LinkedHashMap;import java.util.Map;import java.util.Set;import org.ozoneDB.OzoneInternalException;/** * * @author leo */public class DataFileManager { private static final String DATAFILEPREFIX = ""; private static final String DATAFILEPOSTFIX = ".datafile"; /** * <p>Used to name indexfiles. We could take any value from 2 to * <code>Character.MAX_RADIX</code> (36) here, but we settle for 16 because * it looks nice and nerdy. 36 would look less nice because that would * inevitably lead to filenames with very nasty words in them, like * <code>fuck.datafile</code> and <code>microsoft.datafile</code>.</p> * <p>Before you know it, someone sues you for trademark infringement.</p> * <p>Insiders joke: CAFEBABE is not a trademark, is she?</p> */ private static final int NAMECONVERTRADIX = 16; private LinkedHashMap dataFiles = new LinkedHashMap() { public boolean removeEldestEntry(Map.Entry entry) { if (size() > getMaxOpenDataFiles()) { Storage dataFile = (Storage) entry.getValue(); try { dataFile.close(); } catch (IOException e) { throw new OzoneInternalException(e); } } return false; } }; private int maxOpenDataFiles; private StorageFactory dataFileFactory; private ObjectStreamClasses objectStreamClasses; public DataFileManager() { } public void setObjectStreamClasses(ObjectStreamClasses objectStreamClasses) { this.objectStreamClasses = objectStreamClasses; } public ObjectStreamClasses getObjectStreamClasses() { return objectStreamClasses; } private Map getDataFiles() { return dataFiles; } private static String dataFileIdToStorageName(int dataFileId) { return DATAFILEPREFIX + Integer.toString(dataFileId, NAMECONVERTRADIX) + DATAFILEPOSTFIX; } private Storage getDataFile(int dataFileId) { Storage result = (Storage) getDataFiles().get(new Integer(dataFileId)); if (result == null) { try { result = dataFileFactory.createStorage(dataFileIdToStorageName(dataFileId)); } catch (IOException e) { throw new OzoneInternalException(e); } dataFiles.put(new Integer(dataFileId), result); } return result; } public GammaContainer getContainer(ContainerLocation containerLocation) throws ClassNotFoundException { try { Storage dataFile = getDataFile(containerLocation.getDataFileId()); dataFile.seek(containerLocation.getPosition()); long id = dataFile.readLong(); int size = dataFile.readInt(); byte[] buf = new byte[size]; dataFile.readFully(buf); GammaObjectInputStream in = new GammaObjectInputStream(new ByteArrayInputStream(buf), getObjectStreamClasses()); GammaContainer result = (GammaContainer) in.readObject(); return result; } catch (IOException e) { throw new OzoneInternalException(e); } } public void saveContainer(ContainerLocation containerLocation, GammaContainer container, int freeSize) { try { Storage dataFile = getDataFile(containerLocation.getDataFileId()); dataFile.seek(containerLocation.getPosition()); dataFile.writeLong(container.getObjectId().value()); dataFile.writeInt(freeSize); ByteArrayOutputStream buf = new ByteArrayOutputStream(); GammaObjectOutputStream out = new GammaObjectOutputStream(buf, getObjectStreamClasses()); out.writeObject(container); dataFile.write(buf.toByteArray()); } catch (IOException e) { throw new OzoneInternalException(e); } } public int getMaxOpenDataFiles() { return maxOpenDataFiles; } public void setMaxOpenDataFiles(int maxOpenDataFiles) { this.maxOpenDataFiles = maxOpenDataFiles; if (getDataFiles().size() > getMaxOpenDataFiles()) { Iterator i = getDataFiles().values().iterator(); for (int cnt = 0; cnt < getDataFiles().size(); cnt++) { i.next(); } while (i.hasNext()) { Storage dataFile = (Storage) i.next(); try { dataFile.close(); } catch (IOException e) { throw new OzoneInternalException(e); } i.remove(); } } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -