void JsonDb::SetArray(TransactionHandle &transaction, std::string const &path, size_t total_elements, bool create_if_not_exists) { ValueArray::Type elements(total_elements); for(ValueArray::Type::iterator i = elements.begin(); i != elements.end(); ++i) { ValuePointer new_element(new ValueNull(transaction->GenerateKey())); *i = new_element->GetKey(); transaction->Store(new_element->GetKey(), new_element); } Set(transaction, path, ValuePointer(new ValueArray(null_key, elements)), create_if_not_exists); }
void JsonDb::Transaction::Commit() { if(next_id != start_next_id) { Store(next_id_key, ValuePointer(new ValueNumberInteger(next_id_key, next_id))); // std::cout << "Commit transaction, next id: " << next_id << std::endl; } vltrancommit(db.get()); start_next_id = next_id; }
void StreamType::outputValueRecursive(ostream &o, const ValuePointer vp, ValueOutputState& state) const { if (vp) { const Stream *s = *reinterpret_cast<const Stream**>(vp); outputValueRecursive(o, ValuePointer(&s), state); } else { o << "nil"; } }
ValuePointer JsonDb::Transaction::Retrieve(ValueKey key) { if(key == null_key) return null_element; // std::cout << "Retrieve: key=" << key << std::endl; // Then retrieve the actual data int value_size; CharPtr val = CharPtr(vlget(db.get(), (char const *)&key, sizeof(ValueKey), &value_size)); if(val.get() == NULL) return ValuePointer(); if(value_size <= 0) throw std::runtime_error((boost::format("Element has an invalid size: %d") % key).str().c_str()); std::string val_str(val.get(), value_size); std::istringstream input(val_str); ValuePointer result = Value::Unserialize(key, input); return result; }
JsonDb::Transaction::Transaction(std::string const &filename, ValuePointer const &_null_element) { /* The null element */ null_element = _null_element; /* open the database */ db = StorageDbPointer(vlopen(filename.c_str(), VL_OWRITER | VL_OCREAT, VL_CMPINT), CloseDatabase); /* Start the transaction */ vltranbegin(db.get()); if(db.get() == NULL) throw std::runtime_error((boost::format("Failed to open database: %s") % dperrmsg(dpecode)).str().c_str()); ValuePointer root = Retrieve(root_key); if(root.get() == NULL) { root = ValuePointer(new ValueObject(root_key)); Store(root->GetKey(), root); } ValuePointer next_id_value = Retrieve(next_id_key); if(next_id_value.get() == NULL) { // Initialize values next_id = initial_next_id; start_next_id = next_id; } else { // Retrieve values from database next_id = next_id_value->GetValueInt(); start_next_id = next_id; } // std::cout << "Start transaction, next id: " << next_id << std::endl; }
void JsonDb::AppendArray(TransactionHandle &transaction, std::string const &path, double value) { AppendArray(transaction, path, ValuePointer(new ValueNumberReal(transaction->GenerateKey(), value))); }
void JsonDb::Set(TransactionHandle &transaction, std::string const &path, bool value, bool create_if_not_exists) { Set(transaction, path, ValuePointer(new ValueNumberBoolean(null_key, value)), create_if_not_exists); }
JsonDb::JsonDb(std::string const &_filename) : filename(_filename) { null_element = ValuePointer(new ValueNull(null_key)); }