/// <summary> /// Removing an index from index.dw and AllIndexes /// </summary> /// <param name="idx">Index</param> public void RemoveIndex( Index idx ) { if ( idx != null ) RemoveIndex( idx.DwarfHashCode ); }
/// <summary> /// Finds a chunk with index space, /// that can contain an Index "idx" /// </summary> /// <param name="idx">Index</param> /// <returns>filepath to chunk</returns> private List<string> FindChunkFilesForRecord( Index idx ) { return FindChunkFilesForRecord( idx.DwarfHashCode ); }
/// <summary> /// Searching a Data Container by index /// </summary> /// <param name="dc_index">DC index</param> /// <returns></returns> public DataContainer GetDataContainer( Index dc_index ) { // TODO: search for data container in file chunks return null; }
/// <summary> /// Searching a Record by index /// </summary> /// <param name="dc_index">Record index</param> /// <returns></returns> public Record GetRecord( Index rec_index ) { // Let's find our record in chunk files... foreach (var convenient_chunk in FindChunkFilesForRecord( rec_index )) { var item = ChunkFormat.GetItem( convenient_chunk, rec_index ); if ( item != null ) return (Record)item; } return null; }
/// <summary> /// Getting an item from a chunk file in a multithread mode /// </summary> /// <param name="json_reader">JSON text reader/param> /// <param name="idx">item index</param> private static IStructure GetItemInfile( JsonTextReader json_reader, Index idx ) { InnerChunkElement inner = FindItem( json_reader, idx ); if ( inner != null ) { if ( inner.NeedToRemove == 0 ) { // If we neeed not remove this element var obj_json = inner.Contents; if ( inner.ElementType == InnerChunkElement.ElemType.DATACONTAINER ) { var ret_dc = JsonConvert.DeserializeObject<DataContainer>( obj_json ); return ret_dc; } else { var ret_rec = JsonConvert.DeserializeObject<Record>( obj_json ); return ret_rec; } } else return null; } return null; }
/// <summary> /// Receiving records a record with given index /// </summary> /// <param name="ind"></param> /// <returns></returns> public IStructure GetStructure(Index ind) { try { if (items_dict.ContainsKey(ind.DwarfHashCode)) return items_dict[ind.DwarfHashCode]; else return null; } catch (Exception ex) { Errors.Messages.DisplayError("FAILED TO GET STRUCTURE: " + ex.Message + ":" + ex.StackTrace, "", "", DateTime.Now); } return null; }
private static InnerChunkElement FindItem( JsonTextReader jr, Index idx ) { return FindItem( jr, idx.DwarfHashCode ); }
/// <summary> /// Removing an item to a chunk file in multithread mode /// </summary> /// <param name="filepath">path to file</param> /// <param name="idx">datastructure index</param> public static void RemoveItem( string filepath, Index idx ) { var buffer = new byte[MaxChunkSize]; int offset = 0; int needed_idx_pos = -1; int needed_elem_type_pos = -1; // let's use a direct method to find our element by hash index using ( var fs = File.Open( filepath, FileMode.Open )) { if ( fs.Read( buffer, offset, MaxChunkSize ) > 0 ) { string buf = System.Text.Encoding.UTF8.GetString(buffer); const string tmp_el_type ="\"ElementType\":"; string tmp_el_hash ="\"ElementHash\": \""+idx.DwarfHashCode+"\""; int str_elem_type_pos = buf.IndexOf(tmp_el_type, StringComparison.InvariantCultureIgnoreCase); int str_hash_pos = buf.IndexOf(tmp_el_hash, StringComparison.InvariantCultureIgnoreCase); if ( str_hash_pos > -1 ) needed_idx_pos = str_hash_pos+15; if ( str_elem_type_pos > -1 ) needed_elem_type_pos = str_elem_type_pos + 15; } } // let's delete a hash index for object, that we should remove if ( needed_idx_pos > -1 ) { using ( var fs = File.Open( filepath, FileMode.Open )) { var replacement = new byte[34]; fs.Position = needed_elem_type_pos; fs.Write( System.Text.Encoding.UTF8.GetBytes( 3.ToString() /* REMOVED */ ), 0, 1); fs.Position = needed_idx_pos; fs.Write( replacement, 0, 34); fs.Position -= 34; fs.Write( System.Text.Encoding.UTF8.GetBytes( "null" ), 0, 4); } } }
/// <summary> /// Getting several elements from a chunk file in multithread mode /// </summary> /// <param name="filepath">path to file</param> /// <param name="idx_arr">indexes array</param> public static IStructure[] GetItemRange( string filepath, Index[] idx_arr ) { var ret_arr = new IStructure[0]; var tmp_path = CreateTemporaryFile( filepath ); using (var json_reader = new JsonTextReader(new StreamReader(File.Open( tmp_path, FileMode.Open )))) { json_reader.SupportMultipleContent = true; foreach ( var index in idx_arr ) { var elem = GetItemInfile( json_reader, index ); if ( elem != null ) { Array.Resize( ref ret_arr, ret_arr.Length + 1 ); ret_arr[ret_arr.Length-1] = elem; } } } DeleteTemporaryFile( tmp_path ); return ret_arr; }
/// <summary> /// Getting an item from a chunk file in multithread mode /// </summary> /// <param name="filepath">path to a chunk file</param> /// <param name="idx">item index</param> public static IStructure GetItem( string filepath, Index idx ) { return GetItem( filepath, idx.DwarfHashCode ); }
/// <summary> /// Creating an index from hashcode /// </summary> /// <param name="hash"></param> /// <returns></returns> public static Index CreateFromDwarfHashCode( string hash ) { var new_idx = new Index(); new_idx.DwarfHashCode = hash; return new_idx; }