SlideShare a Scribd company logo
MAPDB
BY DEBMALYA JASH
WHAT IS MAPDB?
• MapDB is an open-source (Apache 2.0 licensed), embedded Java database engine and collection
framework. It provides Maps, Sets, Lists, Queues, Bitmaps with range queries, expiration, compression,
off-heap storage and streaming. MapDB is probably the fastest Java database, with performance
comparable to java.util collections. It also provides advanced features such as ACID transactions,
snapshots, incremental backups and much more.
MAIN CLASSES
• DBMaker
• DB
• HTreeMap
• BTreeMap
• Volume
• SortedTableMap
DBMAKER
• Handles database configuration, creation and opening. Using this class we can set different modes and
configuration options provided by MapDB.
DB
• Represents and open database (or a single transaction session). It is used to create, open and collection
storages.
• Handles database's lifecycle methods like commit(), rollback(), and close().
• To open (or create) a store, use one of the DBMaker.xxxDB() static methods.
• memoryDB() - Creates new in-memory database. Changes are lost after JVM exits. serializes data into byte[].
• memoryDirectDB() - Creates new in-memory database. Changes are lost after JVM exits. This will use
DirectByteBuffer outside of Heap, so Garbage Collector is not affected. Increase memory as per your
requirement with option -XX:MaxDirectMemorySize=10G
• fileDB() – stores serialized record in physical file.
• tempFileDB() - new database in temporary folder. Files are deleted after store was closed.
• appendFileDB() opens a database which uses append-only log files and so on.
• heapDB() - Creates new in-memory database which stores all data on heap without serialization. very fast, but
data will affect Garbage Collector the same way as traditional Java Collections.
HTREEMAP
• HTreeMap provides HashMap and HashSet collections for MapDB. It optionally supports entry
expiration and can be used as a cache. It is thread-safe and scales under parallel updates.
HTREEMAP ADVANTAGES
• HTreeMap is a segmented Hash Tree. Unlike other HashMaps it does not use fixed size Hash Table, and does not
rehash all data when Hash Table grows. HTreeMap uses auto-expanding Index Tree, so it never needs resize. It also
occupies less space, since empty hash slots do not consume any space.
• HTreeMap optionally supports entry expiration based on four criteria: maximal map size, maximal storage size, time-
to-live since last modification and time-to-live since last access. Expired entries are automatically removed. This
feature uses FIFO queue and each segment has independent expiration queue.
MAP LAYOUT
• MapDB has different set of parameters to control its access time and maximal size. Those are grouped
under term Map Layout.
• HTreeMap layout is controlled by layout function. It takes three parameters:
• concurrency, number of segments. Default value is 8, it always rounds-up to power of two.
• maximal node size of Index Tree Dir Node. Default value is 16, it always rounds-up to power of two.
Maximal value is 128 entries.
• number of Levels in Index Tree, default value is 4
CONCURRENCY
• Concurrency is implemented by using multiple segments, each with separate read-write lock. Each concurrent
segment is independent, it has its own Size Counter, iterators and Expiration Queues. Number of segments is
configurable. Too small number will cause congestion on concurrent updates, too large will increase memory
overhead.
• HTreeMap uses Index Tree instead of growing Object[] for its Hash Table. Index Tree is sparse array like structure,
which uses tree hierarchy of arrays. It is sparse, so unused entries do not occupy any space. It does not do rehashing
(copy all entries to bigger array), but also it can not grow beyond its initial capacity.
SHARD STORES FOR BETTER CONCURRENCY
• HTreeMap is split into separate segments. Each segment is independent and does not share any state
with other segments. However they still share underlying Store and that affects performance under
concurrent load. It is possible to make segments truly independent, by using separate Store for each
segment.
EXPIRATION
• HTreeMap offers optional entry expiration if some conditions are met. Entry can expire if:
• An entry exists in the map longer time than the expiration period is. The expiration period could be since the
creation, last modification or since the last read access.
• The number of entries in a map would exceed maximal number
• Map consumes more disk space or memory than space limit
EXPIRATION OVERFLOW
• HTreeMap supports Modification Listeners. It notifies listener about inserts, updates and removes from
HTreeMap. It is possible to link two collections together. Usually faster in-memory with limited size, and
slower on-disk with unlimited size. After an entry expires from in-memory, it is automatically moved to
on-disk by Modification Listener. And Value Loader will load values back to in-memory map, if those are
not found by map.get() operation.
BTREEMAP
• BTreeMap provides TreeMap and TreeSet for MapDB. It is based on lock-free concurrent B-Linked-Tree.
It offers great performance for small keys and has good vertical scalability.
• BTrees store all their keys and values as part of a btree node. Node size affects the performance a lot. A
large node means that many keys have to be deserialized on lookup. A smaller node loads faster, but
makes large BTrees deeper and requires more operations. The default maximal node size is 32 entries
and it can be changed in this way.
FRAGMENTATION
• A trade-off for lock-free design is fragmentation after deletion. The B-Linked-Tree does not delete btree
nodes after entry removal, once they become empty. If you fill a BTreeMap and then remove all entries,
about 40% of space will not be released. Any value updates (keys are kept) are not affected by this
fragmentation.
VOLUME
• MapDB has its own storage abstraction similar to ByteBuffer. It is called Volume and resides in package
org.mapdb.volume. It is growable, works over 2GB and has number of tweaks to work better with
MapDB.
VOLUME IMPLEMENTATION
• Volume over multiple byte[]. Use DBMaker.memoryDB(file)
• DirectByteBuffer for direct memory. Use DBMaker.memoryDirectDB()
• RandomAccessFile is safer way to access files. It is enabled by default, use DBMaker.fileDB(file)
• FileChannel a bit faster then RAF. Use DBMaker.fileDB(file).fileChannelEnable()
• MappedByteBuffer for memory mapped files. Use DBMaker.fileDB(file).fileMMapEnable()
SORTED TABLE MAP
• SortedTableMap is read-only and does not support updates. Changes should be applied by creating new Map with
Data Pump. Usually one places change into secondary map, and periodically merges two maps into
new SortedTableMap.
• SortedTableMap does not use DB object, but operates directly on Volume (MapDB abstraction over ByteBuffer).
COMPOSITE KEYS AND TUPLES
• MapDB allows composite keys in the form of Object[]. Interval submaps can be used to fetch tuple
subcomponents, or to create a simple form of multimap. Object array is not comparable, so you need to
use specialized serializer which provides comparator.
TRANSACTIONS AND CRASH PROTECTION
• By default transaction is disabled. To enable it we have to call transactionEnable() implicitly.
• DBMaker.fileDB(dbName).transactionEnable().make();
• Whenever we enable transaction, WAL (Write ahead log) is enabled, this enable file protection and
transactions atomic and durable.
MEMORY MAPPED FILES
• By default MapDB use a slower and safer disk access mode called Random-Access-File (RAF).
• Mmap files are much faster compared to RAF. The exact speed bonus depends on the operating system
and disk case management, but is typically between 10% and 300%.
FILE CHANNEL
• It is faster than RandomAccessFile, but has bit more overhead. It also works better under concurrent
access (RAF has global lock).
• DB db = DBMaker .fileDB(file) .fileChannelEnable() .make();
• FileChannel was causing problems in combination with Thread.interrupt. If threads gets interrupted
while doing IO, underlying channel is closed for all other threads.
IN MEMORY MODE
• THREE IN MEMORY MODE
• HEAP DB – VERY FAST AND USEFUL FOR SMALL DATASET , BUT AFFECTED BY GC ACTIVITY. stores objects
in Map<recid,Object> and does not use serialization.
• MEMORY DB - Store based on byte[]. In this mode data are serialized and stored into 1MB large byte[]. Technically this is still
on-heap, but is not affected by GC overhead, since data are not visible to GC. This mode is recommended by default, since it
does not require any additional JVM settings. Increasing maximal heap memory with -Xmx10G JVM parameter is enough.
• Memory direct db - Store based on DirectByteBuffer. In this case data are stored completely off-heap. in 1MB
DirectByteBuffers created with ByteBuffer.allocateDirect(size). You should increase maximal direct memory with JVM
parameter. This mode allows you to decrease maximal heap size to very small size (-Xmx128M). Small heap size has usually
better and more predictable performance.
ALLOCATION OPTIONS
• By default MapDB tries minimize space usage and allocates space in 1MB increments. This additional
allocations might be slower than single large allocation. There are two options to control storage initial
size and size increment. Allocation Increment has side effect on performance with mmap files. MapDB
maps file in series of DirectByteBuffer. Size of each buffer is equal to Size Increment (1MB by default), so
larger Size Increment means less buffers for the same disk store size. Operations such as sync, flush and
close have to traverse all buffers. So larger Size Increment could speedup commit and close operations.
PARTITIONING OPTIONS
• To achieve concurrency HashMap is split into segments, each segment is separate HashMap with its
own ReadWriteLock. Segment number is calculated from hash. When expiration is enabled each
segment has its own Expiration Queue.
QUICK TIPS
• Memory mapped files are much faster and should be enabled on 64bit systems for better performance.
• MapDB has Pump for fast bulk import of collections. It is much faster than to Map.put()
• Transactions have a performance overhead, but without them the store gets corrupted if not closed properly.
• Data stored in MapDB (keys and values) should be immutable. MapDB serializes objects on background.
• MapDB needs compaction sometimes. Run DB.compact() or see background compaction options.
• Better to use specific serializer (e.g. Serializer.STRING), otherwise slower generic serializer will be used.
REFERENCE
• MapDB github
• MapDB Manual
THANKS
•

