Skip to content

Commit

Permalink
Fix #3 - bulk insert
Browse files Browse the repository at this point in the history
  • Loading branch information
atilaneves committed Apr 13, 2017
2 parents 409e694 + 86f3a01 commit 5b3a0ea
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Expand Up @@ -21,6 +21,8 @@ script:
matrix:
include:
- d: dmd
- d: dmd-2.073.2
- d: dmd-2.072.2
- d: dmd-2.071.2
- d: dmd-2.070.2
- d: ldc
2 changes: 1 addition & 1 deletion dub.json
Expand Up @@ -4,7 +4,7 @@
"Atila Neves"
],
"dependencies": {
"vibe-d": "~>0.7.30",
"vibe-d": "~>0.8.0-beta.5",
"asdf": "~>0.1.1"
},
"description": "InfluxDB wrapper",
Expand Down
11 changes: 7 additions & 4 deletions dub.selections.json
@@ -1,13 +1,16 @@
{
"fileVersion": 1,
"versions": {
"asdf": "0.1.1",
"asdf": "0.1.3",
"diet-ng": "1.2.0",
"libasync": "0.7.9",
"eventcore": "0.8.10",
"libasync": "0.8.3",
"libevent": "2.0.2+2.0.16",
"memutils": "0.4.9",
"openssl": "1.1.5+1.0.1g",
"unit-threaded": "0.7.7",
"vibe-d": "0.7.30"
"taggedalgebraic": "0.10.5",
"unit-threaded": "0.7.14",
"vibe-core": "1.0.0-beta.5",
"vibe-d": "0.8.0-beta.5"
}
}
19 changes: 19 additions & 0 deletions integration/api.d
Expand Up @@ -31,3 +31,22 @@ unittest {
series.rows.length.shouldEqual(1);
}
}

@Serial
@("Database multiple inserts")
unittest {

import influxdb.api: Database, Measurement;

const database = Database(influxURL, "myspecialDB");
scope(exit) database.drop;

database.insert(Measurement("cpu", ["tag1": "foo"], ["temperature": "42"]),
Measurement("cpu", ["tag1": "bar"], ["temperature": "68"]),
Measurement("cpu", ["tag1": "baz"], ["temperature": "54"]));

const response = database.query("SELECT * from cpu WHERE temperature > 50");
const result = response.results[0];
const series = result.series[0];
series.rows.length.shouldEqual(2);
}
36 changes: 32 additions & 4 deletions source/influxdb/api.d
Expand Up @@ -46,8 +46,9 @@ struct DatabaseImpl(alias manageFunc, alias queryFunc, alias writeFunc) {
Insert data into the DB.
*/
void insert(in Measurement[] measurements) const {
foreach(ref const m; measurements)
writeFunc(url, db, m.toString);
import std.algorithm: map;
import std.array: array, join;
writeFunc(url, db, measurements.map!(a => a.toString).array.join("\n"));
}

/**
Expand All @@ -74,8 +75,8 @@ struct DatabaseImpl(alias manageFunc, alias queryFunc, alias writeFunc) {
string[string][] writes;

alias TestDatabase = DatabaseImpl!(
(url, cmd) => manages ~= ["url": url, "cmd": cmd],
(url, db, query) {
(url, cmd) => manages ~= ["url": url, "cmd": cmd], // manage
(url, db, query) { // query
queries ~= ["url": url, "db": db, "query": query];
return
`{
Expand Down Expand Up @@ -125,6 +126,33 @@ struct DatabaseImpl(alias manageFunc, alias queryFunc, alias writeFunc) {
);
}

@("insert")
@safe unittest {

string[] lines;

alias TestDatabase = DatabaseImpl!(
(url, cmd) { }, // manage
(url, db, query) => `{}`, // query
(url, db, line) => lines ~= line // write
);

const database = TestDatabase("http://db.com", "testdb");
database.insert(
Measurement("cpu", ["index": "1"], ["temperature": "42"]),
Measurement("cpu", ["index": "2"], ["temperature": "42"]),
Measurement("cpu", ["index": "2"], ["temperature": "42"]),
);

() @trusted {
lines.shouldEqual(
[
"cpu,index=1 temperature=42\ncpu,index=2 temperature=42\ncpu,index=2 temperature=42",
]
);
}();
}

/**
An InfluxDB measurement
*/
Expand Down

0 comments on commit 5b3a0ea

Please sign in to comment.