LevelDB.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.yiidata.flume.cache;
  2. import com.yiidata.flume.cache.serde.Serializer;
  3. import com.yiidata.flume.cache.serde.StringSerializer;
  4. import com.yiidata.flume.support.FlumeConfig;
  5. import lombok.NonNull;
  6. import org.iq80.leveldb.DB;
  7. import org.iq80.leveldb.Options;
  8. import org.iq80.leveldb.impl.Iq80DBFactory;
  9. import java.io.Closeable;
  10. import java.io.File;
  11. import java.io.IOException;
  12. /**
  13. *
  14. * Level DB Cache 实现
  15. *
  16. * <pre>
  17. *
  18. * Created by zhenqin.
  19. * User: zhenqin
  20. * Date: 2021/7/20
  21. * Time: 14:46
  22. * Vendor: yiidata.com
  23. *
  24. * </pre>
  25. *
  26. * @author zhenqin
  27. */
  28. public class LevelDB implements ICache<String>, Closeable {
  29. /**
  30. * level db
  31. */
  32. final DB db;
  33. /**
  34. * KEY 序列化
  35. */
  36. private final Serializer<String> KEY_SERDE = new StringSerializer();
  37. public LevelDB() {
  38. final File levelDbPath = new File(FlumeConfig.getAppHome(), "db");
  39. if(!levelDbPath.exists()) {
  40. levelDbPath.mkdirs();
  41. }
  42. try {
  43. final Options options = new Options();
  44. options.createIfMissing(true).errorIfExists(false);
  45. this.db = new Iq80DBFactory().open(levelDbPath, options);
  46. } catch (Exception e) {
  47. throw new IllegalStateException(e);
  48. }
  49. }
  50. @Override
  51. public void add(@NonNull String key, @NonNull String value) {
  52. db.put(KEY_SERDE.serialize(key), KEY_SERDE.serialize(value));
  53. }
  54. @Override
  55. public void add(String key, int exp, String value) {
  56. add(key, value);
  57. }
  58. @Override
  59. public String get(@NonNull String key) {
  60. return KEY_SERDE.deserialize(db.get(KEY_SERDE.serialize(key)));
  61. }
  62. @Override
  63. public String remove(@NonNull String key) {
  64. db.delete(KEY_SERDE.serialize(key));
  65. return null;
  66. }
  67. @Override
  68. public int removeByPrefix(String prefix) {
  69. return 0;
  70. }
  71. @Override
  72. public void clear() {
  73. }
  74. @Override
  75. public int size() {
  76. return 0;
  77. }
  78. @Override
  79. public void shutdown() {
  80. try {
  81. close();
  82. } catch (IOException ignore) {}
  83. }
  84. @Override
  85. public void close() throws IOException {
  86. db.close();
  87. }
  88. public static void main(String[] args) {
  89. final LevelDB levelDB = new LevelDB();
  90. /*
  91. for (int i = 0; i < 100000; i++) {
  92. levelDB.add("hello" + i, "world, " + i);
  93. }*/
  94. long start = System.currentTimeMillis();
  95. for (int i = 0; i < 100; i++) {
  96. final String va = levelDB.get("hello" + i);
  97. System.out.println(va);
  98. }
  99. System.out.println(System.currentTimeMillis() - start);
  100. /*
  101. start = System.currentTimeMillis();
  102. for (int i = 0; i < 100000; i++) {
  103. levelDB.get("hello" + i);
  104. }
  105. System.out.println(System.currentTimeMillis() - start);*/
  106. levelDB.shutdown();
  107. }
  108. }