Example #1
0
void *_write_mmap(struct sst *sst, struct skipnode *x, size_t count, int need_new)
{
	int i, j, c_clone;
	int fd;
	int sizes;
	int result;
	char file[FILE_PATH_SIZE];
	struct skipnode *last;
	struct sst_block *blks;
	struct footer footer;

	int fsize = sizeof(struct footer);

	sizes = count * sizeof(struct sst_block);

	memset(file, 0, FILE_PATH_SIZE);
	snprintf(file, FILE_PATH_SIZE, "%s/%s", sst->basedir, sst->name);
	fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644);

	lseek(fd, sizes - 1, SEEK_SET);
	result = write(fd, "", 1);
	if(result == -1)
		abort();

	blks = mmap(0, sizes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	last = x;
	c_clone = count;
	for(i = 0, j = 0; i < c_clone; i++) {
		if(x->opt == ADD) {
			memset(blks[j].key, 0, NESSDB_MAX_KEY_SIZE);
			memcpy(blks[j].key, x->key, NESSDB_MAX_KEY_SIZE);
			blks[j].offset = to_be64(x->val);
			j++;
		} else 
			count --;
		last = x;
		x = x->forward[0];
	}
#ifdef MSYNC
	if(msync(blks, sizes, , MS_SYNC) == -1) {
		//__DEBUG
	}
#endif 

	if(munmap(blks, sizes) == -1) {
		//__DEBUG
	}

	footer.count = to_be32(count);
	footer.crc = to_be32(F_CRC);
	memset(footer.key, 0, NESSDB_MAX_KEY_SIZE);
	memcpy(footer.key, last->key, NESSDB_MAX_KEY_SIZE);

	result = write(fd, &footer, fsize);
	if(result == -1)
		abort();

	/* Set meta */
	struct meta_node mn;

	mn.count = count;
	memset(mn.end, 0, NESSDB_MAX_KEY_SIZE);
	memcpy(mn.end, last->key, NESSDB_MAX_KEY_SIZE);

	memset(mn.index_name, 0, FILE_NAME_SIZE);
	memcpy(mn.index_name, sst->name, FILE_NAME_SIZE);

	if(need_new)
		meta_set(sst->meta, &mn);
	else 
		meta_set_byname(sst->meta, &mn);

	close(fd);
	return x;
}
Example #2
0
void *_write_mmap(struct silopit *silopit, struct skipnode *x, size_t count, int need_new)
{
	int i, j, c_clone;
	int fd;
	int sizes;
	int result;
	char file[FILE_PATH_SIZE];
	struct skipnode *last;
	struct footer footer;
	struct stats stats;

	int fsize = sizeof(struct footer);
	memset(&footer, 0, fsize);

	_prepare_stats(x, count, &stats);
	sizes = stats.mmap_size;

	struct inner_block {
		char key[stats.max_len];
		char offset[8];
	};

	struct inner_block *blks;

	memset(file, 0, FILE_PATH_SIZE);
	snprintf(file, FILE_PATH_SIZE, "%s/%s", silopit->basedir, silopit->name);
	fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0644);
	if (fd == -1)
		__PANIC("error creating silopit file");

	if (lseek(fd, sizes - 1, SEEK_SET) == -1)
		__PANIC("error lseek silopit");

	result = write(fd, "", 1);
	if (result == -1)
		__PANIC("error writing empty");

	blks = mmap(0, sizes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (blks == MAP_FAILED) {
		__PANIC("error mapping block when write on process");
	}

	last = x;
	c_clone = count;
	for (i = 0, j = 0; i < c_clone; i++) {
		if (x->opt == ADD) {
			buffer_putstr(silopit->buf, x->key);
			buffer_putc(silopit->buf, 0);
			buffer_putlong(silopit->buf, x->val);
			j++;
		} else
			count--;

		last = x;
		x = x->forward[0];
	}

	char *strings = buffer_detach(silopit->buf);
	memcpy(blks, strings, sizes);

#ifdef MSYNC
	if (msync(blks, sizes, MS_SYNC) == -1) {
		__ERROR("Error Msync");
	}
#endif

	if (munmap(blks, sizes) == -1) {
		__ERROR("Un-mmapping the file");
	}
	
	footer.count = to_be32(count);
	footer.crc = to_be32(F_CRC);
	footer.size = to_be32(sizes);
	footer.max_len = to_be32(stats.max_len);
	memcpy(footer.key, last->key, strlen(last->key));

	result = write(fd, &footer, fsize);
	if (result == -1)
		__PANIC("writing the footer");

	struct meta_node mn;

	mn.count = count;
	memset(mn.end, 0, SILOKATANA_MAX_KEY_SIZE);
	memcpy(mn.end, last->key, SILOKATANA_MAX_KEY_SIZE);

	memset(mn.index_name, 0, FILE_NAME_SIZE);
	memcpy(mn.index_name, silopit->name, FILE_NAME_SIZE);
	
	if (need_new) 
		meta_set(silopit->meta, &mn);
	else 
		meta_set_byname(silopit->meta, &mn);

	close(fd);
	return x;
}