forked from apple/turicreate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_stream_sink.cpp
81 lines (70 loc) · 1.93 KB
/
cache_stream_sink.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright © 2017 Apple Inc. All rights reserved.
*
* Use of this source code is governed by a BSD-3-clause license that can
* be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
*/
#include <fileio/cache_stream_sink.hpp>
namespace turi {
namespace fileio_impl {
cache_stream_sink::cache_stream_sink(cache_id_type cache_id) :
cache_manager(fileio::fixed_size_cache_manager::get_instance()),
out_block(cache_manager.new_cache(cache_id)) {
if (out_block->is_file()) {
logstream(LOG_DEBUG) << "Writing " << cache_id << " from "
<< out_block->get_filename() << std::endl;
out_file = std::make_shared<general_fstream_sink>(out_block->get_filename());
}
}
cache_stream_sink::~cache_stream_sink() {
close();
}
std::streamsize cache_stream_sink::write (const char* c, std::streamsize bufsize) {
if (out_file) {
return out_file->write(c, bufsize);
} else {
bool write_success = out_block->write_bytes_to_memory_cache(c, bufsize);
if (write_success) {
return bufsize;
} else {
// In memory cache is full, write out to disk.
// switch to a file handle
out_file = out_block->write_to_file();
return out_file->write(c, bufsize);
}
}
}
void cache_stream_sink::close() {
if (out_file) {
out_file->close();
}
}
bool cache_stream_sink::is_open() const {
if (out_file) {
return out_file->is_open();
} else {
return out_block->get_pointer() != NULL;
}
}
bool cache_stream_sink::good() const {
if (out_file) {
return out_file->good();
} else {
return out_block->get_pointer() != NULL;
}
}
bool cache_stream_sink::bad() const {
if (out_file) {
return out_file->bad();
} else {
return out_block->get_pointer() == NULL;
}
}
bool cache_stream_sink::fail() const {
if (out_file) {
return out_file->fail();
} else {
return out_block->get_pointer() == NULL;
}
}
} // fileio_impl
} // turicreate