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 init(C_NATIVE); 00018 } 00019 00020 CStreamingVwCacheFile::CStreamingVwCacheFile(EVwCacheType cache_type) 00021 : CStreamingFile() 00022 { 00023 init(cache_type); 00024 } 00025 00026 CStreamingVwCacheFile::CStreamingVwCacheFile(char* fname, char rw, EVwCacheType cache_type) 00027 : CStreamingFile(fname, rw) 00028 { 00029 init(cache_type); 00030 } 00031 00032 CStreamingVwCacheFile::~CStreamingVwCacheFile() 00033 { 00034 SG_UNREF(env); 00035 SG_UNREF(cache_reader); 00036 } 00037 00038 void CStreamingVwCacheFile::get_vector(VwExample* &ex, int32_t& len) 00039 { 00040 if (cache_reader->read_cached_example(ex)) 00041 len = 1; 00042 else 00043 len = -1; 00044 } 00045 00046 void CStreamingVwCacheFile::get_vector_and_label(VwExample* &ex, int32_t &len, float64_t &label) 00047 { 00048 if (cache_reader->read_cached_example(ex)) 00049 len = 1; 00050 else 00051 len = -1; 00052 } 00053 00054 void CStreamingVwCacheFile::set_env(CVwEnvironment* env_to_use) 00055 { 00056 if (env) 00057 SG_UNREF(env); 00058 00059 env = env_to_use; 00060 SG_REF(env); 00061 00062 SG_UNREF(cache_reader); 00063 00064 switch (cache_format) 00065 { 00066 case C_NATIVE: 00067 cache_reader = new CVwNativeCacheReader(buf->working_file, env); 00068 return; 00069 case C_PROTOBUF: 00070 SG_ERROR("Protocol buffers cache support is not implemented yet!\n"); 00071 } 00072 00073 SG_ERROR("Unexpected cache type to use for reading!\n"); 00074 } 00075 00076 void CStreamingVwCacheFile::reset_stream() 00077 { 00078 buf->reset_file(); 00079 00080 // Recheck the cache so the parser can directly proceed with the examples 00081 if (cache_format == C_NATIVE) 00082 ((CVwNativeCacheReader*) cache_reader)->check_cache_metadata(); 00083 } 00084 00085 void CStreamingVwCacheFile::init(EVwCacheType cache_type) 00086 { 00087 cache_format = cache_type; 00088 env = new CVwEnvironment(); 00089 00090 switch (cache_type) 00091 { 00092 case C_NATIVE: 00093 cache_reader = new CVwNativeCacheReader(buf->working_file, env); 00094 return; 00095 case C_PROTOBUF: 00096 SG_ERROR("Protocol buffers cache support is not implemented yet!\n"); 00097 } 00098 00099 SG_ERROR("Unrecognized cache type to read from!\n"); 00100 }