More Related Content

What's hot

Hepatitis b y embarazo
Hepatitis b y embarazoHepatitis b y embarazo
Hepatitis b y embarazo
Diego Ramirez
 
Enfermedad trofoblástica Gestacional
Enfermedad trofoblástica GestacionalEnfermedad trofoblástica Gestacional
Enfermedad trofoblástica Gestacional
Jorge Geraldo
 

What's hot (20)

Zika y Embarazo
Zika y EmbarazoZika y Embarazo
Zika y Embarazo
 
Hepatitis b y embarazo
Hepatitis b y embarazoHepatitis b y embarazo
Hepatitis b y embarazo
 
Hepatitis b in children dr. anshu srivastava
Hepatitis b in children dr. anshu srivastavaHepatitis b in children dr. anshu srivastava
Hepatitis b in children dr. anshu srivastava
 
Infecciones congenitas torches
Infecciones congenitas  torchesInfecciones congenitas  torches
Infecciones congenitas torches
 
Vacuna contra el vph
Vacuna contra el vphVacuna contra el vph
Vacuna contra el vph
 
Immumization in special situations
Immumization in special situationsImmumization in special situations
Immumization in special situations
 
sindrome de torch.pptx
sindrome de torch.pptxsindrome de torch.pptx
sindrome de torch.pptx
 
