示例#1
0
void pipe_transform_stream() {
  setup();

  sst_t *src = sst_new();
  sst_t *tx = sst_new();
  src->end_cb  = onsrc_end;

  tx->write = upper_onwrite;
  tx->emit_cb = ondata;
  tx->end = onuppertx_end;

  sst_pipe(src, tx);

  src->write(src, sst_chunk_new("d0", NULL));
  t_ok(emit_count == 1, "after one write, one chunk was emitted");
  t_equal_str(emitted, "D0", "first emitted chunk is the one first emitted uppercased");
  free(emitted);

  src->write(src, sst_chunk_new("d1", NULL));
  t_ok(emit_count == 2, "after two writes, two chunks were emitted");
  t_equal_str(emitted, "D1", "second emitted chunk is the one second emitted uppercased");
  free(emitted);

  src->end(src);
  t_ok(end_src_called == 1, "end src is called exactly once after src stream was ended");
  t_ok(end_uppertx_called == 1, "end upper tx is called exactly once after src stream was ended");
}
int main(void) {
  FILE *infile, *outfile;
  infile = fopen("src/stream.c", "r");
  assert(infile > 0 && "opening infile");

  // outfile = fopen("tmp/stream.txt", "w");
  outfile = stdout;
  assert(outfile > 0 && "opening outfile");

  sst_file_t *infs = sst_file_new(infile, NULL);
  infs->end_cb = oninfile_end;

  sst_t *tx = sst_new();
  tx->write = tx_write;
  tx->end_cb = ontx_end;

  sst_file_t *outfs = sst_file_new(outfile, NULL);
  outfs->end_cb = onoutfile_end;

  sst_pipe((sst_t*)infs, tx, (sst_t*)outfs);

  sst_file_write_init(outfs);
  sst_file_read_start(infs);

  return 0;
}
示例#3
0
void pipe_multi_transform_stream() {
  setup();

  sst_t *src       = sst_new();
  sst_t *uppertx   = sst_new();
  sst_t *reversetx = sst_new();
  sst_t *dst       = sst_new();

  src->end_cb  = onsrc_end;

  uppertx->write = upper_onwrite;
  uppertx->end_cb = onuppertx_end;

  reversetx->write = reverse_onwrite;
  reversetx->end_cb = onreversetx_end;

  dst->emit_cb = ondata;
  dst->end_cb = ondst_end;

  sst_pipe(src, uppertx, reversetx, dst);

  src->write(src, sst_chunk_new("d0", NULL));
  t_ok(emit_count == 1, "after one write, one chunk was emitted by dst");
  t_equal_str(emitted, "0D", "first emitted chunk is the one first emitted reversed and uppercased");
  free(emitted);

  src->write(src, sst_chunk_new("d1", NULL));
  t_ok(emit_count == 2, "after two writes, two chunks were emitted by dst");
  t_equal_str(emitted, "1D", "second emitted chunk is the one second emitted reversed and uppercased");
  free(emitted);

  src->end(src);
  t_ok(end_src_called == 1, "end src is called exactly once after src stream was ended");
  t_ok(end_uppertx_called == 1, "end upper tx is called exactly once after src stream was ended");
  t_ok(end_reversetx_called == 1, "end reverse tx is called exactly once after src stream was ended");
  t_ok(end_dst_called == 1, "end dst is called exactly once after src stream was ended");
}
示例#4
0
struct index *index_new(const char *basedir, int max_mtbl_size, int tolog)
{
	char dbfile[FILE_PATH_SIZE];
	struct index *idx = malloc(sizeof(struct index));
	struct idx_park *park = malloc(sizeof(struct idx_park));

	ensure_dir_exists(basedir);
	
	idx->lsn = 0;
	idx->bloom_hits = 0;
	idx->bg_merge_count = 0;
	idx->max_mtbl = 1;
	idx->max_mtbl_size = max_mtbl_size;
	memset(idx->basedir, 0, FILE_PATH_SIZE);
	memcpy(idx->basedir, basedir, FILE_PATH_SIZE);

	/* sst */
	idx->sst = sst_new(idx->basedir);
	idx->list = skiplist_new(max_mtbl_size);
	pthread_mutex_init(&idx->merge_mutex, NULL);

	/* container */
	park->list = NULL;
	park->lsn = idx->lsn;
	idx->park = park;

	/* log */
	idx->log = log_new(idx->basedir, idx->lsn, tolog);
	
	/*
	 * Log Recovery Processes :
	 * 1) read old log file and add entries to memtable
	 * 2) read new log file abd add entries to memtable
	 * 3) merge the current active log's memtable
	 * 4) remove old log file, new log file 
	 * 5) create new memtable and log file
	 */
	if (log_recovery(idx->log, idx->list)) {
		__DEBUG(LEVEL_DEBUG, "prepare to merge logs, merge count #%d....", idx->list->count);
		sst_merge(idx->sst, idx->list, 1);

		remove(idx->log->log_new);
		remove(idx->log->log_old);

		idx->list = skiplist_new(idx->max_mtbl_size);
	}

	/* Create new log : 0.log */
	log_next(idx->log, 0);

	memset(dbfile, 0, FILE_PATH_SIZE);
	snprintf(dbfile, FILE_PATH_SIZE, "%s/ness.db", idx->basedir);
	idx->db_rfd = open(dbfile, LSM_OPEN_FLAGS, 0644);

	/* Detached thread attr */
	pthread_attr_init(&idx->attr);
	pthread_attr_setdetachstate(&idx->attr, PTHREAD_CREATE_DETACHED);

	return idx;

}