Re: [PR] [core] Introduce SimpleLsmKvDb to put and get based on Paimon SST [paimon]
JingsongLi merged PR #7407: URL: https://github.com/apache/paimon/pull/7407 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
Re: [PR] [core] Introduce SimpleLsmKvDb to put and get based on Paimon SST [paimon]
JingsongLi commented on PR #7407: URL: https://github.com/apache/paimon/pull/7407#issuecomment-4044413736 I will remove all comments... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
Re: [PR] [core] Introduce SimpleLsmKvDb to put and get based on Paimon SST [paimon]
leaves12138 commented on code in PR #7407:
URL: https://github.com/apache/paimon/pull/7407#discussion_r2922569099
##
paimon-common/src/main/java/org/apache/paimon/lookup/sort/db/SimpleLsmKvDb.java:
##
@@ -0,0 +1,573 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.lookup.sort.db;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.compression.CompressOptions;
+import org.apache.paimon.io.cache.CacheManager;
+import org.apache.paimon.lookup.sort.SortLookupStoreFactory;
+import org.apache.paimon.lookup.sort.SortLookupStoreReader;
+import org.apache.paimon.lookup.sort.SortLookupStoreWriter;
+import org.apache.paimon.memory.MemorySlice;
+import org.apache.paimon.options.MemorySize;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A simple LSM-Tree based KV database built on top of {@link
SortLookupStoreFactory}.
+ *
+ * Architecture (Universal Compaction, inspired by RocksDB):
+ *
+ *
+ * ┌──┐
+ * │MemTable (SkipList)│ ← Active writes
+ * ├──┤
+ * │ Sorted Runs (newest → oldest): │
+ * │[Run-0] [Run-1] [Run-2] ... [Run-N] │ ← Each run is a
sorted SST file set
+ * └──┘
+ *
+ *
+ * Compaction is triggered when the number of sorted runs exceeds a
threshold. Runs are selected
+ * for merging based on size ratios between adjacent runs, following RocksDB's
Universal Compaction
+ * strategy.
+ *
+ * Note: No WAL is implemented. Data in the MemTable will be lost on crash.
+ *
+ * This class is not thread-safe. External synchronization is
required if accessed from
+ * multiple threads.
+ */
+public class SimpleLsmKvDb implements Closeable {
+
+private static final Logger LOG =
LoggerFactory.getLogger(SimpleLsmKvDb.class);
+
+/** Tombstone marker for deleted keys. */
+private static final byte[] TOMBSTONE = new byte[0];
+
+/** Maximum number of levels in the LSM tree. */
+static final int MAX_LEVELS = 4;
+
+/**
+ * Estimated per-entry memory overhead in the MemTable's TreeMap, beyond
the raw key/value
+ * bytes. This accounts for:
+ *
+ *
+ * TreeMap.Entry node: ~64 bytes (object header +
left/right/parent/key/value refs +
+ * color)
+ * MemorySlice wrapper: ~32 bytes (object header + segment ref +
offset + length)
+ * MemorySegment backing the key: ~48 bytes (object header +
heapMemory/offHeapBuffer refs
+ * + address + size)
+ * byte[] value array header: ~16 bytes (object header + length)
+ *
+ */
+static final long PER_ENTRY_OVERHEAD = 160;
+
+private final File dataDirectory;
+private final SortLookupStoreFactory storeFactory;
+private final Comparator keyComparator;
+private final long memTableFlushThreshold;
+private final LsmCompactor compactor;
+
+/** Active MemTable: key -> value bytes (empty byte[] = tombstone). */
+private TreeMap memTable;
+
+/** Estimated size of the current MemTable in bytes. */
+private long memTableSize;
+
+/**
+ * Multi-level SST file storage. Each level contains a list of {@link
SstFileMetadata} ordered
+ * by key range. Level 0 files are ordered newest-first (key ranges may
overlap). Level 1+ files
+ * are ordered by minKey (key ranges do NOT overlap).
+ */
+private final List> levels;
+
+/** Cached readers for SST files, keyed by file path. Lazily populated on
first lookup. */
+private final Map readerCache;
+
+private long fileSequence;
+private boolean closed;
+
+private SimpleLsmKvDb(
+File dataDirectory,
+SortLookupStoreFactory storeFactory,
+Comparator keyComparator,
+long memTableFlushThreshold,
+
Re: [PR] [core] Introduce SimpleLsmKvDb to put and get based on Paimon SST [paimon]
leaves12138 commented on code in PR #7407:
URL: https://github.com/apache/paimon/pull/7407#discussion_r2922568558
##
paimon-common/src/main/java/org/apache/paimon/lookup/sort/db/SimpleLsmKvDb.java:
##
@@ -0,0 +1,573 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.lookup.sort.db;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.compression.CompressOptions;
+import org.apache.paimon.io.cache.CacheManager;
+import org.apache.paimon.lookup.sort.SortLookupStoreFactory;
+import org.apache.paimon.lookup.sort.SortLookupStoreReader;
+import org.apache.paimon.lookup.sort.SortLookupStoreWriter;
+import org.apache.paimon.memory.MemorySlice;
+import org.apache.paimon.options.MemorySize;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A simple LSM-Tree based KV database built on top of {@link
SortLookupStoreFactory}.
+ *
+ * Architecture (Universal Compaction, inspired by RocksDB):
+ *
+ *
+ * ┌──┐
+ * │MemTable (SkipList)│ ← Active writes
+ * ├──┤
+ * │ Sorted Runs (newest → oldest): │
+ * │[Run-0] [Run-1] [Run-2] ... [Run-N] │ ← Each run is a
sorted SST file set
+ * └──┘
+ *
+ *
+ * Compaction is triggered when the number of sorted runs exceeds a
threshold. Runs are selected
+ * for merging based on size ratios between adjacent runs, following RocksDB's
Universal Compaction
+ * strategy.
+ *
+ * Note: No WAL is implemented. Data in the MemTable will be lost on crash.
+ *
+ * This class is not thread-safe. External synchronization is
required if accessed from
+ * multiple threads.
+ */
+public class SimpleLsmKvDb implements Closeable {
+
+private static final Logger LOG =
LoggerFactory.getLogger(SimpleLsmKvDb.class);
+
+/** Tombstone marker for deleted keys. */
+private static final byte[] TOMBSTONE = new byte[0];
+
+/** Maximum number of levels in the LSM tree. */
+static final int MAX_LEVELS = 4;
+
+/**
+ * Estimated per-entry memory overhead in the MemTable's TreeMap, beyond
the raw key/value
+ * bytes. This accounts for:
+ *
+ *
+ * TreeMap.Entry node: ~64 bytes (object header +
left/right/parent/key/value refs +
+ * color)
+ * MemorySlice wrapper: ~32 bytes (object header + segment ref +
offset + length)
+ * MemorySegment backing the key: ~48 bytes (object header +
heapMemory/offHeapBuffer refs
+ * + address + size)
+ * byte[] value array header: ~16 bytes (object header + length)
+ *
+ */
+static final long PER_ENTRY_OVERHEAD = 160;
+
+private final File dataDirectory;
+private final SortLookupStoreFactory storeFactory;
+private final Comparator keyComparator;
+private final long memTableFlushThreshold;
+private final LsmCompactor compactor;
+
+/** Active MemTable: key -> value bytes (empty byte[] = tombstone). */
+private TreeMap memTable;
+
+/** Estimated size of the current MemTable in bytes. */
+private long memTableSize;
+
+/**
+ * Multi-level SST file storage. Each level contains a list of {@link
SstFileMetadata} ordered
+ * by key range. Level 0 files are ordered newest-first (key ranges may
overlap). Level 1+ files
+ * are ordered by minKey (key ranges do NOT overlap).
+ */
+private final List> levels;
+
+/** Cached readers for SST files, keyed by file path. Lazily populated on
first lookup. */
+private final Map readerCache;
+
+private long fileSequence;
+private boolean closed;
+
+private SimpleLsmKvDb(
+File dataDirectory,
+SortLookupStoreFactory storeFactory,
+Comparator keyComparator,
+long memTableFlushThreshold,
+
Re: [PR] [core] Introduce SimpleLsmKvDb to put and get based on Paimon SST [paimon]
leaves12138 commented on code in PR #7407:
URL: https://github.com/apache/paimon/pull/7407#discussion_r2922567667
##
paimon-common/src/main/java/org/apache/paimon/lookup/sort/db/SimpleLsmKvDb.java:
##
@@ -0,0 +1,573 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.lookup.sort.db;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.compression.CompressOptions;
+import org.apache.paimon.io.cache.CacheManager;
+import org.apache.paimon.lookup.sort.SortLookupStoreFactory;
+import org.apache.paimon.lookup.sort.SortLookupStoreReader;
+import org.apache.paimon.lookup.sort.SortLookupStoreWriter;
+import org.apache.paimon.memory.MemorySlice;
+import org.apache.paimon.options.MemorySize;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A simple LSM-Tree based KV database built on top of {@link
SortLookupStoreFactory}.
+ *
+ * Architecture (Universal Compaction, inspired by RocksDB):
+ *
+ *
+ * ┌──┐
+ * │MemTable (SkipList)│ ← Active writes
+ * ├──┤
+ * │ Sorted Runs (newest → oldest): │
+ * │[Run-0] [Run-1] [Run-2] ... [Run-N] │ ← Each run is a
sorted SST file set
+ * └──┘
+ *
+ *
+ * Compaction is triggered when the number of sorted runs exceeds a
threshold. Runs are selected
+ * for merging based on size ratios between adjacent runs, following RocksDB's
Universal Compaction
+ * strategy.
+ *
+ * Note: No WAL is implemented. Data in the MemTable will be lost on crash.
+ *
+ * This class is not thread-safe. External synchronization is
required if accessed from
+ * multiple threads.
+ */
+public class SimpleLsmKvDb implements Closeable {
+
+private static final Logger LOG =
LoggerFactory.getLogger(SimpleLsmKvDb.class);
+
+/** Tombstone marker for deleted keys. */
+private static final byte[] TOMBSTONE = new byte[0];
+
+/** Maximum number of levels in the LSM tree. */
+static final int MAX_LEVELS = 4;
+
+/**
+ * Estimated per-entry memory overhead in the MemTable's TreeMap, beyond
the raw key/value
+ * bytes. This accounts for:
+ *
+ *
+ * TreeMap.Entry node: ~64 bytes (object header +
left/right/parent/key/value refs +
+ * color)
+ * MemorySlice wrapper: ~32 bytes (object header + segment ref +
offset + length)
+ * MemorySegment backing the key: ~48 bytes (object header +
heapMemory/offHeapBuffer refs
+ * + address + size)
+ * byte[] value array header: ~16 bytes (object header + length)
+ *
+ */
+static final long PER_ENTRY_OVERHEAD = 160;
+
+private final File dataDirectory;
+private final SortLookupStoreFactory storeFactory;
+private final Comparator keyComparator;
+private final long memTableFlushThreshold;
+private final LsmCompactor compactor;
+
+/** Active MemTable: key -> value bytes (empty byte[] = tombstone). */
+private TreeMap memTable;
+
+/** Estimated size of the current MemTable in bytes. */
+private long memTableSize;
+
+/**
+ * Multi-level SST file storage. Each level contains a list of {@link
SstFileMetadata} ordered
+ * by key range. Level 0 files are ordered newest-first (key ranges may
overlap). Level 1+ files
+ * are ordered by minKey (key ranges do NOT overlap).
+ */
+private final List> levels;
+
+/** Cached readers for SST files, keyed by file path. Lazily populated on
first lookup. */
+private final Map readerCache;
+
+private long fileSequence;
+private boolean closed;
+
+private SimpleLsmKvDb(
+File dataDirectory,
+SortLookupStoreFactory storeFactory,
+Comparator keyComparator,
+long memTableFlushThreshold,
+
Re: [PR] [core] Introduce SimpleLsmKvDb to put and get based on Paimon SST [paimon]
leaves12138 commented on code in PR #7407:
URL: https://github.com/apache/paimon/pull/7407#discussion_r2922565997
##
paimon-common/src/main/java/org/apache/paimon/lookup/sort/db/SimpleLsmKvDb.java:
##
@@ -0,0 +1,573 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.lookup.sort.db;
+
+import org.apache.paimon.annotation.VisibleForTesting;
+import org.apache.paimon.compression.CompressOptions;
+import org.apache.paimon.io.cache.CacheManager;
+import org.apache.paimon.lookup.sort.SortLookupStoreFactory;
+import org.apache.paimon.lookup.sort.SortLookupStoreReader;
+import org.apache.paimon.lookup.sort.SortLookupStoreWriter;
+import org.apache.paimon.memory.MemorySlice;
+import org.apache.paimon.options.MemorySize;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+
+/**
+ * A simple LSM-Tree based KV database built on top of {@link
SortLookupStoreFactory}.
+ *
+ * Architecture (Universal Compaction, inspired by RocksDB):
+ *
+ *
+ * ┌──┐
+ * │MemTable (SkipList)│ ← Active writes
+ * ├──┤
+ * │ Sorted Runs (newest → oldest): │
+ * │[Run-0] [Run-1] [Run-2] ... [Run-N] │ ← Each run is a
sorted SST file set
+ * └──┘
+ *
+ *
+ * Compaction is triggered when the number of sorted runs exceeds a
threshold. Runs are selected
+ * for merging based on size ratios between adjacent runs, following RocksDB's
Universal Compaction
+ * strategy.
+ *
+ * Note: No WAL is implemented. Data in the MemTable will be lost on crash.
+ *
+ * This class is not thread-safe. External synchronization is
required if accessed from
+ * multiple threads.
+ */
+public class SimpleLsmKvDb implements Closeable {
+
+private static final Logger LOG =
LoggerFactory.getLogger(SimpleLsmKvDb.class);
+
+/** Tombstone marker for deleted keys. */
+private static final byte[] TOMBSTONE = new byte[0];
+
+/** Maximum number of levels in the LSM tree. */
+static final int MAX_LEVELS = 4;
+
+/**
+ * Estimated per-entry memory overhead in the MemTable's TreeMap, beyond
the raw key/value
+ * bytes. This accounts for:
+ *
+ *
+ * TreeMap.Entry node: ~64 bytes (object header +
left/right/parent/key/value refs +
+ * color)
+ * MemorySlice wrapper: ~32 bytes (object header + segment ref +
offset + length)
+ * MemorySegment backing the key: ~48 bytes (object header +
heapMemory/offHeapBuffer refs
+ * + address + size)
+ * byte[] value array header: ~16 bytes (object header + length)
+ *
+ */
+static final long PER_ENTRY_OVERHEAD = 160;
+
+private final File dataDirectory;
+private final SortLookupStoreFactory storeFactory;
+private final Comparator keyComparator;
+private final long memTableFlushThreshold;
+private final LsmCompactor compactor;
+
+/** Active MemTable: key -> value bytes (empty byte[] = tombstone). */
+private TreeMap memTable;
+
+/** Estimated size of the current MemTable in bytes. */
+private long memTableSize;
+
+/**
+ * Multi-level SST file storage. Each level contains a list of {@link
SstFileMetadata} ordered
+ * by key range. Level 0 files are ordered newest-first (key ranges may
overlap). Level 1+ files
+ * are ordered by minKey (key ranges do NOT overlap).
+ */
+private final List> levels;
+
+/** Cached readers for SST files, keyed by file path. Lazily populated on
first lookup. */
+private final Map readerCache;
+
+private long fileSequence;
+private boolean closed;
+
+private SimpleLsmKvDb(
+File dataDirectory,
+SortLookupStoreFactory storeFactory,
+Comparator keyComparator,
+long memTableFlushThreshold,
+