Corioamniomnitis
CorioamniomnitisCorioamniomnitis
Corioamniomnitis
 
Convulsion febril
Convulsion febrilConvulsion febril
Convulsion febril
 
Hpv
HpvHpv
Hpv
 
Hígado y embarazo.
Hígado y embarazo.Hígado y embarazo.
Hígado y embarazo.
 
Dengue in pregnancy Spanish 180220
Dengue in pregnancy Spanish 180220Dengue in pregnancy Spanish 180220
Dengue in pregnancy Spanish 180220
 
Torch
TorchTorch
Torch
 
Enfermedad trofoblástica Gestacional
Enfermedad trofoblástica GestacionalEnfermedad trofoblástica Gestacional
Enfermedad trofoblástica Gestacional
 
Meningitis
MeningitisMeningitis
Meningitis
 
Microbiology of hepatitis e virus
Microbiology of hepatitis e virusMicrobiology of hepatitis e virus
Microbiology of hepatitis e virus
 
Acinetobacter baumannii an update
Acinetobacter baumannii an update Acinetobacter baumannii an update
Acinetobacter baumannii an update
 
Zika Virus General overview
Zika Virus General overviewZika Virus General overview
Zika Virus General overview
 
Embarazo multiple 2014
Embarazo multiple 2014Embarazo multiple 2014
Embarazo multiple 2014
 
Ruptura prematura de membranas
Ruptura prematura de membranasRuptura prematura de membranas
Ruptura prematura de membranas
 

Similar to Map db

Similar to Map db (20)

Map db
Map dbMap db
Map db
 
