2020-01-20 14:40:58 +03:00
|
|
|
// Copyright 2020 The Grin Developers
|
2019-02-27 12:47:46 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-05-22 14:51:58 +03:00
|
|
|
use grin_core as core;
|
2019-02-27 12:47:46 +03:00
|
|
|
use grin_store as store;
|
|
|
|
use grin_util as util;
|
|
|
|
|
2020-05-22 14:51:58 +03:00
|
|
|
use crate::core::global;
|
|
|
|
use crate::core::ser::{self, Readable, Reader, Writeable, Writer};
|
2019-02-27 12:47:46 +03:00
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
const WRITE_CHUNK_SIZE: usize = 20;
|
2020-03-10 12:30:12 +03:00
|
|
|
const TEST_ALLOC_SIZE: usize = store::lmdb::ALLOC_CHUNK_SIZE_DEFAULT / 8 / WRITE_CHUNK_SIZE;
|
2019-02-27 12:47:46 +03:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct PhatChunkStruct {
|
|
|
|
phatness: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PhatChunkStruct {
|
|
|
|
/// create
|
|
|
|
pub fn new() -> PhatChunkStruct {
|
|
|
|
PhatChunkStruct { phatness: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Readable for PhatChunkStruct {
|
2020-04-30 18:42:19 +03:00
|
|
|
fn read<R: Reader>(reader: &mut R) -> Result<PhatChunkStruct, ser::Error> {
|
2019-02-27 12:47:46 +03:00
|
|
|
let mut retval = PhatChunkStruct::new();
|
|
|
|
for _ in 0..TEST_ALLOC_SIZE {
|
|
|
|
retval.phatness = reader.read_u64()?;
|
|
|
|
}
|
|
|
|
Ok(retval)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writeable for PhatChunkStruct {
|
|
|
|
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
|
|
|
|
// write many times
|
|
|
|
for _ in 0..TEST_ALLOC_SIZE {
|
|
|
|
writer.write_u64(self.phatness)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn clean_output_dir(test_dir: &str) {
|
|
|
|
let _ = fs::remove_dir_all(test_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(test_dir: &str) {
|
2020-05-22 14:51:58 +03:00
|
|
|
global::set_local_chain_type(global::ChainTypes::Mainnet);
|
2019-02-27 12:47:46 +03:00
|
|
|
util::init_test_logger();
|
|
|
|
clean_output_dir(test_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn lmdb_allocate() -> Result<(), store::Error> {
|
|
|
|
let test_dir = "test_output/lmdb_allocate";
|
|
|
|
setup(test_dir);
|
|
|
|
// Allocate more than the initial chunk, ensuring
|
|
|
|
// the DB resizes underneath
|
|
|
|
{
|
2019-03-06 20:34:39 +03:00
|
|
|
let store = store::Store::new(test_dir, Some("test1"), None, None)?;
|
2019-02-27 12:47:46 +03:00
|
|
|
|
|
|
|
for i in 0..WRITE_CHUNK_SIZE * 2 {
|
|
|
|
println!("Allocating chunk: {}", i);
|
|
|
|
let chunk = PhatChunkStruct::new();
|
2020-04-30 18:47:44 +03:00
|
|
|
let key_val = format!("phat_chunk_set_1_{}", i);
|
2019-02-27 12:47:46 +03:00
|
|
|
let batch = store.batch()?;
|
2020-04-30 18:47:44 +03:00
|
|
|
let key = store::to_key(b'P', &key_val);
|
2019-02-27 12:47:46 +03:00
|
|
|
batch.put_ser(&key, &chunk)?;
|
|
|
|
batch.commit()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println!("***********************************");
|
|
|
|
println!("***************NEXT*****************");
|
|
|
|
println!("***********************************");
|
|
|
|
// Open env again and keep adding
|
|
|
|
{
|
2019-03-06 20:34:39 +03:00
|
|
|
let store = store::Store::new(test_dir, Some("test1"), None, None)?;
|
2019-02-27 12:47:46 +03:00
|
|
|
for i in 0..WRITE_CHUNK_SIZE * 2 {
|
|
|
|
println!("Allocating chunk: {}", i);
|
|
|
|
let chunk = PhatChunkStruct::new();
|
2020-04-30 18:47:44 +03:00
|
|
|
let key_val = format!("phat_chunk_set_2_{}", i);
|
2019-02-27 12:47:46 +03:00
|
|
|
let batch = store.batch()?;
|
2020-04-30 18:47:44 +03:00
|
|
|
let key = store::to_key(b'P', &key_val);
|
2019-02-27 12:47:46 +03:00
|
|
|
batch.put_ser(&key, &chunk)?;
|
|
|
|
batch.commit()?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|