Skip to content

Commit ffd57b4

Browse files
committed
Add BasicTcpDB Client
1 parent ce3eb9c commit ffd57b4

5 files changed

Lines changed: 261 additions & 1 deletion

File tree

basictcp/pom.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright (c) 2015-2016 YCSB contributors. All rights reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you
6+
may not use this file except in compliance with the License. You
7+
may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
implied. See the License for the specific language governing
13+
permissions and limitations under the License. See accompanying
14+
LICENSE file.
15+
-->
16+
17+
<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/maven-v4_0_0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<parent>
20+
<groupId>site.ycsb</groupId>
21+
<artifactId>binding-parent</artifactId>
22+
<version>0.18.0-SNAPSHOT</version>
23+
<relativePath>../binding-parent</relativePath>
24+
</parent>
25+
26+
<artifactId>basictcp-binding</artifactId>
27+
<name>BasicTCP Binding</name>
28+
<packaging>jar</packaging>
29+
30+
<dependencies>
31+
<dependency>
32+
<groupId>site.ycsb</groupId>
33+
<artifactId>core</artifactId>
34+
<version>${project.version}</version>
35+
<scope>provided</scope>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/**
2+
* Copyright (c) 2015 YCSB contributors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License. See accompanying LICENSE file.
15+
*
16+
* Basic TCP based DB client binding for YCSB.
17+
*
18+
* Based off of the S3 client.
19+
*/
20+
package site.ycsb.db;
21+
22+
import java.util.HashMap;
23+
import java.util.Set;
24+
import java.util.Vector;
25+
import java.io.*;
26+
import java.util.*;
27+
import java.net.*;
28+
29+
import site.ycsb.ByteIterator;
30+
import site.ycsb.DB;
31+
import site.ycsb.DBException;
32+
import site.ycsb.Status;
33+
34+
/**
35+
* Basic TCP based DB client for YCSB framework.
36+
*
37+
*/
38+
public class BasicTcpDB extends DB {
39+
public static final String HOST = "basictcpdb.host";
40+
public static final String HOST_DEFAULT = "127.0.0.1";
41+
42+
public static final String PORT = "basictcpdb.port";
43+
public static final String PORT_DEFAULT = "54321";
44+
45+
private Socket socket;
46+
private String host;
47+
private Integer port;
48+
private PrintWriter outStream;
49+
private BufferedReader inStream;
50+
51+
protected static final ThreadLocal<StringBuilder> TL_STRING_BUILDER =
52+
new ThreadLocal<StringBuilder>() {
53+
@Override
54+
protected StringBuilder initialValue() {
55+
return new StringBuilder();
56+
}
57+
};
58+
59+
protected StringBuilder getStringBuilder() {
60+
StringBuilder sb = TL_STRING_BUILDER.get();
61+
sb.setLength(0);
62+
return sb;
63+
}
64+
65+
/**
66+
* Initialize.
67+
*/
68+
@Override
69+
public void init() throws DBException {
70+
host = getProperties().getProperty(HOST, HOST_DEFAULT);
71+
port = Integer.parseInt(getProperties().getProperty(PORT, PORT_DEFAULT));
72+
73+
try {
74+
this.socket = new Socket(host, port);
75+
this.outStream = new PrintWriter(socket.getOutputStream(), true);
76+
this.inStream =
77+
new BufferedReader(new InputStreamReader(socket.getInputStream()));
78+
} catch (IOException e) {
79+
throw new DBException(e);
80+
}
81+
}
82+
83+
/**
84+
* Cleanup.
85+
*/
86+
@Override
87+
public void cleanup() throws DBException {
88+
try {
89+
this.socket.close();
90+
} catch (IOException e) {
91+
throw new DBException(e);
92+
}
93+
}
94+
95+
/**
96+
* Insert.
97+
*/
98+
@Override
99+
public Status insert(String table, String key,
100+
Map<String, ByteIterator> values) {
101+
102+
StringBuilder sb = this.getStringBuilder();
103+
sb.append("INSERT ").append(table).append(" ").append(key).append(" [ ");
104+
if (values != null) {
105+
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
106+
sb.append(entry.getKey()).append("=").append(entry.getValue())
107+
.append(" ");
108+
}
109+
}
110+
111+
sb.append("]");
112+
113+
this.outStream.write(sb.toString());
114+
115+
return Status.OK;
116+
}
117+
118+
/**
119+
* Read.
120+
*/
121+
@Override
122+
public Status read(String table, String key, Set<String> fields,
123+
Map<String, ByteIterator> result) {
124+
125+
StringBuilder sb = this.getStringBuilder();
126+
sb.append("READ ").append(table).append(" ").append(key).append(" [ ");
127+
if (fields != null) {
128+
for (String f : fields) {
129+
sb.append(f).append(" ");
130+
}
131+
} else {
132+
sb.append("<all fields>");
133+
}
134+
135+
sb.append("]");
136+
137+
this.outStream.write(sb.toString());
138+
139+
return Status.OK;
140+
}
141+
142+
/**
143+
* Update.
144+
*/
145+
@Override
146+
public Status update(String table, String key,
147+
Map<String, ByteIterator> values) {
148+
149+
StringBuilder sb = this.getStringBuilder();
150+
sb.append("UPDATE ").append(table).append(" ").append(key).append(" [ ");
151+
if (values != null) {
152+
for (Map.Entry<String, ByteIterator> entry : values.entrySet()) {
153+
sb.append(entry.getKey()).append("=").append(entry.getValue())
154+
.append(" ");
155+
}
156+
}
157+
sb.append("]");
158+
159+
this.outStream.write(sb.toString());
160+
161+
return Status.OK;
162+
}
163+
164+
/**
165+
* Delete.
166+
*/
167+
@Override
168+
public Status delete(String table, String key) {
169+
StringBuilder sb = this.getStringBuilder();
170+
sb.append("DELETE ").append(table).append(" ").append(key);
171+
172+
this.outStream.write(sb.toString());
173+
174+
return Status.OK;
175+
}
176+
177+
/**
178+
* Scan.
179+
*/
180+
@Override
181+
public Status scan(String table, String startkey, int recordcount,
182+
Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
183+
184+
StringBuilder sb = this.getStringBuilder();
185+
sb.append("SCAN ").append(table).append(" ").append(startkey).append(" ")
186+
.append(recordcount).append(" [ ");
187+
if (fields != null) {
188+
for (String f : fields) {
189+
sb.append(f).append(" ");
190+
}
191+
} else {
192+
sb.append("<all fields>");
193+
}
194+
195+
sb.append("]");
196+
197+
this.outStream.write(sb.toString());
198+
199+
return Status.OK;
200+
}
201+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Copyright (c) 2015 YCSB contributors. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License. See accompanying LICENSE file.
15+
*
16+
* Basic TCP based DB client binding for YCSB.
17+
*/
18+
19+
package site.ycsb.db;

