Created
April 7, 2012 23:02
-
-
Save hackaugusto/2332714 to your computer and use it in GitHub Desktop.
beautiful, dont you think?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package recovery; | |
| import java.util.ArrayList; | |
| import javax.swing.JOptionPane; | |
| public class Main { | |
| public static Janela janela; | |
| static ArrayList<Transacao> transacoes = new ArrayList(); | |
| public static final int tamanhoHd = 20; | |
| public static final int tamanhoRam = 5; | |
| public static final int qtd_thread = 4; | |
| static String[] bufferHd = new String[tamanhoHd]; | |
| static String[] bufferRam = new String[tamanhoRam]; | |
| static boolean continuar = true; | |
| public static void main(String[] args) { | |
| janela = new Janela(); | |
| janela.setVisible(true); | |
| //janela.setResizable(false); | |
| rodar(); | |
| } | |
| public static void teste(){ | |
| // Teste adicionarTransacao | |
| ArrayList t = new ArrayList(); | |
| for(int i=0;i<5;i++) | |
| t.add(i); | |
| janela.atualizarLog("Gerando Listas..."); | |
| for(int i=0; i<t.size();i++){ | |
| if(continuar){ | |
| //adicionarTransacao(""+t.get(i)); | |
| try{Thread.sleep(500);}catch(Exception e){} | |
| adicionarBufferRam(""+t.get(i),i); | |
| try{Thread.sleep(200);}catch(Exception e){} | |
| adicionarBufferHd(""+t.get(i),i); | |
| try{Thread.sleep(200);}catch(Exception e){} | |
| } | |
| } | |
| janela.atualizarLog("Removendo os 2..."); | |
| try{Thread.sleep(700);}catch(Exception e){} | |
| // Teste removerTransacao | |
| //removerTransacao(2); | |
| try{Thread.sleep(700);}catch(Exception e){} | |
| // Teste removerBufferRam | |
| removerBufferRam(2); | |
| try{Thread.sleep(700);}catch(Exception e){} | |
| // Teste removerBufferHd | |
| removerBufferHd(2); | |
| janela.atualizarLog("Fim!"); | |
| } | |
| public static void rodar(){ | |
| int qtdtransacoesrodadas = 0; | |
| Transacao t; | |
| Main.iniciarBufferHd(); | |
| while( continuar ){ | |
| // roda alguma das transacoes | |
| if( Math.random() < ((double)transacoes.size() / (double)qtd_thread) ){ | |
| int sel = (int)Math.random() * transacoes.size(); | |
| t = transacoes.get(sel); | |
| janela.atualizarLog(t.getOperations()); | |
| if( t.operations == 0 ){ | |
| janela.atualizarLog("<commit " + t.name + " >"); | |
| t.baixar(); | |
| transacoes.remove(sel); | |
| Main.atualizarInterfaceTransacao(); | |
| } | |
| } | |
| // cria uma nova transacao | |
| else{ | |
| t = geraTransacao("t" + qtdtransacoesrodadas++); | |
| transacoes.add(t); | |
| Main.atualizarInterfaceTransacao(); | |
| } | |
| try{Thread.sleep(4000);}catch(Exception e){} | |
| } | |
| String log = janela.getLog(); | |
| String[] logArray = log.split("\n"); | |
| String listaRedo = ""; | |
| for (int i = 0; i < (logArray.length)-1; i++){ // -1 pq a ultima String eh a "falha!" | |
| if (logArray[i].charAt(1) == 'c') // se for um commit | |
| listaRedo += logArray[i].substring(8, logArray[i].indexOf(' ', 8))+"\n"; // adiciona a transacao na lista redo | |
| } | |
| JOptionPane.showMessageDialog(null, "Lista Redo:\n\n"+listaRedo); | |
| } | |
| // Gerencia dos jTA`s da inferface | |
| // Buffers: | |
| // precisa ter tamanho definido, retornar qtdade de buffers usados | |
| // adicionar e remover buffer no index | |
| public static void atualizarInterfaceTransacao(){ | |
| String str = ""; | |
| for(int i=0; i < Main.transacoes.size(); i++){ | |
| str += Main.transacoes.get(i).name+"\n"; | |
| } | |
| janela.atualizarTransacao(str); | |
| } | |
| public static void adicionarBufferRam(String str, int index){ | |
| Main.bufferRam[index] = str; | |
| String novo = ""; | |
| for(int i=0; i < Main.bufferRam.length; i++) | |
| novo += Main.bufferRam[i]+"\n"; | |
| janela.atualizarBufferRam(novo); | |
| } | |
| public static int tamanhoBufferRam(){ | |
| return bufferRam.length; | |
| } | |
| public static int tamanhoBufferHd(){ | |
| return bufferHd.length; | |
| } | |
| /* | |
| public static int tamanhoBufferRamUsado(){ | |
| int tam =0; | |
| for(int i=0; i < Main.bufferRam.length;i++) | |
| if(Main.bufferRam[i] != null) | |
| tam++; | |
| return tam; | |
| } | |
| */ | |
| public static int primeiroBufferRamVazio(){ | |
| int index = Main.bufferRam.length+1; | |
| for(int i = Main.bufferRam.length-1; i >= 0; i--) | |
| if(Main.bufferRam[i] == null) | |
| index = i; | |
| return index; | |
| } | |
| public static void adicionarBufferHd(String str, int index){ | |
| Main.bufferHd[index] = str; | |
| String novo = ""; | |
| for(int i=0; i < Main.bufferHd.length; i++) | |
| novo += Main.bufferHd[i]+"\n"; | |
| janela.atualizarBufferHd(novo); | |
| } | |
| public static void removerBufferHd(int index){ | |
| Main.adicionarBufferHd(null,index); | |
| } | |
| public static void removerBufferRam(int index){ | |
| Main.adicionarBufferRam(null,index); | |
| } | |
| public static void iniciarBufferHd(){ | |
| String buf = ""; | |
| for(int i=0; i < Main.bufferHd.length; i++){ | |
| buf = ""+(int)(Math.random()*999); | |
| Main.bufferHd[i] = buf; | |
| adicionarBufferHd(buf, i); | |
| } | |
| } | |
| public static void tratarFalha(){ | |
| Main.continuar = false; | |
| janela.atualizarLog("falhou!"); | |
| } | |
| public static void falha(){ | |
| tratarFalha(); | |
| } | |
| public static Transacao geraTransacao(String name){ | |
| return new Transacao((int)(Math.random() * 3)+1,name, bufferRam,bufferHd); | |
| } | |
| public static class Transacao { | |
| public int operations; | |
| public String name; | |
| public String[] bufferRam; | |
| public String[] bufferHd; | |
| public ArrayList<Integer> buffersHdLidos = new ArrayList(); | |
| public ArrayList<Integer> buffersRamUsados = new ArrayList(); | |
| public ArrayList<Integer> buffersHdEscritos = new ArrayList(); | |
| public Transacao(int o,String name,String[] memoria, String[] hd){ | |
| this.operations = o; | |
| this.name = name; | |
| this.bufferRam = memoria; | |
| this.bufferHd = hd; | |
| janela.atualizarLog("<start " + name + " >"); | |
| } | |
| public String getOperations(){ | |
| // precisa testar em Main tambem para remover a transacao da lista de transacoes e para adicionar ela no redo | |
| if(operations==0){ | |
| return ""; | |
| } | |
| int posBufferHd; | |
| // escolhe um buffer randomico do hd para usar na operacao | |
| do{ | |
| posBufferHd = ((int)(Math.random() * (double)Main.tamanhoBufferHd() )); | |
| }while( buffersHdEscritos.contains(posBufferHd)); // nao usar de novo um buffer que ja foi escrito, meio ilogico | |
| int posRam; | |
| // checa para ver se ja temos buffer em ram para isso | |
| if( !buffersHdLidos.contains(posBufferHd) ){ | |
| if( Main.tamanhoBufferRam() < Main.primeiroBufferRamVazio() ){ | |
| return ""; // transa��o esta parada por falta de buffer | |
| } | |
| buffersHdLidos.add(posBufferHd); | |
| posRam = Main.primeiroBufferRamVazio(); | |
| buffersRamUsados.add(posRam); | |
| }else{ | |
| int i = buffersHdLidos.indexOf(posBufferHd); | |
| posRam = buffersRamUsados.get(i); | |
| } | |
| String el = Main.bufferHd[posBufferHd]; | |
| operations--; | |
| // ler | |
| if( Math.floor(Math.random() + 0.5d) == 0d && !buffersHdEscritos.contains(posBufferHd)){ | |
| Main.adicionarBufferRam(el, posRam); | |
| return "<read " + name + " " + posBufferHd + " " + el + ">"; | |
| // escrever | |
| }else{ | |
| int newvalue = (int)(Math.random()*999); | |
| buffersHdEscritos.add(posBufferHd); | |
| Main.adicionarBufferRam(newvalue+"", posRam); | |
| return "<write " + name + " " + posBufferHd + " " + el + " " + newvalue + ">"; | |
| } | |
| } | |
| public void baixar(){ | |
| int pos; | |
| for( int i : buffersHdEscritos ){ | |
| pos = buffersHdLidos.indexOf(i); | |
| pos = buffersRamUsados.get(pos); | |
| Main.adicionarBufferHd(Main.bufferRam[pos], i); | |
| Main.removerBufferRam(pos); | |
| } | |
| this.limparRam(); | |
| } | |
| public void limparRam(){ | |
| int pos; | |
| for( int i : buffersHdLidos ){ | |
| pos = buffersHdLidos.indexOf(i); | |
| pos = buffersRamUsados.get(pos); | |
| Main.removerBufferRam(pos); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment