View Javadoc

1   package net.obsearch.storage.tc;
2   
3   import java.io.File;
4   import java.math.BigInteger;
5   
6   import tokyocabinet.BDB;
7   import tokyocabinet.DBM;
8   import tokyocabinet.FDB;
9   import tokyocabinet.HDB;
10  import tokyocabinet.Util;
11  
12  import net.obsearch.asserts.OBAsserts;
13  import net.obsearch.constants.OBSearchProperties;
14  import net.obsearch.exception.OBException;
15  import net.obsearch.exception.OBStorageException;
16  import net.obsearch.storage.OBStorageConfig;
17  import net.obsearch.storage.OBStore;
18  import net.obsearch.storage.OBStoreByte;
19  import net.obsearch.storage.OBStoreDouble;
20  import net.obsearch.storage.OBStoreFactory;
21  import net.obsearch.storage.OBStoreFloat;
22  import net.obsearch.storage.OBStoreInt;
23  import net.obsearch.storage.OBStoreLong;
24  import net.obsearch.storage.OBStoreShort;
25  import net.obsearch.storage.TupleBytes;
26  import net.obsearch.storage.OBStorageConfig.IndexType;
27  import net.obsearch.storage.bdb.BDBFactoryJe;
28  import net.obsearch.utils.bytes.ByteConversion;
29  
30  public class TCFactory implements OBStoreFactory {
31  	/**
32  	 * Directory where all the objects will be created.
33  	 */
34  	private String directory;
35  	
36  	public TCFactory(File directory){
37  		this.directory = directory.getAbsolutePath();
38  	}
39  
40  	@Override
41  	public void close() throws OBStorageException {
42  
43  	}
44  
45  	private DBM createDB(String name, OBStorageConfig config) throws OBStorageException, OBException{
46  		DBM db = null;
47  		String path = directory + File.separator + name;
48  		if (IndexType.HASH == config.getIndexType()  ) {
49  			HDB tdb = new HDB();
50  			OBAsserts.chkAssertStorage(tdb.tune((long) (OBSearchProperties.getLongProperty("tc.expected.db.count") * 4), OBSearchProperties.getIntProperty("tc.align.bits"), -1, HDB.TLARGE ), "Could not set the tuning parameters for the hash table: " + tdb.errmsg() );
51  			OBAsserts.chkAssertStorage(tdb.setcache(OBSearchProperties.getIntProperty("tc.cache.size")), "Could not enable cache size");
52  			OBAsserts.chkAssertStorage(tdb.setxmsiz(OBSearchProperties.getIntProperty("tc.mmap.size")), "Could not enable mmap  size");
53  			OBAsserts.chkAssertStorage(tdb.open(path, HDB.OCREAT |  HDB.OWRITER | HDB.ONOLCK), "Could not open database: " + tdb.errmsg());			
54  			db = tdb;
55  		} else if (IndexType.BTREE == config.getIndexType() || IndexType.DEFAULT == config.getIndexType()) {
56  			BDB tdb = new BDB();
57  			tdb.tune(-1, -1, OBSearchProperties.getLongProperty("tc.expected.db.count"), -1, -1, BDB.TLARGE);
58  			OBAsserts.chkAssertStorage(tdb.open(path, BDB.OCREAT |  BDB.OWRITER | BDB.ONOLCK), "Could not open database: " + tdb.errmsg() );			
59  			db = tdb;
60  		} else if(IndexType.FIXED_RECORD == config.getIndexType()){
61  			FDB tdb = new FDB();
62  			OBAsserts.chkAssert(config.getRecordSize() > 0, "Invalid record size");
63  			OBAsserts.chkAssertStorage(tdb.tune(config.getRecordSize(), OBSearchProperties.getLongProperty("tc.fdb.max.file.size")), "Could not set the tuning parameters for the fixed record device");
64  			OBAsserts.chkAssertStorage(tdb.open(path, FDB.OCREAT |  FDB.OWRITER | FDB.ONOLCK), "Could not open database: " + tdb.errmsg() );
65  			db = tdb;
66  		}else{
67  			OBAsserts.fail("Fatal error, invalid index type.");
68  		}
69  		return db;
70  	}
71  	
72  	@Override
73  	public OBStore<TupleBytes> createOBStore(String name, OBStorageConfig config)
74  			throws OBStorageException, OBException {
75  
76  		DBM db = createDB(name, config);
77  		TCOBStorageBytesArray t = new TCOBStorageBytesArray(name, db, this, config);
78  		return t;
79  	}
80  
81  	@Override
82  	public OBStoreByte createOBStoreByte(String name, OBStorageConfig config)
83  			throws OBStorageException, OBException {
84  		// TODO Auto-generated method stub
85  		return null;
86  	}
87  
88  	@Override
89  	public OBStoreDouble createOBStoreDouble(String name, OBStorageConfig config)
90  			throws OBStorageException, OBException {
91  		// TODO Auto-generated method stub
92  		return null;
93  	}
94  
95  	@Override
96  	public OBStoreFloat createOBStoreFloat(String name, OBStorageConfig config)
97  			throws OBStorageException, OBException {
98  		// TODO Auto-generated method stub
99  		return null;
100 	}
101 
102 	@Override
103 	public OBStoreInt createOBStoreInt(String name, OBStorageConfig config)
104 			throws OBStorageException, OBException {
105 		// TODO Auto-generated method stub
106 		return null;
107 	}
108 
109 	@Override
110 	public OBStoreLong createOBStoreLong(String name, OBStorageConfig config)
111 			throws OBStorageException, OBException {		
112 		DBM db = createDB(name, config);
113 		TCOBStorageLong t = new TCOBStorageLong(name, db, this, config);
114 		return t;
115 	}
116 
117 	@Override
118 	public OBStoreShort createOBStoreShort(String name, OBStorageConfig config)
119 			throws OBStorageException, OBException {
120 		// TODO Auto-generated method stub
121 		return null;
122 	}
123 
124 	@Override
125 	public BigInteger deSerializeBigInteger(byte[] value) {		
126 		return null;
127 	}
128 
129 	@Override
130 	public byte deSerializeByte(byte[] value) {
131 		return ByteConversion.bytesToByte(value);
132 	}
133 
134 	@Override
135 	public double deSerializeDouble(byte[] value) {
136 		return ByteConversion.bytesToDouble(value);
137 	}
138 
139 	@Override
140 	public float deSerializeFloat(byte[] value) {
141 		return ByteConversion.bytesToFloat(value);
142 	}
143 
144 	@Override
145 	public int deSerializeInt(byte[] value) {
146 		//return ByteConversion.bytesToInt(value);
147 		return Util.unpackint(value);
148 	}
149 
150 	@Override
151 	public long deSerializeLong(byte[] value) {
152 		//return ByteConversion.bytesToLong(value);
153 		//return BDBFactoryJe.bytesToLong(value);
154 		return Long.parseLong(new String(value));
155 	}
156 
157 	@Override
158 	public short deSerializeShort(byte[] value) {
159 		return ByteConversion.bytesToShort(value);
160 	}
161 
162 	@Override
163 	public String getFactoryLocation() {
164 		return this.directory;
165 	}
166 
167 	@Override
168 	public void removeOBStore(OBStore storage) throws OBStorageException, OBException {
169 		storage.deleteAll();
170 		storage.close();
171 	}
172 
173 	@Override
174 	public byte[] serializeBigInteger(BigInteger value) {
175 		// TODO Auto-generated method stub
176 		return null;
177 	}
178 
179 	@Override
180 	public byte[] serializeByte(byte value) {
181 		return ByteConversion.byteToBytes(value);
182 	}
183 
184 	@Override
185 	public byte[] serializeDouble(double value) {
186 		return ByteConversion.doubleToBytes(value);
187 	}
188 
189 	@Override
190 	public byte[] serializeFloat(float value) {
191 		return ByteConversion.floatToBytes(value);
192 	}
193 
194 	@Override
195 	public byte[] serializeInt(int value) {
196 		//return ByteConversion.intToBytes(value);
197 		return Util.packint(value);
198 	}
199 
200 	@Override
201 	public byte[] serializeLong(long value) {
202 		//return ByteConversion.longToBytes(value);
203 		//return BDBFactoryJe.longToBytes(value);
204 		return String.valueOf(value).getBytes();
205 	}
206 
207 	@Override
208 	public byte[] serializeShort(short value) {
209 		return ByteConversion.shortToBytes(value);
210 	}
211 
212 	@Override
213 	public Object stats() throws OBStorageException {
214 		// TODO Auto-generated method stub
215 		return null;
216 	}
217 
218 }