Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BACKPORT] TS code sample [HZ-4382] (#620) #625

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions enterprise/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<module>tls-rbac-demo</module>
<module>tls-custom-login-module</module>
<module>user-code-namespaces</module>
<module>tiered-store</module>
</modules>

<repositories>
Expand Down
24 changes: 24 additions & 0 deletions enterprise/tiered-store/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

<artifactId>tiered-store</artifactId>
<name>Tiered Store</name>
<description>All the code samples for Tiered Store</description>

<parent>
<artifactId>enterprise</artifactId>
<groupId>com.hazelcast.samples.enterprise</groupId>
<version>0.1-SNAPSHOT</version>
</parent>

<properties>
<!-- needed for checkstyle/findbugs -->
<main.basedir>${project.parent.parent.basedir}</main.basedir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
98 changes: 98 additions & 0 deletions enterprise/tiered-store/src/main/java/TSSimplePopulation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import com.hazelcast.config.Config;
import com.hazelcast.config.InMemoryFormat;
import com.hazelcast.config.LocalDeviceConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.NativeMemoryConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.memory.Capacity;
import com.hazelcast.memory.MemoryUnit;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

import static com.hazelcast.config.NativeMemoryConfig.MemoryAllocatorType.POOLED;
import static com.hazelcast.examples.helper.LicenseUtils.ENTERPRISE_LICENSE_KEY;
import static com.hazelcast.memory.MemoryUnit.GIGABYTES;

/**
* You have to set your Hazelcast Enterprise license key to make this code sample work.
* Please have a look at {@link com.hazelcast.examples.helper.LicenseUtils} for details.
* <p>
* The sample shows how to configure an IMap backed by tiered store, and that it can be populated
* and further used the same as HD IMap.
*/
public class TSSimplePopulation {

public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(newConfig());

Map<String, String> map = hazelcastInstance.getMap("map");
map.put("1", "Tokyo");
map.put("2", "Paris");
map.put("3", "New York");

System.out.println("Inserted " + map.size() + " entries into map backed by tiered store");

hazelcastInstance.shutdown();
}

private static Config newConfig() {
// Configure pooled memory
Capacity memoryCapacity = Capacity.of(1, GIGABYTES);
NativeMemoryConfig memoryConfig = new NativeMemoryConfig();
memoryConfig.setEnabled(true);
memoryConfig.setCapacity(memoryCapacity);
memoryConfig.setAllocatorType(POOLED);

// Create device config
String deviceName = "local-device";
Capacity diskCapacity = Capacity.of(128, GIGABYTES);
LocalDeviceConfig localDeviceConfig = new LocalDeviceConfig()
.setName("local-device")
.setCapacity(diskCapacity)
.setBaseDir(createNewFolder(deviceName));

Config config = new Config();
config.setNativeMemoryConfig(memoryConfig);
config.setLicenseKey(ENTERPRISE_LICENSE_KEY);
config.addDeviceConfig(localDeviceConfig);

// Configure map backed by tiered store
MapConfig mapConfig = new MapConfig();
mapConfig.setName("default");
mapConfig.setInMemoryFormat(InMemoryFormat.NATIVE);

// Configure disk tier
mapConfig.getTieredStoreConfig()
.setEnabled(true)
.getDiskTierConfig()
.setEnabled(true)
.setDeviceName(deviceName);

// Configure memory tier
mapConfig.getTieredStoreConfig().getMemoryTierConfig()
.setCapacity(Capacity.of(8, MemoryUnit.MEGABYTES));

config.addMapConfig(mapConfig);

return config;
}

private static File createNewFolder(String deviceName) {
try {
String path = Paths.get(deviceName).toString();

Path tempDirectory = Files.createTempDirectory(path);
File file = tempDirectory.toFile();
file.deleteOnExit();
return file;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}