Bigtable and Boxwood
Bigtable and BoxwoodBigtable and Boxwood
Bigtable and Boxwood
 
Introduction To Maxtable
Introduction To MaxtableIntroduction To Maxtable
Introduction To Maxtable
 
Hbase: an introduction
Hbase: an introductionHbase: an introduction
Hbase: an introduction
 
Computre_Engineering_Introduction_FPGA.ppt
Computre_Engineering_Introduction_FPGA.pptComputre_Engineering_Introduction_FPGA.ppt
Computre_Engineering_Introduction_FPGA.ppt
 
Designing data intensive applications
Designing data intensive applicationsDesigning data intensive applications
Designing data intensive applications
 
HBase Sizing Guide
HBase Sizing GuideHBase Sizing Guide
HBase Sizing Guide
 
#GeodeSummit - Off-Heap Storage Current and Future Design
#GeodeSummit - Off-Heap Storage Current and Future Design#GeodeSummit - Off-Heap Storage Current and Future Design
#GeodeSummit - Off-Heap Storage Current and Future Design
 
Responding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in JavaResponding rapidly when you have 100+ GB data sets in Java
Responding rapidly when you have 100+ GB data sets in Java
 
Tez Shuffle Handler: Shuffling at Scale with Apache Hadoop
Tez Shuffle Handler: Shuffling at Scale with Apache HadoopTez Shuffle Handler: Shuffling at Scale with Apache Hadoop
Tez Shuffle Handler: Shuffling at Scale with Apache Hadoop
 
Hadoop – Architecture.pptx
Hadoop – Architecture.pptxHadoop – Architecture.pptx
Hadoop – Architecture.pptx
 
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar AhmedPGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
PGConf.ASIA 2019 Bali - Tune Your LInux Box, Not Just PostgreSQL - Ibrar Ahmed
 
Apache Geode Offheap Storage
Apache Geode Offheap StorageApache Geode Offheap Storage
Apache Geode Offheap Storage
 
Apache Spark
Apache SparkApache Spark
Apache Spark
 
Hbase 20141003
Hbase 20141003Hbase 20141003
Hbase 20141003
 
7. Key-Value Databases: In Depth
7. Key-Value Databases: In Depth7. Key-Value Databases: In Depth
7. Key-Value Databases: In Depth
 
HBaseCon 2015: HBase Performance Tuning @ Salesforce
HBaseCon 2015: HBase Performance Tuning @ SalesforceHBaseCon 2015: HBase Performance Tuning @ Salesforce
HBaseCon 2015: HBase Performance Tuning @ Salesforce
 
Apache HBase Performance Tuning
Apache HBase Performance TuningApache HBase Performance Tuning
Apache HBase Performance Tuning
 
No sql
No sqlNo sql
No sql
 
Deep Dive into DynamoDB
Deep Dive into DynamoDBDeep Dive into DynamoDB
Deep Dive into DynamoDB
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 

Recently uploaded (20)

Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with StrimziStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi
 
iGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by SkilrockiGaming Platform & Lottery Solutions by Skilrock
iGaming Platform & Lottery Solutions by Skilrock
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdf
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 

