1 21 package com.db4o.inside.freespace; 22 23 import com.db4o.*; 24 25 public abstract class FreespaceManager { 26 27 final YapFile _file; 28 29 public static final byte FM_DEFAULT = 0; 30 public static final byte FM_LEGACY_RAM = 1; 31 public static final byte FM_RAM = 2; 32 public static final byte FM_IX = 3; 33 public static final byte FM_DEBUG = 4; 34 35 private static final int INTS_IN_SLOT = 12; 36 37 public FreespaceManager(YapFile file){ 38 _file = file; 39 } 40 41 public static byte checkType(byte systemType){ 42 if(systemType == FM_DEFAULT){ 43 return FM_RAM; 44 } 45 return systemType; 46 } 47 48 public static FreespaceManager createNew(YapFile file){ 49 return createNew(file, file.systemData().freespaceSystem()); 50 } 51 52 public abstract void onNew(YapFile file); 53 54 public static FreespaceManager createNew(YapFile file, byte systemType){ 55 systemType = checkType(systemType); 56 switch(systemType){ 57 case FM_IX: 58 return new FreespaceManagerIx(file); 59 default: 60 return new FreespaceManagerRam(file); 61 62 } 63 } 64 65 public static int initSlot(YapFile file){ 66 int address = file.getSlot(slotLength()); 67 slotEntryToZeroes(file, address); 68 return address; 69 } 70 71 static void slotEntryToZeroes(YapFile file, int address){ 72 YapWriter writer = new YapWriter(file.getSystemTransaction(), address, slotLength()); 73 for (int i = 0; i < INTS_IN_SLOT; i++) { 74 writer.writeInt(0); 75 } 76 if (Debug.xbytes) { 77 writer.setID(YapConst.IGNORE_ID); } 79 writer.writeEncrypt(); 80 } 81 82 83 final static int slotLength(){ 84 return YapConst.INT_LENGTH * INTS_IN_SLOT; 85 } 86 87 public abstract void beginCommit(); 88 89 final int blockSize(){ 90 return _file.blockSize(); 91 } 92 93 public abstract void debug(); 94 95 final int discardLimit(){ 96 return _file.configImpl().discardFreeSpace(); 97 } 98 99 public abstract void endCommit(); 100 101 public abstract int entryCount(); 102 103 public abstract void free(int a_address, int a_length); 104 105 public abstract int freeSize(); 106 107 public abstract void freeSelf(); 108 109 public abstract int getSlot(int length); 110 111 public abstract void migrate(FreespaceManager newFM); 112 113 public abstract void read(int freeSlotsID); 114 115 public abstract void start(int slotAddress); 116 117 public abstract byte systemType(); 118 119 public abstract int write(boolean shuttingDown); 120 121 public boolean requiresMigration(byte configuredSystem, byte readSystem) { 122 return (configuredSystem != 0 || readSystem == FM_LEGACY_RAM ) && (systemType() != configuredSystem); 123 } 124 125 public static void migrate(FreespaceManager oldFM, FreespaceManager newFM) { 126 oldFM.migrate(newFM); 127 oldFM.freeSelf(); 128 newFM.beginCommit(); 129 newFM.endCommit(); 130 } 131 132 } 133 | Popular Tags |