Repository: diennea/herddb
Pull request #4 (2016-04-08 13:55:04)

introducing server


Patch:
diff --git a/herddb-core/pom.xml b/herddb-core/pom.xml
index 01e187a9..eced43c7 100644
--- a/herddb-core/pom.xml
+++ b/herddb-core/pom.xml
@@ -11,6 +11,13 @@
     <artifactId>herddb-core</artifactId>
     <name>HerdDB Core</name>
     <packaging>jar</packaging>
+    <dependencies>
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>herddb-net</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+    </dependencies>
     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     </properties>
diff --git a/herddb-core/src/main/java/herddb/file/FileCommitLogManager.java b/herddb-core/src/main/java/herddb/file/FileCommitLogManager.java
index 69953977..f59ecf02 100644
--- a/herddb-core/src/main/java/herddb/file/FileCommitLogManager.java
+++ b/herddb-core/src/main/java/herddb/file/FileCommitLogManager.java
@@ -27,6 +27,7 @@ import java.nio.file.Path;
 
 /**
  * Commit logs on local files
+ *
  * @author enrico.olivelli
  */
 public class FileCommitLogManager extends CommitLogManager {
@@ -34,7 +35,7 @@ public class FileCommitLogManager extends CommitLogManager {
     private final Path baseDirectory;
 
     public FileCommitLogManager(Path baseDirectory) {
-        this.baseDirectory = baseDirectory;
+        this.baseDirectory = baseDirectory.resolve("txlog");
     }
 
     @Override
diff --git a/herddb-core/src/main/java/herddb/file/FileDataStorageManager.java b/herddb-core/src/main/java/herddb/file/FileDataStorageManager.java
index c2330a21..0f104093 100644
--- a/herddb-core/src/main/java/herddb/file/FileDataStorageManager.java
+++ b/herddb-core/src/main/java/herddb/file/FileDataStorageManager.java
@@ -56,7 +56,7 @@ public class FileDataStorageManager extends DataStorageManager {
     private final AtomicLong newPageId = new AtomicLong();
 
     public FileDataStorageManager(Path baseDirectory) {
-        this.baseDirectory = baseDirectory;
+        this.baseDirectory = baseDirectory.resolve("data");
     }
 
     @Override
diff --git a/herddb-core/src/main/java/herddb/file/FileMetadataStorageManager.java b/herddb-core/src/main/java/herddb/file/FileMetadataStorageManager.java
index 5bff83c1..bf6f3c7f 100644
--- a/herddb-core/src/main/java/herddb/file/FileMetadataStorageManager.java
+++ b/herddb-core/src/main/java/herddb/file/FileMetadataStorageManager.java
@@ -56,7 +56,7 @@ public class FileMetadataStorageManager extends MetadataStorageManager {
     private final static Logger LOGGER = Logger.getLogger(FileMetadataStorageManager.class.getName());
 
     public FileMetadataStorageManager(Path baseDirectory) {
-        this.baseDirectory = baseDirectory;
+        this.baseDirectory = baseDirectory.resolve("metadata");
     }
 
     @Override
diff --git a/herddb-core/src/main/java/herddb/server/Server.java b/herddb-core/src/main/java/herddb/server/Server.java
new file mode 100644
index 00000000..148c939d
--- /dev/null
+++ b/herddb-core/src/main/java/herddb/server/Server.java
@@ -0,0 +1,108 @@
+/*
+ Licensed to Diennea S.r.l. under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. Diennea S.r.l. 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 herddb.server;
+
+import herddb.core.DBManager;
+import herddb.file.FileCommitLogManager;
+import herddb.file.FileDataStorageManager;
+import herddb.file.FileMetadataStorageManager;
+import herddb.metadata.MetadataStorageManager;
+import herddb.network.Channel;
+import herddb.network.ServerSideConnection;
+import herddb.network.ServerSideConnectionAcceptor;
+import herddb.network.netty.NettyChannelAcceptor;
+import herddb.storage.DataStorageManager;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.logging.Logger;
+
+/**
+ * HerdDB Server
+ *
+ * @author enrico.olivelli
+ */
+public class Server implements AutoCloseable, ServerSideConnectionAcceptor<ServerSideConnection> {
+
+    private static final Logger LOGGER = Logger.getLogger(Server.class.getName());
+    private final DBManager dbmanager;
+    private final NettyChannelAcceptor networkServer;
+    private final ServerConfiguration configuration;
+    private final Path baseDirectory;
+    private final Map<Long, ServerSideConnectionPeer> connections = new ConcurrentHashMap<>();
+
+    public Server(ServerConfiguration configuration) {
+        this.configuration = configuration;
+        String nodeId = configuration.getString(ServerConfiguration.PROPERTY_NODEID, "");
+        this.baseDirectory = Paths.get(configuration.getString(ServerConfiguration.PROPERTY_BASEDIR, ".")).toAbsolutePath();
+        this.dbmanager = new DBManager(nodeId,
+                buildMetadataStorageManager(),
+                buildDataStorageManager(),
+                buildFileCommitLogManager());
+        this.networkServer = buildChannelAcceptor();
+        this.networkServer.setAcceptor(this);
+    }
+
+    private NettyChannelAcceptor buildChannelAcceptor() {
+        return new NettyChannelAcceptor(
+                configuration.getString(ServerConfiguration.PROPERTY_HOST, "localhost"),
+                configuration.getInt(ServerConfiguration.PROPERTY_PORT, 0),
+                configuration.getBoolean(ServerConfiguration.PROPERTY_SSL, false));
+    }
+
+    private MetadataStorageManager buildMetadataStorageManager() {
+        return new FileMetadataStorageManager(baseDirectory);
+    }
+
+    private DataStorageManager buildDataStorageManager() {
+        return new FileDataStorageManager(baseDirectory);
+    }
+
+    private FileCommitLogManager buildFileCommitLogManager() {
+        return new FileCommitLogManager(baseDirectory);
+    }
+
+    public void start() throws Exception {
+        this.dbmanager.start();
+        this.networkServer.start();
+    }
+
+    @Override
+    public void close() throws Exception {
+        try {
+            networkServer.close();
+        } finally {
+            dbmanager.close();
+        }
+    }
+
+    @Override
+    public ServerSideConnection createConnection(Channel channel) {
+        ServerSideConnectionPeer peer = new ServerSideConnectionPeer(channel, this);
+        connections.put(peer.getConnectionId(), peer);
+        return peer;
+    }
+
+    void connectionClosed(ServerSideConnectionPeer connection) {
+        connections.remove(connection.getConnectionId());
+    }
+
+}
diff --git a/herddb-core/src/main/java/herddb/server/ServerConfiguration.java b/herddb-core/src/main/java/herddb/server/ServerConfiguration.java
new file mode 100644
index 00000000..97e6a1a8
--- /dev/null
+++ b/herddb-core/src/main/java/herddb/server/ServerConfiguration.java
@@ -0,0 +1,78 @@
+/*
+ Licensed to Diennea S.r.l. under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. Diennea S.r.l. 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 herddb.server;
+
+import java.nio.file.Path;
+import java.util.Properties;
+
+/**
+ * Server configuration
+ *
+ * @author enrico.olivelli
+ */
+public class ServerConfiguration {
+
+    private final Properties properties;
+
+    public static final String PROPERTY_NODEID = "nodeId";
+    public static final String PROPERTY_BASEDIR = "baseDir";
+    public static final String PROPERTY_HOST = "host";
+    public static final String PROPERTY_PORT = "port";
+    public static final String PROPERTY_SSL = "ssl";
+
+    public ServerConfiguration(Properties properties) {
+        this.properties = properties;
+    }
+
+    public ServerConfiguration(Path baseDir) {
+        this();
+        set(PROPERTY_BASEDIR, baseDir.toAbsolutePath());
+    }
+
+    public ServerConfiguration() {
+        this.properties = new Properties();
+    }
+
+    public int getInt(String key, int defaultValue) {
+        return Integer.parseInt(this.properties.getProperty(key, defaultValue + ""));
+    }
+
+    public boolean getBoolean(String key, boolean defaultValue) {
+        return Boolean.getBoolean(this.properties.getProperty(key, defaultValue + ""));
+    }
+
+    public long getLong(String key, long defaultValue) {
+        return Long.parseLong(this.properties.getProperty(key, defaultValue + ""));
+    }
+
+    public String getString(String key, String defaultValue) {
+        return this.properties.getProperty(key, defaultValue);
+    }
+
+    public ServerConfiguration set(String key, Object value) {
+        if (value == null) {
+            this.properties.remove(key);
+        } else {
+            this.properties.setProperty(key, value + "");
+        }
+        return this;
+    }
+
+}
diff --git a/herddb-core/src/main/java/herddb/server/ServerSideConnectionPeer.java b/herddb-core/src/main/java/herddb/server/ServerSideConnectionPeer.java
new file mode 100644
index 00000000..fdbe015c
--- /dev/null
+++ b/herddb-core/src/main/java/herddb/server/ServerSideConnectionPeer.java
@@ -0,0 +1,61 @@
+/*
+ Licensed to Diennea S.r.l. under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. Diennea S.r.l. 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 herddb.server;
+
+import herddb.network.Channel;
+import herddb.network.ChannelEventListener;
+import herddb.network.Message;
+import herddb.network.ServerSideConnection;
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * Handles a client Connection
+ *
+ * @author enrico.olivelli
+ */
+public class ServerSideConnectionPeer implements ServerSideConnection, ChannelEventListener {
+
+    private static final AtomicLong IDGENERATOR = new AtomicLong();
+    private final long id = IDGENERATOR.incrementAndGet();
+    private final Channel channel;
+    private final Server server;
+
+    public ServerSideConnectionPeer(Channel channel, Server server) {
+        this.channel = channel;
+        this.channel.setMessagesReceiver(this);
+        this.server = server;
+    }
+
+    @Override
+    public long getConnectionId() {
+        return id;
+    }
+
+    @Override
+    public void messageReceived(Message message) {
+
+    }
+
+    @Override
+    public void channelClosed() {
+        this.server.connectionClosed(this);
+    }
+
+}


Test patch:
diff --git a/herddb-core/src/test/java/herddb/server/ServerBootTest.java b/herddb-core/src/test/java/herddb/server/ServerBootTest.java
new file mode 100644
index 00000000..4cf29759
--- /dev/null
+++ b/herddb-core/src/test/java/herddb/server/ServerBootTest.java
@@ -0,0 +1,42 @@
+/*
+ Licensed to Diennea S.r.l. under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. Diennea S.r.l. 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 herddb.server;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+/**
+ * Basic server boot test
+ *
+ * @author enrico.olivelli
+ */
+public class ServerBootTest {
+
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder();
+
+    @Test
+    public void test() throws Exception {
+        try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
+            server.start();
+        }
+    }
+}