Map db

  • 2. WHAT IS MAPDB? • MapDB is an open-source (Apache 2.0 licensed), embedded Java database engine and collection framework. It provides Maps, Sets, Lists, Queues, Bitmaps with range queries, expiration, compression, off-heap storage and streaming. MapDB is probably the fastest Java database, with performance comparable to java.util collections. It also provides advanced features such as ACID transactions, snapshots, incremental backups and much more.
  • 3. MAIN CLASSES • DBMaker • DB • HTreeMap • BTreeMap • Volume • SortedTableMap
  • 4. DBMAKER • Handles database configuration, creation and opening. Using this class we can set different modes and configuration options provided by MapDB.
  • 5. DB • Represents and open database (or a single transaction session). It is used to create, open and collection storages. • Handles database's lifecycle methods like commit(), rollback(), and close(). • To open (or create) a store, use one of the DBMaker.xxxDB() static methods. • memoryDB() - Creates new in-memory database. Changes are lost after JVM exits. serializes data into byte[]. • memoryDirectDB() - Creates new in-memory database. Changes are lost after JVM exits. This will use DirectByteBuffer outside of Heap, so Garbage Collector is not affected. Increase memory as per your requirement with option -XX:MaxDirectMemorySize=10G • fileDB() – stores serialized record in physical file. • tempFileDB() - new database in temporary folder. Files are deleted after store was closed. • appendFileDB() opens a database which uses append-only log files and so on. • heapDB() - Creates new in-memory database which stores all data on heap without serialization. very fast, but data will affect Garbage Collector the same way as traditional Java Collections.
  • 6. HTREEMAP • HTreeMap provides HashMap and HashSet collections for MapDB. It optionally supports entry expiration and can be used as a cache. It is thread-safe and scales under parallel updates.
  • 7. HTREEMAP ADVANTAGES • HTreeMap is a segmented Hash Tree. Unlike other HashMaps it does not use fixed size Hash Table, and does not rehash all data when Hash Table grows. HTreeMap uses auto-expanding Index Tree, so it never needs resize. It also occupies less space, since empty hash slots do not consume any space. • HTreeMap optionally supports entry expiration based on four criteria: maximal map size, maximal storage size, time- to-live since last modification and time-to-live since last access. Expired entries are automatically removed. This feature uses FIFO queue and each segment has independent expiration queue.
  • 8. MAP LAYOUT • MapDB has different set of parameters to control its access time and maximal size. Those are grouped under term Map Layout. • HTreeMap layout is controlled by layout function. It takes three parameters: • concurrency, number of segments. Default value is 8, it always rounds-up to power of two. • maximal node size of Index Tree Dir Node. Default value is 16, it always rounds-up to power of two. Maximal value is 128 entries. • number of Levels in Index Tree, default value is 4
  • 9. CONCURRENCY • Concurrency is implemented by using multiple segments, each with separate read-write lock. Each concurrent segment is independent, it has its own Size Counter, iterators and Expiration Queues. Number of segments is configurable. Too small number will cause congestion on concurrent updates, too large will increase memory overhead. • HTreeMap uses Index Tree instead of growing Object[] for its Hash Table. Index Tree is sparse array like structure, which uses tree hierarchy of arrays. It is sparse, so unused entries do not occupy any space. It does not do rehashing (copy all entries to bigger array), but also it can not grow beyond its initial capacity.
  • 10. SHARD STORES FOR BETTER CONCURRENCY • HTreeMap is split into separate segments. Each segment is independent and does not share any state with other segments. However they still share underlying Store and that affects performance under concurrent load. It is possible to make segments truly independent, by using separate Store for each segment.
  • 11. EXPIRATION • HTreeMap offers optional entry expiration if some conditions are met. Entry can expire if: • An entry exists in the map longer time than the expiration period is. The expiration period could be since the creation, last modification or since the last read access. • The number of entries in a map would exceed maximal number • Map consumes more disk space or memory than space limit
  • 12. EXPIRATION OVERFLOW • HTreeMap supports Modification Listeners. It notifies listener about inserts, updates and removes from HTreeMap. It is possible to link two collections together. Usually faster in-memory with limited size, and slower on-disk with unlimited size. After an entry expires from in-memory, it is automatically moved to on-disk by Modification Listener. And Value Loader will load values back to in-memory map, if those are not found by map.get() operation.
  • 13. BTREEMAP • BTreeMap provides TreeMap and TreeSet for MapDB. It is based on lock-free concurrent B-Linked-Tree. It offers great performance for small keys and has good vertical scalability. • BTrees store all their keys and values as part of a btree node. Node size affects the performance a lot. A large node means that many keys have to be deserialized on lookup. A smaller node loads faster, but makes large BTrees deeper and requires more operations. The default maximal node size is 32 entries and it can be changed in this way.
  • 14. FRAGMENTATION • A trade-off for lock-free design is fragmentation after deletion. The B-Linked-Tree does not delete btree nodes after entry removal, once they become empty. If you fill a BTreeMap and then remove all entries, about 40% of space will not be released. Any value updates (keys are kept) are not affected by this fragmentation.
  • 15. VOLUME • MapDB has its own storage abstraction similar to ByteBuffer. It is called Volume and resides in package org.mapdb.volume. It is growable, works over 2GB and has number of tweaks to work better with MapDB.
  • 16. VOLUME IMPLEMENTATION • Volume over multiple byte[]. Use DBMaker.memoryDB(file) • DirectByteBuffer for direct memory. Use DBMaker.memoryDirectDB() • RandomAccessFile is safer way to access files. It is enabled by default, use DBMaker.fileDB(file) • FileChannel a bit faster then RAF. Use DBMaker.fileDB(file).fileChannelEnable() • MappedByteBuffer for memory mapped files. Use DBMaker.fileDB(file).fileMMapEnable()
  • 17. SORTED TABLE MAP • SortedTableMap is read-only and does not support updates. Changes should be applied by creating new Map with Data Pump. Usually one places change into secondary map, and periodically merges two maps into new SortedTableMap. • SortedTableMap does not use DB object, but operates directly on Volume (MapDB abstraction over ByteBuffer).
  • 18. COMPOSITE KEYS AND TUPLES • MapDB allows composite keys in the form of Object[]. Interval submaps can be used to fetch tuple subcomponents, or to create a simple form of multimap. Object array is not comparable, so you need to use specialized serializer which provides comparator.
  • 19. TRANSACTIONS AND CRASH PROTECTION • By default transaction is disabled. To enable it we have to call transactionEnable() implicitly. • DBMaker.fileDB(dbName).transactionEnable().make(); • Whenever we enable transaction, WAL (Write ahead log) is enabled, this enable file protection and transactions atomic and durable.
  • 20. MEMORY MAPPED FILES • By default MapDB use a slower and safer disk access mode called Random-Access-File (RAF). • Mmap files are much faster compared to RAF. The exact speed bonus depends on the operating system and disk case management, but is typically between 10% and 300%.
  • 21. FILE CHANNEL • It is faster than RandomAccessFile, but has bit more overhead. It also works better under concurrent access (RAF has global lock). • DB db = DBMaker .fileDB(file) .fileChannelEnable() .make(); • FileChannel was causing problems in combination with Thread.interrupt. If threads gets interrupted while doing IO, underlying channel is closed for all other threads.
  • 22. IN MEMORY MODE • THREE IN MEMORY MODE • HEAP DB – VERY FAST AND USEFUL FOR SMALL DATASET , BUT AFFECTED BY GC ACTIVITY. stores objects in Map<recid,Object> and does not use serialization. • MEMORY DB - Store based on byte[]. In this mode data are serialized and stored into 1MB large byte[]. Technically this is still on-heap, but is not affected by GC overhead, since data are not visible to GC. This mode is recommended by default, since it does not require any additional JVM settings. Increasing maximal heap memory with -Xmx10G JVM parameter is enough. • Memory direct db - Store based on DirectByteBuffer. In this case data are stored completely off-heap. in 1MB DirectByteBuffers created with ByteBuffer.allocateDirect(size). You should increase maximal direct memory with JVM parameter. This mode allows you to decrease maximal heap size to very small size (-Xmx128M). Small heap size has usually better and more predictable performance.
  • 23. ALLOCATION OPTIONS • By default MapDB tries minimize space usage and allocates space in 1MB increments. This additional allocations might be slower than single large allocation. There are two options to control storage initial size and size increment. Allocation Increment has side effect on performance with mmap files. MapDB maps file in series of DirectByteBuffer. Size of each buffer is equal to Size Increment (1MB by default), so larger Size Increment means less buffers for the same disk store size. Operations such as sync, flush and close have to traverse all buffers. So larger Size Increment could speedup commit and close operations.
  • 24. PARTITIONING OPTIONS • To achieve concurrency HashMap is split into segments, each segment is separate HashMap with its own ReadWriteLock. Segment number is calculated from hash. When expiration is enabled each segment has its own Expiration Queue.
  • 25. QUICK TIPS • Memory mapped files are much faster and should be enabled on 64bit systems for better performance. • MapDB has Pump for fast bulk import of collections. It is much faster than to Map.put() • Transactions have a performance overhead, but without them the store gets corrupted if not closed properly. • Data stored in MapDB (keys and values) should be immutable. MapDB serializes objects on background. • MapDB needs compaction sometimes. Run DB.compact() or see background compaction options. • Better to use specific serializer (e.g. Serializer.STRING), otherwise slower generic serializer will be used.