public void deleteRecord(int storage_id, SqlConnection conn) { lock (_sync_object) { int cache_index = 0; _Block b = _getBlockByIndex(conn, storage_id, ref cache_index); int data_offset; if (b.offsets != null) { data_offset = b.offsets[storage_id - b.first_index]; } else { data_offset = b.pending_offsets[storage_id - b.first_index]; } if (b.data != null) { b.data[data_offset - 1] = 0; } else if (b.pending_data != null) { b.pending_data[data_offset - 1] = 0; } else { throw new Exception("Internal error: cannot delete record"); } b.dirty = true; } }
private void _convertBlockToPendingBlock(_Block block) { if (block.pending_data != null) { return; } block.pending_data = new List <byte>(); if (block.data != null) { block.pending_data.AddRange(block.data); } block.data = null; block.pending_offsets = new List <int>(); if (block.offsets != null) { block.pending_offsets.AddRange(block.offsets); } block.offsets = null; block.pending_lengths = new List <short>(); if (block.lengths != null) { block.pending_lengths.AddRange(block.lengths); } block.lengths = null; }
private _Block _getBlockByIndex(SqlConnection conn, int index, ref int cache_index) { if (cache_index >= _blocks.Count || cache_index < 0) { cache_index = 0; } _validateBlock((short)cache_index, conn); _Block b = _blocks[cache_index]; if (index < b.first_index || index >= b.end_index) { // Find another block cache_index = -1; do { cache_index++; if (cache_index >= _blocks.Count) { throw new Exception( String.Format("index {0} wasn't found in the storage", index)); } _validateBlock((short)cache_index, conn); b = _blocks[cache_index]; } while (index < b.first_index || index >= b.end_index); } return(b); }
private void SpawnPoint(int index) { Vector2 position = _vertices[index]; // GameObject pointObj = Instantiate( vertexPrefab) as GameObject; // pointObj.transform.parent = transform; // pointObj.transform.localRotation = Quaternion.identity; // pointObj.transform.localPosition = position; // // _Point point = pointObj.GetComponent<_Point>(); // points.Add (point); _Vertex point = vertexPrefab.Spawn(transform.position, transform.rotation); points.Add(point); point.transform.parent = transform; point.transform.localRotation = Quaternion.identity; point.transform.localPosition = position; // GameObject blockObj = Instantiate( edgePrefab) as GameObject; // blockObj.transform.parent = transform; // // _Block block = blockObj.GetComponent<_Block>(); // blocks.Add (block); _Block block = edgePrefab.Spawn(transform.position, transform.rotation); blocks.Add(block); block.transform.parent = transform; block.SetEndsPosition(_vertices[PreIndex(index)], position); }
public void InitialPoints() { foreach (var pos in InitialVertices) { _Vertex vertex = vertexPrefab.Spawn(); vertex.transform.SetParent(transform); vertex.transform.localPosition = pos; vertex.Border = this; vertexs.Add(vertex); } Face2D face = Shape.AddPoints(vertexs.Select(v => v.Point).ToArray()); face.Name = "BorderFace"; foreach (var e in face.BasicEdges) { _Block block = blockPrefab.Spawn(); block.transform.SetParent(transform); block.Border = this; block.Edge = e; blocks.Add(block); } _lazyAllVertices.isValeChanged = true; }
public int add(byte[] data, SqlConnection conn) { lock (_sync_object) { _Block b = _getPendingBlock(data.Length, conn); // Add one byte for remove mark b.pending_data.Add(1); b.pending_offsets.Add(b.pending_data.Count); if (data.Length > short.MaxValue) { throw new Exception("Data length is to long. Unexpected."); } b.pending_lengths.Add((short)data.Length); b.pending_data.AddRange(data); total_items++; b.end_index++; b.dirty = true; return(total_items - 1); } }
public GameObject InsertPointAt(Transform edge, Vector3 position) { _Block originBlock = edge.GetComponent <_Block>(); int index = blocks.IndexOf(originBlock); _Vertex point = vertexPrefab.Spawn(); points.Insert(index, point); point.transform.parent = transform; point.transform.localRotation = Quaternion.identity; point.transform.position = position; //ition; Vector2 localPosition = point.transform.localPosition; originBlock.SetEndsPosition(localPosition, _vertices[index]); _Block block = edgePrefab.Spawn(); blocks.Insert(index, block); block.transform.parent = transform; block.SetEndsPosition(_vertices[PreIndex(index)], localPosition); _vertices.Insert(index, localPosition); return(point.gameObject); }
private bool _blockLoaded(short block_index) { if (_blocks.Count > block_index) { _Block b = _blocks[block_index]; if (b == null) { return(false); } return(true); } return(false); }
private void _convertPendingBlockToNormal(_Block block) { if (block.data != null) { return; } block.data = block.pending_data.ToArray(); block.pending_data = null; block.offsets = block.pending_offsets.ToArray(); block.pending_offsets = null; block.lengths = block.pending_lengths.ToArray(); block.lengths = null; }
public static List <_Block> GetBlocks(int did) { MySqlConnection scon = new MySqlConnection(WebConfigurationManager.ConnectionStrings["LocalMySqlServer"].ConnectionString); MySqlCommand scmd = new MySqlCommand(); scon.Open(); scmd.Connection = scon; List <_Block> blkList = new List <_Block>(); try { scmd.CommandText = "SELECT * from blocks where did=" + did; scmd.Prepare(); MySqlDataReader sdr = scmd.ExecuteReader(); if (sdr.HasRows) { while (sdr.Read()) { _Block blk = new _Block(); blk.BID = Convert.ToInt32(sdr.GetString(0)); blk.DID = Convert.ToInt32(sdr.GetString(1)); blk.BCODE = sdr.GetString(2); blk.BNAME = sdr.GetString(3); blkList.Add(blk); } } sdr.Close(); sdr.Dispose(); } catch (Exception ee) { } finally { if (scmd != null) { scmd.Dispose(); } if (scon.State == ConnectionState.Open) { scon.Dispose(); scon.Close(); } } return(blkList); }
// If length if -1 then all rest data is returned private void _getDataWithOffset(int index, int offset, int length, out byte[] dest_data, out int dest_offset, out int dest_len, SqlConnection conn, ref int cache_index) { _Block b = _getBlockByIndex(conn, index, ref cache_index); int sub_offset = index - b.first_index; if (length == -1) { if (b.lengths != null) { length = b.lengths[sub_offset] - offset; } else { length = b.pending_lengths[sub_offset] - offset; } } int data_offset; if (b.offsets != null) { data_offset = b.offsets[sub_offset]; } else { data_offset = b.pending_offsets[sub_offset]; } if (b.data != null) { dest_data = b.data; dest_len = length; dest_offset = data_offset + offset; } else { dest_data = new byte[length]; b.pending_data.CopyTo(data_offset + offset, dest_data, 0, dest_data.Length); dest_len = length; dest_offset = 0; } }
public void RemovePointAt(Transform point) { _Vertex originPoint = point.GetComponent <_Vertex>(); int index = points.IndexOf(originPoint); _Block originBlock = blocks[index]; blocks[NextIndex(index)].SetEndsPosition(_vertices[PreIndex(index)], _vertices[NextIndex(index)]); points.Remove(originPoint); blocks.Remove(originBlock); _vertices.RemoveAt(index); originBlock.Recycle(); originPoint.Recycle(); if (_vertices.Count < 1) { RecyclePoints(); } }
public void flush(SqlConnection conn) { lock (_sync_object) { for (int i = 0; i < _blocks.Count; i++) { _Block b = _blocks[i]; if (b != null) { if (b.dirty) { _flushBlock(conn, b); BingoLog.logMessage("Disposing memory for block {0}...", b.block_index); _blocks[i] = null; BingoLog.logMessage(" Done"); } } } } }
public void InsertPointAt(_Block block, Vector3 position) { //_Block block = blockObj.GetComponent<_Block>(); Edge2D edge = block.Edge; block.Recycle(); blocks.Remove(block); _Vertex vertex = vertexPrefab.Spawn(); vertex.transform.SetParent(transform); vertex.transform.position = position; vertex.Border = this; edge.MiddlePoint = vertex.Point; Edge2D e0 = new Edge2D(edge.Points[0], vertex.Point); Edge2D e1 = new Edge2D(vertex.Point, edge.Points[1]); _Block b0 = blockPrefab.Spawn(); _Block b1 = blockPrefab.Spawn(); b0.transform.SetParent(transform); b1.transform.SetParent(transform); b0.Border = this; b1.Border = this; b0.Edge = e0; b1.Edge = e1; blocks.Add(b0); blocks.Add(b1); vertexs.Add(vertex); _lazyAllVertices.isValeChanged = true; //Debug.Log("Border Vertices Count : " + Vertices.Count); }
private _Block _getPendingBlock(int data_length, SqlConnection conn) { bool need_new_block = false; if (_blocks.Count == 0) { need_new_block = true; } else { _Block last = _blocks[_blocks.Count - 1]; if (last == null) { _validateBlock((short)(_blocks.Count - 1), conn); } last = _blocks[_blocks.Count - 1]; ICollection collection = null; if (last.data != null) { collection = last.data; } else { collection = last.pending_data; } if (collection.Count + data_length > MAX_BLOCK_SIZE) { need_new_block = true; } } _Block block = null; if (need_new_block) { // Flush all dirty blocks and dispose memory flush(conn); _blocks.Add(new _Block()); block = _blocks[_blocks.Count - 1]; block.block_index = _blocks.Count - 1; if (_blocks.Count == 1) { block.first_index = 0; } else { block.first_index = total_items; } block.end_index = block.first_index; block.dirty = true; } else { block = _blocks[_blocks.Count - 1]; } _convertBlockToPendingBlock(block); return(block); }
private void _flushBlock (SqlConnection conn, _Block b) { BingoLog.logMessage("Flushing storage block {0}", b.block_index); BingoSqlUtils.ExecNonQueryNoThrow(conn, "DELETE FROM {0} WHERE id={1}", _index_data.storageTable, b.block_index); string text = String.Format(@"INSERT INTO {0} ([id], [first_index], [count], [offsets], [lengths], [data]) VALUES ({1}, {2}, {3}, @offsets, @lengths, @data)", _index_data.storageTable, b.block_index, b.first_index, b.end_index - b.first_index); using (SqlCommand command = new SqlCommand(text, conn)) { command.CommandTimeout = 3600; byte[] data; if (b.pending_data != null) data = b.pending_data.ToArray(); else data = b.data; SqlBinary binary_data = new SqlBinary(data); command.Parameters.AddWithValue("@data", binary_data); ICollection<int> offsets; if (b.pending_offsets != null) offsets = b.pending_offsets; else offsets = b.offsets; MemoryStream mem_stream = new MemoryStream(offsets.Count * 4); BinaryWriter writer = new BinaryWriter(mem_stream); foreach (int offset in offsets) writer.Write(offset); byte[] buffer = mem_stream.GetBuffer(); command.Parameters.AddWithValue("@offsets", buffer); ICollection<short> lengths; if (b.pending_lengths != null) lengths = b.pending_lengths; else lengths = b.lengths; mem_stream = new MemoryStream(offsets.Count * 2); writer = new BinaryWriter(mem_stream); foreach (short length in lengths) writer.Write(length); buffer = mem_stream.GetBuffer(); command.Parameters.AddWithValue("@lengths", buffer); command.ExecuteNonQuery(); } _convertPendingBlockToNormal(b); }
private void _validateBlock(short block_index, SqlConnection conn) { if (_blockLoaded(block_index)) { return; } lock (_sync_object) { // Double-checked locking if (_blockLoaded(block_index)) { return; } BingoTimer timer = new BingoTimer("storage.validate_block"); BingoLog.logMessage("Loading storage block {0} for table {1}...", block_index, _index_data.id.InformationName()); string text = "SELECT [data], [first_index], [offsets], [lengths], [count] from " + _index_data.storageTable + " where id = " + block_index; using (SqlCommand command = new SqlCommand(text, conn)) { command.CommandTimeout = 3600; using (SqlDataReader reader = command.ExecuteReader()) { if (!reader.Read()) { throw new Exception("Block cannot be found"); } while (_blocks.Count <= block_index) { _blocks.Add(new _Block()); } if (_blocks[block_index] == null) { _blocks[block_index] = new _Block(); } _Block block = _blocks[block_index]; block.block_index = block_index; block.data = (byte[])reader["data"]; block.first_index = (int)reader["first_index"]; int count = (int)reader["count"]; block.offsets = new int[count]; MemoryStream mem_stream = new MemoryStream((byte[])reader["offsets"]); BinaryReader bin_reader = new BinaryReader(mem_stream); for (int i = 0; i < count; i++) { block.offsets[i] = bin_reader.ReadInt32(); } block.lengths = new short[count]; mem_stream = new MemoryStream((byte[])reader["lengths"]); bin_reader = new BinaryReader(mem_stream); for (int i = 0; i < count; i++) { block.lengths[i] = bin_reader.ReadInt16(); } block.end_index = block.first_index + count; } } BingoLog.logMessage(" Done."); timer.end(); } }
private void _convertBlockToPendingBlock (_Block block) { if (block.pending_data != null) return; block.pending_data = new List<byte>(); if (block.data != null) block.pending_data.AddRange(block.data); block.data = null; block.pending_offsets = new List<int>(); if (block.offsets != null) block.pending_offsets.AddRange(block.offsets); block.offsets = null; block.pending_lengths = new List<short>(); if (block.lengths != null) block.pending_lengths.AddRange(block.lengths); block.lengths = null; }
private void _convertPendingBlockToNormal (_Block block) { if (block.data != null) return; block.data = block.pending_data.ToArray(); block.pending_data = null; block.offsets = block.pending_offsets.ToArray(); block.pending_offsets = null; block.lengths = block.pending_lengths.ToArray(); block.lengths = null; }
private void _validateBlock (short block_index, SqlConnection conn) { if (_blockLoaded(block_index)) return; lock (_sync_object) { // Double-checked locking if (_blockLoaded(block_index)) return; BingoTimer timer = new BingoTimer("storage.validate_block"); BingoLog.logMessage("Loading storage block {0} for table {1}...", block_index, _index_data.id.InformationName()); string text = "SELECT [data], [first_index], [offsets], [lengths], [count] from " + _index_data.storageTable + " where id = " + block_index; using (SqlCommand command = new SqlCommand(text, conn)) { command.CommandTimeout = 3600; using (SqlDataReader reader = command.ExecuteReader()) { if (!reader.Read()) throw new Exception("Block cannot be found"); while (_blocks.Count <= block_index) _blocks.Add(new _Block()); if (_blocks[block_index] == null) _blocks[block_index] = new _Block(); _Block block = _blocks[block_index]; block.block_index = block_index; block.data = (byte[])reader["data"]; block.first_index = (int)reader["first_index"]; int count = (int)reader["count"]; block.offsets = new int[count]; MemoryStream mem_stream = new MemoryStream((byte[])reader["offsets"]); BinaryReader bin_reader = new BinaryReader(mem_stream); for (int i = 0; i < count; i++) block.offsets[i] = bin_reader.ReadInt32(); block.lengths = new short[count]; mem_stream = new MemoryStream((byte[])reader["lengths"]); bin_reader = new BinaryReader(mem_stream); for (int i = 0; i < count; i++) block.lengths[i] = bin_reader.ReadInt16(); block.end_index = block.first_index + count; } } BingoLog.logMessage(" Done."); timer.end(); } }
private void _flushBlock(SqlConnection conn, _Block b) { BingoLog.logMessage("Flushing storage block {0}", b.block_index); BingoSqlUtils.ExecNonQueryNoThrow(conn, "DELETE FROM {0} WHERE id={1}", _index_data.storageTable, b.block_index); string text = String.Format(@"INSERT INTO {0} ([id], [first_index], [count], [offsets], [lengths], [data]) VALUES ({1}, {2}, {3}, @offsets, @lengths, @data)", _index_data.storageTable, b.block_index, b.first_index, b.end_index - b.first_index); using (SqlCommand command = new SqlCommand(text, conn)) { command.CommandTimeout = 3600; byte[] data; if (b.pending_data != null) { data = b.pending_data.ToArray(); } else { data = b.data; } SqlBinary binary_data = new SqlBinary(data); command.Parameters.AddWithValue("@data", binary_data); ICollection <int> offsets; if (b.pending_offsets != null) { offsets = b.pending_offsets; } else { offsets = b.offsets; } MemoryStream mem_stream = new MemoryStream(offsets.Count * 4); BinaryWriter writer = new BinaryWriter(mem_stream); foreach (int offset in offsets) { writer.Write(offset); } byte[] buffer = mem_stream.GetBuffer(); command.Parameters.AddWithValue("@offsets", buffer); ICollection <short> lengths; if (b.pending_lengths != null) { lengths = b.pending_lengths; } else { lengths = b.lengths; } mem_stream = new MemoryStream(offsets.Count * 2); writer = new BinaryWriter(mem_stream); foreach (short length in lengths) { writer.Write(length); } buffer = mem_stream.GetBuffer(); command.Parameters.AddWithValue("@lengths", buffer); command.ExecuteNonQuery(); } _convertPendingBlockToNormal(b); }