pxt-calliope/libs/microbit/storage.cpp

43 lines
1.1 KiB
C++
Raw Normal View History

2016-04-16 16:45:04 +02:00
#include "ksbit.h"
/**
2016-04-18 18:21:11 +02:00
* This allows reading and writing of small blocks of data to FLASH memory.
2016-04-16 16:45:04 +02:00
*/
//% weight=10 color=#cc6600
namespace storage {
/**
2016-04-18 18:21:11 +02:00
* Writes the key and buffer pair into FLASH. This operation is rather costly as all the key/value pairs
* have to be rewritten as well.
2016-04-16 16:45:04 +02:00
*/
2016-04-18 18:21:11 +02:00
//%
2016-04-16 16:45:04 +02:00
void putBuffer(StringData* key, Buffer buffer) {
uBit.storage.put(ManagedString(key), ManagedBuffer(buffer).getBytes());
}
/**
* Gets the buffer at the given key if any. If no key is available, empty buffer is returned.
*/
2016-04-18 18:21:11 +02:00
//%
2016-04-16 16:45:04 +02:00
Buffer getBuffer(StringData* key) {
KeyValuePair* pv = uBit.storage.get(ManagedString(key));
if (pv == NULL) return ManagedBuffer().leakData();
return ManagedBuffer(pv->value, sizeof(pv->value)).leakData();
}
/**
* Removes an entry identified by the key.
*/
2016-04-18 18:21:11 +02:00
//%
2016-04-16 16:45:04 +02:00
void remove(StringData * key) {
uBit.storage.remove(ManagedString(key));
}
/**
* The number of entries in the key value store
*/
2016-04-18 18:21:11 +02:00
//%
2016-04-16 16:45:04 +02:00
int size() {
return uBit.storage.size();
}
}