Is there any way to cast HazelCast IMap to java.util.map(HashMap)?
IMap<K, V> imap = ...;
Map<K, V> map;
Didn't find any library method to do so. One way is forming the Map using entrySet of IMap but that includes iterating over complete entrySet and may slow down for huge IMaps.
Related
I have a scenario whereby I want to store the value of a PathBuf in two different collections, in a HashSet<PathBuf> and as a field in a vector of structs Vec<Contract> (Contract is a custom struct defined in my project).
I looked at the docs but I couldn't find a clone functionality for PathBuf.
I know that there are alternative solutions, such as converting the PathBuf to a String or generating a unique hash for the PathBuf, but I would prefer to use the same type.
Is it possible to clone PathBuf?
Update: as pointed out by #vallenting in the comments, I was looking at an old version of PathBuf. In v1.8.0 and above, clone is available.
In v0.99.10 you can use to_path_buf().
let p = PathBuf::from("...");
let p_clone = p.to_path_buf();
However, in v1.8.0 you can use clone() as you'd expect.
let p = PathBuf::from("...");
let p_clone = p.clone();
I'm trying to provide "views" of non-owned structs to separate components of a system.
Assume a set of traits with distinct methods: Drawable, Modifiable and a number of structs which implement at least one of the traits - SimpleBox, Panel, Expression.
Different components of the system will need to frequently access sequences of these objects, using methods of specific traits; consider a DrawingManager or a ModifyManager:
struct DrawingManager {
items: Vec<Weak<Drawable>>,
}
struct ModifyManager {
items: Vec<Weak<Modifiable>>
}
While a single object may be referenced in both managers, assume that there is a separate single owner of all structs:
struct ObjectManager {
boxes: Vec<Rc<Box>>,
panels: Vec<Rc<Panel>>,
expressions: Vec<Rc<Expression>>,
}
Ideally, it would be useful to be able to manage deleting structs from one place - i.e simply removing it from the ObjectManager being enough to invalidate references in all other components (hence the use of Weak).
Is there a way of doing this?
Is this the correct way to achieve this?
Is there a more idiomatic way of implementing this functionality?
The system contains several traits, so making a single trait using methods of all the other traits seems like a bad idea. Several traits have more than one method, so replacing them with closures is not possible.
What I have tried
As one object may produce one or more Rc<Trait>, we might envision implementing this with a HashMap<ID, Vec<Rc<Any>>> whereby we make each struct have a unique ID, which maps to a list of all Rc that have been made for it.
When we want to remove an object, we remove it from the corresponding list, and remove the entry in the hashmap, invalidating all Weak references.
However, implementing this fails, as to insert into the HashMap, one must upcast a Rc<Trait> -> Rc<Any>, only to downcast it later.
I'm not sure if this is the idiomatic way of doing this, but I've since developed a crate providing this functionality - dependent_view.
Using the crate, the initial problem can be solved by using DependentRc instead of plain Rc's:
struct ObjectManager {
boxes: Vec<DependentRc<Box>>,
panels: Vec<DependentRc<Panel>>,
expressions: Vec<DependentRc<Expression>>
}
let object_manager : ObjectManager = ObjectManager::new();
Then using macros provided by the crate, we can obtain Weak<> references to these structs:
let box_view : Weak<Drawable> = to_view!(object_manager.boxes[0]);
let panel_view : Weak<Drawable> = to_view!(object_manager.panels[0]);
let expression_view : Weak<Drawable> = to_view!(object_manager.expressions[0]);
With this, dropping the corresponding DependentRc<> will invalidate all Weak<> references that have been made of it.
I have a Hazelcast instance that organizes data based on map names. The map names are dynamic, so I do not know what they will be until the Hazelcast instance is already started. I now want to store this data in a database via the MapStore functionality, but retain the organization I have setup with the map names. When looking at the MapStore functionality, I do not see any way to retrieve the map or map name that the object came from. Looks like all I get is the key-value pair in the MapStore implementation.
On a broader note, is there any way to get ANY information (not just map name) about the key-value pair needing to be stored? I would like to transfer some knowledge about how to store the data... I know the information when I call map.put(..), but I do not know how to transfer that information to the MapStore call...
I just needed something similar and found that you can implement com.hazelcast.core.MapStoreFactory interface whose 'newMapStore' method gives you the map name and properties from config. From there 'new' your own MapStore implementation passing the map name.
public class MyMapStoreFactory implements MapStoreFactory {
#Override
public MapStore newMapStore(String mapName, Properties properties) {
return new MyMapStoreImplementation(mapName, properties);
}
}
And configure this factory class in hazelcast.xml config like this:
<map name="MapX*">
<map-store enabled="true" initial-mode="EAGER">
<factory-class-name>MyMapStoreFactory</factory-class-name>
</map-store>
</map>
Notice the wildcard on the map name and that <class-name> element must not appear once you set <factory-class-name>.
Tested this with Hazelcast 3.6.1 and it works very fine.
As per my understanding, there is no support out of the box in hazelcast. The following are couple of workarounds I can think of:
Encapsulate the extra information (map name, how to store the data etc) in a context object and store it in a different java map against your key. Use this map in your MapStore implementation later to retrieve respective information which will help you to persist your key,value pairs.
You put operation might look like.
hzMap.put(key, value);
Context context = new Context();
context.setHowToStoreData();
context.setMapName();
// any othe rother information
context.xxx();
// create a unique context key which can be later deduced from (key,value) pair.
contextKey = getUniqueContextKey(key, value);
contextMap.put(contextKey, context);
In your MapStore implementation, you can then make use of this contextMap to retrieve additional values.
Second way would be to encapsulate the information within the (key, value) pair. You can create a new class called CacheEntry to store cache values as well as additional information. You can then later retrieve your cache value as well as additional information from iMap itself.
You put operation might look like.
CacheEntry<YourValueClass> cacheEntry = new CacheEntry<YourValueClass>();
cacheEntry.setValue(value);
cacheEntry.howToStoreData(..);
cacheEntry.setMapName(..);
imap.put(key, cacheEntry);
In your MapStore implementation, you can then make use of the value (which would be a CacheEntry object) itself to retrieve additional information as well as actual value (instance of YourValueClass).
I'm trying to use following DB API: https://godoc.org/github.com/syndtr/goleveldb/leveldb#
(simple file based key/value DB)
I was able to put and get "key"s into the database.
However, I'm wondering if value can be a struct such as:
type Thm struct {
Name string
Age int
}
Then,
var Tmp Thm
Tmp.Name = "Gon"
Tmp.Age = 33
db.Put([]byte("test3"), []byte(Tmp), nil)
Right now, the error I'm getting is "cannot covert Tmp (type Thm) to type []byte.
If you have experiences with levelDB, could you help me how normally this will be done?
OR, should I convert struct into byte in order to make this work?
Thank you
levelDB only supports strings/byte arrays as keys and values. This is actually a pretty smart feature, because it keeps serialization of complex data structures at the application level. To serialize your Thm struct you can try the gob package if you don't need applications in other languages to be able to read the values, or protobufs, json, or msgpack if you need the serialized data to be accessible to other languages.
I've been using the--quite excellent--playframework and have had trouble finding documentation/examples on how to access a Map data structure from a view, using play's template engine.
To be even more specific, I wish to access the Map as I iterate over a List of objects, e.g.,
#{list items:itemList, as:'item'}
// access the map value using the ${item?.id} as the key
#{/list}
Thank's for looking.
This is a generic solution to iterate on Map in using Play! Framework:
in the controller :
render(map);
in the template:
#{list items:map.keySet(), as:'key'}
${map.get(key)}
#{/list}
The fact that you want to rely on a side List to iterate on your Map suggest me that you are searching a predictible way for your iteration process.
In that case, just remember that iteration will be unpredictable if you don't use an ordered/sorted Map implementation.
HashMap gives you an unsorted, unordered Map
LinkedHashMap maintains insertion order
TreeMap is the only JDK implementation of a sorted Map. By default it allows you to iterate following the natural order of the elements. You can also specify a custom sort order and implements the Comparable interface. This will lead you to override the compareTo() method.
Assuming you do in the Controller:
render(map, itemList); //map is a Map
This should work:
#{list items:itemList, as:'item'}
// access the map value using the ${item?.id} as the key
${map.get(item.?id)}
#{/list}
I don't know anything about the play framework, but this would work in a GSP
#{list items:itemList, as:'item'}
${map[item?.id]}
#{/list}
I'm doing things like that on a map:
*{ Map<User, Integer> shareHolders = ... }*
#{list shareHolders.sort{it.value}.collect {k,v -> k}.reverse(), as:'holder'}
<tr>
<td>${holder.name}</td>
<td>${shareHolders[holder]}</td>
</tr>
#{/list}