Пример #1
0
  public void save() {
    boolean isNoticed = false;
    if (!notice.isVisible()) {
      isNoticed = true;
      notice.setVisible(true);
      notice.setText("Saving...");
    }
    if (getOptions() != null) {
      DB database = null;
      try {
        database = factory.open(this.leveldbStore.getSelectedFile(), getOptions());
        DBIterator iterator = database.iterator();
        HashSet<byte[]> keys = new HashSet<>();

        for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
          keys.add(iterator.peekNext().getKey());
        }
        iterator.close();

        for (byte[] key : keys) {
          database.delete(key);
        }

        for (int i = 0; i < dataList.getModel().getSize(); ++i) {
          DBItem item = dataList.getModel().getElementAt(i);
          database.put(item.key, item.value);
        }
      } catch (Exception e) {
        JOptionPane.showMessageDialog(pane, "Unable to open database:\n" + e);
        e.printStackTrace();
      } finally {
        if (database != null) {
          try {
            database.close();
          } catch (IOException e) {
            JOptionPane.showMessageDialog(pane, "Unable to close database:\n" + e);
            e.printStackTrace();
          }
        }
        saveButton.setEnabled(false);
      }
    }
    if (isNoticed) {
      try {
        Thread.sleep(100);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      notice.setVisible(false);
      notice.setText("");
    }
  }
Пример #2
0
 public void put(byte[] key, byte[] value) {
   if (getOptions() != null) {
     DB database = null;
     try {
       database = factory.open(this.leveldbStore.getSelectedFile(), getOptions());
       database.put(key, value);
     } catch (Exception e) {
       JOptionPane.showMessageDialog(pane, "Unable to open database:\n" + e);
       e.printStackTrace();
     } finally {
       if (database != null) {
         try {
           database.close();
         } catch (IOException e) {
           JOptionPane.showMessageDialog(pane, "Unable to close database:\n" + e);
           e.printStackTrace();
         }
       }
     }
   }
 }