-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP try to force it to store byte arrays
The trouble is github.com/ferristseng/rust-ipfs-api does not offer dag_put with cbor: ferristseng/rust-ipfs-api#63 only JSON; and serde_json won't let us sneak a byte array into a string value either, because Rust expects strings to always be valid UTF-8. It would probably be a dead end anyway because of ipfs/kubo#4313 : there's no way to read back CBOR data via the http API, so I couldn't write the select function.
- Loading branch information
Showing
5 changed files
with
212 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ futures = "0.3" | |
serde = "1.0" | ||
serde_json = "1.0" | ||
clap = "~2.27.0" | ||
bytes = "0.4.12" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include <QCoreApplication> | ||
#include <QCborArray> | ||
#include <QCborMap> | ||
#include <QCborValue> | ||
#include <QDebug> | ||
#include <QFile> | ||
#include <QJsonDocument> | ||
#include <QJsonObject> | ||
|
||
static const QLatin1String TypeKey("type"); | ||
static const QLatin1String ValuesKey("values"); | ||
|
||
/*! | ||
A utility to create a "head record" of the form | ||
{ | ||
"_timestamp": { | ||
"type": "u64", | ||
"values": b"" | ||
}, | ||
"numeric_field": { | ||
"type": "f32" | ||
"values": b"" | ||
} | ||
} | ||
where b"" represents a byte array, NOT a string. | ||
(This can be represented in CBOR but not in JSON.) | ||
It can be inserted as a DAG node like this: | ||
$ ipfs dag put --input-enc cbor headRecord.cbor | ||
However, updating it is difficult, because of | ||
https://github.com/ipfs/go-ipfs/issues/4313 : | ||
the IPFS HTTP API doesn't provide a way to read it back | ||
in CBOR format, only as JSON, but it can't be correctly | ||
represented in JSON. | ||
*/ | ||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc < 2) | ||
qFatal("required argument: file.json"); | ||
|
||
QCoreApplication a(argc, argv); | ||
|
||
QFile j(a.arguments().last()); | ||
if (!j.open(QIODevice::ReadOnly)) { | ||
qFatal("couldn't open input file"); | ||
} | ||
|
||
QJsonDocument jd = QJsonDocument::fromJson(j.readAll()); | ||
QCborMap headRecord; | ||
|
||
Q_ASSERT(jd.isObject()); | ||
auto jdo = jd.object(); | ||
auto it = jdo.constBegin(); | ||
while (it != jdo.constEnd()) { | ||
Q_ASSERT(it.value().isObject()); | ||
auto kv = it.value().toObject(); | ||
qDebug() << it.key() << kv; | ||
Q_ASSERT(kv.contains(TypeKey)); | ||
QCborMap field; | ||
field.insert(TypeKey, kv.value(TypeKey).toString()); | ||
field.insert(ValuesKey, QByteArray()); | ||
// field.insert(ValuesKey, QByteArray::fromHex("9f018202039f0405ffff")); | ||
headRecord.insert(it.key(), field); | ||
++it; | ||
} | ||
|
||
qDebug() << headRecord; | ||
|
||
QFile f("headRecord.cbor"); | ||
if (!f.open(QIODevice::WriteOnly)) { | ||
qWarning("Couldn't write file."); | ||
return -1; | ||
} | ||
|
||
f.write(headRecord.toCborValue().toCbor()); | ||
f.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
QT -= gui | ||
|
||
CONFIG += c++11 console | ||
CONFIG -= app_bundle | ||
|
||
# You can make your code fail to compile if it uses deprecated APIs. | ||
# In order to do so, uncomment the following line. | ||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 | ||
|
||
SOURCES += \ | ||
main.cpp | ||
|
||
# Default rules for deployment. | ||
qnx: target.path = /tmp/$${TARGET}/bin | ||
else: unix:!android: target.path = /opt/$${TARGET}/bin | ||
!isEmpty(target.path): INSTALLS += target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"_timestamp": { | ||
"type": "u64" | ||
}, | ||
"ch2o": { | ||
"type": "f32" | ||
}, | ||
"co2": { | ||
"type": "u16" | ||
}, | ||
"cpm":{ | ||
"type": "u16" | ||
}, | ||
"humidity": { | ||
"type": "f32" | ||
}, | ||
"noise":{ | ||
"type": "f32" | ||
}, | ||
"pm25":{ | ||
"type": "u16" | ||
}, | ||
"pressure": { | ||
"type": "u32" | ||
}, | ||
"temperature": { | ||
"type": "f32" | ||
}, | ||
"uptime": { | ||
"type": "u32" | ||
}, | ||
"voc": { | ||
"type": "u32" | ||
}, | ||
"voltage": { | ||
"type": "u16" | ||
} | ||
} |