SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2011 Shashwat Lal Das 00008 * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society 00009 */ 00010 #include <shogun/io/streaming/StreamingVwCacheFile.h> 00011 00012 using namespace shogun; 00013 00014 CStreamingVwCacheFile::CStreamingVwCacheFile() 00015 : CStreamingFile() 00016 { 00017 buf=NULL; 00018 init(C_NATIVE); 00019 } 00020 00021 CStreamingVwCacheFile::CStreamingVwCacheFile(EVwCacheType cache_type) 00022 : CStreamingFile() 00023 { 00024 init(cache_type); 00025 } 00026 00027 CStreamingVwCacheFile::CStreamingVwCacheFile(char* fname, char rw, EVwCacheType cache_type) 00028 : CStreamingFile(fname, rw) 00029 { 00030 init(cache_type); 00031 } 00032 00033 CStreamingVwCacheFile::~CStreamingVwCacheFile() 00034 { 00035 SG_UNREF(env); 00036 SG_UNREF(cache_reader); 00037 } 00038 00039 void CStreamingVwCacheFile::get_vector(VwExample* &ex, int32_t& len) 00040 { 00041 if (cache_reader->read_cached_example(ex)) 00042 len = 1; 00043 else 00044 len = -1; 00045 } 00046 00047 void CStreamingVwCacheFile::get_vector_and_label(VwExample* &ex, int32_t &len, float64_t &label) 00048 { 00049 if (cache_reader->read_cached_example(ex)) 00050 len = 1; 00051 else 00052 len = -1; 00053 } 00054 00055 void CStreamingVwCacheFile::set_env(CVwEnvironment* env_to_use) 00056 { 00057 SG_REF(env_to_use); 00058 SG_UNREF(env); 00059 env = env_to_use; 00060 00061 SG_UNREF(cache_reader); 00062 00063 switch (cache_format) 00064 { 00065 case C_NATIVE: 00066 cache_reader = new CVwNativeCacheReader(buf->working_file, env); 00067 return; 00068 case C_PROTOBUF: 00069 SG_ERROR("Protocol buffers cache support is not implemented yet!\n") 00070 } 00071 00072 SG_ERROR("Unexpected cache type to use for reading!\n") 00073 } 00074 00075 void CStreamingVwCacheFile::reset_stream() 00076 { 00077 buf->reset_file(); 00078 00079 // Recheck the cache so the parser can directly proceed with the examples 00080 if (cache_format == C_NATIVE) 00081 ((CVwNativeCacheReader*) cache_reader)->check_cache_metadata(); 00082 } 00083 00084 void CStreamingVwCacheFile::init(EVwCacheType cache_type) 00085 { 00086 cache_format = cache_type; 00087 env = new CVwEnvironment(); 00088 00089 switch (cache_type) 00090 { 00091 case C_NATIVE: 00092 if (buf) 00093 cache_reader = new CVwNativeCacheReader(buf->working_file, env); 00094 else 00095 cache_reader=NULL; 00096 return; 00097 case C_PROTOBUF: 00098 SG_ERROR("Protocol buffers cache support is not implemented yet!\n") 00099 } 00100 00101 SG_ERROR("Unrecognized cache type to read from!\n") 00102 }