Exemplo n.º 1
0
	bool Ardb::Init()
	{
		if (NULL == m_engine)
		{
			INFO_LOG("Start init storage engine.");
			m_engine = m_engine_factory->CreateDB(
			        m_engine_factory->GetName().c_str());

			KeyObject verkey(Slice(), KEY_END, 0xFFFFFF);
			ValueObject ver;
			if (0 == GetValue(verkey, &ver, NULL))
			{
				if (ver.v.int_v != ARDB_FORMAT_VERSION)
				{
					ERROR_LOG(
					        "Incompatible data format version:%d in DB", ver.v.int_v);
					return false;
				}
			}
			else
			{
				ver.v.int_v = ARDB_FORMAT_VERSION;
				ver.type = INTEGER;
				SetValue(verkey, ver);
			}
			if (NULL != m_engine)
			{
				INFO_LOG("Init storage engine success.");
			}
		}
		return m_engine != NULL;
	}
Exemplo n.º 2
0
	int Ardb::GetScript(const std::string& funacname, std::string& funcbody)
	{
		KeyObject verkey(funacname, SCRIPT, ARDB_GLOBAL_DB);
		ValueObject v;
		if (0 == GetValue(verkey, &v))
		{
			funcbody = v.v.raw->AsString();
			return 0;
		}
		return -1;
	}
Exemplo n.º 3
0
	bool Ardb::Init(uint32 check_expire_period)
	{
		if (NULL == m_engine)
		{
			INFO_LOG("Start init storage engine.");
			m_engine = m_engine_factory->CreateDB(m_engine_factory->GetName().c_str());
			if (NULL == m_engine)
			{
				return false;
			}
			KeyObject verkey(Slice(), KEY_END, ARDB_GLOBAL_DB);
			ValueObject ver;
			if (0 == GetValue(verkey, &ver))
			{
				if (ver.v.int_v != ARDB_FORMAT_VERSION)
				{
					ERROR_LOG("Incompatible data format version:%d in DB", ver.v.int_v);
					return false;
				}
			} else
			{
				ver.v.int_v = ARDB_FORMAT_VERSION;
				ver.type = INTEGER;
				SetValue(verkey, ver);
			}
			if (NULL != m_engine)
			{
				INFO_LOG("Init storage engine success.");

				//launch a threading task to check expired keys
				struct ExpireCheckThread: public Thread
				{
						Ardb* adb;
						volatile bool running;
						uint32 check_period;
						ExpireCheckThread(Ardb* db, uint32 period) :
								adb(db), running(true), check_period(period)
						{
						}
						void Run()
						{
							while (running)
							{
								DBID dbid = 0;
								while (dbid < ARDB_GLOBAL_DB)
								{
									DBID nexdb = 0;
									if (adb->DBExist(dbid, nexdb))
									{
										adb->CheckExpireKey(dbid);
										dbid++;
									} else
									{
										if (nexdb == dbid || nexdb == ARDB_GLOBAL_DB)
										{
											break;
										}
										adb->CheckExpireKey(nexdb);
										dbid = nexdb + 1;
									}
								}
								uint64 end = get_current_epoch_micros();
								uint64 sleep = check_period;
								if (adb->m_min_expireat > end)
								{
									sleep = (adb->m_min_expireat - end) / 1000;
									if (sleep > check_period)
									{
										sleep = check_period;
									}
									if (sleep == 0)
									{
										sleep = 1;
									}
								}
								Thread::Sleep(sleep);
							}
						}
						~ExpireCheckThread()
						{
							running = false;
						}
				};
				m_expire_check_thread = new ExpireCheckThread(this, check_expire_period);
				m_expire_check_thread->Start();
			}
		}
		return m_engine != NULL;
	}