bin/bindings.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ arangodb3:site.ycsb.db.arangodb.ArangoDBClient
3333
azurecosmos:site.ycsb.db.AzureCosmosClient
3434
azuretablestorage:site.ycsb.db.azuretablestorage.AzureClient
3535
basic:site.ycsb.BasicDB
36+
basictcp:site.ycsb.db.BasicTcpDB
3637
basicts:site.ycsb.BasicTSDB
3738
cassandra-cql:site.ycsb.db.CassandraCQLClient
3839
cassandra2-cql:site.ycsb.db.CassandraCQLClient
@@ -73,4 +74,4 @@ solr7:site.ycsb.db.solr7.SolrClient
7374
tarantool:site.ycsb.db.TarantoolClient
7475
tablestore:site.ycsb.db.tablestore.TableStoreClient
7576
voltdb:site.ycsb.db.voltdb.VoltClient4
76-
zookeeper:site.ycsb.db.zookeeper.ZKClient
77+
zookeeper:site.ycsb.db.zookeeper.ZKClient

bin/ycsb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ DATABASES = {
5959
"azurecosmos" : "site.ycsb.db.AzureCosmosClient",
6060
"azuretablestorage" : "site.ycsb.db.azuretablestorage.AzureClient",
6161
"basic" : "site.ycsb.BasicDB",
62+
"basictcp" : "site.ycsb.db.BasicTcpDB",
6263
"basicts" : "site.ycsb.BasicTSDB",
6364
"cassandra-cql": "site.ycsb.db.CassandraCQLClient",
6465
"cassandra2-cql": "site.ycsb.db.CassandraCQLClient",

0 commit comments

Comments
 (0)