/** * get the region with a int ip address with memory binary search algorithm * * @param ip * @ */ public DataBlock memorySearch(long ip) { int blen = IndexBlock.getIndexBlockLength(); if (dbBinStr == null) { dbBinStr = new byte[(int)raf.length()]; raf.seek(0L); raf.readFully(dbBinStr, 0, dbBinStr.Length); //initialize the global vars firstIndexPtr = Util.getIntLong(dbBinStr, 0); lastIndexPtr = Util.getIntLong(dbBinStr, 4); totalIndexBlocks = (int)((lastIndexPtr - firstIndexPtr) / blen) + 1; } //search the index blocks to define the data int l = 0, h = totalIndexBlocks; long sip, eip, dataptr = 0; while (l <= h) { int m = (l + h) >> 1; int p = (int)(firstIndexPtr + m * blen); sip = Util.getIntLong(dbBinStr, p); if (ip < sip) { h = m - 1; } else { eip = Util.getIntLong(dbBinStr, p + 4); if (ip > eip) { l = m + 1; } else { dataptr = Util.getIntLong(dbBinStr, p + 8); break; } } } //not matched if (dataptr == 0) { return(null); } //get the data int dataLen = (int)((dataptr >> 24) & 0xFF); int dataPtr = (int)((dataptr & 0x00FFFFFF)); int city_id = (int)Util.getIntLong(dbBinStr, dataPtr); //String region = new String(dbBinStr, dataPtr + 4, dataLen - 4, "UTF-8"); var region = Encoding.UTF8.GetString(dbBinStr, dataPtr + 4, dataLen - 4); return(new DataBlock(city_id, region, dataPtr)); }
public RegionFile(File path) { _offsets = new int[SectorInts]; _chunkTimeStamps = new int[SectorInts]; _fileName = path; _sizeDelta = 0; try { if (path.exists()) { LastModified = path.lastModified(); } _file = new RandomAccessFile(path, "rw"); if (_file.length() < SectorBytes) { // we need to write the chunk offset table for (int i = 0; i < SectorInts; ++i) { _file.writeInt(0); } // write another sector for the timestamp info for (int i = 0; i < SectorInts; ++i) { _file.writeInt(0); } _sizeDelta += SectorBytes * 2; } if ((_file.length() & 0xfff) != 0) { // the file size is not a multiple of 4KB, grow it for (int i = 0; i < (_file.length() & 0xfff); ++i) { _file.write(0); } } // set up the available sector map int nSectors = (int)_file.length() / SectorBytes; _sectorFree = new List <bool>(nSectors); for (int i = 0; i < nSectors; ++i) { _sectorFree.Add(true); } _sectorFree[0] = false; // chunk offset table _sectorFree[1] = false; // for the last modified info _file.seek(0); for (int i = 0; i < SectorInts; ++i) { int offset = _file.readInt(); _offsets[i] = offset; if (offset == 0 || (offset >> 8) + (offset & 0xFF) > _sectorFree.Count) { continue; } for (int sectorNum = 0; sectorNum < (offset & 0xFF); ++sectorNum) { _sectorFree[(offset >> 8) + sectorNum] = false; } } for (int i = 0; i < SectorInts; i++) { int lastModValue = _file.readInt(); _chunkTimeStamps[i] = lastModValue; } } catch (IOException e) { Console.WriteLine(e.ToString()); } }
protected virtual void Write(int x, int z, byte[] data, int length) { try { int offset = GetOffset(x, z); int sectorNumber = offset >> 8; int sectorsAllocated = offset & 0xFF; int sectorsNeeded = (length + ChunkHeaderSize) / SectorBytes + 1; // maximum chunk size is 1MB if (sectorsNeeded >= 256) { return; } if (sectorNumber != 0 && sectorsAllocated == sectorsNeeded) { // we can simply overwrite the old sectors Write(sectorNumber, data, length); } else { // we need to allocate new sectors // mark the sectors previously used for this chunk as free for (int i = 0; i < sectorsAllocated; ++i) { _sectorFree[sectorNumber + i] = true; } // scan for a free space large enough to store this chunk int runStart = _sectorFree.IndexOf(true); int runLength = 0; if (runStart != -1) { for (int i = runStart; i < _sectorFree.Count; ++i) { if (runLength != 0) { if (_sectorFree[i]) { runLength++; } else { runLength = 0; } } else if (_sectorFree[i]) { runStart = i; runLength = 1; } if (runLength >= sectorsNeeded) { break; } } } if (runLength >= sectorsNeeded) { // we found a free space large enough sectorNumber = runStart; SetOffset(x, z, (sectorNumber << 8) | sectorsNeeded); for (int i = 0; i < sectorsNeeded; ++i) { _sectorFree[sectorNumber + i] = false; } Write(sectorNumber, data, length); } else { // no free space large enough found -- we need to grow // the file _file.seek(_file.length()); sectorNumber = _sectorFree.Count; for (int i = 0; i < sectorsNeeded; ++i) { _file.write(_emptySector); _sectorFree.Add(false); } _sizeDelta += SectorBytes * sectorsNeeded; Write(sectorNumber, data, length); SetOffset(x, z, (sectorNumber << 8) | sectorsNeeded); } } SetTimestamp(x, z, (int)(JavaSystem.CurrentTimeMillis() / 1000L)); } catch (IOException e) { Console.WriteLine(e); } }
public void write(int i, int j, byte[] abyte0, int k) { try { int l = getOffset(i, j); int i1 = l >> 8; int l1 = l & 0xff; int i2 = (k + 5) / 4096 + 1; if (i2 >= 256) { return; } if (i1 != 0 && l1 == i2) { debug("SAVE", i, j, k, "rewrite"); write(i1, abyte0, k); } else { for (int j2 = 0; j2 < l1; j2++) { sectorFree.set(i1 + j2, Boolean.valueOf(true)); } int k2 = sectorFree.indexOf(Boolean.valueOf(true)); int l2 = 0; if (k2 != -1) { int i3 = k2; do { if (i3 >= sectorFree.size()) { break; } if (l2 != 0) { if (((Boolean)sectorFree.get(i3)).booleanValue()) { l2++; } else { l2 = 0; } } else if (((Boolean)sectorFree.get(i3)).booleanValue()) { k2 = i3; l2 = 1; } if (l2 >= i2) { break; } i3++; } while (true); } if (l2 >= i2) { debug("SAVE", i, j, k, "reuse"); int j1 = k2; setOffset(i, j, j1 << 8 | i2); for (int j3 = 0; j3 < i2; j3++) { sectorFree.set(j1 + j3, Boolean.valueOf(false)); } write(j1, abyte0, k); } else { debug("SAVE", i, j, k, "grow"); dataFile.seek(dataFile.length()); int k1 = sectorFree.size(); for (int k3 = 0; k3 < i2; k3++) { dataFile.write(emptySector); sectorFree.add(Boolean.valueOf(false)); } sizeDelta += 4096 * i2; write(k1, abyte0, k); setOffset(i, j, k1 << 8 | i2); } } setChunkTimestamp(i, j, (int)(java.lang.System.currentTimeMillis() / 1000L)); } catch (IOException ioexception) { ioexception.printStackTrace(); } }
public RegionFile(File file) { lastModified = 0L; fileName = file; debugln((new StringBuilder()).append("REGION LOAD ").append(fileName).toString()); sizeDelta = 0; try { if (file.exists()) { lastModified = file.lastModified(); } dataFile = new RandomAccessFile(file, "rw"); if (dataFile.length() < 4096L) { for (int i = 0; i < 1024; i++) { dataFile.writeInt(0); } for (int j = 0; j < 1024; j++) { dataFile.writeInt(0); } sizeDelta += 8192; } if ((dataFile.length() & 4095L) != 0L) { for (int k = 0; k < (dataFile.length() & 4095L); k++) { dataFile.write(0); } } int l = (int)dataFile.length() / 4096; sectorFree = new ArrayList(l); for (int i1 = 0; i1 < l; i1++) { sectorFree.add(Boolean.valueOf(true)); } sectorFree.set(0, Boolean.valueOf(false)); sectorFree.set(1, Boolean.valueOf(false)); dataFile.seek(0L); for (int j1 = 0; j1 < 1024; j1++) { int l1 = dataFile.readInt(); offsets[j1] = l1; if (l1 == 0 || (l1 >> 8) + (l1 & 0xff) > sectorFree.size()) { continue; } for (int j2 = 0; j2 < (l1 & 0xff); j2++) { sectorFree.set((l1 >> 8) + j2, Boolean.valueOf(false)); } } for (int k1 = 0; k1 < 1024; k1++) { int i2 = dataFile.readInt(); chunkTimestamps[k1] = i2; } } catch (IOException ioexception) { ioexception.printStackTrace(); } }
/** * make the Db file * * @param dbFile target output file path * @ */ public void make(String dbFile) { //check and load the gloabl region if (globalRegionFile != null) { Console.WriteLine("+-Try to load the global region data ..."); BufferedReader greader = new BufferedReader(new FileReader(globalRegionFile)); String gline = null; while ((gline = greader.readLine()) != null) { String[] p = gline.split(","); if (p.Length != 5) { continue; } //push the mapping globalRegionMap.put(p[2], Int32.Parse(p[0])); } greader.close(); Console.WriteLine("|--[Ok]"); } //alloc the header size BufferedReader reader = new BufferedReader(new FileReader(this.ipSrcFile)); RandomAccessFile raf = new RandomAccessFile(dbFile, "rw"); //init the db file initDbFile(raf); Console.WriteLine("+-Db file initialized."); //analysis main loop Console.WriteLine("+-Try to write the data blocks ... "); String line = null; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.length() == 0) { continue; } if (line.charAt(0) == '#') { continue; } //1. get the start ip int sIdx = 0, eIdx = 0; if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) { continue; } String startIp = line.substring(sIdx, eIdx); //2. get the end ip sIdx = eIdx + 1; if ((eIdx = line.indexOf('|', sIdx + 1)) == -1) { continue; } String endIp = line.substring(sIdx, eIdx); //3. get the region sIdx = eIdx + 1; String region = line.substring(sIdx); Console.WriteLine("+-Try to process item " + line); addDataBlock(raf, startIp, endIp, region); Console.WriteLine("|--[Ok]"); } Console.WriteLine("|--Data block flushed!"); Console.WriteLine("|--Data file pointer: " + raf.getFilePointer() + "\n"); //write the index bytes Console.WriteLine("+-Try to write index blocks ... "); //record the start block IndexBlock indexBlock = null; HeaderBlock hb = null; indexBlock = indexPool.getFirst(); long indexStartIp = indexBlock.getStartIp(), indexStratPtr = raf.getFilePointer(), indexEndPtr; headerPool.add(new HeaderBlock(indexStartIp, (int)(indexStratPtr))); int blockLength = IndexBlock.getIndexBlockLength(); int counter = 0, shotCounter = (dbConfig.getIndexBlockSize() / blockLength) - 1; //var indexIt = indexPool.iterator(); //while (indexIt.hasNext()) foreach (var indexIt in indexPool.iterator()) { indexBlock = indexIt; if (++counter >= shotCounter) { hb = new HeaderBlock( indexBlock.getStartIp(), (int)raf.getFilePointer() ); headerPool.add(hb); counter = 0; } //write the buffer raf.write(indexBlock.getBytes()); } //record the end block if (counter > 0) { indexBlock = indexPool.getLast(); hb = new HeaderBlock( indexBlock.getStartIp(), ((int)raf.getFilePointer()) - IndexBlock.getIndexBlockLength() ); headerPool.add(hb); } indexEndPtr = raf.getFilePointer(); Console.WriteLine("|--[Ok]"); //write the super blocks Console.WriteLine("+-Try to write the super blocks ... "); raf.seek(0L); //reset the file pointer byte[] superBuffer = new byte[8]; Util.writeIntLong(superBuffer, 0, indexStratPtr); Util.writeIntLong(superBuffer, 4, indexEndPtr - blockLength); raf.write(superBuffer); Console.WriteLine("|--[Ok]"); //write the header blocks Console.WriteLine("+-Try to write the header blocks ... "); //var headerIt = headerPool.iterator(); //while (headerIt.hasNext()) //{ // HeaderBlock headerBlock = headerIt.next(); // raf.write(headerBlock.getBytes()); //} foreach (var headerBlock in headerPool.iterator()) { raf.write(headerBlock.getBytes()); } //write the copyright and the release timestamp info Console.WriteLine("+-Try to write the copyright and release date info ... "); raf.seek(raf.length()); //Calendar cal = Calendar.getInstance(); //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); String copyright = "Created by lionsoul at " + DateTime.Now.ToString("yyyy/MM/dd"); //dateFormat.format(cal.getTime()); var timestamp = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000; raf.write((Int32)timestamp); //the unix timestamp raf.write(copyright.getBytes()); Console.WriteLine("|--[Ok]"); reader.close(); raf.close(); }